#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define ON 0
#define OFF 1
sbit B_G=P2^3; // B???
sbit B_R=P2^2; // B???
sbit A_G=P2^1; // A???
sbit A_R=P2^0; // A???
sbit LED_B2=P3^7;
sbit LED_B1=P3^6; // B???????
sbit LED_A2=P3^1;
sbit LED_A1=P3^0; // A???????
sbit key1=P3^2; //?????
sbit key2=P3^3;
sbit key3=P3^4;
/********* ?????? *************/
uchar AG=45,AR=5,BG=25,BR=5,ABR=20,AK=30,BK=30;
uchar count;
void keyscan1() //k1????
{
if(!key1)
{
while(!key1); //????
}
}
void keyscan2() //k2????
{
if(!key2)
{
while(!key2); //????
}
}
void keyscan3() //k2????
{
if(!key3)
{
while(!key3); //????
}
}
void init(void)
{
TMOD |= 0x01; //???,???,???,16???
TH0=(65536-40000)/256;
TL0=(65536-40000)%256;
EA=1;
ET0=1;
TR0=1;
}
void timer1(void) interrupt 1
{
TMOD |= 0x01; //???,???,???,16???
TH0=(65536-40000)/256;
TL0=(65536-40000)%256;
EA=1;
ET0=1;
TR0=1;
count++;
if(count>44)
{
AG--;
AR--;
BG--;
BR--;
ABR--;
AK--;
count=0;
}
}
/****************** ?? **********************/
void delay(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/******************led ?? *******************/
unsigned char table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};// ????????
void display1(uchar num1,uchar num2) //??A?? led ??
{
P0=table[num1];
LED_A1=ON;
delay(1);
LED_A1=OFF;
P0=table[num2];
LED_A2=ON;
delay(1);
LED_A2=OFF;
}
void display2(uchar num3,uchar num4) // ??B??led ??
{
P0=table[num3];
LED_B1=ON;
delay(1);
LED_B1=OFF;
P0=table[num4];
LED_B2=ON;
delay(1);
LED_B2=OFF;
}
void main()
{
int i;
/************ ????************/
A_R=ON; //A?????
B_R=ON; //B?????
for(i=0;i<210;i++)
{
delay(10);
}
A_R=OFF;//A?????
B_R=OFF;// B?????
/************** ??1:A???? (45s),?? (5s)************/
/************** ??2:B???? (25s),?? (5s)***************/
while(1)
{
init(); // ??????
A_G=ON; //A?????
while(AG!=0)
{
display1(AG/10,AG%10);// A??? (45s)
A_G=OFF;
A_R=ON;
while(AG==0)
{
while(AR!=0)
{
if(!key1)//??k1
{
display2(20/10,20%10);
}
if(!key2)
{
display2(30/10,30%10);
}
if(!key3)
{
display2(30/10,30%30);
}
else{
display2(AR/10,AR%10);//A??? (5s)
A_R=OFF;
B_G=ON;
} }
}
}
while(BG!=0)
{
display1(BG/10,BG%10);//B???(25s)
B_G=OFF;
B_R=ON;
while(BG==0)
{
while(BR!=0)
{
if(!key1)//??k1
{
display2(AR/20,AR%20);
}
if(!key2)
{
display2(AR/30,AR%30);
}
if(!key3)
{
display2(BR/30,BR%30);
}
else{
display2(BR/10,BR%10);//B???(5s)
B_R=OFF;
}}
}
}
AG=45,AR=5,BG=25,BR=5,ABR=20,AK=30,BK=30;// ????
}
}
以MCS-51系统为核心,设计交通灯控制器。十字路口交通灯由红、绿两色LED显示器(两位8段LED显示器)组成,LED显示器显示切换倒计时,以秒为单位,每秒更新一次;为确保安全,绿LED计数到0转红,经5秒延时(显示红色0)后,另一道开始绿色倒计时。基本要求:主干道(A道)先通行且通行时间为45 s(加5秒红灯延时,共50秒);支道(B道)通行时间为25 s(加5秒红灯延时,共30秒);主道与支道的车辆交错通行;若遇紧急情况,按开关K1时,主道与支道都为红灯20 s;根据实时交通堵塞情况人为控制时,按K2时,主道延时30 s通行,按K3时,支道延时30 s通行
为什么我这个45s倒计时的时候还正常,到了5s倒计时的时候不是从5s开始而是从6s开始,并且往后一直在09重复倒计时