void UltrasonicWave_CalculateTime(void)
{
UltrasonicWave_Distance=TIM_GetCounter(TIM2)*5*34/2000;//5,34,2000这几个数字代表什么?
}
/*
* 函数名:UltrasonicWave_StartMeasure
* 描述 :开始测距,发送一个>10us的脉冲,然后测量返回的高电平时间
* 输入 :无
* 输出 :无
*/
void UltrasonicWave_StartMeasure(void)
{
GPIO_SetBits(TRIG_PORT,TRIG_PIN); //送>10US的高电平
DelayTime_us(20); //延时20US
GPIO_ResetBits(TRIG_PORT,TRIG_PIN);
while(!GPIO_ReadInputDataBit(ECHO_PORT,ECHO_PIN)); //等待高电平
TIM_Cmd(TIM2, ENABLE); //开启时钟
while(GPIO_ReadInputDataBit(ECHO_PORT,ECHO_PIN)); //等待低电平
TIM_Cmd(TIM2, DISABLE); //定时器2失能
UltrasonicWave_CalculateTime(); //计算距离
TIM_SetCounter(TIM2,0);
printf("\r\ndistance:%d%d cm\r\n",UltrasonicWave_Distance/256,UltrasonicWave_Distance%256);//为什么是256?
} |