/*———————————————–
注:P1口衔接至LED用于显现红绿灯,P0口接数码管段码,用于显现时刻;
两个74HC573操控段码和位码。段码接P20,位码接P21
————————————————*/
#include
#define uchar unsigned char
#define uint unsigned int
sbit DUAN=P2^0;
sbit WEI=P2^1;
/*————————————————
全局变量
————————————————*/
bit red,green,yellow,turnred;//界说红绿黄灯标志位
code unsigned char tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,
0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00} ;
//共阴数码管0-9
uchar shi,ge;
/*————————————————
————————————————*/
void delay(uint t)
{
while(t–);
}
/*————————————————
主程序
————————————————*/
main()
{
TMOD |=0x01;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
EA=1;//开中止
ET0=1;
TR0=1;//开定时器
P1=0xfc;//红灯亮
red =1;
while(1)
{
WEI=1;
P0=0xfe;
WEI=0;
DUAN=1;
P0=shi;//显现十位
DUAN=0;
delay(300);
WEI=1;
P0=0xfd;
WEI=0;
DUAN=1;
P0=ge;//显现十位
DUAN=0;
delay(300);
}
}
/*————————————————
定时器0中止函数
————————————————*/
void tim(void) interrupt 1
{
static uchar second=60,count;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
count++;
if (count==20)
{
count=0;
second–;//秒减1
if(second==0)
{
if(red)
{
red=0;yellow=1;
second=5;
P1=0xF3;//黄灯亮5秒
}
else if(yellow && !turnred)
{
yellow=0;green=1;
second=50;
P1=0xCF;//绿灯亮50秒
}
else if(green)
{
yellow=1;green=0;
second=5;
P1=0xF3;//黄灯亮5秒
turnred=1;
}
else if(yellow && turnred)
{
red=1;yellow=0;
P1=0xFC;//红灯亮60秒
second=60;
turnred=0;
}
}
shi=tab[second/10];
ge=tab[second%10];
}
}