任务目的
设计一路口的简单人行道交通灯,指挥车辆和行人安全通行。红灯亮禁止通行,绿灯亮允许通行。红灯的设计时间为45秒,绿灯为30秒。傍边还有一个警示牌写着“ sidewalk caution”,译为 人行道,注意安全。来提醒往来的车辆。
单片机源程序如下:
- #include <reg51.h>
- #define uchar unsigned char
- #define DATAPORT P3
- sbit RS=P1^7;
- sbit RW=P1^6;
- sbit E=P1^5;
- sbit key0=P1^0;
- sbit key1=P1^1;
- sbit key2=P1^2;
- sbit RED=P1^3;
- sbit GREEN=P1^4;
- unsigned char num;
- unsigned char s=45;
- unsigned char flag=0;
- void init(void);
- void writeCOM(uchar i);
- void writeData(uchar i);
- void fbusy();
- void DelayMs(unsigned char y)
- {
- while(y--);
- }
- unsigned code DuanMa[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
- unsigned char code WeiMa[]={0x01,0x02};
- void seg(unsigned char c )
- {
-
- P2=WeiMa[0];
- P0=DuanMa[c%10];
- DelayMs(10);
- P0=0x00;
- P2=WeiMa[1];
- P0=DuanMa[c/10];
- DelayMs(10);
- P0=0x00;
- }
- void time_djs()
- {
- if(num==0)
- {
- s--;
- if(s==0)
- {
- flag++;
- s=30;
- s--;
- if(flag==2)
- {
- s=45;
- flag=0;
- }
- }
- }
- }
- void LED_RED_GREEN()
- {
- if(flag==0)
- {
- RED=1;
- }
- else if(flag==1)
- {
- RED=0;
- GREEN=1;
- if(s==1)
- {
- GREEN=0;
- }
- if(s==1)
- {
- GREEN=0;
- }
- }
- }
- void T0_time() interrupt 1
- {
- TH0=(65536-45872)/256;
- TL0=(65536-45872)%256;
- num++;
- if(num==20)
- {
- num=0;
- time_djs();
-
- }
- }
- void Init_Timer0()
- {
- TMOD = 0x01;
- TH0=(65536-45872)/256;
- TL0=(65536-45872)%256;
- EA = 1;
- ET0 = 1;
- TR0 = 1;
- }
- void keycan()
- {
- if (key0==0)
- {
- DelayMs(20);
- if (key0==0)
- {
- EA=~EA;
- }
- while(!key0);
- }
- if(key1==0)
- {
- DelayMs(20);
- if(key1==0)
- {
- s++;
- }
- if(s==45)
- {
- s=0;
- }
- while(!key1);
- }
- if(key2==0)
- {
- DelayMs(20);
- if(key2==0)
- {
- s--;
- }
- if(s==0)
- {
- s=45;
- }
- while(!key2);
- }
- }
- void init()
- {
- writeCOM(0x01);
- writeCOM(0x38);
- writeCOM(0x0f);
- writeCOM(0x06);
- }
- void fbusy()
- {
- DATAPORT=0Xff;
- RS=0;RW=1;
- E=0;E=1;
- while (DATAPORT&0x80)
- {E=0;E=1;}
- }
- void writeCOM(uchar j)
- {
- fbusy();
- RS=0;RW=0;
- E=1;
- DATAPORT=j;
- E=0;
- }
- void writeData(uchar j)
- {
- fbusy();
- RS=1;RW=0;
- E=1;
- DATAPORT=j;
- E=0;
- }
-
- void lcd_xianshi()
- {
-
-
- writeCOM(0x80);
- writeData('s');
- writeData('i');
- writeData('d');
- writeData('e');
- writeData('W');
- writeData('a');
- writeData('l');
- writeData('k');
- writeCOM(0xc4);
- writeData('C');
- writeData('a');
- writeData('u');
- writeData('t');
- writeData('i');
- writeData('O');
- writeData('n');
- writeData('!');
- }
- void main()
- {
- RED=0;
- GREEN=0;
- init();
- lcd_xianshi();
- Init_Timer0();
- while(1)
- {
- seg(s);
- keycan();
- LED_RED_GREEN();
- }
- }
复制代码
所有资料51hei提供下载:
renxingdeng.zip
(71.74 KB, 下载次数: 26)
|