Project practice: 51 single-chip microcomputer controls traffic lights

Publisher:RadiantSerenityLatest update time:2022-01-13 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

51 single-chip microcomputer controls traffic lights
Description: Recently, I took on a small project to use a single-chip microcomputer to control traffic lights at an intersection.

I tried to use simulation software and the effect was pretty good. I hope to learn with you all.

Overall design simulation diagram insert image description here
Single chip computer simulation design part
insert image description here
Highway signal light display simulation part
insert image description here
Four direction simulation design part
insert image description here
Start timing after starting simulation
insert image description here

C language reference source code:


/************************************************************

Crossroad traffic light control C program


**********************************************************/


#define uchar unsigned char

#define uint unsigned int

#include

/Define control bits*****************/

sbit Time_Show_LED2=P2^5; //Time_Show_LED2 control bit

sbit Time_Show_LED1=P2^4; //Time_Show_LED1 control bit

sbit EW_LED2=P2^3; //EW_LED2 control bit

sbit EW_LED1=P2^2; //EW_LED1 control bit

sbit SN_LED2=P2^1; //SN_LED2 control bit

sbit SN_LED1=P2^0; //SN_LED1 control bit

sbit SN_Yellow=P1^6; //SN yellow light

sbit EW_Yellow=P1^2; //EW yellow light

sbit EW_Red=P1^3; //EW red light

sbit SN_Red=P1^7; //SN red light

sbit EW_ManGreen=P3^0; //EW sidewalk green light

sbit SN_ManGreen=P3^1; //SN pedestrian green light

sbit Special_LED=P2^6; //Normal traffic indicator light

sbit Busy_LED=P2^7; //Busy traffic indicator light

sbit Nomor_Button=P3^5; // Normal traffic button

sbit Busy_Btton=P3^6; //Busy traffic button

sbit Special_Btton=P3^7; // Traffic special button

sbit Add_Button=P3^3; //Time addition

sbit Reduces_Button=P3^4; //Time reduction

bit Flag_SN_Yellow; //SN yellow light flag bit

bit Flag_EW_Yellow; //EW yellow light flag

char Time_EW; //East-West direction countdown unit

char Time_SN; //Countdown unit in north-south direction

uchar EW=60,SN=40,EWL=19,SNL=19; //Program initialization assignment, normal mode

uchar EW1=60,SN1=40,EWL1=19,SNL1=19; //Variables used to store modified values

