您的位置 首页 编程

51单片机交通灯程序以及仿真

includedefineuintunsignedintdefineucharunsignedcharsbitred_nb=P0^0;sbitgreen_nb=P0^1;sbityellow_nb=

#include

#define uint unsigned int
#define uchar unsigned char
sbit red_nb = P0^0;
sbit green_nb = P0^1;
sbit yellow_nb = P0^2;

sbit red_dx = P0^3;
sbit green_dx = P0^4;
sbit yellow_dx = P0^5;

unsigned char code Disp_Tab[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40}; //段码操控

unsigned char code dispbit[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdF,0xbF,0x7F}; //位选操控

unsigned char LedOut[4];

uchar switch_type = 1;
uchar Traffic_time = 0;
uchar ds_time = 0;
uchar i,t;

void delay(uint i)
{
char j;
for(i; i > 0; i–)
for(j = 200; j > 0; j–);
}

void Traffic_Light() // 界说 四种状况 显现
{
switch(switch_type)
{
case 1:
for( i=0; i<4; i++)
{
P1 = dispbit[i]; //运用查表法进行位选
P3 = LedOut[i];

delay(100);
} //使数码管在此 不断的进行扫描显现

red_nb = 0;//南北红灯亮 绿灯灭黄灯灭
green_nb = 1;
yellow_nb = 1;

red_dx = 1;//东西红灯灭绿灯亮 黄灯灭
green_dx = 0;
yellow_dx = 1;
if(Traffic_time != 30)
{
LedOut[0]=Disp_Tab[(35 – Traffic_time)0/10]; //数码管显现的数据
LedOut[1]=Disp_Tab[(35 – Traffic_time)];
LedOut[2]=Disp_Tab[(29 – Traffic_time)0/10];
LedOut[3]=Disp_Tab[(29 – Traffic_time)];
return; //回来case1
}
Traffic_time = 0;
switch_type = 2; //进入第二种状况
break;

case 2:
for( i=0; i<4; i++)
{
P1 = dispbit[i]; //运用查表法进行位选
P3 = LedOut[i];

delay(100);
}
red_nb = 0;//南北红灯亮 绿灯灭黄灯灭
green_nb = 1;
yellow_nb = 1;

red_dx = 1;//东西红灯灭绿灯灭 黄灯亮
green_dx = 1;

if(Traffic_time != 6) //黄等坚持的时刻
{
if(Traffic_time != t)
{
t = Traffic_time;
yellow_dx = ~yellow_dx;

}
{
LedOut[0]=Disp_Tab[(5 – Traffic_time)0/10];
LedOut[1]=Disp_Tab[(5 – Traffic_time)];
LedOut[2]=Disp_Tab[(5 – Traffic_time)0/10];
LedOut[3]=Disp_Tab[(5 – Traffic_time)];
return;
}
return;
}

Traffic_time = 0;
switch_type = 3;
break;

case 3:
for( i=0; i<4; i++)
{
P1 = dispbit[i]; //运用查表法进行位选
P3 = LedOut[i];

delay(100);
}
red_nb = 1;//南北红灯灭 绿灯亮黄灯灭
green_nb = 0;
yellow_nb = 1;

red_dx = 0;//东西红灯亮绿灯灭 黄灯灭
green_dx = 1;
yellow_dx = 1;
if(Traffic_time != 19)
{LedOut[0]=Disp_Tab[(24 – Traffic_time)0/10];
LedOut[1]=Disp_Tab[(24 – Traffic_time)];
LedOut[2]=Disp_Tab[(18 – Traffic_time)0/10];
LedOut[3]=Disp_Tab[(18 – Traffic_time)];
return;
}
Traffic_time = 0;
switch_type = 4;
break;

case 4:
for( i=0; i<4; i++)
{
P1 = dispbit[i]; //运用查表法进行位选
P3 = LedOut[i];

delay(100);
}
red_nb = 1;//南北红灯灭 绿灯灭黄灯亮
green_nb = 1;

red_dx = 0;//东西红灯亮绿灯灭 黄灯灭
green_dx = 1;
yellow_dx =1;

if(Traffic_time != 6) //黄等坚持的时刻
{
if(Traffic_time != t)
{
t = Traffic_time;
yellow_nb = ~yellow_nb;

}
LedOut[0]=Disp_Tab[(5 – Traffic_time)0/10];
LedOut[1]=Disp_Tab[(5 – Traffic_time)];
LedOut[2]=Disp_Tab[(5 – Traffic_time)0/10];
LedOut[3]=Disp_Tab[(5 – Traffic_time)];
return;
}
Traffic_time = 0;
switch_type = 1;
break;

}
}

void main()
{
TMOD = 0x01;
TH0 = (65536 – 50000)/256;
TL0 = (65536 – 50000)%6;
EA = 1; //开总中止
ET0 = 1; //开定时器0中止
TR0 = 1; //发动定时器

while(1)
{
Traffic_Light();

}

}

void T0_time() interrupt 1
{
TH0 = (65536 – 50000)/256;
TL0 = (65536 – 50000)%6;
ds_time++;
if(ds_time == 20)
{
ds_time = 0;
Traffic_time++;

}

}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/fangan/biancheng/259517.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部