>_<: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 #include8 #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:MCU Xiaobai's Learning Series 3 Encountering 51 MCU
Next article:Analysis of startup code provided by Keil (STARTUP.A51)
- 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
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Three steps to govern hybrid multicloud environments
- Three steps to govern hybrid multicloud environments
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Analysis of Typical Anti-Reverse Connection Circuits for Automotive Electronics
- Consulting employment
- [Runhe Neptune Review] Four network communications
- EEWORLD University Hall----What can universal fast charging bring?
- Complete learning manual for using RT-Thread on RISC-V (based on Longan development board)
- How does Tbox protect itself to ensure data security? What good methods can you recommend?
- The amount of heat required to heat water
- High-efficiency integrated power supplies from Texas Instruments
- Encoder Problems
- When defining global variables in Keil, if an initial value is assigned, will the value of the variable remain the same every time the power is turned on again?