#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit RED_A=P1^0; //东西向红灯
sbit YEL_A=P1^1; //东西向黄灯
sbit GRE_A=P1^2; //东西向绿灯
sbit RED_B=P1^3; //南北向红灯
sbit YEL_B=P1^4; //南北向黄灯
sbit GRE_B=P1^5; //南北向绿灯
uchar Time_Count=0,Flash_Count=0,Operation_Type=1; //分别用于计中断次数,计闪烁次数(2倍),保存当前状态值
void time0() interrupt 1 //T0定时器中断服务函数
{
TH0=(65536-50000)/256; //重置定时器初值
TL0=(65536-50000)%256;
switch(Operation_Type) //根据状态值执行相应语句
{
case 1: RED_A=0;YEL_A=0;GRE_A=1; //东西向绿灯亮
RED_B=1;YEL_B=0;GRE_B=0; //南北向红灯亮
if(++Time_Count!=100)return; //5s时间未到退出switch语句
Time_Count=0; //5s时间到计数值清零
Operation_Type=2; //状态转移值变为2,准备状态转移
break; //退出switch语句
case 2: if(++Time_Count!=8)return; //400ms未到退出switch语句
Time_Count=0;
YEL_A=!YEL_A;GRE_A=0; //东西向黄灯闪,绿灯灭
if(++Flash_Count!=10)return; //闪烁5次未到退出
Flash_Count=0; //闪烁次数清零
Operation_Type=3; //状态转移值变为3,准备状态转移
break;
case 3: RED_A=1;YEL_A=0;GRE_A=0; //东西向红灯亮
RED_B=0;YEL_B=0;GRE_B=1; //南北向绿灯亮
if(++Time_Count!=100)return; //5s未到退出
Time_Count=0;
Operation_Type=4; //状态转移值变为4,准备状态转移
break;
case 4: if(++Time_Count!=8)return; //0.4s未到退出
Time_Count=0;
YEL_B=!YEL_B;GRE_B=0; //南北向黄灯闪烁,绿灯灭
if(++Flash_Count!=10)return; //闪烁5次未到退出
Flash_Count=0;
Operation_Type=1; //重新转移到状态1
break;
}
}
void main()
{
TMOD=0x01;
ET0=1;
EA=1;
TH0=(65536-50000)/256; //定时50ms初值
TL0=(65536-50000)%256;
TR0=1; //启动T0定时器
while(1);
}
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit RED_A=P1^0;
sbit YEL_A=P1^1;
sbit GRE_A=P1^2;
sbit RED_B=P1^3;
sbit YEL_B=P1^4;
sbit GRE_B=P1^5;
uchar Time_Count=0,Flash_Count=0,Operation_Type=1;
void time0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
switch(Operation_Type)
{
case 1: RED_A=0;YEL_A=0;GRE_A=1;
RED_B=1;YEL_B=0;GRE_B=0;
if(++Time_Count!=100)return;
Time_Count=0;
Operation_Type=2;
break;
case 2: if(++Time_Count!=8)return;
Time_Count=0;
YEL_A=!YEL_A;GRE_A=0;
if(++Flash_Count!=10)return;
Flash_Count=0;
Operation_Type=3;
break;
case 3: RED_A=1;YEL_A=0;GRE_A=0;
RED_B=0;YEL_B=0;GRE_B=1;
if(++Time_Count!=100)return;
Time_Count=0;
Operation_Type=4;
break;
case 4: if(++Time_Count!=8)return;
Time_Count=0;
YEL_B=!YEL_B;GRE_B=0;
if(++Flash_Count!=10)return;
Flash_Count=0;
Operation_Type=1;
break;
}
}
void main()
{
TMOD=0x01;
ET0=1;
EA=1;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=1;
while(1);
}
|