51单片机模拟红绿灯仿真原理图如下(proteus仿真工程和程序文件可到本帖附件中下载)
单片机代码如下:
- #include<reg52.h>
- #define uint unsigned int
- #define uchar unsigned char
- #define ON 0
- #define OFF 1
-
- sbit g1=P1^0;
- sbit y1=P1^1;
- sbit l1=P1^2;
- sbit g2=P1^3;
- sbit y2=P1^4;
- sbit l2=P1^5;
-
- uchar num,num1,key,a,b,c,d;
- uint t0,t1; /*t0,t1,要大于255!!*/
- uchar code table[]={ 0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71,0};
- /*uchar code table[]={ 0xf9,0xdb,0xa4,0xb0,0x80,0x90,0x88,0x83,0x99,0x92,0x82,0xf8, 0xc6,0xa1,0x86,0x8e,0xff};*/
-
- void init(); //初值函数
-
- void display(uchar a,uchar b,uchar c,uchar d); //显示程序
-
- void Keyscan();
-
- void delay (uint z) //延时函数
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- void main()
- {
- init();
- while(1)
- {
- display(a,b,c,d);
- Keyscan();
- if(key) //如果有按键按下,
- {
- TR0=0;
- TR1=0; //不计时
- P0=table[9]; //显示99
- P2=0; //全部显示
- if(key==1) //东西方向
- {
- P1=0xde; //LED状态
- }
- if(key==2) //南北方向
- {
- P1=0xf3; //LED状态
- }
- while(key)
- {
- Keyscan(); //等待按下正常
- }
- if(t0<=400)
- P1=0xde; //11 01 1 110 绿红
- else if(t0>400&&t0<=460)
- P1=0xdd; //11 01 1 101 黄红
- else if(t0>460&&t0<=920)
- if(t1>860) //南北为黄灯
- P1=0xeb; //11 10 1 011
- else
- P1=0xf3; //11 11 0 011
- TR0=1;
- TR1=1;
- }
- }
- }
- void Keyscan()
- {
- uchar temp;
- temp=P3;
- while(temp!=0xff)
- {
- delay(5); //延时,消抖
- temp=P3; //再次确认是否有健按下
- temp=temp&0xff;
- while(temp!=0xff)
- {
- temp=P3;
- switch(temp)
- {
- case 0xfe:key=1; break;
- case 0xfd:key=2; break;
- case 0xfb:key=0; break;
- }
- while(temp!=0xff) //松手检测
- {
- temp=P3;
- temp=temp&0xff;
- }
- }
- }
- }
-
- void init() //初值函数
- {
- g1=ON;
- y1=OFF;
- l1=OFF;
- g2=OFF;
- y2=OFF;
- l2=ON;
- num=20;
- num1=25;
- a=num/10;
- b=num%10;
- c=num1/10;
- d=num1%10;
-
- TMOD=0x11;
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- TH1=(65536-50000)/256;
- TL1=(65536-50000)%256;
- EA=1;
- ET0=1;
- ET1=1;
- TR0=1;
- TR1=1;
- }
- void timer0() interrupt 1 //定时器0中断
- {
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- t0++;
- if(t0%20==0) //每一秒进入 一次
- {
- if(t0<=400) //绿灯 时间
- {
- num--;
- if(num==0) //绿灯结束,开黄灯
- {
- num=5;
- g1=OFF;
- y1=ON;
- l1=OFF;
- }
- }
- if(t0>400&&t0<=500) //黄灯时间
- {
- num--;
- if(num==0) //黄灯结束,开红灯
- {
- num=25;
- g1=OFF;
- y1=OFF;
- l1=ON;
- }
- }
- if(t0>500&&t0<=1000) //红灯时间
- {
- num--;
- if(num==0) //红灯结束,开绿灯
- {
- num=20;
- g1=ON;
- y1=OFF;
- l1=OFF;
- }
- if(t0==1000)
- t0=0;
- }
- a=num/10;
- b=num%10;
- }
- }
- void timer1() interrupt 3 //定时器1中断
- {
- TH1=(65536-50000)/256;
- TL1=(65536-50000)%256;
- t1++;
- if(t1%20==0)
- {
- if(t1<=500)
- {
- num1--;
- if(num1==0)
- {
- num1=20;
- g2=ON;
- y2=OFF;
- l2=OFF;
- }
- }
- if(t1>500&&t1<=900)
- {
- num1--;
- if(num1==0)
- {
- num1=5;
- g2=OFF;
- y2=ON;
- l2=OFF;
- }
- }
- if(t1>900&&t1<=1000)
- {
- num1--;
- if(num1==0)
- {
- num1=25;
- g2=OFF;
- y2=OFF;
- l2=ON;
- }
- if(t1==1000)
- t1=0;
- }
- c=num1/10;
- d=num1%10;
- }
- }
- /*void display(uchar a,uchar b,uchar c,uchar d) //显示程序
- {
- P0=table[a]; P2=1; delay(1); P2=255;
- P0=table[b]; P2=2; delay(1); P2=255;
- P0=table[c]; P2=4; delay(1); P2=255;
- P0=table[d]; P2=8; delay(1); P2=255; } */
- void display(uchar a,uchar b,uchar c,uchar d) //显示程序
- {
- P0=table[a]; P2=0xfe; delay(1); P2=0xff;
- P0=table[b]; P2=0xfd; delay(1); P2=0xff;
- P0=table[c]; P2=0xfb; delay(1); P2=0xff;
- P0=table[d]; P2=0xf7; delay(1); P2=0xff;
- }
复制代码 全部资料51hei下载地址:
红绿灯(proteus仿真+程序).rar
(77.63 KB, 下载次数: 35)
|