简易交通灯
- #include <reg51.h>
- sbit dred=P0^0;
- sbit dyellow=P0^1;
- sbit dgreen=P0^2;
- sbit nred=P0^3;
- sbit nyellow=P0^4;
- sbit ngreen=P0^5;
- sbit led3=P2^2;
- unsigned char rtime,ytime,gtime;
- unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
- unsigned char flag;
- void display(unsigned char a);
- void delay();
- void main()
- {
- TMOD=0x01;
- TH0=0x3c;
- TL0=0xb0;
- ET0=1;EA=1;TR0=1;
- flag=1;
- rtime=60;
- ytime=15;
- gtime=30;
- while(1)
- {if(flag==1)
- {display(rtime);
- dred=1;dyellow=1;dgreen=0;
- nred=0;nyellow=1;ngreen=1;}
- if(flag==2)
- {display(ytime);
- dred=1;dyellow=0;dgreen=1;
- nred=0;nyellow=1;ngreen=1;}
- if(flag==3)
- {display(gtime);
- dred=0;dyellow=1;dgreen=1;
- nred=1;nyellow=1;ngreen=0;}
- if(flag==4)
- {display(ytime);
- dred=0;dyellow=1;dgreen=1;
- nred=1;nyellow=0;ngreen=1;}
- if(flag==5)
- {display(ytime);
- dred=1;dyellow=0;dgreen=1;
- nred=1;nyellow=0;ngreen=1;}
- }}
- void display(unsigned char a)
- {
- unsigned char ge,shi,i;
- ge=a%10;
- shi=a/10%10;
- for(i=0;i<2;i++)
- {
- P2=0xfd;
- P1=table[ge];
- delay();
- P2=0xff;
- P2=0xfe;
- P1=table[shi];
- delay();
- P2=0xff;}}
- void delay()
- {
- unsigned char i;
- for(i=0;i<200;i++);
- }
- void time0()interrupt 1
- {
- unsigned char n;
- TH0=0x3c;
- TL0=0xb0;
- n=n+1;
- if(n==20)
- { n=0;
- if(led3==0)
- {
- flag=5;
- }
-
- if(flag==5)
- {
- if(ytime==0)
- {flag=1;rtime=15;}
- else
- {ytime=ytime-1;flag=5;} }
- if(flag==1)
- {
- if(rtime==0)
- {flag=2;ytime=15;}
- else
- {rtime=rtime-1;flag=1;} }
- if(flag==2)
- { if(ytime==0)
- {flag=3;gtime=30;}
- else
- {ytime=ytime-1;flag=2; }}
- if(flag==3)
- {
- if(gtime==0)
- {flag=4;ytime=15;}
- else
- {gtime=gtime-1; flag=3;} }
- if(flag==4)
- {
- if(ytime==0)
- {flag=1;rtime=60;}
- else
- {ytime=ytime-1; flag=4;}
- }
- }
- }
复制代码
可拓展功能交通灯.rar
(39.71 KB, 下载次数: 5)
|