The ultrasonic model I used is US-020. The ultrasonic usage of the four interfaces is basically the same.
I. Overview
US-020 ultrasonic module ranging from 2cm ~ 7 m
Power supply voltage 5 V, static power consumption less than 3mA
Ii. Physical Map
Size: 45mm * 20mm * 1.6mm
Iii. Interfaces
1. VCC power supply, DC 5 V
2. Trig inputs over 10 US high to this pin to trigger module ranging
3. At the end of ECHO ranging, a high level will be output, and the electric power is usually the sum of the round-trip time of the ultrasonic signal.
4. Gnd grounding
Iv. Working Principle of ranging
As long as a high level above 10 US is input on the trig pin, the system will automatically emit 8 40 kHz ultrasonic pulses and then detect the echo signal. Output through ECHO pins after detection.
Calculation method: (echo high time * 340 m/s)/2
[Note]: Use a counter to detect the echo high time. The timer counting frequency is 1/12 of the crystal oscillator. The crystal oscillator frequency is xmhz and the Count value is T (mode 1 T = th0 * 256 + tl0 ).
Distance from L = (T * 12/(x * 10 ^ 6) * 340000/2 (mm)
= 0.17 * t (x = 12)
= (2.04/11.0592) * t (x = 11.0592)
V. Code Analysis
1. initialize the program and set a timer to calculate the echo high time.
Tmod = 0x01; // set t0 as Mode 1; th0 = 0; tl0 = 0; tr0 = 1; et0 = 1; // allows t0 to interrupt Ea = 1; // enable the total interrupt TR = 0;
2. Send more than 10 US high to trig
Tr = 1; _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); Tr = 0;
3. Wait for the echo high level and calculate the time
While (! EC); // wait for the High Level tr0 = 1; // open the timer while (EC); // wait for the low level tr0 = 0; // disable the timer time = th0 * 256 + tl0; // The calculation time l = 0.18446 * time; th0 = 0; // reset the timer tl0 = 0;
Vi. Total Code (including digital display)
# Include <reg51.h> # include <intrins. h> # define uchar unsigned char # define uint unsigned int // pin definition sbit TR = P2 ^ 0; // ultrasonic triggering sbit EC = P2 ^ 1; // ultrasonic output sbit key = P2 ^ 4; // press sbit ENCC = P2 ^ 5; // 38 decoder sbit encb = P2 ^ 6; sbit enca = P2 ^ 7; // P0 port connected to digital tube // definition variable float L = 0; // distance length (MM) unsigned char code led [] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f}; // function declaration void ultinit (); // ultrasonic initialization void ultstart (); // ultrasonic Trigger void count (); // calculate the distance from void show (); // display the distance from the screen void delay (); // delay function // ----------------- // ultrasonic initialization // ----------------- void ultinit () {tmod = 0x01; // set t0 as method 1; th0 = 0; tl0 = 0; tr0 = 1; et0 = 1; // allows t0 interrupt Ea = 1; // enables the total interrupt TR = 0 ;} // --------------- // ultrasonic triggered // ----------------- void ultstart () {TR = 1; _ NOP _(); _ NOP _(); _ NOP _ (); Tr = 0 ;} // --------------- // Calculate the distance // --------------- void count () {uint time = 0; while (! EC); // wait for the High Level tr0 = 1; // open the timer while (EC); // wait for the low level tr0 = 0; // disable the timer time = th0 * 256 + tl0; // The calculation time l = 0.18446 * time; th0 = 0; // reset the timer tl0 = 0 ;} // --------------- // display distance from the screen // ----------------- void show () {p0 = led [(INT) l % 10]; ENCC = 0; encb = 1; enca = 1; p0 = led [(INT) L/10% 10]; ENCC = 0; encb = 1; enca = 0; p0 = led [(INT) l/100% 10]; ENCC = 0; encb = 0; enca = 1; p0 = led [(INT) L/1000% 10]; ENCC = 0; encb = 0; enca = 0;} // --------------- // delay function // ----------------- void delay () {_ NOP _(); _ NOP _ ();} // ----------------- // main function // ----------------- void main () {ultinit (); Key = 1; while (1) {If (0 = Key) {delay (); If (0 = Key) {ultstart (); count (); Key = 1 ;}} show ();}}
"51 Single-Chip Microcomputer" Integrated Ultrasonic Ranging module Analysis + code