Program code
simulation diagram
The microcontroller source program is as follows:
#include #include /*************************Macro definition************************/ #define uchar unsigned char #define uint unsigned int /************Bit definition********************************/ sbit Trig=P3^7; //Ultrasonic module trigger bit sbit LEDRED=P1^0; sbit LEDGREEN=P1^2; sbit speaker=P1^7; sbit person=P1^6; sbit TLED = P1^5; /************Global variable declaration************************/ uint H,T; //define global variables, H--distance, T--time /***********Common cathode digital tube segment code table********************/ uchar a_code[]={ //Common cathode digital tube code 0~9 0x3f,0x06,0x5b,0x4f,0x66, 0x6d,0x7d,0x07,0x7f,0x6f, /*0x77,0x7c,0x39,0x5e,0x79, 0x71*/}; /***************Delay function****************************/ void delay(uint z) //*1ms delay function { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); } /*************Ultrasonic module startup function**************/ void start() { Trig=1; //Pull up the trigger signal to start the ultrasonic module to emit ultrasonic waves _nop_(); //Delay 10us _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); Trig=0; //Pull the trigger signal low } /*****************Digital tube display function*****************/ void display(unsigned int a,unsigned int b,unsigned int c,unsigned int d) { P2=0xfe; P0=a_code[a]; delay(5); P2=0xfd; P0=a_code[b]; delay(5); P2=0xfb; P0=a_code[c]; delay(5); P2=0xf7; P0=a_code[d]; delay(5); } /*********************Main function****************************/ void main() { Trig=0; LEDRED=1; LEDGREEN=1; speaker=0; EA=1; TMOD=0x19; //Timer 0 working mode: /*GATE=1 (Timer 0 is started by external interrupt 0 pin and TR0) C/T=0 (timing mode) M1M0=01(16-bit timing)*/ TH1=64580/256; TL1=65480%256; ET1=1; TR0=1; //Start timer 0 EX0=1; //Open external interrupt 0 IT0=1; //Set external interrupt 0 to falling edge trigger mode while(1) { start(); //Call the ultrasonic module start function display(H/1000,H%1000/100,H%1000%100/10,H%10); //Call the digital tube display function if(person==1) { TLED=1; if(H<=25) { LEDRED=0; LEDGREEN=1; TR1=1; } else { LEDRED=1; LEDGREEN=0; TR1=0; } } else { TLED=0; LEDRED=1; LEDGREEN=1; TR1=0; } } } /***************External interrupt 0 function**************************** The high level width of the echo output terminal is measured (when the rising edge arrives, timer 0 starts timing. When the falling edge arrives, an external interrupt is triggered, timer 0 is turned off, timing is stopped, and distance is calculated. ************************************************************/ void int0() interrupt 0 { TR0=0; //Turn off timer 0 EX0=0; //Disable external interrupt 0 T=(TH0*256+TL0); //Calculate the ultrasonic transmission time. The CPU uses a 12M clock, so the calculation unit is us. TH0=0; //Timer cleared TL0=0; H=T*0.017; //Calculate the distance in cm (Note: H=(T*17)/1000 cannot be used, as this will result in rounding) H=H+2; TR0=1; //Start timer 0 EX0=1; // Enable external interrupt 0 } void time1() interrupt 3 { TR1=0; TH1=65480/256; TL1=65480%256; speaker=~speaker; TR1=1; }
Previous article:Body temperature monitoring sound and light alarm device based on DS18B20
Next article:Taxi meter based on single chip microcomputer control
- 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
- Embedded System Basics: What is a Microcontroller MCU?
- Evaluation board quick test - based on TI Sitara Cortex-A9(3)
- Three-phase watt-hour meter transformer wiring
- How to export the sensor data on sensorile?
- Application of Doherty amplifier in 450W digital TV transmitter
- #Idle Market# is open, you sell your idle items and I'll give you gifts!
- Research on integrated navigation algorithm in case of GPS signal loss.pdf
- I want to build a data access platform for NB-IOT devices based on the COAP protocol. In addition to LIBCOAP, what other technologies are used?
- Beijing's first bicycle lane, 6.5 kilometers, 20 minutes, check-in successful
- Today at 10:00 AM, live broadcast with awards: Internet of Everything - Shijian Company and Microchip experts jointly discuss IoT solutions