#include
#define uint unsigned int#define uchar unsigned charsbit red_nb = P0^0;sbit green_nb = P0^1;sbit yellow_nb = P0^2;
本文引用地址:http://www.eepw.com.cn/article/201611/321470.htm
sbit red_dx = P0^3;sbit green_dx = P0^4;sbit yellow_dx = P0^5;
unsigned char codeDisp_Tab[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};//段碼控制
unsigned char codedispbit[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdF,0xbF,0x7F}; //位選控制
unsigned char LedOut[4];
uchar switch_type = 1;uchar Traffic_time = 0;uchar ds_time = 0;uchar i,t;void delay(uint i){char j;for(i; i >0; i--)for(j = 200; j >0; j--);}
void Traffic_Light()// 定義 四種狀態(tài) 顯示{switch(switch_type){case 1:for( i=0; i<4; i++){P1 = dispbit[i];//使用查表法進(jìn)行位選P3 = LedOut[i];delay(100);}//使數(shù)碼管在此 不斷的進(jìn)行掃描顯示
red_nb = 0;//南北紅燈亮 綠燈滅黃燈滅green_nb = 1;yellow_nb = 1;red_dx = 1;//東西紅燈滅綠燈亮 黃燈滅green_dx = 0;yellow_dx = 1;if(Traffic_time != 30){LedOut[0]=Disp_Tab[(35 - Traffic_time)0/10];//數(shù)碼管顯示的數(shù)據(jù)LedOut[1]=Disp_Tab[(35 - Traffic_time)];LedOut[2]=Disp_Tab[(29 - Traffic_time)0/10];LedOut[3]=Disp_Tab[(29 - Traffic_time)];return;//返回case1}Traffic_time = 0;switch_type = 2;//進(jìn)入第二種狀態(tài)break;
case 2:for( i=0; i<4; i++){P1 = dispbit[i];//使用查表法進(jìn)行位選P3 = LedOut[i];delay(100);}red_nb = 0;//南北紅燈亮 綠燈滅黃燈滅green_nb = 1;yellow_nb = 1;red_dx = 1;//東西紅燈滅綠燈滅 黃燈亮green_dx = 1;if(Traffic_time != 6)//黃等保持的時間{if(Traffic_time != t){t = Traffic_time;yellow_dx = ~yellow_dx;}{LedOut[0]=Disp_Tab[(5 - Traffic_time)0/10];LedOut[1]=Disp_Tab[(5 - Traffic_time)];LedOut[2]=Disp_Tab[(5 - Traffic_time)0/10];LedOut[3]=Disp_Tab[(5 - Traffic_time)];return;}return;}Traffic_time = 0;switch_type = 3;break;
case 3:for( i=0; i<4; i++){P1 = dispbit[i];//使用查表法進(jìn)行位選P3 = LedOut[i];delay(100);}red_nb = 1;//南北紅燈滅 綠燈亮黃燈滅green_nb = 0;yellow_nb = 1;red_dx = 0;//東西紅燈亮綠燈滅 黃燈滅green_dx = 1;yellow_dx = 1;if(Traffic_time != 19){LedOut[0]=Disp_Tab[(24 - Traffic_time)0/10];LedOut[1]=Disp_Tab[(24 - Traffic_time)];LedOut[2]=Disp_Tab[(18 - Traffic_time)0/10];LedOut[3]=Disp_Tab[(18 - Traffic_time)];return;}Traffic_time = 0;switch_type = 4;break;
case 4:for( i=0; i<4; i++){P1 = dispbit[i];//使用查表法進(jìn)行位選P3 = LedOut[i];delay(100);}red_nb = 1;//南北紅燈滅 綠燈滅黃燈亮green_nb = 1;red_dx = 0;//東西紅燈亮綠燈滅 黃燈滅green_dx = 1;yellow_dx =1;
if(Traffic_time != 6)//黃等保持的時間{if(Traffic_time != t){t = Traffic_time;yellow_nb = ~yellow_nb;}LedOut[0]=Disp_Tab[(5 - Traffic_time)0/10];LedOut[1]=Disp_Tab[(5 - Traffic_time)];LedOut[2]=Disp_Tab[(5 - Traffic_time)0/10];LedOut[3]=Disp_Tab[(5 - Traffic_time)];return;}Traffic_time = 0;switch_type = 1;break;
}}
void main(){TMOD = 0x01;TH0 = (65536 - 50000)/256;TL0 = (65536 - 50000)%6;EA = 1;//開總中斷ET0 = 1;//開定時器0中斷TR0 = 1;//啟動定時器
while(1){Traffic_Light();
}
}
void T0_time() interrupt1{TH0 = (65536 - 50000)/256;TL0 = (65536 - 50000)%6;ds_time++;if(ds_time == 20){ds_time = 0;Traffic_time++;}
}