Ultrasonic module HC-SR04

Insert picture description here
Insert picture description here

Basic working principle:

1. Use IO port TRIG to trigger distance measurement and give at least 10us high-level signal;
2. The module automatically sends 8 40khz square waves to automatically detect whether there is a signal return;
3. When there is a signal return, one output through the IO port ECHO High level, the duration of high level is the time from emission to return of the ultrasonic wave. Test distance = (high level time * speed of sound (340M/S))/2

Then what we have to do is to get the duration, which is the difficulty of using this module

Using timer counting, reading timer register CNT method

CUBEMX configuration

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Implementation code

First read the CNT value in a low level cycle, and when the high level jumps out of the cycle, record the start time of the high level at the moment, and then read the CNT value in a high level cycle until the end of the high level, record the CNT value, and take the difference to obtain a high level Flat duration

void Stat(void)
{
    
    
	/**********************给一个1毫秒(高于1微秒)的高电平触发信号*************************/
		HAL_GPIO_WritePin(HC_SR04_TRIGGER_GPIO_Port,HC_SR04_TRIGGER_Pin,GPIO_PIN_RESET);
		HAL_GPIO_WritePin(HC_SR04_TRIGGER_GPIO_Port,HC_SR04_TRIGGER_Pin,GPIO_PIN_SET);
		HAL_Delay(1);
		HAL_GPIO_WritePin(HC_SR04_TRIGGER_GPIO_Port,HC_SR04_TRIGGER_Pin,GPIO_PIN_RESET);
}

//采用寄存器接收模式htim3.Instance->CNT里的值单位由分频器决定
void Receive(void)
{
    
    
	float t1,t2,distance;
	
	HAL_TIM_Base_Start_IT(&htim3);
	htim3.Instance->CNT = 0;
   
  while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_5) == GPIO_PIN_RESET)
	t1=htim3.Instance->CNT;
	
  while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_5) == GPIO_PIN_SET)
  t2=htim3.Instance->CNT;
	
	//340m/1s = 340mm/1ms = 0.34mm/1us =0.034cm/1us
	
  distance=(t2-t1)*0.017;
  printf("The distance is %0.1fcm\n",distance);
}

Using timer input capture method

CUBEMX configuration

Insert picture description here

Implementation code

The function HAL_TIM_ReadCapturedValue also thinks of reading the CNT register

float   dis_fm = 0;
uint8_t   Edge = 0;
uint32_t  HighTime, RisingTime, FallingTime;

oid HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
    
    
	if(htim3.Instance==TIM3)
	{
    
    
		if(htim3.Channel==HAL_TIM_ACTIVE_CHANNEL_1)
		{
    
    
			if(Edge == 0)          //捕获上升沿
			{
    
    
				RisingTime = HAL_TIM_ReadCapturedValue(&htim3, TIM_CHANNEL_1);                         //获取上升沿时间点
				__HAL_TIM_SET_CAPTUREPOLARITY(&htim3, TIM_CHANNEL_1, TIM_INPUTCHANNELPOLARITY_FALLING);        //切换捕获低电平
				HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_1);    					 //切换捕获极性后需重新启动
 
				Edge = 1;          	 //上升沿、下降沿捕获标志位
			}
			else if(Edge == 1)     //捕获下降沿
			{
    
    
				FallingTime = HAL_TIM_ReadCapturedValue(&htim3, TIM_CHANNEL_1);                       //获取下降沿时间点
				__HAL_TIM_SET_CAPTUREPOLARITY(&htim3, TIM_CHANNEL_1, TIM_INPUTCHANNELPOLARITY_RISING);        //切换捕获高电平
				HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_1);   	 //切换捕获极性后需重新启动
 
				//高电平持续时间 = 下降沿时间点 - 上升沿时间点
				HighTime =FallingTime - RisingTime;
				
				dis_fm = HighTime * 0.017; //计算超声波测量距离
				printf("dis_fm = %0.1fcm \r\n", dis_fm);
				
				Edge = 0;		  			 //一次采集完毕,清零
			}
		}
	}
}

Guess you like

Origin blog.csdn.net/qq_45830608/article/details/115011930