1. Ultrasonic module model
Ultrasonic module is a commonly used distance measurement tool, generally used in obstacle avoidance vehicles and water level monitoring systems. The module model used in this tutorial is HC-SR04, and its appearance is as follows:
In addition to two power pins, the module also has TRIG and ECHO pins, which are connected to P2.0 and P2.1 of our development board respectively.
The working principle is to pull both pins low at first, then pull the TRIG pin high for more than 10 microseconds and then pull it low to generate a pulse start signal (in the program, we pull it high for 20 microseconds). Once the start signal is turned on, the ECHO pin will be pulled high and the ultrasonic wave will start to emit. When the ECHO pin detects the reflected signal, it will be pulled low because the speed of sound is 340m/s. Then the measured distance is
(The duration of the high level of the ECHO pin * 340m/s)/2.
In middle school, we learned to use sound waves to measure the depth of the seabed. Time * speed of sound = round trip distance, so it needs to be divided by 2 to get the true depth. The same is true for ultrasonic modules.
2. Software Analysis
The timer count function is used to calculate the duration of the ECHO pin high level. If the timer count overflows, it proves that the measured distance is too far and exceeds the module's distance measurement range (up to 4 meters). We will display 999 on the digital tube to indicate that the distance is too far. Within the measurement range, our digital tube will display the measured number of centimeters.
For example, if the timer counts 9216, then the duration of the high level is 9216*(12/11059200)=0.01s
The measured distance is 0.01*340/2=1.7m
The digital tube displays 170.
However, in order to simplify the calculation process of the microcontroller, we can calculate it like this
“( (X*12)/11059200 )*340*100/2” is the number of centimeters, which can be simplified to “X/54”, where X is the count value of the timer.
The code in this lecture requires the use of "_nop_();" in "#include 3. Code #include #include #include #include sbit TRIG = P2^0; sbit ECHO = P2^1; u8 FLAG = 0; void delay_20us() { _nop_();_nop_();_nop_();_nop_();_nop_(); _nop_();_nop_();_nop_();_nop_();_nop_(); _nop_();_nop_();_nop_();_nop_();_nop_(); _nop_();_nop_();_nop_();_nop_();_nop_(); } void main() { u16 time_data,CM_data; TRIG = 0; ECHO = 0; LED_Init(); //Initialize LED hardware module EA = 1; //Close the main interrupt switch TIM1_Init(1000,0); //1ms timing, used to refresh the digital tube display, no fine-tuning is required if the timing accuracy is not high TMOD &= 0xF0; TMOD |= 0x01; ET0 = 1; while(1) { TH0 = 0; TL0 = 0; FLAG = 0; //If the measurement was out of range last time, FLAG was set to 1, and it should be cleared to 0 this time. //Start transmitting sound waves TRIG = 1; delay_20us(); TRIG = 0; while(!ECHO); //The sound wave starts to be emitted, waiting for the ECHO pin to be pulled high to exit this loop TR0 = 1; //When the ECHO pin is pulled high, the timer is turned on to count while(ECHO==1 && FLAG==0); //When receiving the signal from the sound wave, the ECHO pin is pulled low and the loop is exited; if the timer overflows, the interrupt function will be executed "FLAG=1;" //That is, the measured distance is too far, and no reflected signal is received. The condition of "FLAG==0" is not met, so this loop statement can only be ended. TR0 = 0; //Close the timer and end the count time_data =TH0; time_data=(time_data<<8)|TL0; //Combining the values in two eight-bit registers into a 16-bit variable value, which is the timer count value CM_data = time_data/54; //Get the expression of centimeters if(FLAG==1){ShowNumber(999); delay_ms(100);} //Show 999 when out of range. The purpose of delay: for example, when measuring between 5cm and 6cm, the digital tube will frequently display between 5 and 6. Adding delay will make them display less frequently. else {ShowNumber(CM_data); delay_ms(100);} } } void TIM0_IRQHandler() interrupt 1 { FLAG=1; } void TIM1_IRQHandler() interrupt 3 { TH1 = T1RH; //Reload the reload value TL1 = T1RL; SEG_Scan(); }
Previous article:51 single chip microcomputer-servo and buttons
Next article:51 MCU-Serial Communication Introduction
- 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
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- Design of high-precision multi-channel time-to-digital converter based on FPGA
- Design ideas for lithium battery protection circuit and charging scheme
- The problem of not being able to find playback data occurs when configuring openpilot on Ubuntu
- Discussion: When 5G encounters an earthquake
- [GD32E231 DIY Contest] 06. Automatic fish feeding robot: ADC/DMA/TIMER0/serial port
- Are there any recommendations for 5V boost switching chips?
- Excellent materials, no points required: Album of previous competition questions of the National Undergraduate Electronic Design Competition
- How to determine the grounding method for a circuit board built with circuit modules?
- 50% charge in 5 minutes! How powerful is the much-hyped GaN fast charging?
- A comprehensive analysis of the principles of various lightning protection circuits