1. Design requirements:
Original title of graduation project:
This design is mainly used in indoor parking lots. It uses a 51 single-chip microcomputer or an embedded system, combined with an ultrasonic sensor, to detect whether there are vehicles parked in the parking space, and displays the parking space occupancy status in real time in the form of an LED module. It can also display the number of remaining parking spaces.
Ultrasonic sensors are used to detect parking space occupancy and calculate the remaining parking spaces in the area. We assume that there are 4 parking spaces in an area. Ultrasonic detection is used to detect that the digital lights at the corresponding positions of the occupied parking spaces are lit, and it is displayed at the same time: "2" parking spaces are remaining.
An ultrasonic sensor is installed at a certain distance on each parking space to prevent misjudgment. The remaining parking spaces are next to the LEDs to help give the number of remaining parking spaces in this area, rather than the total number of remaining parking spaces in the entire parking lot.
LED is the display module. This system is made of 2 parking spaces facing each other in the parking lot. A sensor is installed on each parking space. The ultrasonic ranging module must have 8 ultrasonic sensors. Instead of 16 pins, 9 pins can be used. One ultrasonic sensor is connected to one pin at the transmitting end, and an OR gate is used at the receiving end, occupying one pin of the microcontroller. Then the program is used to determine which ultrasonic sensor has an echo.
The digital tube only displays the number of remaining parking spaces in the area, not the number of occupied parking spaces. The total number of parking spaces remains unchanged and is divided into two areas, Area A and Area B. The eight parking spaces remain unchanged, but there are two more digital tubes, one more red light and one more green light. The buttons for controlling the traffic flow (add one and subtract one) are used instead of switches because the function of the ultrasonic sensor cannot be reflected during simulation. Odd numbers represent the entry of the parking space, and even numbers represent the exit of the parking space. Then only one ultrasonic ranging module is used for the or door, and the distance is set to 40-50 cm. A switch is placed on each parking space, a total of eight parking spaces (the questioner's problem has been solved)
Solution:
The 51 series single chip microcomputer (stc89c52) can meet the needs.
Three digital tubes, one to show the total number of remaining parking spaces, one to show the remaining parking spaces in area A, and one to show the remaining parking spaces in area B. Dynamic scanning requires 3+7=10 IO ports
Eight ultrasonic modules, the sending end is connected to the same IO port, and the eight receiving ends are connected to an IO port each. Occupies 9 IO ports
Eight LEDs, each corresponding to the status of a parking space. The red light is on when there is a car in the garage, and it is off when there is no car in the garage. It occupies 8 IO ports
One traffic light, if all 8 parking spaces in the garage have red lights, if there are still remaining spaces, the lights will be green. Occupies 2 IO ports
Total 29 IO ports
2. Simulation test:
The simulation uses an ultrasonic ranging module that is not available in the proteus library. It can 100% simulate the communication principle of the actual hc-sr04 ultrasonic ranging module and use the plus and minus buttons to simulate the actual distance of the object.
3. Code Interpretation
1. Configure the header file, which contains the register definition of the microcontroller
#include "reg52.H"
2. Redefine IO ports for easier reading and writing
sbit RX0 =P2^0; //ultrasonic receiving
sbit RX1 =P2^1;
sbit RX2 =P2^2;
sbit RX3 =P2^3;
sbit RX4 =P2^4;
sbit RX5 =P2^5;
sbit RX6 =P2^6;
sbit RX7 =P2^7;
sbit TX = P3^0; //Ultrasonic transmission
sbit LS1 = P3^3; //digital tube position
sbit LS2 = P3^4;
sbit LS3 = P3^5;
sbit LED_Green = P3^6; //Green light
sbit LED_Red = P3^7; //Red light
3. Define global variables to save parking space information, distance information, etc.
unsigned int time=0; //Ranging time
unsigned char flag=0; //Overflow flag
unsigned long S[8]=0; //distance data
unsigned char sg[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
unsigned char wei[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
unsigned char R=0; //8 indicator lights of the parking space
char a_carport,b_carport; //Remaining parking spaces in area A and area B
4. Delay function, use the execution of empty statements to consume CPU time to achieve the purpose of delay.
void delay_ms(unsigned int m) // millisecond delay
{
unsigned int b,c;
for(c=0;c } void delay(unsigned int t) // microsecond delay { while(t--); } 5. Digital tube display function, The digital tube is displayed in a dynamic scanning mode. First, the position is selected, then the data is sent, and the digital tubes are lit up in sequence. The visual residual effect makes the eyes see static. At the beginning, the first digital tube is displayed first, the first digital tube is selected, and the total remaining parking space information segment code value is sent to display the digital tube, then the second digital tube is displayed, and the segment code value of the remaining parking space in area A is sent, and then the third digital tube is displayed, and the segment code value of the remaining parking space in area B is sent. The digital tube can be displayed by executing the loop. void display(unsigned char ab,unsigned char a,unsigned char b) { TX=0; LS1=0;LS2=1;LS3=1;//bit selection, the first digital tube lights up P0=sg[ab]; //Send segment code and display numbers delay_ms(1); //stable LS1=1;LS2=0;LS3=1; P0=sg[a]; delay_ms(1); LS1=1;LS2=1;LS3=0; P0=sg[b]; delay_ms(1); } 6. Ultrasonic ranging program Since there are 8 ultrasonic modules here, there are 8 sending pins and 8 receiving pins. Obviously, this requires 16 pins, which takes up too many IO ports. Therefore, by combining the 8 sending pins into one sending pin, 9 IOs can be used to drive 8 ultrasonic modules. Each time, the 8 ultrasonic modules transmit ultrasonic waves, and then the first ultrasonic module starts to receive distance data. The steps for receiving and processing distance data are as follows: first read the timing value of T0. The T0 counters TH0 and TL0 are combined into a 16-bit counter, which counts by one every microsecond. Therefore, the value of the counter read is the time for the ultrasonic wave to go back and forth, the time for the two distances. Then, the distance calculation formula S[i]=(int)(time*1.7)/100 is obtained; the distance of each ultrasonic module is measured and repeated, i is counted from 0 to 7 until the 8th distance data is measured, the data is saved to the array, and the next step is started. void Superwave_Conut(void) //Ultrasonic distance measurement 8 groups { int i; for(i=0; i<8; i++) { if(i==0) { TX=1; delay(10); TX=0; while(!RX0); TR0=1; while(RX0); TR0=0; } if(i==1) { TX=1; delay(10); TX=0; while(!RX1); TR0=1; while(RX1); TR0=0; } if(i==2) { TX=1; delay(10); TX=0; while(!RX2); TR0=1; while(RX2); TR0=0; } if(i==3) { TX=1; delay(10); TX=0; while(!RX3); TR0=1; while(RX3); TR0=0; } if(i==4) { TX=1; delay(10); TX=0; while(!RX4); TR0=1; while(RX4); TR0=0; } if(i==5) { TX=1; delay(10); TX=0; while(!RX5); TR0=1; while(RX5); TR0=0; } if(i==6) { TX=1; delay(10); TX=0; while(!RX6); TR0=1; while(RX6); TR0=0; } if(i==7) { TX=1; delay(10); TX=0; while(!RX7); TR0=1; while(RX7); TR0=0; } time=TH0*256+TL0; TH0=0; TL0=0; if(!flag) { S[i]=(time*2)/100; }else flag=0; } } 7.8 groups of distance data processing The distance data of 8 parking spaces have been measured by the previous ultrasonic program. Now we only need to process the distance data. First, determine whether the distance to the first parking space is greater than 200CM. If it is greater, it means there is no car in the parking space, the remaining parking space count increases by one, and the corresponding parking space light shows no car. If it is less than, it means there is a car in the parking space, the remaining parking space count remains unchanged, and the corresponding parking space light shows there is a car. By determining the distance data of the 8 parking spaces in turn, the remaining parking space information of Area A, Area B and the total garage can be calculated, and the traffic lights and parking space indicator LEDs can be controlled based on this information. void Carport_Count(void) //Data processing { int i; a_carport=0;b_carport=0,R=0; for(i=0; i<8; i++) { if(S[i]>230) { if(i<4)a_carport++; if(i>=4)b_carport++; R|=wei[i]; } } } 8. Traffic light display Traffic lights are mainly used to remind or warn outsiders whether there are any remaining parking spaces in the garage. The number of remaining parking spaces in the garage has been obtained in the 8-group distance data processing function. By judging the number of remaining parking spaces, the traffic lights can be correctly displayed and controlled. If there are remaining parking spaces, the green light will be displayed, and if there are no remaining parking spaces, the red light will be displayed. void led_gr(void ) //traffic light display { if((a_carport+b_carport)>0) { LED_Green=0; LED_Red=1; }else { LED_Green=1; LED_Red=0; } } 9. Timer interrupt is used to determine whether the timer overflows, that is, whether the 16-bit counter has counted from 0 to 65535. If so, it overflows and the overflow flag is set. If the ultrasonic ranging function detects an overflow, the ranging is invalid to ensure the correctness of the data. void zd0() interrupt 1 //Timer interrupt, { flag=1; } 10. Main function The main function is the entry point for the entire program execution. First, the initialization settings are performed, that is, the timer parameters are set, and the 16-bit timer is set to count up. Then, the display function, keyboard scanning function, and ultrasonic function are set to an infinite loop to perform real-time display and continuously scan independent keys. The ultrasonic detected data is read every 50ms or so, and the distance detected by the ultrasonic wave is used to determine whether there is a car in the parking space, thereby driving the digital tube to display the number of remaining parking spaces, the number of parking spaces currently remaining, and the LED to display whether there is a car in the current parking space. The main program flowchart is shown in Figure 5.1.
Previous article:51 MCU project design: timed pet feeding system
Next article:51 single chip microcomputer 8X8 dot matrix screen circularly moves left
- 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
- 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
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- [NUCLEO-L552ZE Review] Unboxing and Onboard Resource Analysis
- GD32L233C-START Evaluation——02_2. Build development environment and simple debugging
- [Zhongke Bluexun AB32VG1 RISC-V board "encounter" RTT evaluation] 1: Fill the pit and run the first light-up program
- IAR platform transplants TI OSAL to STC8A8K64S4A12 MCU
- [Xianji HPM6750 Review] PWM Control Buzzer Sound
- [NXP Rapid IoT Review] + How to import the project downloaded from WEB IDE into MCUXpresso IDE and debug it?
- EEWORLD University Hall----Engineering is smarter, industrial design is more powerful-field transmitter and smart meter design solution
- Startup interface kernel code modification
- DC regulated power supply to charge the battery
- [2022 Digi-Key Innovation Design Competition] K210 transmits images to DWIN serial port display