超声波型号HC-SR04
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);
这是我的型号,求大神解答我的疑问。 |