/*名稱:定時器控制交通指示燈
說明:東西向綠燈亮5s后,黃燈閃爍,閃爍5次亮紅燈,
紅燈亮后,南北向由紅燈變成綠燈,5s后南北向黃燈閃爍,
閃爍5次后亮紅燈,東西向綠燈亮,如此往復。
*/
#include<reg51.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);
} |