|
基于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灯等模块,还是比较简单的。喜欢的朋友可以自己动手尝试一下。
|
|