利用89c51單片機進行交通燈的變換: 利用定時器T0,外部中斷0,外部中斷1。實現(xiàn)的一個可以路口交通燈轉換,并且?guī)в袞|西(南北)道路的通行禁行,用于交通擁堵時疏導道路交通
還有當救護車通過時的緊急情況全面禁行道路的功能。
同時進行現(xiàn)場保護,使得程序可以無限循環(huán)而不怕打斷。
正常通行狀況:
QQ圖片20181202140529.png (26.77 KB, 下載次數(shù): 29)
下載附件
正常通行狀況
2018-12-2 14:10 上傳
QQ圖片20181202140550.png (26.15 KB, 下載次數(shù): 36)
下載附件
正常通行狀況
2018-12-2 14:10 上傳
QQ圖片20181202140610.png (28.25 KB, 下載次數(shù): 39)
下載附件
東西通行南北禁行
2018-12-2 14:11 上傳
東西通行南北禁行
QQ圖片20181202140631.png (29.02 KB, 下載次數(shù): 25)
下載附件
緊急情況全面禁行
2018-12-2 14:11 上傳
緊急情況全面禁行
單片機源程序如下:
- #include <reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- uint timer0=0;
- uint timer1=0;
- uint timer2=0;
- uint timer3=0;
- uchar second;
- uchar second1;
- uchar a,b,c,d,e,f,m,n,x,v,j,h;
- uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- sbit g=P1^5;
- sbit y=P1^6;
- sbit r=P1^7;
- sbit G1=P3^5;
- sbit Y1=P3^6;
- sbit R1=P3^7;
- sbit ZUO1=P1^0;
- sbit YOU1=P1^1;
- sbit ZUO2=P1^2;
- sbit YOU2=P1^3;
- sbit key1=P3^0;
- sbit key2=P3^1;
- void delay(uint i)
- {
- uint j,k;
- for(j=0;j<i;j++)
- for(k=0;k<115;k++);
- }
- void init()
- {
- second=25;
- second1=30;
- TMOD=0x01;
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- EA=1;
- ET0=1;
- EX0=1;
- IT0=1;
- EX1=1;
- IT1=1;
- IP=0x02;
- TR0=1;
- a=0;
- b=0;
- c=10;
- d=10;
- e=10;
- f=10;
- }
- void main()
- {
- init();
- while(1)
- {
- // P1=0x02;
- YOU1=1;ZUO1=0;ZUO2=0;YOU2=0;
- P2=~table[second/10];
- delay(10);
- // P1=0x01;
- YOU1=0;ZUO1=1;ZUO2=0;YOU2=0;
- P2=~table[second%10];
- delay(10);
- // P1=0x04;
- ZUO1=0;YOU1=0;ZUO2=1;YOU2=0;
- P0=~table[second1/10];
- delay(10);
- // P1=0x08;
- ZUO1=0;YOU1=0;ZUO2=0;YOU2=1;
- P0=~table[second1%10];
- delay(10);
- if(a==0)
- {g=0;r=1;}
- if(a==1)
- {y=0;g=1;}
- if(a==2)
- {r=0;y=1;}
- if(b==0)
- {R1=0;Y1=1;}
- if(b==1)
- {G1=0;R1=1;}
- if(b==2)
- {Y1=0;G1=1;}
- }
- }
- void T_0timer0() interrupt 1
- {
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- if(a==0)
- {
- timer0++;
- if(timer0==15)
- {
- timer0=0;
- second--;
- }
- if(second==0)
- {
- second=5;
- a=1;
- timer0=0;
-
- }
- }
- if(a==1)
- {
-
- timer0++;
- if(timer0==15)
- {
- timer0=0;
- second--;
- }
- if(second==0)
- {
- second=20;
- timer0=0;
- a=2;
-
- }
- }
- if(a==2)
- {
-
- timer0++;
- if(timer0==15)
- {
- timer0=0;
- second--;
- }
- if(second==0)
- {
- a=3;
-
- }
- }
- if(a==3)
- {
- a=0;
- second=25;
- }
- if(b==0)
- {
- timer1++;
- if(timer1==15)
- {
- timer1=0;
- second1--;
- }
- if(second1==0)
- {
- second1=15;
- b=1;
- timer1=0;
- }
- }
- if(b==1)
- {
- timer1++;
- if(timer1==15)
- {
- timer1=0;
- second1--;
- }
- if(second1==0)
- {
- second1=5;
- timer1=0;
- b=2;
- }
- }
- if(b==2)
- {
- timer1++;
- if(timer1==15)
- {
- timer1=0;
- second1--;
- }
- if(second1==0)
- {
- b=3;
- }
- }
- if(b==3)
- {
- b=0;
- second1=30;
- }
- if(c==0)
- {
- timer2++;
- if(timer2==15)
- {
- timer2=0;
- second--;
- }
- if(second==0)
- {
- c=0;d=0;
- second=j;second1=h;
- }
- }
-
- if(d==0)
- {
- timer2++;
- if(timer2==15)
- {
- timer2=0;
- second1--;
- }
- }
- if(e==0)
- {
- timer3++;
- if(timer3==15)
- {
- timer3=0;
- second--;
- }
- if(second==0)
- {
- a=m;b=n;
- e=10;f=10;
- second=v;second1=x;
- }
- if(f==0)
- {
- timer3++;
- if(timer3==15)
- {
- timer3=0;
- second1--;
- }
- }
- }
- }
- void waibu1(void) interrupt 2
- {
- TR0=0;
- if(key1==0)
- {
- TR0=0;
- j=second;h=second1;
- second=20;
- second1=20;
- a=10;b=10;
- c=0;d=0;
- e=10;f=10;
- TR0=1;
- g=1;r=0;y=1;
- G1=0;R1=1;Y1=1;
- }
- if(key2==0)
- {
- TR0=0;
- j=second;h=second1;
- second=20;
- second1=20;
- a=10;b=10;
- c=0;d=0;
- e=10;f=10;
- TR0=1;
- g=0;r=1;y=1;
- G1=1;R1=0;Y1=1;
- }
- }
- void waibu0(void) interrupt 0
- {
- TR0=0;
- m=a;n=b;v=second;x=second1;
- second=20;
- second1=20;
- a=10;b=10;
- c=10;d=10;
- e=0;f=0;
- EX0=0;
- EX1=0;
- TR0=1;
- g=1;r=0;y=1;
- G1=1;R1=0;Y1=1;
- }
復制代碼
|