51 single chip microcomputer project design: ultrasonic-based garage parking system and parking space detection

Publisher:忠正Latest update time:2022-01-12 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.


insert image description here

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        for(b=0;b<120;b++);

}

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.

[1] [2]
Reference address:51 single chip microcomputer project design: ultrasonic-based garage parking system and parking space detection

Previous article:51 MCU project design: timed pet feeding system
Next article:51 single chip microcomputer 8X8 dot matrix screen circularly moves left

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号