甲乙主从通道实现,另外实现调节时间和暂停运行
单片机源程序如下:
- /***********************************************************
- 名称:基于AT89S52数码管系统交通灯程序
- 要求:红禄灯灯电路,数码管电路,独立式键盘电路,蜂鸣器电路,统一交通灯设计
- 作者:黄*
- 时间:2019年9月1日
- 电路描述:S1到S5一键一功能;S1开始,S2暂停,S3调整时间加,S4调整时间减,S5进入紧急状态;
- ************************************************************/
- #include<reg52.h>
- #include<intrins.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit S1=P1^0; //开始运行按键
- sbit S2=P1^1; //暂停运行按键
- sbit S3=P1^2; //调整时间加按键
- sbit S4=P1^3; //调整时间减按键
- sbit S5=P1^4; //进入紧急状态按键
- sbit red1=P3^2; //甲通道红灯
- sbit green1=P3^3; //甲通道禄灯
- sbit yellow1=P3^4; //甲通道黄灯
- sbit red2=P3^5; //乙通道红灯
- sbit green2=P3^6; //乙通道禄灯
- sbit yellow2=P3^7; //乙通道黄灯
- sbit beep=P3^1; //蜂鸣器报警控制端
- uchar code discode[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90}; //送段码0-9
- uchar code discode1[]={0x92,0x92,0x88,0x8C,0x8C,0xC0,0x87,0x92}; //送段码STOP PASS
- uchar disbitcode[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; //送位码
- uchar disbuf[2]={0,0}; //显示缓冲区
- uchar cent,second1=60,second=60,second2=30,a;
- /*函数申明*/
- void display(void);
- void ini(void);
- void t0(void);
- void stop(void);
- /*************延时程序**************/
- void delay(uint m)
- {
- while(--m);
- }
- /*************初始化程序**************/
- void ini(void)
- {
- TMOD=0x01;
- TH0=(65536-46082)/256;
- TL0=(65536-46082)%256;
- ET0=1;
- EA=1;
- }
- /*************显示紧急通道程序**************/
- void stop(void)
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- P0=discode1[i];
- P2=disbitcode[i];
- delay(100);
- P2=0X00;
- }
- }
- /*************显示程序**************/
- void display(void)
- {
- uchar i;
- for(i=0;i<2;i++)
- {
- P0=discode[disbuf[i]];
- P2=disbitcode[i];
- delay(100);
- P2=0X00;
- }
- }
- /*************中断程序**************/
- void t0(void) interrupt 1 using 0 //用第0组寄存器
- {
- cent++;
- if(cent==20)
- {
- cent=0;
- second--;
- if(second==0&&a==0)
- {
- second=3;
- a=1;
- }
- if(second==0&&a==1)
- {
- second=second2;
- a=2;
- }
- if(second==0&&a==2)
- {
- second=3;
- a=3;
- }
- if(second==0&&a==3)
- {
- second=second1;
- a=0;
- }
- }
- disbuf[0]=second%10; //更新显示缓冲区
- disbuf[1]=second/10;
- TH0=(65536-46082)/256;
- TL0=(65536-46082)%256;
- }
- /*************键扫描程序**************/
- uchar keyscan()
- {
- uchar tmp; //列号,行号
- P1=0xff; //发全行0扫描码
- if((P1&0xff)!=0xff) //若有键按下
- {
- delay(1000); // 延时去抖
- if((P1&0xff)!=0xff) //有键按下
- {
- tmp=(P1&0xff);
- do{}while((P1&0xff)!=0xff);
- tmp=~tmp;
- return (tmp); //还回键值
- }
- }
- return (0); //无键按下,还回0
- }
- /*************主程序**************/
- void main(void)
- {
- uchar v,b=0;
- ini();
- v=keyscan();
- while(v==0)
- {
- P0=0X0C;
- P2=0X01;
- P3=0XFF;
- v=keyscan();
- }
- if(v==1||v==16)
- {
- while(1)
- {
- switch(v)
- {
- case 1:
- TR0=1;
- while(v==1||v==0)
- {
- if(a==0)
- {
- loop1: yellow2=1;
- red1=1;
- green1=0;
- red2=0;
- }
- if(a==1)
- {
- yellow1=0;
- green1=1;
- delay(100);
- yellow1=1;
- }
- if(a==2)
- {
- loop2: red1=0;
- red2=1;
- green2=0;
- yellow1=1;
- }
- if(a==3)
- {
- green2=1;
- yellow2=0;
- delay(100);
- yellow2=1;
- }
- display();
- v=keyscan(); //调键扫描程序
- }
- break;
- case 2:
- while(v==2||v==0)
- {
- TR0=0;
- display();
- v=keyscan();
- }
- break;
- case 4:
- b++;
- v=keyscan();
- break;
- case 8:
- if(b%2==0)
- {
- second1++;
- if(second1==100)
- second1=0;
- disbuf[0]=second1%10; //更新显示缓冲区
- disbuf[1]=second1/10;
- }
- else
- {
- second2++;
- if(second2==100)
- second2=0;
- disbuf[0]=second2%10; //更新显示缓冲区
- disbuf[1]=second2/10;
- }
- display();
- v=keyscan();
- break;
-
- case 16:
- if(b%2==0)
- {
- second1--;
- if(second1==0)
- second1=99;
- disbuf[0]=second1%10; //更新显示缓冲区
- disbuf[1]=second1/10;
- }
- else
- {
- second2--;
- if(second2==0)
- second2=99;
- disbuf[0]=second2%10; //更新显示缓冲区
- disbuf[1]=second2/10;
- }
- display();
- v=keyscan();
- break;
- case 32:
- while(v==32||v==0) /*显示八个8*/
- {
- P3=0XFF;
- red1=0;
- red2=0;
- beep=0;
- stop();
- delay(10000);
- beep=1;
- v=keyscan(); //调键扫描程序
- }
- break;
- case 64:
- second=60;
- P3=0XFF;
- a=0;
- goto loop1;
- break;
- case 128:
- second=30;
- P3=0XFF;
- a=2;
- goto loop2;
- break;
- }
- display();
- v=keyscan();
- }
- }
- }
复制代码
所有资料51hei提供下载:
交通灯.zip
(29.72 KB, 下载次数: 8)
|