#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
#define dula P0 //段选信号的锁存器控制
#define wela P2 //位选信号的锁存器控制,这里只用到P2.4-P2.7
sbit beep=P2^3;
unsigned int pp;
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar j,k,l=255;
uchar a1,a0;
uchar shijian,deng=2;
void delay(unsigned int i)
{
for(j=i;j>0;j--)
for(k=125;k>0;k--);
}
void display(unsigned char sh_c,unsigned char g_c)
{
wela = wela&0x0f;
dula=table[g_c];
wela = wela|0x80;
delay(5);
wela = wela&0x0f;
dula=table[sh_c];
wela = wela|0x40;
delay(5);
}
void main()
{
TMOD=0x01;
TR0=1;
TH0=(65536-46080)/256;// 由于晶振为11.0592,故所记次数应为46080,计时器每隔50000微秒发起一次中断。
TL0=(65536-46080)%256;//46080的来历,为50000*11.0592/12
ET0=1;
EA=1;
while(1)
{
if(shijian==0)
{
switch(deng)
{
case 0: shijian=50; //红灯
deng=1;
break;
case 1: shijian=15; //黄灯
deng=2;
break;
case 2: shijian=50; //绿灯
deng=3;
break;
case 3: shijian=15; //黄灯
deng=0;
break;
}
}
if(pp==20)
{ pp=0;
shijian--;
}
a0=shijian%10;
a1=shijian/10;
display(a1,a0);
switch(deng)
{
case 1: P1=0X00; //红灯
break;
case 2: //黄灯
if(shijian%2==0)
P1=0x00;
if(shijian%2==1)
P1=0xff;
break;
case 3: //绿灯
if(shijian%2==0)
P1=0x55;
if(shijian%2==1)
P1=0xaa;
break;
case 0: if(shijian%2==0) //黄灯
P1=0x00;
if(shijian%2==1)
P1=0xff;
break;
}
}
}
void time0() interrupt 1
{ TH0=(65536-46080)/256;
TL0=(65536-46080)%256;
pp++;
}
|