利用单片机AT89C51、9个LED指示灯以及LED数码管组成带倒计时显示的交通灯控制系统。
东南西北四个路口各三个LED指示灯,分别为红色、黄色、绿色。绿灯先亮9秒,最后2秒快闪,然后黄灯亮3秒,最后红灯亮9秒。南北向、东西向交替通行。南北向、东西向各有一位数码管显示红灯倒计时。
原理电路如下图所示:
#define uint unsigned int
//数码管编码
uchar code smgduan[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
uint time=0;//计时
//主函数
void main()
{
TMOD|=0X01;//初始化定时器
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
ET0=1;//打开定时器0中断允许
EA=1;//打开总中断
TR0=1;//打开定时器
while(1)
{
//东西绿灯亮
time=10000;//9s
P1=0xf3;
while(time>2000)
{
P3=smgduan[time/1000];
}
while(time>0)//快闪
{
P3=smgduan[time/1000];
if(time%100>50)
P1=0xf7;
else
P1=0xf3;
}
//黄灯亮
P3=0xff;
time=3000;
P1=0xf5;
while(time>0);
//南北绿灯亮
time=10000;//9s
P1=0xde;
while(time>2000)
{
P2=smgduan[time/1000];
}
while(time>0)//快闪
{
P2=smgduan[time/1000];
if(time%100>50)
P1=0xfe;
else
P1=0xde;
}
//黄灯亮
P2=0xff;
time=3000;
P1=0xee;
while(time>0);
}
资料借鉴于此纷传
阅读全文