標題:
51單片機交通燈(Proteus仿真電路,C語言程序代碼)
[打印本頁]
作者:
abba
時間:
2020-11-28 15:40
標題:
51單片機交通燈(Proteus仿真電路,C語言程序代碼)
東西向綠燈;
南北向紅燈;
東西向紅燈;
南北向綠燈;
南北向向黃燈閃爍;
時間顯示(時間自定);
下面是仿真電路;
交通燈.jpg
(337.15 KB, 下載次數: 54)
下載附件
2020-11-28 15:34 上傳
(注:沒畫最小系統(tǒng))
篇幅有限,代碼放附件了
單片機源程序如下:
#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)
2020-11-28 15:39 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
作者:
杜少卿
時間:
2020-11-28 18:22
nice
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1