>_<: Ultrasonic rangefinder module:
>_<:51 MCU, 11.0592MHz crystal oscillator, basic example of sending collected data to the serial port:
>_<:Code:
1 /************************************************ *************************************************** *********/
2 //HC-SR04 Ultrasonic Distance Measurement Module DEMO Program
3 //Crystal: 11.0592
4 // Wiring: module TRIG connects to P1.2 ECH0 connects to P1.1
5 //Serial port baud rate 9600
6/************************************************ *************************************************** *********/
7 #include
8 #include
9 #include
10
11 #define uchar unsigned char
12 #define uint unsigned int
13 #define RX P1_1
14 #define TX P1_2
15
16
17 unsigned int time=0;
18 unsigned int timer=0;
19 float S=0;
20 bit flag =0;
twenty one
twenty two
twenty three /************************************************ **********/
24 void Conut(void)
25 {
26 time=TH0*256+TL0;
27 TH0=0;
28 TL0=0;
29 S=(time*1.87)/100; //The calculated value is CM
30 if(flag==1) //Out of measurement
31 {
32 flag=0;
33 printf("-----\n");
34 }
35 printf("S=%f\n",S);
36 }
37 /************************************************ **********/
38 void delayms(unsigned int ms)
39 {
40 unsigned char i=100,j;
41 for(;ms;ms--)
42 {
43 while(--i)
44 {
45 j=10;
46 while(--j);
47 }
48 }
49 }
50 /************************************************ **********/
51 void zd0() interrupt 1 //T0 interrupt is used for counter overflow, exceeding the ranging range
52 {
53 flag=1; //interrupt overflow flag
54 }
55 /************************************************ **********/
56 void StartModule() //T1 interrupt is used to scan the digital tube and count 800MS to start the module
57 {
58 TX=1; //800MS start the module once
59 _nop_();
60 _nop_();
61 _nop_();
62 _nop_();
63 _nop_();
64 _nop_();
65 _nop_();
66 _nop_();
67 _nop_();
68 _nop_();
69 _nop_();
70 _nop_();
71 _nop_();
72 _nop_();
73 _nop_();
74 _nop_();
75 _nop_();
76 _nop_();
77 _nop_();
78 _nop_();
79 _nop_();
80 TX=0;
81 }
82 /************************************************ **********/
83 void main(void)
84 {
85 TMOD=0x21; //Set T0 to mode 1, GATE=1;
86 SCON=0x50;
87 TH1=0xFD;
88 TL1=0xFD;
89 TH0=0;
90 TL0=0;
91 TR0=1;
92 ET0=1; //Enable T0 interrupt
93 TR1=1; //Start the timer
94 TI=1;
95
96 EA=1; // Enable general interrupt
97
98
99 while(1)
100 {
101 StartModule();
102 while(!RX); //Wait when RX is zero
103 TR0=1; //Start counting
104 while(RX); //When RX is 1, count and wait
105 TR0=0; //Close counting
106 Conut(); //Calculation
107 delayms(10); //100MS
108 }
109 }
Previous article:EEPROM 24c02 + digital tube + interrupt [statistics of boot times]
Next article:Programming to control the laptop Bluetooth to communicate with external Bluetooth devices
- 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
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- What is the difference between low inertia and high inertia of servo motors?
- "Playing with the board" + lighting up the SPI touch screen of Xintao
- 2020-7-5-Abdominal breathing training trainer
- ST MEMS Device Resource Library - Hardware Design Guide
- Do you have any use for STM8S005C6T6? Special price for delivery
- Explanation on DSP devices and general-purpose processors (GPP)
- Impedance matching of power amplifiers
- About electromagnetic shielding
- About Phase Modulation
- I am confused about K60, hope for your advice
- Coding and decoding of MSP430 infrared remote control