I. Preface
The HC-SR04 ultrasonic ranging module provides 2cm-400cm non-contact distance sensing, ranging accuracy up to 3mm, and the module includes an ultrasonic transmitter, receiver and control circuitry. such as a smart car ranging and steering, or some projects, often used. Intelligent car ranging can be found in the front of the obstacles in time, so that the smart car can be turned in time to avoid obstacles, so, we will learn this sensor today.
Two. Working principle
1. The ultrasonic module is connected to the power supply and ground.
2. Input a high-power square wave with a length of 20us to the Pulse trigger pin (trig)
3. After entering the square wave, the module will automatically emit 8 40KHz of sound waves, while the echo pin (echo) End of the level will be changed from 0 to 1; (timer timer should be started at this time)
4. When the ultrasonic return is received by the module, the level of the echo pin end will be changed from 1 to 0, (at this time the timer count should be stopped), the timer noted that the timing of the ultrasonic wave from the launch to the total length of return.
5. According to the sound in the air speed of 344 m/s, the measured distance can be calculated.
To learn and apply sensors, it is critical to learn to understand the timing diagram of a sensor, so let's take a look at the timing trigger diagram of hc-sr04.
Let's analyze this time series diagram, start the Hc-rs04 ranging module by the trigger signal, that is, the host to send at least 10us high level, trigger Hc-rs04, the module internal signal is the sensor automatically respond, we do not have to control it. The output echo signal is what we need to focus on. The high level of the signal output is the time it takes for the ultrasound to be sent back to the reception. With a timer, you can record this period of time, calculate the distance, do not forget the result to be in addition to 2, because the total time is the sum of time sent and received.
Three. Procedures
#include <reg52.h>
#define UCHAR unsigned char
#define UINT unsigned int
Sbit echo=p3^2;
Sbit trig=p1^5;
Bit flag=0;
/*********************************** function: void Delay5us ()----------------------Description: 5 microsecond delay function parameter: No return value: No ********** *************************/
void Delay5us ()
{Uchar I; i--; i--; i--; i--; i--; i--; }
void delayms (UINT ms)
{uint x, y; for (x=ms;x>0;x--)
for (y=110;y>0;y--); }
void Init ()
{
tmod=0x21;
SCON = 0x50;
Ea=1;
Es=1;
Et0=1;
Et1=1;
TH1=0XFD;
TL1=0XFD;
Tr1=1;
}
void Send (UINT dat)
{
Sbuf= (dat/100) + 48; Send hundreds
while (! TI);
ti=0;
sbuf= (DAT%100/10) +48; Send 10-bit
while (! TI); ti=0;
sbuf= (dat%10) +48; Send Bits
while (! TI); ti=0;
}
UINT Conut (void)
{
UINT Time; Used to record the measured time
UINT SS; Used to record the measured distance
Time = (th0*256+tl0) *1.09; Total time
th0=0; tl0=0;
if (flag==1)//If the measurement distance is exceeded, the data is not transmitted
{flag=0; ss=0; }
Else
{
ss=time*1.7/100; The speed of sound in the air is 340m/s
}
return SS; }
void Main (void) {
UINT SS; Init ();
while (1)
{
trig=1;
Delay5us ();
Delay5us ();
trig=0;
while (! Echo);
Tr0=1;
while (Echo);
tr0=0;
Ss=conut (); Calculation
Send (ss); Send the calculated value through the serial port
Delayms (800);
}
Four. Summary
Above this program is the function of the distance from the sensor to obtain the value of the unit after the conversion with the serial port output, displayed in the serial assistant, the conversion unit for centimeters, there is a point to note that the trigger input and echo output on different boards are not the same, We have to according to their own schematic diagram and the microcontroller to modify their own, what problems can communicate with each other 0.0
}
void Zd0 () interrupt 1 {flag=1; }
Ultrasonic distance measuring hc-sr04 of sensors