/* 名称:定时器控制交通指示灯
说明:东西向绿灯亮5s后,黄灯闪烁,闪烁5次亮红灯,
红灯亮后,南北向由红灯变成绿灯,5s后南北向黄灯闪烁,
闪烁5次后亮红灯,东西向绿灯亮,如此往复。
*/
#includereg51.h>
#defineucharunsignedchar
#defineuintunsignedint
sbitRED_A=P0^0;//东西向指示灯
sbitYELLOW_A=P0^1;
sbitGREEN_A=P0^2;
sbitRED_B=P0^3;//南北向指示灯
sbitYELLOW_B=P0^4;
sbitGREEN_B=P0^5;
//延时倍数,闪烁次数,操作类型
变量
ucharTime_Count=0,Flash_Count=0,Operation_Type=1;
//定时器0中断函数
voidT0_INT()interrupt1
{
TL0=-50000/256;
TH0=-50000%256;
switch(Operation_Type)
{
case1: //东西向绿灯与南北向红灯亮5s
RED_A=0;YELLOW_A=0;GREEN_A=1;
RED_B=1;YELLOW_B=0;GREEN_B=0;
if(++Time_Count!=100)return;//5s(100*50ms)切换
Time_Count=0;
Operation_Type=2;
break;
case2: //东西向黄灯开始闪烁,绿灯关闭
if(++Time_Count!=8)return;
Time_Count=0;
YELLOW_A=~YELLOW_A;GREEN_A=0;
if(++Flash_Count!=10)return; //闪烁
Flash_Count=0;
Operation_Type=3;
break;
case3: //东西向红灯与南北向绿灯亮5s
RED_A=1;YELLOW_A=0;GREEN_A=0;
RED_B=0;YELLOW_B=0;GREEN_B=1;
if(++Time_Count!=100)return;//5s(100*50ms)切换
Time_Count=0;
Operation_Type=4;
break;
case4: //南北向黄灯开始闪烁,绿灯关闭
if(++Time_Count!=8)return;
Time_Count=0;
YELLOW_B=~YELLOW_B;GREEN_A=0;
if(++Flash_Count!=10)return; //闪烁
Flash_Count=0;
Operation_Type=1;
break;
}
}
//主程序
voidmain()
{
TMOD=0x01; //T0方式1
IE=0x82;
TR0=1;
while(1);
}
拓展阅读:按键控制定时器选播多段音乐程序