51单片机交通灯程序以及仿真
来源:网络 2021-12-22 22:00:13
#include
#define uint unsigned int#define uchar unsigned charsbit red_nb = P0^0;sbit green_nb = P0^1;sbit yellow_nb = P0^2;
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()// 定义 四种状态 显示{switch(switch_type){case 1:for( i=0; i<4; i++){P1 = dispbit[i];//使用查表法进行位选P3 = LedOut[i];delay(100);}//使数码管在此 不断的进行扫描显示
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];//数码管显示的数据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;//进入第二种状态break;
case 2:for( i=0; i<4; i++){P1 = dispbit[i];//使用查表法进行位选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];//使用查表法进行位选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];//使用查表法进行位选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++;}
}
标签:
51单片机交通灯仿