The use of GY-53 infrared laser ranging module and the realization of pwm mode code

1. Introduction to the module

GY-53 is a low-cost digital infrared distance sensor module. Working voltage 3-5v, low power consumption, small size.
The working principle is that the infrared LED emits light, and after irradiating the object to be measured, the return light is received by the MCU, and the MCU calculates the time difference to obtain the distance, and directly outputs the distance value. This module has two ways to read data, that is, serial port UART (TTL level) + PWM (1 line) or chip IIC mode, the baud rate of the serial port is 9600bps and 115200bps, configurable, there are two ways of continuous and query output , the settings can be saved after power off. In addition, the module can set the working mode of a separate sensor chip. As a simple sensor module, the MCU does not participate in data processing.
insert image description here

2. Pin description

The module has 12 pins in total, two VCCs, two GNDs, the TX pin is the serial port USART_TX, the RX pin is the serial port USART_RX, the PWM pin can convert the distance into PWM output, SDA is the chip SDA pin, SCL That is, the chip SCL pin, the PS pin is the serial port/IIC mode conversion pin, when ps=1 (default), the serial port UART mode, Pin3 is TX, Pin4 is RX, TTL level, PWM output works; when ps = 0 (When connected to GND) In ​​IIC mode, the user can operate the chip by himself, the module's own MCU does not operate the chip, and the PWM output does not work.

3. Introduction to use

I am using PWM mode, so here I will only introduce how to use PWM mode.
This ranging module is very similar to the ultrasonic module in PWM mode (it doesn’t matter if you haven’t used the ultrasonic module). The programming idea is roughly as follows:
When a high-level signal is received, the timer is turned on to start timing, and the signal is turned off after the signal is over. Timer, so that the high level time is obtained, the internal chip of the module will output a PWM wave with a period of 20HZ, and the high level time corresponds to the measured distance (this is determined by the internal chip of the module, we don’t care about it), the formula As follows:
* Distance (mm) = high level time (ms) 100 = high level time (us)/10
For example: the measured high level time is 10000us, then distance = 10000/10 = 1000mm

4. Code implementation

The basic timer TIM6 used to obtain the time is counted by interrupts.
Then by calling this function, you can get the distance:

  float Hcsr04GetLength(void)  
  {  
	uint32_t t = 0;  
	float  lengthTemp = 0;  
     delay_ms(500);

	while (GY53_Receive == 0);      //等待接收口高电平输出  
	OpenTimerForGY();               //打开定时器  
		
	while (GY53_Receive == 1);    //
	CloseTimerForGY();             //关闭定时器 
	
	t = GetGYTimer();              //获取时间,分辨率为1US   
	
	lengthTemp = (float)t / 10;
		
	return lengthTemp;
}  

The function to turn on the timer:

static void OpenTimerForGY(void)         
{  
//设置TIM计数器寄存器的值
TIM_SetCounter(BASIC_TIM,0); //清除计数  
time = 0;  
TIM_Cmd(BASIC_TIM,ENABLE);  //使能TIMX外设  
} 

The function to turn off the timer:

static void CloseTimerForGY(void)           
{  
	TIM_Cmd(BASIC_TIM,DISABLE); //使能TIMX外设  
} 

Function to get time:

uint32_t GetGYTimer(void)  
{  
	uint32_t t = 0;  
	t = time * 1000;      //将ms转化为us 
  t = t + TIM_GetCounter(BASIC_TIM);    //得到总us  
	TIM6-> CNT = 0;       //将TIM6计数寄存器的计数值清零 (上一个函数返回值)  
	delay_ms(50);
	return t;  
}  

5. Results

Call the above function in the main function, and print the obtained data through the serial port.
The main function is as follows:

int main (void)
{
	BASIC_TIM_Init();//基本定时器初始化
	
	USART_Config();  //串口初始化
	
	GY53_Init();
	
	TIM_Init();							//高级定时器初始化
	
	delay_ms(500);  
	
	while(1)
	{
		printf("距离为%3fmm\n",Hcsr04GetLength());
		delay_ms(500);
	}
}

The display results are as follows:
insert image description here
Note : I found in use that if the inclination of this module changes during use, it will have a greater impact on the measured value, so try to make the module vertical to the horizontal plane when using it, so as to get more accurate values.

Original code download address: https://download.csdn.net/download/weixin_43737995/11574506

Guess you like

Origin blog.csdn.net/weixin_43737995/article/details/99695863