【HC-SR04】
HC-HR04 ultrasonic distance measurement can provide non-contact distance sensing function of 2cm-400cm, and the ranging accuracy can reach 3mm.
Basic working principle
1. Use IO port TRIG to trigger the ranging and give a high level signal of at least 10us.
2. The module automatically sends 8 40KHz square waves and automatically detects whether there is a signal returned.
3. When a signal is returned, a high level is output through the IO port ECHO. The duration of the high level is the time from the ultrasonic wave being emitted to the return. Test distance = (high level time * sound speed (340m/s))/2.
Timing diagram
The above timing diagram shows that you only need to provide a pulse trigger signal of more than 10uS, and the module will: send out 8 40kHz cycle levels and detect echoes. Once an echo signal is detected, an echo signal is output. The pulse width of the echo signal is proportional to the measured distance. Therefore, the distance can be calculated by the time interval from the transmitted signal to the received echo signal. Formula: uS/58=cm or uS/148=inch; or: distance = high level time * sound speed (340M/S) /2; It is recommended that the measurement cycle be more than 60ms to prevent the influence of the transmitted signal on the echo signal.
【program】
I use the STC15F2K60S2 microcontroller, and the internal crystal oscillator is set to 12M. The following program omits irrelevant statements such as pin definitions, and only provides program ideas for reference.
uchar Flag_hypertelorism = 0; //Ultrasonic distance is too far flag
float distance = 0;
uchar Flag_Fucker = 0; //Ultrasonic on flag
void main()
{
Time0_Init();
Time1_Init();
while(1)
{
if(Flag_Fucker == 1)
Ultrasonic();
}
}
/*Initialize timer 0*/
void Time0_Init(void)
{
// AUXR |= 0x80; //Timer 0 is in 1T mode
AUXR &= 0x7f; //Timer 0 is in 12T mode
TMOD = 0x00; //Set the timer to 16-bit auto-reload
TL0 = 0x60; //Initialize the timing value to 4ms
TH0 = 0xF0;
TR0 = 1; //Timer 0 starts timing
ET0 = 1; // Enable timer 0 interrupt
EA = 1;
}
//Timer 1 initialization
void Time1_Init()
{
// AUXR |= 0x40; //Timer 1 is in 1T mode
AUXR &= 0xdf; //Timer 1 is in 12T mode
TMOD = 0x00; //Set timer 1 to 16-bit auto-reload
TL1 = 0x60; //Initialize the timing value to 4ms
TH1 = 0xF0;
// TR1 = 1; // Temporarily disable timer 1
ET1 = 1; // Enable timer 1 interrupt
EA = 1;
}
/*Timer 0 interrupt*/
void Time0 () interrupt 1
{
static uchar time = 0;
// Can be omitted in automatic loading mode
// TL0 = T0MS;
// TH0 = T0MS >> 8;
time++;
if(time == 25) //Measure distance once every 100ms
{
time = 0;
Flag_Fucker = 1;
}
}
//Timer 1 overflow interrupt
void Time1() interrupt 3
{
Flag_hypertelorism = 1; //The ultrasonic distance is too far
}
//Ultrasonic distance measurement
void Ultrasonic()
{
uchar i;
Trig = 1; //Ultrasonic trigger signal is turned on
_nop_(); //Delay more than 10us
i = 30;
while(--i);
Trig = 0; //Ultrasonic trigger signal is turned off
while(!Echo); //Wait when Echo is 0
TR1 = 1; //Start timer 1
while(Echo == 1 && TF1 == 0); //Wait when Echo is 1
TR1 = 0; // Turn off timer 1
Distance_Count(); //Calculate distance
Flag_Fucker = 0; //Turn off ultrasonic ranging
}
// Distance value calculation
void Distance_Count()
{
float Text_time = 0;
if(Flag_hypertelorism == 0)
{
Text_time = ((TH1 - temp_TH) * 256 + (TL1 - temp_TL)) / 2.0;
TL1 = 0x60; //Initialize the timing value to 4ms
TH1 = 0xF0;
distance = Text_time * 0.34; //Unit: mm
}
else
{
distance = 9999;
Flag_hypertelorism = 0;
}
}
Previous article:51 MCU uses slot type optical coupler speed measurement module
Next article:STC15F2K60S2 sets the internal clock operating frequency
- Popular Resources
- Popular amplifiers
- Principles and Applications of Single Chip Microcomputers 3rd Edition (Zhang Yigang)
- Microcontroller Principles and Application Project Tutorial - Based on STC15W4K32S4 Series Microcontroller
- STC15 series emulation MCU project application tutorial (C language)
- MCU Principles and Interface Technology C51 Programming (Edited by Zhang Yigang)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Problems with sensor acquisition circuits
- What are the RF challenges of small cells for 5G networks?
- Basic Features of DSP TMS320C6000
- Let's take a look at this power switching circuit.
- What is the reason for the Flash Timeout error message when downloading the program on STM32F4?
- EEWORLD University ---- FreeRTOS on stm32 ST
- Read the white paper on Renesas Electronics' power module series and win double gifts: energy boost gift & 100% recommendation gift!
- Understanding common mode and differential mode
- 【McQueen Trial】Unboxing and microbit
- STM32f767zi UART