|
基于AT89C52單片機(jī)的交通燈主要具有如下功能。
基本要求如下:
1. 按鍵1為交通燈“深夜模式”開/關(guān),按下后進(jìn)入深夜模式,4個(gè)方向LED(黃)閃爍;再次按下后則退出深夜模式,交通燈正常運(yùn)行。
2. 按鍵2為“時(shí)間調(diào)整”開/關(guān),上電后,交通燈正常運(yùn)行,按下此按鍵后,進(jìn)入調(diào)時(shí)模式,此時(shí)按鍵3和4均有效。
3. 按鍵3為通行“時(shí)間加”,按鍵4為通行“時(shí)間減”。
4. 設(shè)定好通行時(shí)間后,按鍵2再次按下,退出調(diào)時(shí)模式。
5. 交通燈設(shè)置好正常運(yùn)行時(shí),按鍵3和按鍵4均無效。
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
單片機(jī)源程序如下:
- #include<reg52.h>
- typedef unsigned char uint8;
- typedef unsigned int uint16;
- sbit renrh=P3^0; //人行道右紅
- sbit renrl=P3^1;// 人行道右綠
- sbit rensh=P2^0; //人上下紅燈
- sbit rensl=P2^1; //人上下綠燈
- sbit cherh=P2^5; //車左右紅燈
- sbit chery=P2^6; //車左右黃燈
- sbit cherl=P2^7; //車左右綠燈
- sbit chesh=P2^2; //車上下紅燈
- sbit chesy=P2^3;//車上下黃燈
- sbit chesl=P2^4;//車上下綠燈
- uint8 smgduan[16]={0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07,
- 0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71};
- uint8 smgwei[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
- uint8 sec,sum;
- uint8 smgshu[8];
- void delay(uint8 i)
- {
- uint8 j,k;
- for(;i>0;i--)
- for(j=10;j>0;j--)
- for(k=130;k>0;k--);
- }
- void init0()
- {
- TMOD=0x01;
- TH1=0x3c;
- TL1=0xb0; //定時(shí)50ms
- TR0=1;
- ET0=1;
- EA=1;
- }
- void smgsaomiao()
- {
- uint8 i;
- for(i=0;i<8;i++)
- {
- P0=smgshu[i];
- delay(1);
- P1=smgwei[i];
- //delay(1);
- }
- }
- void main()
- {
- init0();
- sec=1;
- while(1)
- {
- if(sec==70)
- {
- sec=1;
- }
- if(sec<31)
- {
- P2=0xff;
- P3=0xff;
- renrh=0;
- chesl=0;
- cherh=0;
- rensl=0;
- smgshu[0]=0x00;
- smgshu[1]=0x00;
- smgshu[2]=smgduan[(30-sec)/10];
- smgshu[3]=smgduan[(30-sec)%10];
- smgshu[4]=0x00;
- smgshu[5]=0x00;
- smgshu[6]=smgduan[(30-sec)/10];
- smgshu[7]=smgduan[(30-sec)%10];
- smgsaomiao();
- }
- if(sec>30&&sec<36)
- {
- P3=0xff;
- P2=0xff;
- renrh=0;
- chesy=0;
- chery=0;
- rensh=0;
- smgshu[0]=0x00;
- smgshu[1]=0x00;
- smgshu[2]=smgduan[(36-sec)/10];
- smgshu[3]=smgduan[(36-sec)%10];
- smgshu[4]=0x00;
- smgshu[5]=0x00;
- smgshu[6]=smgduan[(36-sec)/10];
- smgshu[7]=smgduan[(36-sec)%10];
- smgsaomiao();
- }
- if(sec>35&&sec<66)
- {
- P3=0xff;
- P2=0xff;
- renrl=0;
- chesh=0;
- cherl=0;
- rensh=0;
- smgshu[0]=0x00;
- smgshu[1]=0x00;
- smgshu[2]=smgduan[(65-sec)/10];
- smgshu[3]=smgduan[(65-sec)%10];
- smgshu[4]=0x00;
- smgshu[5]=0x00;
- smgshu[6]=smgduan[(65-sec)/10];
- smgshu[7]=smgduan[(65-sec)%10];
- smgsaomiao();
- }
- if(sec>65&&sec<71)
- {
- P3=0xff;
- P2=0xff;
- renrh=0;
- chesy=0;
- chery=0;
- rensh=0;
- smgshu[0]=0x00;
- smgshu[1]=0x00;
- smgshu[2]=smgduan[(70-sec)/10];
- smgshu[3]=smgduan[(70-sec)%10];
- smgshu[4]=0x00;
- smgshu[5]=0x00;
- smgshu[6]=smgduan[(70-sec)/10];
- smgshu[7]=smgduan[(70-sec)%10];
- smgsaomiao();
- }
- }
- }
- void time0() interrupt 1
- {
- TH1=0x3c;
- TL1=0xb0;
- sum++;
- if(sum==20)
- {
- sum=0;
- sec++;
- }
- }
復(fù)制代碼
仿真程序:
交通燈仿真實(shí)驗(yàn).zip
(78.25 KB, 下載次數(shù): 30)
2023-12-12 14:01 上傳
點(diǎn)擊文件名下載附件
|
評分
-
查看全部評分
|