|
基于51單片機的交通燈仿真模擬實驗
仿真軟件是 preteus pro 7.8
用這個軟件時一定要注意電腦的用戶最好是英文的否則不能仿真(如果不是英文的可以在電腦上注冊一個英文的用戶)
下面讓我們來看一下這個程序
#include "reg51.h"
sbit keyjian=P3^0;
sbit keyjia=P3^1;
sbit keyopen=P3^2;
sbit keyzhuan=P3^3;
sbit lednbr=P1^4; //紅(南北)
sbit lednby=P1^5; //黃
sbit lednbg=P1^6; //綠
sbit lednbz=P1^7; //左
sbit leddxr=P1^0; //東西
sbit leddxy=P1^1;
sbit leddxg=P1^2;
sbit leddxz=P1^3;
sbit ad0=P0^0;
sbit ad1=P0^1;
sbit ad2=P0^2;
sbit ad3=P0^3;
unsigned int code smgduan[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//顯示0-9
unsigned int weixuan[4];
int flag=0;
int ssec=0;
int sec=0;
int time1=40,time2=20;//time1南北,time2東西
/***************************延遲函數************************/
void delay(int a)
{
while(a--);
}
/**************************定時器中斷*****************************/
void time0()
{
TMOD=TMOD|0X01;
TH0=0Xd8;
TL0=0Xf0;
ET0=1;
EA=1;
TR0=1;
}
/************************外部中斷********************************/
void waibu()
{
EA=1;
IT0=0;
EX0=1;
}
/************************數碼管顯示函數**************************/
void xuanwei()
{
unsigned int i;
for(i=0;i<4;i++)
{
switch(i)
{
case(0):
ad0=0;ad1=1;ad2=1;ad3=1;break;
case(1):
ad0=1;ad1=0;ad2=1;ad3=1;break;
case(2):
ad0=1;ad1=1;ad2=0;ad3=1;break;
case(3):
ad0=1;ad1=1;ad2=1;ad3=0;break;
}
P2=weixuan[ i]; [ i]
delay(100);
P2=0x00;
}
}
void date()
{
if(time1>time2)
{
if(time2-sec<0)
{
weixuan[0]=smgduan[(time1-sec)/10];
weixuan[1]=smgduan[(time1-sec)%10];
weixuan[2]=smgduan[(time1-sec)/10];
weixuan[3]=smgduan[(time1-sec)%10];
}
else
{
weixuan[0]=smgduan[(time1-sec)/10];
weixuan[1]=smgduan[(time1-sec)%10];
weixuan[2]=smgduan[(time2-sec)/10];
weixuan[3]=smgduan[(time2-sec)%10];
}
}
if(time1<time2)
{
if(time1-sec<0)
{
weixuan[0]=smgduan[(time2-sec)/10];
weixuan[1]=smgduan[(time2-sec)%10];
weixuan[2]=smgduan[(time2-sec)/10];
weixuan[3]=smgduan[(time2-sec)%10];
}
else
{
weixuan[0]=smgduan[(time1-sec)/10];
weixuan[1]=smgduan[(time1-sec)%10];
weixuan[2]=smgduan[(time2-sec)/10];
weixuan[3]=smgduan[(time2-sec)%10];
}
}
}
/***************************主函數*************************/
void main()
{
time0();
waibu();
while(1)
{
date();
xuanwei();
}
}
/****************************中斷函數**********************/
void waibu0() interrupt 0
{
if(keyopen==0)
{
delay(100);
if(keyopen==0)
{
if(keyzhuan==0)
{
delay(100);
if(keyzhuan==0)
// flag=!(flag);
// delay(40000);
delay(40000);
if(keyzhuan==1)
flag=!(flag);
}
if(flag==1)
{
if(keyjia==0)
{
delay(100);
if(keyjia==0)
{
// time1++;
// delay(40000);
delay(40000);
if(keyjia==1)
time1++;
}
}
if(keyjian==0)
{
delay(100);
if(keyjian==0)
{
// time1--;
// delay(40000);
delay(40000);
if(keyjian==1)
time1--;
}
}
}
if(flag==0)
{
if(keyjia==0)
{
delay(100);
if(keyjia==0)
{
// time2++;
// delay(40000);
delay(40000);
if(keyjia==1)
time2++;
}
}
if(keyjian==0)
{
delay(100);
if(keyjian==0)
{
// time2--;
// delay(40000);
delay(40000);
if(keyjian==1)
time2--;
}
}
}
if(time1<=0||time2<=0||time1>100||time2>100)
{
time1=40;
time2=20;
}
}
}
}
void timer0() interrupt 1
{
int t;
TH0=0Xd8;
TL0=0Xf0; //10ms
ssec++;
if(ssec>100)
{
ssec=0;
sec++;
}
if(time1>time2) //東西
{
if(time2-sec<=5&&time2-sec>0)
{
if(sec%2==1)
{
leddxy=0;
leddxg=0;
leddxr=1;
leddxz=1;
lednbr=0;
lednby=1;
lednbg=1;
lednbz=1;
}
else
leddxy=1;
}
else if(time2-sec>5)
{
leddxy=1;
leddxg=0;
leddxr=1;
leddxz=1;
lednbr=0;
lednby=1;
lednbg=1;
lednbz=1;
}
else if(time2-sec<=0&&time1-sec>5)
{
leddxy=1;
leddxg=0;
leddxr=1;
leddxz=0;
lednbr=0;
lednby=1;
lednbg=1;
lednbz=1;
}
}
if(time1<time2) //南北
{
if(time1-sec<=5&&time1-sec>0&&time2-sec>0)
{
if(sec%2==1)
{
leddxy=1;
leddxg=1;
leddxr=0;
leddxz=1;
lednbr=1;
lednby=0;
lednbg=0;
lednbz=1;
}
else
lednby=1;
}
else if(time1-sec>5)
{
leddxy=1;
leddxg=1;
leddxr=0;
leddxz=1;
lednbr=1;
lednby=1;
lednbg=0;
lednbz=1;
}
else if(time1-sec<=0&&time2-sec>5)
{
leddxy=1;
leddxg=1;
leddxr=0;
leddxz=1;
lednbr=1;
lednby=1;
lednbg=0;
lednbz=0;
}
}
if((time1-sec<=0&&time2-sec<=5)||(time2-sec<=0&&time1-sec<=5))
{
if(sec%2==0)
{
leddxy=1;
lednby=1;
}
else
{
leddxy=0;
lednby=0;
}
}
if(sec>time1&&sec>time2)
{
sec=0;
t=time1;
time1=time2;
time2=t;
}
}
這個程序主要用到了外部中斷,按鍵,定時器中斷,led燈等模塊,還是比較簡單的。喜歡的朋友可以自己動手嘗試一下。
|
-
這是交通燈的仿真圖
-
-
實驗室6.zip
2018-5-21 15:00 上傳
點擊文件名下載附件
42.93 KB, 下載次數: 29
左轉交通燈的仿真和代碼
|