An ultrasonic rangefinder is made using the STC89C52 microcontroller, ultrasonic module (HC-RS04), DS18B20 digital temperature sensor and LCD1602 liquid crystal display module. The first line of the LCD screen displays the temperature and ultrasonic speed "T:30°C V:349m/s", and the second line displays the measured distance "S=X.XXXm".
The designed rangefinder measures in meters, accurate to 3 decimal places (mm), and has a measuring range of 0.05m~5m.
1. Basic part
1. LCD display function
(1) When the power is turned on, the first line of the LCD screen displays the temperature and ultrasonic velocity, for example, "T: 30°C V: 349m/s", and the second line displays the measured distance "S = 0.000m"
(2) When you operate the corresponding function button, the first line of the LCD screen displays the temperature and ultrasonic velocity, for example, "T:30°C V:349m/s", and the second line displays the measured distance "S=X.XXXm".
(3) This ultrasonic rangefinder uses 4 independent buttons. The first button is for reset function; the second button is for single measurement function; the third button is for instantaneous continuous measurement function; the fourth button is for average continuous measurement function.
(4) The first button is for reset. Whenever you press this button, the second line of the LCD screen will display the measured distance "S=0.000m".
(5) The second button is for single measurement. Each time you press the button, the ultrasonic wave is emitted and the second line of the LCD screen displays a corresponding measurement distance "S=X.XXXm".
(6) The third button is for instantaneous continuous measurement. Press this button to continuously transmit ultrasonic waves, and the second line of the LCD screen will display the instantaneous measured distance "S=X.XXXm". Press this button again to keep the number for easy reading.
(7) The fourth button is for the average continuous measurement function. Press this button to continuously transmit ultrasonic waves, and the second line of the LCD screen will display the average measured distance "S=X.XXXm". Press this button again to keep the number for easy reading.
(8) The fifth button is the automatic/manual temperature setting button. It switches to manual temperature setting. At this time, you can use the plus and minus keys to adjust the temperature within the range of 0 to 50 degrees. Press the button again to return to the automatic temperature control state, and repeat this cycle.
(9) The sixth button is the temperature increase button. When in manual temperature control mode, pressing this button can increase the temperature to a maximum of 50 degrees Celsius.
(10) The 7th button is the temperature minus button. When in manual temperature control mode, pressing this button can reduce the temperature to a minimum of 0 degrees Celsius.
The program code is as follows:
/******************************************/
/* Ultrasonic ranging (automatic/manual temperature correction) */
/* (ultrasonic module + 18B20 + 1602 LCD) */
/******************************************/
#include #include #define uchar unsigned char #define uint unsigned int #define LcdData P0 //1602 data port sbit LCD_RS=P2^6; //1602 RS port sbit LCD_RW=P2^5; //1602 RW port sbit LCD_EN=P2^7; //1602 EN port sbit Echo=P2^4; //HC-SR04 receiving port sbit Trig=P2^3; //HC-SR04 transmitter port sbit Resets_Key=P1^0; //Reset key sbit Single_Key=P1^1; // Single measurement key error is large sbit Contin_Key=P1^2; //Continuous measurement key, small error sbit Averag_Key=P1^3; //Continuous (average measurement) key press, small error sbit Setting_Key=P1^4; //Temperature correction button sbit Add_Key=P1^5; //Temperature plus 1 button sbit Sub_Key=P1^6; //Temperature minus 1 button sbit DQ=P3^7; //DS18B20 single bus interface bit Temp_Flag; //Positive and negative temperature flag: Temp_Flag=0 if the temperature is positive, otherwise 1 uint temp=25; //temperature value bit flag_flow=0,flag_one=0,flag_clear=0,flag_con1=0,flag_con2=0,flag_temp=0; uchar i=0,m,j,k; uint time=0,S=0,S1=0,totle=0; float V=346.0; uint Sav[11]; //10 average values array for continuous measurement uchar Line1[16]={"T: C V:346m/s"}; //1602 The first line of initial character display array uchar Line2[16]={"S= m "}; //1602 second line initial character display array void Delayms(uchar xms); //delay xms function void WriteLcd(uchar Dat,bit x); //1602 write function void InitLcd(void); //1602 initialization function void DisplayLcd(); //1602 display function void init(); //initialization function void keyscan(); //key scan function void StartModule(); //Start module function void Conut(void); //Measurement calculation function void Delayus(uchar xus); //us level delay function bit Init_DS18B20(void); //Initialize DS18B20 function uchar Read_DS18B20(void); //Read DS18B20 function void Write_DS18B20(uchar Dat); //write DS18B20 function void GetTemp(); //Get temperature function void CalcTestTemp(); //temperature processing function void main(void) //main function { init(); InitLcd(); while(1) { keyscan(); //key scan function DisplayLcd(); if(flag_temp==0) { GetTemp(); CalcTestTemp(); } if(flag_one==1||flag_con1==1||flag_con2==1) { StartModule(); //Start transmitting ultrasonic waves 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 Delayus(200); flag_one=0; } } } void Delayms(uchar xms) //delay ms function { uchar i,j; for(i=xms;i>0;i--) for(j=110;j>0;j--); } void WriteLcd(uchar Dat,bit x) //1602 write function (x=0 when writing instructions, x=1 when writing data) { LCD_EN=0; LcdData=Dat; LCD_RS=x; LCD_RW=0; LCD_EN=1; Delayms(1); LCD_EN=0; } void InitLcd(void) //1602 initialization function { WriteLcd(0x38,0); //Function setting (38H), 8-bit data, 2-line display, 5*7 dot matrix WriteLcd(0x0C,0); //Display on/off setting (0CH), display on, cursor off, cursor does not flash WriteLcd(0x06,0); //Input mode setting (06H), after reading or writing a character, the address pointer increases by 1, and the cursor increases by 1 WriteLcd(0x01,0); // Clear the display (01H), clear the data in the data RAM } void DisplayLcd() //LCD display function { uchar y; V=(331.4+temp*0.607); Line1[2]=temp/10+0x30; Line1[3]=temp%10+0x30; Line1[4]=0xDF; //Display the small circle in front of C in ℃ Line1[10]=(uint)V/100+0x30; Line1[11]=(uint)V%100/10+0x30; Line1[12]=(uint)V%10+0x30; if(flag_clear==1) S1=0; if((S1>=7000)||flag_flow==1) //Out of measurement range, display "-" { flag_flow=0; Line2[2]='-'; Line2[3]='.'; Line2[4]='-'; Line2[5]='-'; Line2[6]='-'; } else { Line2[2]=S1/1000+0x30; Line2[3]='.'; Line2[4]=S1%1000/100+0x30; Line2[5]=S1%100/10+0x30; Line2[6]=S1%10+0x30; } WriteLcd(0x80,0); //Set the first line address for(y=0; y<16; y++) WriteLcd(Line1[y],1); WriteLcd(0xc0,0); //Set the second line address for(y=0; y<16; y++) WriteLcd(Line2[y],1); } void init() //initialization function { TMOD=0x01; //Set T0 to mode 1, GATE=1; TH0=0; TL0=0; ET0=1; //Enable T0 interrupt EA=1; // Enable general interrupt } void keyscan() //key scan function { if(Resets_Key==0) //Reset key { Delayms(5); if(Resets_Key==0) { while(Resets_Key==0); flag_clear=1; } } if(Single_Key==0) //Single measurement key { Delayms(5); if(Single_Key==0) { while(Single_Key==0); flag_one=1; flag_con1=0; flag_con2=0; flag_clear=0; } } if(Contin_Key==0) //Continuous instantaneous measurement key { Delayms(5); if(Contin_Key==0) { while(Contin_Key==0); flag_con1=~flag_con1; if(flag_con1==1) { flag_one=0; flag_con2=0; flag_clear=0; } } } if(Averag_Key==0) //Continuous average measurement key { Delayms(5); if(Averag_Key==0) { while(Averag_Key==0); flag_con2=~flag_con2; if(flag_con2==1) { flag_one=0; flag_con1=0; flag_clear=0; } } } if(Setting_Key==0) //Automatic and manual conversion keys { Delayms(5); if(Setting_Key==0) { while(Setting_Key==0); flag_temp=~flag_temp; } } if(Add_Key==0) //Temperature reduction key { Delayms(5); if(Add_Key==0) { while(Add_Key==0); if(flag_temp==1) { temp++; if(temp>=50) temp=50; } } } if(Sub_Key==0) //Temperature key { Delayms(5); if(Sub_Key==0) { while(Sub_Key==0); if(flag_temp==1) { if(temp>0) temp--; if(temp<=0) temp=0; } } } } void StartModule() //Start module function (trigger once, provide a high level greater than 10us) { Trig=1; //Start the module once Delayus(15); //Delay 2i+5=35us Trig=0; } void Conut(void) //Measurement calculation function { time=TH0*256+TL0;
Previous article:Tutorial on single chip servo control program and proteus simulation schematic diagram
Next article:Design of digital FM radio using single chip microcomputer + CXA1019S + phase-locked loop BU2614
- 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
- 【2022 Digi-Key Innovation Design Competition】Font Application in U8g2
- I want to buy a power supply. Does anyone have any recommendations?
- SinlinxA33 SD card programming
- 【ST NUCLEO-H743ZI Review】——by 54chenjq
- An indispensable element! Make fast charging design simple
- FAQ_ How to choose a high-speed crystal for BlueNRG-x
- 2021 STMicroelectronics Industry Summit Tour- Shanghai/Beijing
- Summary: RCSN Play Anxinke's new esp32s3 development board
- How should Japan deal with nuclear waste water? Give us some ideas
- RTL diagram timing, can anyone help me explain why the two outputs in this diagram are in the two-choice mode?