|
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit RED_A = P0^0;
sbit YELLOW_A = P0^1;
sbit GREEN_A = P0^2;
sbit RED_B = P0^3;
sbit YELLOW_B = P0^4;
sbit GREEN_B = P0^5;
sbit K1=P1^0;
sbit K2=P1^1;
sbit K3=P1^2;
uchar reckon ,Flash_Count = 0,Operation_Type = 1,Key_Flag_Idx,t,Second_Counts,yellow=0,i,m;
uchar DSY_CODE[]=
{
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f
};//顯示代碼表
void T0_INT() interrupt 1
{
TH0 = (65535-50000)/256;
TL0 = (65535-50000)%256;
if(++i==20)//精確到一秒
{
i = 0;
--Second_Counts;
P3 = DSY_CODE[Second_Counts/10];//十位
P2 = DSY_CODE[Second_Counts%10];//個(gè)位
if(Second_Counts == 0) Second_Counts = t;//等于0后重新開始
switch(Operation_Type)
{
case 1:
RED_A=0;YELLOW_A=0;GREEN_A=1;//東西道綠燈亮
RED_B=1;YELLOW_B=0;GREEN_B=0;//南北道紅燈亮
if(--reckon != 0) return;
reckon=m;
Operation_Type = 2;
break;
case 2:
if(++yellow !=1) return;
yellow=0;
YELLOW_A=!YELLOW_A;//東西道黃燈閃
GREEN_A=0;
if(++Flash_Count !=5) return;//閃爍5秒
Flash_Count=0;
Operation_Type = 3;
break;
case 3:
RED_A=1;YELLOW_A=0;GREEN_A=0;//東西道紅燈亮
RED_B=0;YELLOW_B=0;GREEN_B=1;//南北道綠燈亮
if(--reckon !=0 ) return;
reckon=m;
Operation_Type = 4;
break;
case 4:
if(++yellow != 1) return;
yellow=0;
YELLOW_B=!YELLOW_B;//南北道黃燈閃
GREEN_B=0;
if(++Flash_Count !=5)//閃爍5秒
return;
Flash_Count=0;
Operation_Type = 1;
break;
}
}
}
void main()
{
t=20;
m=t-5;//為黃燈閃爍節(jié)省出時(shí)間
EA=1;
ET0=1;
TR0=1;
P1 = 0x3f;
P2 = 0x3f;
i = 0;
reckon=m;
Second_Counts = t;
TMOD = 0x01;//定時(shí)器為模式以1
while(1)
{
if(K1==0)//時(shí)間變長(zhǎng)
{
t=t+5;
if(t>=70)
t=20;
m=t-5;
}
if(K2==0)//時(shí)間變短
{
t=t-5;
if(t<=5) //警告不可連續(xù)按按鈕,一次紅綠燈時(shí)長(zhǎng)餒只可按一次
t=20;
m=t-5;
}
if(K3==0)//恢復(fù)正常
{
t=20;
m=t-5;
}
}
}
|
|