|
#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; //分別用于計(jì)中斷次數(shù),計(jì)閃爍次數(shù)(2倍),保存當(dāng)前狀態(tài)值
void time0() interrupt 1 //T0定時(shí)器中斷服務(wù)函數(shù)
{
TH0=(65536-50000)/256; //重置定時(shí)器初值
TL0=(65536-50000)%256;
switch(Operation_Type) //根據(jù)狀態(tài)值執(zhí)行相應(yīng)語(yǔ)句
{
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時(shí)間未到退出switch語(yǔ)句
Time_Count=0; //5s時(shí)間到計(jì)數(shù)值清零
Operation_Type=2; //狀態(tài)轉(zhuǎn)移值變?yōu)?,準(zhǔn)備狀態(tài)轉(zhuǎn)移
break; //退出switch語(yǔ)句
case 2: if(++Time_Count!=8)return; //400ms未到退出switch語(yǔ)句
Time_Count=0;
YEL_A=!YEL_A;GRE_A=0; //東西向黃燈閃,綠燈滅
if(++Flash_Count!=10)return; //閃爍5次未到退出
Flash_Count=0; //閃爍次數(shù)清零
Operation_Type=3; //狀態(tài)轉(zhuǎn)移值變?yōu)?,準(zhǔn)備狀態(tài)轉(zhuǎn)移
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; //狀態(tài)轉(zhuǎn)移值變?yōu)?,準(zhǔn)備狀態(tài)轉(zhuǎn)移
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; //重新轉(zhuǎn)移到狀態(tài)1
break;
}
}
void main()
{
TMOD=0x01;
ET0=1;
EA=1;
TH0=(65536-50000)/256; //定時(shí)50ms初值
TL0=(65536-50000)%256;
TR0=1; //啟動(dòng)T0定時(shí)器
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);
}
|
|