uchar code table[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; //1~~~~9 segment code selection

uchar code S[8]={0X28,0X48,0X18,0X48,0X82,0X84,0X81,0X84}; //Traffic light control code

/Delay subroutine**/

void Delay(uchar a)

{

uchar i;

i=a;

while(i–){;}

}

/Display subfunction*********/

void Display(void)

{

char h,l;

h=Time_EW/10;

l=Time_EW%10;

P0=table[l];

EW_LED2=1;

Delay(2);

EW_LED2=0;

P0=table[h];

EW_LED1=1;

Delay(2);

EW_LED1=0;


h=Time_SN/10;

l=Time_SN%10;

P0=table[l];

SN_LED2=1;

Delay(2);

SN_LED2=0;

P0=table[h];

SN_LED1=1;

Delay(2);

SN_LED1=0;


h = EW1/10;

l = EW1%10;

P0=table[l];

Time_Show_LED1=1;

Delay(2);

Time_Show_LED1=0;

P0=table[h];

Time_Show_LED2=1;

Delay(2);

Time_Show_LED2=0;

}

/External 0 interrupt service routine**/


void EXINT0(void)interrupt 0 using 1

{

EX0=0; //Disable interrupt


if(Add_Button0) //Time addition

{

EW1+=5;

SN1+=5;

if(EW1>=100)

{

EW1=99;

SN1=79;

}

}

if(Reduces_Button0) //Time reduction

{

EW1-=5;

SN1-=5;

if(EW1<=40)

{

EW1=40;

SN1=20;

}


    } 

1

if(Nomor_Button0) //Test whether the button is pressed, if pressed it is normal

{

EW1=60;

SN1=40;

EWL1=19;

SNL1=19;

Busy_LED=0; //Turn off the busy signal light

Special_LED =0; //Turn off the special signal light

}

if(Busy_Btton0) //Test whether the button is pressed, if pressed it is busy

{

EW1=45;

SN1=30;

EWL1=14;

SNL1=14;

Special_LED=0; //Turn off the special signal light

Busy_LED=1; //Turn on the busy signal light


}

if(Special_Btton==0)//Test whether the button is pressed, pressing is a special state

{

EW1=75;

SN1=55;

EWL1=19;

SNL1=19;

Busy_LED=0; //Turn off the busy signal light

Special_LED =1; //Turn on the special signal light


}

EX0=1; //Open interrupt

}

/***T0 interrupt service routine/

void timer0(void)interrupt 1 using 1

{

static uchar count;

TH0=(65536-50000)/256;

TL0=(65536-50000)%256;

count++;


if(count==10)

{

1

2

if(Flag_SN_Yellow1) //Test the north and south yellow light flag

{SN_Yellow=~SN_Yellow;}

if(Flag_EW_Yellow1) //Test the east-west yellow light flag

{EW_Yellow=~EW_Yellow;}

}

if(count20)

{

Time_EW–;

Time_SN–;

if(Flag_SN_Yellow1) //Test the north and south yellow light flag

{SN_Yellow=~SN_Yellow;}

if(Flag_EW_Yellow==1)//Test the east-west yellow light flag

{EW_Yellow=~EW_Yellow;}

count=0;

}


}

/Main program starts*/

void main(void)


{

Busy_LED=0;

Special_LED=0;

IT0=1; //INT0 negative transition trigger

TMOD=0x01; //Timer works in mode 1

TH0=(65536-50000)/256; //Timer initial value

TL0=(65536-50000)%256;

EA=1; //CPU interrupt is always allowed

ET0=1; //Open timing interrupt

EX0=1; //Open external INTO interrupt

TR0=1; //Start timing


while(1)


{ /S0 status***/

EW_ManGreen=0; //EW sidewalk prohibited

SN_ManGreen=1; //SN pedestrian traffic

Flag_EW_Yellow=0; //EW turns off the yellow light to display the signal

Time_EW=EW;

Time_SN=SN;

while(Time_SN>=5)

{P1=S[0]; //SN is allowed, EW is red light

Display();}

/S1 status***/

P1=0x00;

while(Time_SN>=0)

{Flag_SN_Yellow=1; //SN turns on the yellow light signal position

EW_Red=1; //SN yellow light is on, waiting for left turn signal, EW red light


Display();

}

/S2 status***/

Flag_SN_Yellow=0; //SN turns off the yellow light to display the signal

Time_SN=SNL;

while(Time_SN>=5)

{P1=S[2]; //SN left turn green light on, EW red light

Display();}


/S3 Status***/

P1=0x00;

while(Time_SN>=0)

{Flag_SN_Yellow=1; //SN turns on the yellow light signal position

EW_Red=1; //SN yellow light is on, waiting for stop signal, EW red light


Display();}


/*Assignment/

EW=EW1;

SN=SN1;

EWL=EWL1;

SNL=SNL1;


/S4 status***/

EW_ManGreen=~EW_ManGreen; //EW sidewalk access

SN_ManGreen=~SN_ManGreen; //SN sidewalk prohibited

Flag_SN_Yellow=0; //SN turns off the yellow light to display the signal

Time_EW=SN;

Time_SN=EW;

while(Time_EW>=5)

{P1=S[4]; //EW, SN red light

Display();}

/S5 status***/

P1=0X00;

while(Time_EW>=0)

{Flag_EW_Yellow=1; //EW yellow light signal position

SN_Red=1; //EW yellow light is on, waiting for the left turn signal, SN red light

Display();}

/S6 status***/

Flag_EW_Yellow=0; //EW turns off the yellow light to display the signal

Time_EW=EWL;

while(Time_EW>=5)

{P1=S[6]; //EW left turn green light on, SN red light

Display();}


/S7 Status***/

P1=0X00;

while(Time_EW>=0)

{Flag_EW_Yellow=1; //EN turns on the yellow light signal

SN_Red=1; //EW yellow light is on, waiting for stop signal, SN red light

Display();}


/*Assignment/

EW=EW1;

SN=SN1;

EWL=EWL1;

SNL=SNL1;

}

Reference address:Project practice: 51 single-chip microcomputer controls traffic lights

Previous article:Single chip microcomputer water tower control system
Next article:Digital thermometer controlled by 51 single chip microcomputer

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号