#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];//个位
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;//为黄灯闪烁节省出时间
EA=1;
ET0=1;
TR0=1;
P1 = 0x3f;
P2 = 0x3f;
i = 0;
reckon=m;
Second_Counts = t;
TMOD = 0x01;//定时器为模式以1
while(1)
{
if(K1==0)//时间变长
{
t=t+5;
if(t>=70)
t=20;
m=t-5;
}
if(K2==0)//时间变短
{
t=t-5;
if(t<=5) //警告不可连续按按钮,一次红绿灯时长馁只可按一次
t=20;
m=t-5;
}
if(K3==0)//恢复正常
{
t=20;
m=t-5;
}
}
}
|