东西向绿灯;
南北向红灯;
东西向红灯;
南北向绿灯;
南北向向黄灯闪烁;
时间显示(时间自定);
下面是仿真电路;
(注:没画最小系统)
篇幅有限,代码放附件了
单片机源程序如下:
- #include<reg51.h>
- //#define uchar unsigned char
- //#define uint unsigned int
- char code smg_dw[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
- //共阳0-9
- sbit lR=P1^0;
- sbit lY=P1^1;
- sbit lG=P1^2;
- sbit hR=P1^3;
- sbit hY=P1^4;
- sbit hG=P1^5;
- sbit shi=P3^0;
- sbit ge=P3^1;
- int a,b,c,type,j,i;
- void Delay(int x);
- void delay_ms(int q);
- void shumaguan(int k);
- main()
- {
- TMOD=0x01;
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- TR0=1;
- ET0=1;
- EX0=1;
- EA=1;
- a=10;
- type=1;
- while(1){
- switch(type)
- {
- case 1: //东西向绿灯与南北向红灯亮
- lR=0;lY=1;lG=1;
- hR=1;hY=1;hG=0;
- shumaguan(a);
- break;
- case 2: //东西向黄灯闪烁,绿灯关闭
- if(a%2!=0)
- {
- lR=1;lY=0;lG=1;
- }
- if(a%2==0)
- {
- lR=1;lY=1;lG=1;
- }
- shumaguan(a);
- break;
- case 3: //东西向红灯,南北向绿灯亮
- lR=1;lY=1;lG=0;
- hR=0;hY=1;hG=1;
- shumaguan(a);
- break;
- case 4: //南北向黄灯闪烁3次
- if(a%2!=0)
- {
- hR=1;hY=0;hG=1;
- }
- if(a%2==0)
- {
- hR=1;hY=1;hG=1;
- }
- shumaguan(a);
- break;
- }
- }
- }
- void T0_int0(void) interrupt 1 //中断
- {
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- i++;
- if(i==20)
- {
- i=0;
- a--;
- if(a==0)
- {
- if(type==1||type==3)
- {
- a=5;
- }
- if(type==2||type==4)
- {
- a=10;
- }
- type++;
- if(type==5)
- {
- type=1;
- }
- }
- }
- }
- void shumaguan(int k) //数码管显示
- {
- b=k/10;
- c=k%10;
- P3=0xfd;
- P2=smg_dw[b];//选段码
- delay_ms(20);//延时5ms
- P3=0xfe;
- P2=smg_dw[c];//选段码
- delay_ms(20);
- }
- void Delay(int x) //延时
- {
- char i;
- while(x--)for(i=0;i<120;i++);
- }
- void delay_ms(int q) //延时
- {
- int i,j;
- for(i=q;i>0;i--)
- for(j=141;j>0;j--);
- }
复制代码
所有资料51hei提供下载:
交通灯.zip
(46.36 KB, 下载次数: 91)
|