基于51单片机和8155扩展的交通灯 红绿黄灯切换及手动切换南北的灯及东西方向的灯
proteus8.6链接:游客,本帖隐藏的内容需要积分高于 10 才可浏览,您当前积分为 0
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机代码:
- #include<REG52.H>
- #include<absacc.h>
- #define COMMAND XBYTE[0X1F00]
- #define PA XBYTE[0X1F01]
- #define PB XBYTE[0x1f02]
- #define uchar unsigned char
- #define uint unsigned int
- sbit cs=P2^5;
- sbit om=P2^4;
- sbit state=P2^6;
- sbit int0=P3^2;
- sbit int1=P3^3;
- uchar k1=1,k2=1;
- uchar a,shi,ge,shi1,ge1,num1,num2,num3;
- uchar t;
- uchar t1;
- uchar dx_hong,dx_huang,dx_lv,nb_hong,nb_huang,nb_lv;
- uchar run_state=1;
- uchar ledflag1=1,ledflag2=3;//r y g y
- uchar code table[]={
- 0x3f,0x06,0x5b,0x4f,
- 0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,
- 0x39,0x5e,0x79,0x71};
- void delayms(uint xms);
- void display1(uchar shi,uchar ge,uchar shi1,uchar ge1);
- void djs(uchar xs,uchar xs1);
- void init(void);
- void keyscan(void);
- void main()
- {init();
- cs=0;
- om=1;
- COMMAND=0X0F;
- PB=0XFF;
- IT0=1;
- IT1=1;
- EX0=EX1=1;
- while(1)
- {
-
- keyscan();
-
- if(run_state)
- { TR0=1;
- if(ledflag1==1){t=10;/*dx_hong=0; dx_huang=1; dx_lv=1;*/PB&=~(0X07);PB|=0X06;}
- else if((ledflag1==2)||(ledflag1==4)){t=2;/*dx_hong=1; dx_huang=0; dx_lv=1;*/PB&=~(0X07);PB|=0X05;}
- else if(ledflag1==3){t=7;/*dx_hong=1; dx_huang=1; dx_lv=0;*/PB&=~(0X07);PB|=0X03;}
-
- if(ledflag2==1){t1=10;/*nb_hong=0;nb_huang=1;nb_lv=1;*/PB&=~(0X07<<3);PB|=0X06<<3;}
- else if((ledflag2==2)||(ledflag2==4)){t1=2;/*nb_hong=1;nb_huang=0;nb_lv=1;*/PB&=~(0X07<<3);PB|=0X05<<3;}
- else if(ledflag2==3){t1=7;/*nb_hong=1;nb_huang=1;nb_lv=0;*/PB&=~(0X07<<3);PB|=0X03<<3;}
- djs(t,t1);
- }else
- {
- TR0=0;
- PA=0X00;
-
- }
- }
- }
- void keyscan(void)//p2^6
- {
- static key_state=1;
- if(key_state&&(state==0))
- {
- delayms(10);
- if(state==0)
- {
- key_state=0;
- run_state=!run_state;
-
- }
- }else if(state)
- {
- key_state=1;
- }
- }
- void delayms(uint xms)
- {
- uint i,j;
- for(i=xms;i>0;i--)
- for(j=110;j>0;j--);
- }
- void display1(uchar shi,uchar ge,uchar shi1,uchar ge1)
- {
- P2=0xfe;
- PA=0x00;
- PA=table[shi];
- delayms(5);
- P2=0xfd;
- PA=0x00;
- PA=table[ge];
- delayms(5);
-
- P2=0xfb;
- PA=0x00;
- PA=table[shi1];
- delayms(5);
- P2=0xf7;
- PA=0x00;
- PA=table[ge1];
- delayms(5);
- }
- void djs(uchar xs,uchar xs1)
- {
-
- shi=(xs-num2)/10;
- ge=(xs-num2)%10;
- shi1=(xs1-num3)/10;
- ge1=(xs1-num3)%10;
- display1(shi,ge,shi1,ge1);
- if((xs-(num2-1))==0)
- {
- num2=0;
- if(ledflag1<4)ledflag1++;
- else ledflag1=1;
- }
-
- if((xs1-(num3-1))==0)
- {
- num3=0;
- if(ledflag2<4)ledflag2++;
- else ledflag2=1;
- }
-
- // }
- }
- void init(void)
- {
- TMOD=0x11;
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- EA=1;
- ET0=1;
- TR0=1;
- }
- void T0_time()interrupt 1
- {
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- num1++;
- if(num1==20)
- {
- num1=0;
- num2++;
- num3++;
- }
- }
- void INT0_HANDER(void) interrupt 0
- {
- delayms(10);
- if(int0==0)
- {
- if(run_state==0)
- {
- if(k1<3)k1++;
- else k1=1;
- if(k1==1){PB&=~(0X07);PB|=0X06;}
- else if(k1==2){PB&=~(0X07);PB|=0X05;}
- else if(k1==3){PB&=~(0X07);PB|=0X03;}
-
- }
-
- }
-
- }
- void INT1_HANDER(void) interrupt 2
- {
- delayms(10);
-
- if(int1==0)
- {
- if(run_state==0)
- {
- if(k2<3)k2++;
- else k2=1;
- if(k2==1){PB&=~(0X07<<3);PB|=(0X06<<3);}
- else if(k2==2){PB&=~(0X07<<3);PB|=(0X05<<3);}
- else if(k2==3){PB&=~(0X07<<3);PB|=(0X03<<3);}
-
- }
- }
-
- }
复制代码
全部资料51hei下载地址:
交通灯.rar
(122.91 KB, 下载次数: 106)
动态图片效果.rar
(5.68 MB, 下载次数: 23)
|