Design an ultrasonic rangefinder that can be used for position monitoring in car reversing, construction sites, and some industrial sites, and can also be used for measuring liquid level, well depth, and pipeline length. The measurement range is required to be 0.10-3.00m, the measurement accuracy is 1cm, there is no direct contact with the object being measured, and the measurement results can be displayed clearly and stably.
2. Design Idea
Ultrasonic sensor and its distance measurement principle
Ultrasonic waves refer to mechanical waves with a frequency higher than 20KHz. In order to use ultrasonic waves as a detection method, ultrasonic waves must be generated and received. The device that performs this function is an ultrasonic sensor, which is usually called an ultrasonic transducer or ultrasonic probe. Ultrasonic sensors have a transmitter and a receiver, but an ultrasonic sensor can also have the dual function of sending and receiving sound waves. Ultrasonic sensors use the principle of piezoelectric effect to convert electrical energy and ultrasonic waves into each other, that is, when transmitting ultrasonic waves, electrical energy is converted to emit ultrasonic waves; and when receiving echoes, ultrasonic vibrations are converted into electrical signals.
The principle of ultrasonic distance measurement generally adopts the time of flight method TOF (time off light). First, measure the time it takes for the ultrasonic wave to be emitted and return after encountering an obstacle, and then multiply it by the speed of the ultrasonic wave to get twice the distance between the sound source and the obstacle.
There are many ways to measure distance. For short distances, you can use a ruler, for long distances, there are laser distance measurement, etc. Ultrasonic distance measurement is suitable for high-precision medium and long distance measurements. Because the propagation speed of ultrasound in standard air is 331.45 meters per second, the microcontroller is responsible for timing, and the microcontroller uses a 12.0M crystal oscillator, so the measurement accuracy of this system can theoretically reach the millimeter level.
Ultrasonic waves can be used to measure distances because they have strong directivity, slow energy consumption, and long propagation distances in the medium. Using ultrasonic waves to detect distances is convenient to design, simple to calculate, and can meet the requirements in terms of measurement accuracy.
Ultrasonic generators can be divided into two categories: one is to generate ultrasonic waves electrically, and the other is to generate ultrasonic waves mechanically. This topic belongs to close-range measurement, which can be achieved using commonly used piezoelectric ultrasonic transducers.
According to the design requirements and comprehensive factors, the AT89S51 microcontroller can be used as the main controller, the dynamic scanning method can be used to realize the LED digital display, and the ultrasonic drive signal is completed by the microcontroller timer. The system block diagram of the ultrasonic rangefinder is shown in the figure below:
Ultrasonic range finder system design block diagram
3. System composition
Hardware parts
It is mainly composed of three parts: single-chip computer system and display circuit, ultrasonic transmitting circuit and ultrasonic detection receiving circuit. AT89S51 is used to control the CX20106A infrared receiving chip and TCT40-10 series ultrasonic conversion module. The single-chip computer controls the transmission of ultrasonic waves through the P1.0 pin through the inverter, and then the single-chip computer continuously detects the INT0 pin. When the level of the INT0 pin changes from high level to low level, it is considered that the ultrasonic wave has returned. The data counted by the counter is the time experienced by the ultrasonic wave, and the distance between the sensor and the obstacle can be obtained by conversion.
Software
It mainly consists of the main program, ultrasonic generation subroutine, ultrasonic reception interrupt program and display subroutine.
4. System Hardware Circuit Design
1. Single chip microcomputer system and display circuit
The single-chip microcomputer uses 89S51 or its compatible series. A 12MHz high-precision crystal oscillator is used to obtain a more stable clock frequency and reduce measurement errors. The single-chip microcomputer uses the P1.0 port to output the 40KHz square wave signal required by the ultrasonic converter, and uses the external interrupt 0 port to detect the return signal output by the ultrasonic receiving circuit. The display circuit uses a simple and practical 4-bit common anode LED digital tube, the segment code is driven by 74LS244 , and the bit code is driven by a PNP transistor. The single-chip microcomputer system and display circuit are shown in the figure below
Schematic diagram of single chip microcomputer and display circuit
2. The schematic diagram of the ultrasonic transmitter circuit is shown in the reference journal:
Ultrasonic transmitter circuit schematic diagram
Function of piezoelectric ultrasonic transducer: It works by using the resonance of piezoelectric crystal. The internal structure is shown in the figure above. It has two piezoelectric chips and a resonance plate. When a pulse signal is applied to its two poles, and its frequency is equal to the natural oscillation frequency of the piezoelectric chip, the piezoelectric chip will resonate and drive the resonance plate to vibrate and generate ultrasonic waves. At this time, it is an ultrasonic generator; if no voltage is applied, when the resonance plate receives ultrasonic waves, it will compress the piezoelectric oscillator to vibrate and convert mechanical energy into electrical signals. At this time, it becomes an ultrasonic receiving transducer. The structure of the ultrasonic transmitting transducer is slightly different from that of the receiving transducer.
3. Ultrasonic detection receiving circuit
The circuit of the infrared conversion receiving journal uses the integrated circuit CX20106A , which is a special chip for infrared detection reception and is commonly used in infrared remote control receivers for televisions. Considering that the commonly used carrier frequency 38KHz of infrared remote control is close to the ranging ultrasonic frequency 40KHz, it can be used as an ultrasonic detection circuit. Experiments have shown that it has high sensitivity and strong anti-interference ability. By appropriately changing the size of C4, the sensitivity and anti-interference ability of the receiving circuit can be changed.
Ultrasonic receiving circuit diagram
5. System Programming
The ultrasonic distance measurement software design mainly consists of the main program, ultrasonic emission subroutine, ultrasonic receiving interrupt program and display subroutine. The following introduces the algorithm, main program, ultrasonic emission subroutine and ultrasonic receiving interrupt program of the ultrasonic distance meter one by one.
1. Algorithm design of ultrasonic rangefinder
The figure below illustrates the principle of ultrasonic ranging, that is, an ultrasonic signal is emitted by the ultrasonic generator T at a certain moment. When the ultrasonic wave encounters the object to be measured and reflects back, it is received by the ultrasonic receiver R. In this way, as long as the time from the generation of the signal to the reception of the return signal is calculated, the distance between the ultrasonic generator and the reflecting object can be calculated.
Distance calculation formula: d=s/2=(c*t)/2
*d is the distance between the object being measured and the rangefinder, s is the round trip distance of the sound wave, c is the speed of sound, and t is the time taken for the sound wave to go back and forth
The speed of sound c is related to temperature. If the temperature does not change much, the speed of sound can be considered to be basically unchanged. If the ranging accuracy is very high, it should be corrected by temperature compensation. After the speed of sound is determined, the distance can be obtained by measuring the round trip time of the ultrasonic wave. Temperature compensation can be performed by adding a temperature sensor to the system to monitor the ambient temperature. Here, DS18B20 can be used to measure the ambient temperature, and the speed of sound can be determined according to different ambient temperatures to improve the stability of ranging. In order to enhance the reliability of the system, anti-interference measures should be adopted in software and hardware.
Ultrasonic sound velocity table at different temperatures
temperature/
-30
-20
-10
0
10
20
30
100
Speed of sound c(m/s)
313
319
325
323
338
344
349
386
2. Main program
The main program first initializes the system environment, sets the working mode of timer T0 to 16-bit timer counter mode, sets the general interrupt enable bit EA and clears the display ports P0 and P2 to 0. Then the ultrasonic generator subroutine is called to send out an ultrasonic pulse. To avoid direct wave triggering caused by ultrasonic waves being directly transmitted from the transmitter to the receiver, it is necessary to delay 0.1ms (this is why the rangefinder has a minimum measurable distance) before opening the external interrupt 0 to receive the returned ultrasonic signal. Since a 12MHz crystal oscillator is used, the machine cycle is 1us. When the main program detects the flag bit of successful reception, the number in counter T0 (i.e. the time taken for the ultrasonic wave to go back and forth) is calculated according to the following formula to measure the distance between the measured object and the rangefinder. When designing, the speed of sound at 20°C is 344m/s, so:
d=(C*T0)/2=172T0/10000cm (where T0 is the count value of counter T0)
After measuring the distance, the result will be displayed in decimal BCD code, and then the ultrasonic pulse will be sent to repeat the measurement process. The main program flowchart is as follows
3. Ultrasonic generation subroutine and ultrasonic receiving interrupt program
The function of the ultrasonic generator subroutine is to send about 2 ultrasonic signals with a frequency of about 40KHz square waves and a pulse width of about 12us through the P1.0 port, and at the same time turn on the counter T0 for timing. The ultrasonic rangefinder main program uses external interrupt 0 to detect the return ultrasonic signal. Once the return ultrasonic signal is received (the INT0 pin appears low level), it immediately enters the interrupt program. After entering the interrupt, the timer T0 is immediately turned off to stop timing, and the ranging success flag word is assigned a value of 1. If the ultrasonic return signal has not been detected when the timer overflows, the timer T0 overflow interrupt will turn off the external interrupt 0, and the ranging success flag word is assigned a value of 2 to indicate that the ranging is unsuccessful.
6. Software and hardware debugging and performance
The ultrasonic rangefinder is made and debugged. The ultrasonic transmitter and receiver use Φ15 ultrasonic transducers TCT40-10 F1 (T transmitter) and TCT40-10 S1 (R receiver). The center frequency is 40kHz. The center axes of the two transducers should be kept parallel and 4 to 8 cm apart during installation. There are no special requirements for other components. If the ultrasonic receiving circuit can be shielded with a metal shell, the anti-interference ability can be improved. According to the different measurement range requirements, the size of the filter capacitor C4 connected in parallel with the receiving transducer can be appropriately adjusted to obtain the appropriate receiving sensitivity and anti-interference ability.
After the hardware circuit is completed and debugged, the program can be compiled and downloaded to the microcontroller for trial operation. According to the actual situation, the pulse width sent by the ultrasonic generator subroutine and the interval between two measurements can be modified to meet the measurement needs of different distances. According to the designed circuit parameters and program, the range of the rangefinder is 0.07~5.5m, and the maximum error of the rangefinder does not exceed 1cm. After the system is debugged, multiple experimental analyses of measurement errors and repeated consistency should be carried out to continuously optimize the system to meet the measurement requirements of actual use.
Subsequent work needs to be verified after experimentation
According to the reference circuit and the integrated circuit device, the ranging range is limited to 10m.
Program List
The following is the ultrasonic ranging control source program written in assembly language:
Using AT89S51 12MHz crystal oscillator
Display buffer unit is between 40H and 43H, use memory 44H, 45H, 46H to calculate distance
20H for logo
VOUT EQU P1.0 ;Pulse output port
*Interrupt entry procedure*
ORG 0000H
LJMP START
ORG 0003H
LJMP PINT0
ORG 000BH
LJMP INTT0
ORG 0013H
RARELY
ORG 001BH
LJMP INTT1
ORG 0023H
RARELY
ORG 002BH
RARELY
*Main Program*
START: MOV SP, #4FH
MOV R0, #40H; 40~43H are display data storage units (40 is the highest bit)
MOV R7,#0BH
CLEARDISP:MOV @R0, #00H
INC R0
DJNZ R7, CLEARDISP
MOV 20H, #00H
MOV TMOD, #21H ; T1 is 8-bit auto-reload mode, T0 is 16-bit timer
MOV TH0, #00H; 65ms initial value
MOV TL0, #00H ;40KHz initial value
MOV TH1, #0F2H
MOV TL1, #0F2H
MOV P0, #0FFH
MOV P1, #0FFH
MOV P2, #0FFH
MOV P3, #0FFH
MOV R4, #04H; Ultrasonic pulse number control (half of the assigned value)
SETB PX0
SETB ET0
STEB EA
CLR 00H
SETB TR0 ; Start the ranging timer
START1: LCALL DISPLAY
JNB 00H, START1; The flag is 1 when the reflected signal is received
CLR EA
LCALL WORK ; Distance calculation subroutine
SETB OF
CLR 00H
SETB TR0 ;Restart the ranging timer
MOV R2, #64H; Measurement interval control (about 4*100=400ms)
LOOP: LCALL DISPLAY
DJNZ R2, LOOP
SJMP START 1
*Interrupt program*
;T0 interrupt, interrupt once every 65ms
INTT0: CLR EA
CLR TR0
MOV TH0, #00H
MOV TL0, #00H
SETB ET1
SETB OF
SETB TR0; Start timer T0 to calculate the ultrasonic round trip time
SETB TR1; Start the ultrasonic timer T1
OUT: RETI
;T1 interrupt, used for sending ultrasonic waves
INTT1: CPL VOUT
DJNZ R4,RETIOUT
CLR TR1; Ultrasonic transmission is completed, turn off T1
CLR ET1
MOV R4,#04H
SETB EX0 ; Enable receive echo interrupt
RETIOUT: RETI
;External interrupt 0, enter when receiving echo
PINT0: CLR TR0; turn off the counter
CLR TR1
CLR ET1
CLR EA
CLR EX0
MOV 44H, TL0; Move the count value into the processing unit
MOV 45H, TH0
SETB 00H ; Receive success flag
RARELY
*Delayed Program*
DL1MS: MOV R6, #14H
DL1: MOV R7, #19H
DL2: DJNZ R6, DL2
DJNZ R6, DL1
RIGHT
*Show Program*
;40H is the highest bit, 43H is the lowest bit, scan the highest bit first
DISPLAY: MOV R1, #40H;G
MOV R5,#0F7H;G
PLAY: MOV A, R5
MOV P0, #0FFH
MOV P2, A
MOV A, @R1
MOV DPTR, #TAB
MOVC A, @A+DPTR
MOV P0, A
LCALL DLIMS
INC R1
MOV A, R5
JNB ACC.0, ENDOUT;G
RR A
MOV R5, A
AJMP PLAY
ENDOUT; MOV P2, #0FFH
MOV P0, #0FFH
RIGHT
TAB; DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H,0FFH,88H,0BFH
; Common anode digital tube 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, not bright, A, —
*Distance calculation program (= calculated value × 17/1000cm) Approximate
WORK: PUSH ACC
PUSH PSW
PUSH B
MOV PSW, #18H
MOV R3, 45H
MOV R2, 44H
MOV R1, #00D
MOV R0, #17D
LCALL MUL2BY2
MOV R3, #03H
MOV R2, #0E8H
LCALL DIV4BY2
LCALL DIV4BY2
MOV 40H, R4
MOV A, 40H
JNZ JJ0
MOV 40H, #0AH; the highest bit is 0, not lit
JJ0: MOV A R0
MOV R4, A
MOV A R1
MOV R5 A
MOV R3, #00D
MOV R2, #100D
LCALL DIV4BY2
MOV 41H, R4
MOV A, 41H
JNZ JJ1
MOV A, 40H; this high bit is 0, first check whether the highest bit is off
SUBB A, #0AH
JNZ JJ1
MOV 41H, #0AH; The highest bit is not lit, and the second highest bit is not lit either
JJ1: MOV A, R0
MOV R4, A
MOV A, R1
MOV R5, A
MOV R3, #00D
MOV R2, #10D
LCALL DIV4BY2
MOV 42H, R4
MOV AT 42H
JNZ JJ2
MOV A, 41H; The second highest bit is 0, first check whether the second highest bit is off
SUBB A, #0AH
JNZ JJ2
MOV 42H, #0AH; The second highest bit is not lit, and the second highest bit is not lit either
JJ2: MOV 43H, R0
POP B
POP PSW
POP ACC
RIGHT
* Two-byte unsigned number multiplication program
MUL2BY2: CLR A
MOV R7, A
MOV R6, A
MOV R5, A
MOV R4, A
MOV 46H, #10H
MULLOOP1: CLR C
MOV A, R4
RLC A
MOV R4, A
MOV A, R5
RLC A
MOV R5, A
MOV A, R6
RLC A
MOV R6, A
MOV A, R7
RLC A
MOV R7, A
MOV A, R0
RLC A
MOV R0, A
MOV A, R1
RLC A
MOV R1, A
JNC MULLOOP2
MOV A, R4
ADD A, R2
MOV R4, A
MOV A, R5
ADDC A, R3
MOV R5, A
MOV A, R6
ADDC A, #00H
MOV R6, A
MOV A, R7
ADDC A, #00H
MOV R7, A
MULLOOP2: DJNZ 46H, MULLOOP1
RIGHT
*Four-byte/two-byte unsigned number division program*
DIV4BY2: MOV 46H, #20H
MOV R0, #00H
MOV R1, #00H
DIVLOOP1: MOV A, R4
RLC A
MOV R4, A
MOV A, R5
RLC A
MOV R5, A
MOV A, R6
RLC A
MOV R6, A
MOV A, R7
RLC A
MOV R7, A
MOV A, R0
RLC A
MOV R0, A
MOV A, R1
RLC A
MOV R1, A
CLR C
MOV A, R0
SUBB A, R2
MOV B, A
MOV A, R1
SUBB A, R3
JC DIVLOOP2
MOV R0, B
MOV R1, A
DIVLOOP2: CPL C
DJNZ 46H, DIVLOOP1
MOV A, R4
RLC A
MOV R4, A
MOV A, R5
RLC A
MOV R5, A
MOV A, R6
RLC A
MOV R6, A
MOV A, R7
RLC A
MOV R7, A
RIGHT
;
END
Attached C51 program
#include
#define uchar unsigned char
#define uint unsigned int
#define long unsigned long
extern void cs_t(void);
extern void delay(uint);
extern void display(uchar*);
//data fly display(fly*);
data flying testok;
void main (void)
{
data fly dispram[5];
data uint i;
data head time;
P0=0xff;
P2=0xff;
TMOD=0x11;
IE=0x80;
while (1)
{
cs_t();
delay(1);
testok=0;
EX0=1;
ET0=1;
while(! testok) display(dispram);
if (1==testok)
{
time=TH0;
time=(time<<8)| TL0;
time*=172;
time/=10000;
dispram[0]=(fly) (time%10);
time/=10;
dispram[1]=(fly) (time%10);
time/=10;
dispram[2]=(fly) (time%10);
dispram[3]=(fly) (time/10);
if (0==dispram[3]) dispram[3]=17;
} else
{
dispram [0]=16;
dispram [1]=16;
dispram [2]=16;
dispram [3]=16;
}
for (i=0;i<300;i++) display(dispram);
}
}
void cs_r(void) interrupt 0
{
TR0=0;
ET0=0;
EX0=0;
testok=1;
}
void overtime(void) interrupt 1
{
EX0=0;
TR0=0;
ET0=0;
testok=2;
}
NAME CS_T
?PR?CS_T?CS_T SEGMENT CODE
PUBLIC CS_T
RSEG ?PR?CS_T?CS_T
CS_T: PUSH ACC
MOV TH0, #00H
MOV TL0, #00H
MOV A, #4D
SETB TR0
CS_T1: CPL p1.0
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
DJNZ ACC,CS_T1
POP ACC
RIGHT
;
END
name delay
?pr?_delay?delay segment code
public _delay
rseg ?pr?_delay?delay
_delay: push acc
move a,r7
jz dela1
inc r6
dela1: mov r5,#50d
djnz r5, $
djnz r7,dela1
djnz r6,dela1
pop acc
right
end
NAME DISPLAY
?PR?_DISPLAY?display segment code
?co?_DISPLAY?display segment data
EXTRN CODE (_DELAY)
PUBLIC _DISPLAY
RSEG ?CO?_DISPLAY?DISPLAY
?_display?byte:
dispbit: ds 1
dispnum: ds 1
rseg ?pr?_display?display
_display: push acc
push dph
push dpl
push psw
inc dispnum
mov a,dispnum
cjne a,#4d,disp1
DISP1: JC DISP2
MOV DISPNUM,#00H
MOV DISPBIT,#0FEH
DISP2: MOV A,R1
ADD A,DISPNUM
MOV R0,A
MOV A,@R0
MOV DPTR,#DISPTABLE
MOVC A,@A+DPTR
MOV P0,A
MOV A,DISPNUM
CJNE A,#2D,DISP3
CLR P0.7
DISP3: MOV P2,DISPBIT
MOV R5, #00H
MOV R7,#0AH
LCALL _DELAY
MOV P0,#0FFH
MOV P2,#0FFH
MOV A,DISPBIT
RL A
MOV DISPBIT,A
POP PSW
POP DPL
POP DPH
POP ACC
RIGHT
DISPTABLE: DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H,88H,83H,0C6H,0A1H,86H,8EH,0BFH,0FFH
END
Previous article:C language program using DS18B20 temperature sensor in single chip microcomputer (reference 3)
Next article:C language program using DS18B20 temperature sensor in single chip microcomputer (reference 8)
- Popular Resources
- Popular amplifiers
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
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- MEMS sensor with AI programmable core (ISPU - intelligent sensor processing unit)
- Application of Finite State Machine in Embedded Software
- I'm begging for a solution for medical ultrasonic atomizer!!!
- How to set the parameters of RC snubber circuit of mosfet switch tube in boost circuit
- Why is there a small series resistor on the digital signal line? (DC2222A LTC2500)
- DSP and Always-on Function
- A boss can work in multiple companies at the same time, so why would an engineer be fired if he has work experience in two companies?
- 【CH579M-R1】What is TMOS?
- Why is there a 50mV difference between the two input terminals of an op amp working in negative feedback?
- Award-winning live broadcast: Bluetooth 5.0, Thread, Zigbee, TI 15.4-Stack, multi-protocol management, all explained to you at once