仿真结果是红灯会亮30秒,黄灯5秒,绿灯20秒,每个led在最后5秒都会闪烁。按下中断按键是直接全灭(包括倒数数码管),按复位键即可重新开机。
代码
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit red=P0^0;
sbit yellow=P0^1;
sbit green=P0^2;
uint x,y;
uchar i;
uchar count;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
/*ÑÓ³Ùº¯Êý*/
void delay(uint time)
{
for(x=time;x>0;x--)
{
for(y=110;y>0;y--);
}
}
void zhongduan() interrupt 0
{
red=1;
yellow=1;
green=1;
P1=0x00;
P2=0x00;
while(1);
}
void daoshu()
{
red=0;
yellow=1;
green=1;
count=30;
for(i=25;i>0;i--)
{
count=count-1;
P1=table[count%10];
P2=table[count/10];
delay(1000);
};
for(i=5;i>0;i--)
{
count=count-1;
P1=table[count%10];
P2=table[count/10];
red=1;
delay(500);
red=0;
delay(500);
}
;
red=1;
yellow=0;
green=1;
count=5;
for(i=5;i>0;i--)
{
count=count-1;
P1=table[count%10];
P2=table[count/10];
yellow=1;
delay(500);
yellow=0;
delay(500);
}
;
red=1;
yellow=1;
green=0;
count=20;
for(i=15;i>0;i--)
{
count=count-1;
P1=table[count%10];
P2=table[count/10];
delay(1000);
};
for(i=5;i>0;i--)
{
count=count-1;
P1=table[count%10];
P2=table[count/10];
green=1;
delay(500);
green=0;
delay(500);
}
}
void main()
{
IT0=1;
EX0=1;
EA=1;
while(1)
{
daoshu();
}
}