1、基于单片机的交通灯设计c程序#includesbit RED_E = P10;sbit YELLOW_E = P11;sbit GREEN_E = P12;sbit RED_N = P13;sbit YELLOW_N = P14;sbit GREEN_N = P15;sbit s1 = P24;sbit s2 = P25;sbit s3 = P26;sbit s4 = P27;sbit type = P16;sbit k1 = P20;sbit k2 = P21;sbit k3 = P22;sbit k4 = P23;#define uchar unsigned char/宏定义#define
2、 uint unsigned intvoid cpuInit();void Display();void key_analyse();void smec(unsigned char x);void Display2();void t0_time();void kv_set_E();void kv_set_N();void pc_EN();/void kv_set_pc();void set_pc();uchar R_E,Y_E,G_E,R_N,Y_N,G_N;uchar Flash_Count = 0,Operation_Type=1,key,Set_flag,flag=0,x=0,x1=0,
3、x2=0,x3=0,m1,m2,flag_pc=0,temp;uchar led_seg_code10=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f;int a=0;uchar time;int t,t2,t3,t4;void cpuInit() /初始化 TMOD=0x21; /定时器0按方式1工作 /PCON=0x7f; SCON=0x70; ET0=1; EA=1; TH0=0x3C; /定时50ms (11.0592M) TL0=0x0B0; TH1=0x0f3; TL1=0x0f3; ES=1; TR1=1; P1=0x0ff;
4、/关LED s1=1; s2=1; s3=1; s4=1; P0=0x00; P2=0x0ff; R_E=14; Y_E=4; G_E=15; R_N=19; Y_N=4; G_N=10;void Display() /普通模式下显示/ uchar time;/ int t,t2; uchar i; /int j; m1=P0; m2=P0; switch(Operation_Type) case 1: RED_E=1; YELLOW_E=1; GREEN_E=0; RED_N=0; YELLOW_N=1; GREEN_N=1; if(x=0) i=G_E; x=1; time=R_N; t=
5、time/10; t2=(int)time%10; t3=i/10; t4=(int)i%10; P0=led_seg_codet2; s2=0; s2=1; P0=led_seg_codet4; s4=0; s4=1; P0=led_seg_codet; s1=0; s1=1; P0=led_seg_codet3; s3=0; s3=1; if(flag=1) flag=0; i-; time-; t=time/10; t2=(int)time%10; t3=i/10; t4=(int)i%10; P0=led_seg_codet2; s2=0; s2=1; P0=led_seg_codet
6、4; s4=0; s4=1; P0=led_seg_codet; s1=0; s1=1; P0=led_seg_codet3; s3=0; s3=1; if(i=0) x=0; Operation_Type=2; break; case 2: GREEN_E=1; YELLOW_E=0; P0=led_seg_codet2; s2=0; s4=0; s2=1; s4=1; P0=led_seg_codet; s1=0; s3=0; s1=1; s3=1; if(flag=1) flag=0; x1+; time-; t=time/10; t2=(int)time%10; P0=led_seg_
7、codet2; s2=0; s4=0; s2=1; s4=1; P0=led_seg_codet; s1=0; s3=0; s1=1; s3=1; if(x1=4) x1=0; Operation_Type=3; break; case 3: RED_E=0; YELLOW_E=1; GREEN_E=1; RED_N=1; YELLOW_N=1; GREEN_N=0; if(x2=0) i=G_N; x2=1; time=R_E; t=time/10; t2=(int)time%10; t3=i/10; t4=(int)i%10; P0=led_seg_codet2; s4=0; s4=1;
8、P0=led_seg_codet4; s2=0; s2=1; P0=led_seg_codet; s3=0; s3=1; P0=led_seg_codet3; s1=0; s1=1; if(flag=1) flag=0; i-; time-; t=time/10; t2=(int)time%10; t3=i/10; t4=(int)i%10; P0=led_seg_codet2; s4=0; s4=1; P0=led_seg_codet4; s2=0; s2=1; P0=led_seg_codet; s3=0; s3=1; P0=led_seg_codet3; s1=0; s1=1; if(i
9、=0) x2=0; Operation_Type=4; break; case 4: GREEN_N=1; YELLOW_N=0; P0=led_seg_codet2; s2=0; s4=0; s2=1; s4=1; P0=led_seg_codet; s1=0; s3=0; s1=1; s3=1; if(flag=1) flag=0; x3+; time-; t=time/10; t2=(int)time%10; P0=led_seg_codet2; s2=0; s4=0; s2=1; s4=1; P0=led_seg_codet; s1=0; s3=0; s1=1; s3=1; if(x3
10、=4) x3=0; Operation_Type=1; break; /void delay1ms() /1ms延时/ int i,j;/ for(i=2;i0;i-)/ for(j=248;j0;j-);/void key_analyse() unsigned char key_value; key_value=P2; if(key_value&0x0f)!=0x0f)/ 有键按下 smec(150); key_value=key_value&0x0f; key=key_value; switch(key) case 0x0e: key=0; kv_set_E(); break; /进入东西
11、功能键 case 0x07: key=0; kv_set_N(); break; /进入南北功能键 /case 0x0d: key=0; kv_set_pc(); break; /进入PC机修改 /case 0x06: key=0; kv_down_E(); break; /东西减 /case 0x09: key=0; kv_up_N(); break; /南北加 /case 0x05: key=0; kv_down_N(); break; /南北减 /case 0x07: key=0; Kv_Enter(); break; /确定键 default: break; if(type=0) sm
12、ec(150); Display2(); void kv_set_E() /uchar time2; /int tt,tt2; int n1=0,n2=0; unsigned char key_s,key_d; key_s=P0; key_d=P2; P2=0x0ff;/ time2=G_E;/ t=time/10;/ t2=(int)time%10; while(1) /k4为确定键 if(k4=0) smec(150); k4=1; /P2=0x0ff; /P0=0x0ff; break; /time=G_E; /tt=time2/10; /tt2=(int)time2%10; /P0=l
13、ed_seg_codett2; /s2=0; /s2=1; /P0=led_seg_codett; /s1=0; /s1=1; if(k2=0) smec(150); +G_E; /东西加 /time2=G_E; +R_N; n1+;/ t=time/10;/ t2=(int)time%10; s1=0; P0=led_seg_coden1;/ s2=0;/ s2=1;/ P0=led_seg_codet;/ s1=0;/ s1=1; k2=1; if(k3=0) smec(150); -G_E; /东西减 /time2=G_E; -R_N; n2+; s1=0; P0=led_seg_cod
14、en2; k3=1; /P0=key_s; /P2=key_d;void kv_set_N() /uchar time2; /int tt,tt2; int n1=0,n2=0; unsigned char key_s,key_d; key_s=P0; key_d=P2; P2=0x0ff; /time2=G_E; while(1) /k1为确定键 if(k1=0) smec(150); k1=1; /P2=0x0ff; /P0=0x0ff; break; /tt=time2/10; /tt2=(int)time2%10; /P0=led_seg_codett2; /s2=0; /s2=1;
15、/P0=led_seg_codett; /s1=0; /s1=1; if(k2=0) smec(150); +G_N; /南北加 +R_E; /time2=G_N; n1+; s1=0; P0=led_seg_coden1; k2=1; if(k3=0) smec(150); -G_N; /南北减 -R_E; /time2=G_N; n2+; s1=0; P0=led_seg_coden2; k3=1; /P0=key_s; /P2=key_d; /void kv_set_pc()/ int i=1;/ while(i)/ / / while(RI=0)/ / if(k3=0) /k3键退出/
16、 / smec(150);/ i=0;/ break;/ / / RI=0;/ G_E=SBUF;/ /void Display2()/智能模式下显示 int i=10; s1=1; s2=1; s3=1; s4=1; while(i) if(flag=1) flag=0; i-; RED_E=1; YELLOW_E=1; GREEN_E=0; RED_N=0; YELLOW_N=1; GREEN_N=1; void pc_EN() if(flag_pc=1) flag_pc=0; G_E=temp; R_N=G_E+4; void smec(unsigned char x)/1MS unsi
17、gned int j; while(x-) for(j=0;j80;j+); /void Uart_sc2(uchar byte)/ SBUF =byte;/ while(!RI);/ RI =0;/ G_E=SBUF;/int main() cpuInit();/初始化 TR0=1; while(1) Display(); key_analyse(); /查询是否有键按下 pc_EN(); void t0_time() interrupt 1 TH0=0X3C; TL0=0X0B0; a+; if(a=20) a=0; flag=1; void set_pc() interrupt 4 RI=0; temp=SBUF; flag_pc=1;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1