#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define led P0
uchar tab[11]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
uchar count, count0;
void delay(uint x);
void play();
void main()
{
TMOD = 0X01;//定時器0的工作方式為1;
TH0 = (65536 - 40000) / 256;
TL0 = (65536 - 40000) % 256;
EA = 1;//開中斷
ET0 = 1;//開定時器0中斷
TR0 = 1;//啟動定時器0
led = 0x0c;
while(1)
{
play();
}
}
void timer0() interrupt 1//定時器0中斷服務程序
{
TH0 = (65536 - 40000) / 256;
TL0 = (65536 - 40000) % 256;
count++;//中斷次數累加
if(count == 50)
{
count = 0;//30次的50毫秒為1秒
count0++;
if((count0>=5)&&(count0<=8))
{
P2=~tab[8-count0];
}
else
P2=0xff;
if((count0>=13)&&(count0<=16))
{
P3=~tab[16-count0];
}
else
P3=0xff;
if(count0 == 16)
{
count0 = 0;
}
}
}
void play()
{
if(count0 == 5) //5s后南北黃,東西紅
{
led = 0x0a;
delay(100);
led = 0x08;
delay(100);
led = 0x0a;
delay(100);
led = 0x08;
delay(100);
led = 0x0a;
}
else if(count0 == 8) //8s后南北紅,東西綠
{
led = 0x21;
}
else if(count0 == 13) //13s南北紅,東西黃
{
led = 0x11;
delay(100);
led = 0x01;
delay(100);
led = 0x11;
delay(100);
led = 0x01;
delay(100);
led = 0x11;
}
else if(count0 == 0) //16s后南北綠,東西紅
{
led = 0x0c;
}
}
void delay(uint x)
{
uint a, b;
for(a = x; a > 0; a--)
for(b = 1000; b > 0; b--);
}