1. Basic working principle
(1) Use IO port TRIG to trigger 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 return.
(3) If there is a signal return, a high level is output through the IO port ECHO. The duration of the high level is the time from the ultrasonic wave to the return. The test distance = (high level time * sound speed (340M/s) / 2 (the time for the ultrasonic wave to be transmitted and returned, so it must be divided by 2))
2. Module physical picture
3. Driver
There is a link below the article. Since there is no HR04 module in proteus, another microcontroller can be used to simulate the HR-04 module during simulation, and then the microcontroller and the microcontroller communicate. Serial communication cannot be used because the ultrasonic ranging is using a timer for timing, and the baud rate setting uses timer 1. There is no way to use timer 1 to set the baud rate. The process is a bit troublesome, so I didn't build it, but the others are built.
4. Effect diagram
The AT89C52 microcontroller source program is as follows:
/*
Experimental description:
STC89C52 Crystal: 12Mhz Module: HR04 LCD1602
LCD1602 LCD module-->MCU pins
Refer to the LCD1602 liquid crystal display experiment wiring (the experimental phenomenon in the corresponding experimental chapter of the development strategy has wiring instructions)
Ultrasonic module-->MCU pins
TRIG-->P21
ECHO-->P20
LCD1602_RW-->P2^5
LCD1602_RS-->P2^6
LCD1602_E-->P2^7
Because there is no HR04 module in proteus, HR04 is an analog signal, we can use an analog signal to replace HR04, the processing method is the same
Experimental phenomena:
The distance detected by ultrasonic wave is displayed on LCD1602 in mm.
Only for communication and learning
*/
#include #include #include "lcd.h" sbit Trig = P2^1; sbit Echo = P2^0; unsigned char PuZh[]=" Pechin Science "; //Display distance unsigned char code ASCII[15] = {'0','1','2','3','4','5','6','7','8','9','.','-','M'}; static unsigned char DisNum = 0; //display pointer unsigned int time=0; unsigned long S=0; bit flag =0; unsigned char disbuff[4] ={ 0,0,0,0,}; /*********************************************************************************** * Function name: main * Function: Main function * Input: None * Output: None ***********************************************************************************/ void Conut(void) { time=TH0*256+TL0; TH0=0; TL0=0; S=(time*1.7)/100; //The calculated value is CM if((S>=700)||flag==1) //Out of measurement range, display "-" { flag=0; DisplayOneChar(0, 1, ASCII[11]); DisplayOneChar(1, 1, ASCII[10]); //Display dot DisplayOneChar(2, 1, ASCII[11]); DisplayOneChar(3, 1, ASCII[11]); DisplayOneChar(4, 1, ASCII[12]); //Display M } else { disbuff[0]=S%1000/100; disbuff[1]=S%1000%100/10; disbuff[2]=S%1000%10%10; DisplayOneChar(0, 1, ASCII[disbuff[0]]); DisplayOneChar(1, 1, ASCII[10]); //Display dot DisplayOneChar(2, 1, ASCII[disbuff[1]]); DisplayOneChar(3, 1, ASCII[disbuff[2]]); DisplayOneChar(4, 1, ASCII[12]); //Display M } } void zd0() interrupt 1 //T0 interrupt is used for counter overflow, exceeding the ranging range { flag=1; //interrupt overflow flag } void StartModule() //Start module { Trig=1; //Start the module once _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); Trig=0; } void delayms(unsigned int ms) { unsigned char i=100,j; for(;ms;ms--) { while(--i) { j=10; while(--j); } } } void main(void) { TMOD=0x01; //Set T0 to mode 1, GATE=1; TH0=0; TL0=0; ET0=1; //Enable T0 interrupt EA=1; // Enable general interrupt LcdInit(); LcdShowStr(0,0,PuZh); while(1) { StartModule(); while(!Echo); //Wait when RX is zero TR0=1; //Start counting while(Echo); //When RX is 1 count and wait TR0=0; //Close the count Conut(); //Calculation delayms(80); } }
Previous article:The production of a simple calculator based on 51 single chip microcomputer matrix keyboard
Next article:Use a single-chip microcomputer to create the highest precision time system, which can be transplanted into any clock program
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- CodeBug Hands-on Learning 1 - Unboxing
- Please help me solve the problem of converting gerber files to PCB files
- HP laptop battery is swollen, where can I get a new one?
- pyRTOS(1): Introduction
- CircuitPython 5.0.0 Beta 5 released
- Problem with conditional execution with $value$plusargs
- An Engineer's Guide to EMI - Specification and Measurement
- C5000 compiles SUBC instruction to implement division
- Improvements and updates to the forum's "Invite Netizens" function
- Using Mao Zedong Thought to Improve the Efficiency of Counter-inspiration