|
#include<reg52.h>
sbit LACTCH1=P2^2;
sbit LACTCH2=P2^3;
bit red,green,yellow,turnred;//定義紅綠黃燈標(biāo)志位
code unsigned char tab[]=
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
//共陰數(shù)碼管 0-9
unsigned char Dis_Shiwei;//定義十位
unsigned char Dis_Gewei; //定義個(gè)位
/*------------------------------------------------
------------------------------------------------*/
void delay(unsigned int cnt)
{
while(--cnt);
}
/*------------------------------------------------
主程序
------------------------------------------------*/
main()
{
TMOD |=0x01;//定時(shí)器設(shè)置 10ms in 12M crystal
TH0=0xd8;
TL0=0xf0;
IE= 0x82; //打開中斷
TR0=1;
P1=0xfc; //紅燈亮
red =1;
while(1)
{
P0=Dis_Shiwei;//顯示十位
LACTCH1=1; //鎖存
LACTCH1=0;
P0=0xfe;
LACTCH2=1; // 鎖存
LACTCH2=0;
delay(300); //短暫延時(shí)
P0=Dis_Gewei; //顯示個(gè)位
LACTCH1=1; //鎖存
LACTCH1=0;
P0=0xfd;
LACTCH2=1; // 鎖存
LACTCH2=0;
delay(300);
}
}
void tim(void) interrupt 1
{
static unsigned char second=60,count; //初值99
TH0=0xd8;//重新賦值
TL0=0xf0;
count++;
if (count==100)
{
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;
}
}
Dis_Shiwei=tab[second/10];//十位顯示值處理
Dis_Gewei=tab[second%10]; //個(gè)位顯示處理
}
}
|
|