Design of traffic lights based on 51 single chip microcomputer

Publisher:码农创想家Latest update time:2021-10-15 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Hardware Solution

This design can simulate the basic traffic control system, using red, green and yellow lights to indicate the occurrence of no-entry, pass and waiting signals, and can also display countdown. The buttons can control the no-entry, late night mode, reset, east-west passage, north-south passage, time addition, time subtraction, switching and other functions. There are four two-digit cathode digital tubes, one for each of the southeast, northwest and northeast to display the time. The cathodes of the four digital tubes are connected to the P1 port of the STC89C51, and the anodes are connected to the 74HC245 chip. The 74HC245 chip is controlled through the P0 port to play a driving and amplifying role.


Main hardware: 51 single-chip microcomputer + minimum system + digital tube display module + LED indicator module + button module;

2. Design function

(1) This design uses a four-directional digital tube to display time, which is more in line with the design of a real traffic light;

(2) 7 buttons: reset, night mode, emergency mode, set time, time increase, time decrease, and debug confirmation button;

(3) Night mode: After pressing the "Night mode" button, the yellow lights in the four directions flash;

(4) Emergency mode: When an ambulance or fire truck passes by, press the "Emergency mode" button and all red lights in the four directions will light up;

(5) The travel time in the east-west direction and the north-south direction can be set separately, which simulates the main roads and branch roads in actual traffic.


3. Design Schematic

(1) The schematic diagram is mainly designed using AD software, as shown in the figure:

(2) The Protues simulation diagram is as follows:

4. Software Design

(1) Program flow chart

(1) Main program source code:


//Main program

void main()

{

TMOD=0X11; //Timer setting    

TH1=0X3C;

TL1=0XB0;

TH0=0X3C; //Timer 0 is initialized to 0.05S

TL0=0XB0;

EA=1; //Open the general interrupt

ET0=1; //Timer 0 interrupt is enabled

ET1=1;    //Timer 1 interrupt is enabled

TR0=1; //Start timing 0

TR1=0; //Close timer 1

    logo(); //Power on initialization

P2=0Xc3; //Start with default status, green light for east and west, yellow light for north and south   

    sec_nb=sec_dx+5; //The default north-south travel time is 5 seconds longer than the east-west travel time

while(1)   //main loop

{  

key(); //Call the key scanning program

display(); //Call the display program

}

}

//Function definition part

void key(void) //key scan subroutine

{  

if(k1!=1) //When K1 (time plus) is pressed

{

display();        //Call display for delay and debounce

if(k1!=1) //If confirmed, press

{

  TR0=0; //Turn off the timer        

shanruo=0; //Blinking flag off

P2=0x00; //Display off

TR1=0; //Start timing 1

if(set==0) //Set key pressed

set_timedx++;    //North and South plus 1S

else

set_timenb++;    //add 1S to something

if(set_timenb==100)

set_timenb=1;

if( set_timedx == 100 )

set_timedx=1;    // add to 100 and set to 1

sec_nb=set_timenb; //Assign the set value to the east, west, south, and north

sec_dx=set_timedx;

  do

display(); //Call display for delay

}

while(k1!=1); //Wait for the button to be released

}

}


if(k2!=1) //When the K2 (time minus) button is pressed

{

display();        //Call display for delay and debounce  

if(k2!=1) //If confirmed, press

{

TR0=0;          //Turn off timer 0

shanruo=0; //Blinking flag off

P2=0x00; //Display off

TR1=0; //Turn off timer 1

if(set==0)

set_timedx--;  //South-North minus 1S

else

set_timenb--;  //subtract 1S from something

if(set_timenb==0)

set_timenb=99;

if( set_timedx==0 )

set_timedx=99;    //decrease to 1 and reset to 99

sec_nb = set_timenb; // Assign the set value to the east, west, south, and north

sec_dx=set_timedx;

  do

    { 

display();        //Call display for delay

}

while(k2!=1);    //Wait for the button to be released

}

}


if(k3!=1) //When the K3 (confirm) key is pressed

{

display();        //Call display for delay and debounce

if(k3!=1) //If confirmed, press

{

TR0=1;  //Start timer 0

sec_nb=set_timenb; //Resume from interruption, still display the set value

sec_dx=set_timedx; //Display the set time

TR1=0; //Turn off timer 1

if(set==0) //Time counts down to 0

P2=0X00; //Display off

Green_dx=1; //The green light is on

Red_nb=1; //North and South red lights are on

sec_nb=sec_dx+5; //Return to initial value

}

else 

P2=0x00; //Green light for north and south, red light for east and west

Green_nb=1;

Red_dx=1;

sec_dx=sec_nb+5; 

}

}

}

 

if(k4!=1) //When K4 (switch) key is pressed

    {

display();        //Call display for delay and debounce

if(k4!=1) //If confirmed, press

{  

TR0=0; //Turn off timer 0

set=!set; //Invert the set flag to switch the adjustment direction

TR1=0; //Turn off timer 1

dx_nb=set;

do

{

display();    //Call display for delay

}

while(k4!=1);   //Wait for the button to be released

}

}


if(k5!=1) //When the K5 (disable) key is pressed

{

    display();        //Call display for delay and debounce

    if(k5!=1) //If confirmed, press

    {

    TR0=0; //Turn off the timer

P2=0x00; //Display off

Red_dx=1;

Red_nb=1; //Set all lights to red

TR1=0;

sec_dx=00; //The time in all four directions is 00

sec_nb=00;

do

{

display(); //Call display for delay

}

while(k5!=1); //Wait for the button to be released

    }

}

if(k6!=1) //When K6 (night mode) is pressed 

{

    display();        //Call display for delay and debounce 

    if(k6!=1) //If confirmed, press

    {

    TR0=0; //Turn off the timer

P2=0x00;

TR1=1;

sec_dx=00; //The time in all four directions is 00

sec_nb=00;

do

{

display();   //Call display for delay

}

while(k6!=1); //Wait for the button to be released

    }

}

}

Reference address:Design of traffic lights based on 51 single chip microcomputer

Previous article:Design of electronic perpetual calendar based on 51 single chip microcomputer
Next article:Tetris based on 51 single chip microcomputer

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号