单片机源程序如下:
- #include <reg51.h>#define uchar unsigned char#define uint unsigned int
- sbit Time_Show_LED2=P2^5;sbit Time_Show_LED1=P2^4;sbit EW_LED2=P2^3; sbit EW_LED1=P2^2; sbit SN_LED2=P2^1;sbit SN_LED1=P2^0; sbit SN_Yellow=P1^6;sbit EW_Yellow=P1^2;sbit EW_Red=P1^3;sbit SN_Red=P1^7;sbit EW_ManGreen=P3^4;sbit EW_ManRed=P3^5;sbit SN_ManGreen=P3^2;sbit SN_ManRed=P3^3; bit Flag_SN_Yellow; bit Flag_EW_Yellow;uchar Time_EW;uchar Time_SN;uchar EW=60,SN=40,EWL=19,SNL=19; uchar EW1=60,SN1=40,EWL1=19,SNL1=19;uchar table[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};uchar S[8]={0x28,0x48,0x18,0x48,0x82,0x84,0x81,0x84};
- void Delay(uchar a) { uchar i; i=a; while(i--); }
- void Display(void) { char h,l; h=Time_EW/10; l=Time_EW%10; P0=table[l]; EW_LED2=1; Delay(2); EW_LED2=0; P0=table[h]; EW_LED1=1; Delay(2); EW_LED1=0;
- h=Time_SN/10; l=Time_SN%10; P0=table[l]; SN_LED2=1; Delay(2); SN_LED2=0; P0=table[h]; SN_LED1=1; Delay(2); SN_LED1=0;
- h= EW1/10; l= EW1%10; P0=table[l]; Time_Show_LED1=1; Delay(2); Time_Show_LED1=0; P0=table[h]; Time_Show_LED2=1; Delay(2); Time_Show_LED2=0;}
- void timer0(void)interrupt 1 using 1{ static uchar count; TH0=(65536-50000)/256; TL0=(65536-50000)%256; count++; if(count==10) { if(Flag_SN_Yellow==1) {SN_Yellow=~SN_Yellow;} if(Flag_EW_Yellow==1) {EW_Yellow=~EW_Yellow;} } if(count==20) { Time_EW--; Time_SN--; if(Flag_SN_Yellow==1) {SN_Yellow=~SN_Yellow;} if(Flag_EW_Yellow==1) {EW_Yellow=~EW_Yellow;} count=0; } }
- void main(void)
- { IT0=1; TMOD=0x01; TH0=(65536-50000)/256; TL0=(65536-50000)%256; EA=1; ET0=1; EX0=1; TR0=1; while(1)
- { Time_EW=EW; Time_SN=SN; while(Time_SN>=5) {P1=S[0]; EW_ManGreen=0; EW_ManRed=1; SN_ManGreen=1; SN_ManRed=0; Flag_EW_Yellow=0; Flag_SN_Yellow=0; EW_Red=1; Display();} P1=0x00; while(Time_SN<5) {P1=S[1]; Flag_SN_Yellow=1; EW_ManGreen=0; EW_ManRed=1; SN_ManGreen=1; SN_ManRed=0; Flag_EW_Yellow=0; EW_Red=1; Display(); } Time_SN=SNL; while(Time_SN>=5) {P1=S[2]; SN_ManGreen=0; SN_ManRed=1; EW_ManGreen=0; EW_ManRed=1; Flag_SN_Yellow=0; Flag_EW_Yellow=0; EW_Red=1; Display();}
- P1=0x00; while(Time_SN<5) {P1=S[3]; Flag_SN_Yellow=1; SN_ManGreen=0; SN_ManRed=1; EW_ManGreen=0; EW_ManRed=1; Flag_EW_Yellow=0; EW_Red=1; Display();}
- EW=EW1; SN=SN1; EWL=EWL1; SNL=SNL1;
- Time_EW=SN; Time_SN=EW; while(Time_EW>=5) {P1=S[4]; SN_Red=1; SN_ManGreen=0; SN_ManRed=1; EW_ManGreen=1; EW_ManRed=0; Flag_EW_Yellow=0; Flag_SN_Yellow=0; Display();} P1=0X00; while(Time_EW<5) {P1=S[5]; Flag_EW_Yellow=1; SN_ManGreen=0; SN_ManRed=1; EW_ManGreen=1; EW_ManRed=0; Flag_SN_Yellow=0; SN_Red=1; Display();} Time_EW=EWL; while(Time_EW>=5) {P1=S[6]; SN_Red=1; SN_ManGreen=0; SN_ManRed=1; EW_ManGreen=0; EW_ManRed=1; Flag_EW_Yellow=0; Flag_SN_Yellow=0; Display();}
- P1=0X00; while(Time_EW<5) {P1=S[7]; Flag_EW_Yellow=1; SN_Red=1; SN_ManGreen=0; SN_ManRed=1; EW_ManGreen=0; EW_ManRed=1; Flag_SN_Yellow=0; Display();} EW=EW1; SN=SN1; EWL=EWL1; SNL=SNL1; }
- }
复制代码
所有资料51hei提供下载:
51交通灯.zip
(96.65 KB, 下载次数: 15)
|