📄 交通灯设计.txt
字号:
/*****************************************************
十字路口交通灯控制 C 程序
******************************************************/
#include <at89x52.h>
#define uchar unsigned char
#define uint unsigned int
static uchar count;
uchar a;
sbit time_show_LED2=P1^6;//Time_Show_LED2控制位
sbit time_show_LED1=P1^7;//Time_Show_LED1控制位
sbit bjeee=P2^4;
sbit ns_green=P1^0; //NS绿灯
sbit ns_yellow=P1^2; //NS黄灯
sbit nsl_green=P1^1; //NS左拐绿灯
sbit we_green=P1^3; //WE绿灯
sbit we_yellow=P1^5; //WE黄灯
sbit wel_green=P1^4; //WE左拐绿灯
sbit we_LED2=P2^3; //WE_LED2控制位
sbit we_LED1=P2^2; //WE_LED1控制位
sbit ns_LED2=P2^1; //NS_LED2控制位
sbit ns_LED1=P2^0; //NS_LED1控制位
sbit add_button=P3^5; //时间加
sbit reduces_button=P3^6;//时间减
sbit special_btton=P3^7; //交通特殊按键
sbit wel_jc=P3^4; //东路口违规检测
sbit nsl_jc=P3^3; //南路口违规检测
sbit we_jc=P2^6; //西路口违规检测
sbit ns_jc=P2^7; //北路口违规检测
bit flag_ns_yellow; //SN黄灯标志位
bit flag_we_yellow; //EW黄灯标志位
uchar time_we; //东西方向倒计时单元
uchar time_ns; //南北方向倒计时单元
uchar special; //特殊按键控制位
bit wel_jcw; //东西左拐检测控制位
bit we_jcw; //东西检测控制位
bit ns_jcw; //南北检测控制位
bit nsl_jcw; //南北左拐检测控制位
uchar we=60,ns=40,wel=20,nsl=20; //程序初始化赋值,正常模式
uchar we1=60,ns1=40,wel1=20,nsl1=20;//用于存放修改值的变量
uchar code table[]={0xC0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
/**********************初始化程序************************/
void init()
{
IT0=1; //INT0负跳变触发
TMOD=0x21; //定时器工作于方式1
TH0=(65536-50000)/256;//定时器赋初值
TL0=(65536-50000)%256;
TH1=0xfd;
TL1=0xfd;
SCON=0x70;
PCON=0x00;
EA=1; //CPU开中断总允许
ES=1; //开串行口中断
ET0=1;
// ET1=1; //开定时中断
EX0=1; //开外部INTO中断
TR0=1;
TR1=1; //启动定时
ns_green=1;
nsl_green=0;
ns_yellow=0;
we_yellow=0;
we_green=0;
wel_green=0;
}
/**********************延时子程序************************/
void delay(uchar a)
{
uchar i;
i=a;
while(i--){;}
}
/*****************显示子函数**************************/
void display(void)
{
char h,l;
h=time_we/10;
l=time_we%10;
P0=table[l];
we_LED2=1;
delay(2);
we_LED2=0;
P0=table[h];
we_LED1=1;
delay(2);
we_LED1=0;
h=time_ns/10;
l=time_ns%10;
P0=table[l];
ns_LED2=1;
delay(2);
ns_LED2=0;
P0=table[h];
ns_LED1=1;
delay(2);
ns_LED1=0;
h= we1/10;
l= we1%10;
P0=table[l];
time_show_LED1=1;
delay(2);
time_show_LED1=0;
P0=table[h];
time_show_LED2=1;
delay(2);
time_show_LED2=0;
if(we_jcw==1|wel_jcw==1|ns_jcw==1|nsl_jcw==1) //报警检测
{
bjeee=0;
}
else
{
bjeee=1;
}
}
/**********************串行口中断服务程序************************/
void como() interrupt 4
{
EA=0; //关总中断
if(RI)
{ a=SBUF;
if(a=='a') //时间加
{ we1+=5;
ns1+=5;
if(we1>=100)
{
we1=99;
ns1=79;
}
}
if(a=='r') //时间减
{ we1-=5;
ns1-=5;
if(we1<=40)
{
we1=40;
ns1=20;
}
}
if(a=='s') //测试按键是否按下,按下为特殊状态
{
special++; //特殊按键控制位加1
if(special%2==1)
{ ET0=0; //定时器停止
ns_green=0; //全部亮红灯
nsl_green=0;
we_green=0;
wel_green=0;
ns_yellow=0;
we_yellow=0;
}
if(special%2==0)ET0=1; //开定时器
}
SBUF=1; //返回1
RI=0; //清标志位
}
else if(TI)
{ TI=0; //清标志位
}
EA=1; //开总中断
}
/**********************外部0中断服务程序************************/
void exinto(void) interrupt 0 using 1
{
EX0=0; //关中断
if(add_button==0) //时间加
{
we1+=5;
ns1+=5;
if(we1>=100)
{
we1=99;
ns1=79;
}
}
if(reduces_button==0) //时间减
{
we1-=5;
ns1-=5;
if(we1<=40)
{
we1=40;
ns1=20;
}
}
if(special_btton==0) //测试按键是否按下,按下为特殊状态
{
special++; //特殊按键控制位加1
if(special%2==1)
{ ET0=0; //定时器停止
ns_green=0; //全部亮红灯
nsl_green=0;
we_green=0;
wel_green=0;
ns_yellow=0;
we_yellow=0;
}
if(special%2==0)ET0=1; //开定时器
}
EX0=1; //开中断
}
/**********************T0中断服务程序*******************/
void timer0(void)interrupt 1 using 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==10)
{
if(flag_ns_yellow==1) //测试南北黄灯标志位
{ns_yellow=~ns_yellow;}
if(flag_we_yellow==1) //测试东西黄灯标志位
{we_yellow=~we_yellow;}
}
if(count==20)
{
time_we--;
time_ns--;
// SBUF=time_we;
while (TI== 0);
// SBUF=time_ns;
while (TI== 0);
if(flag_ns_yellow==1) //测试南北黄灯标志位
{ns_yellow=~ns_yellow;}
if(flag_we_yellow==1) //测试东西黄灯标志位
{we_yellow=~we_yellow;}
count=0;
}
}
/*********************主程序开始**********************/
void main(void)
{
init(); //初始化
while(1)
{
/*******S0状态**********/
flag_we_yellow=0; //EW关黄灯显示信号
time_we=we;
time_ns=ns;
ns_green=1; //SN通行,EW红灯
while(time_ns>5)
{
if(we_jc==0)we_jcw=1; //违规检测
if(wel_jc==0)wel_jcw=1;
if(nsl_jc==0)nsl_jcw=1;
display(); //显示
}
/*******S1状态**********/
while(time_ns>0)
{flag_ns_yellow=1; //SN开黄灯信号位 SN黄灯亮,等待左拐信号,EW红灯
if(we_jc==0)we_jcw=1; //违规检测
if(wel_jc==0)wel_jcw=1;
if(nsl_jc==0)nsl_jcw=1;
display();
}
if(we_jcw==1)SBUF='w'; while (TI== 0); //检测通信
if(wel_jcw==1)SBUF='x'; while (TI== 0);
if(ns_jcw==1)SBUF='y'; while (TI== 0);
if(nsl_jcw==1)SBUF='z'; while (TI== 0);
/*******S2状态**********/
flag_ns_yellow=0; //SN关黄灯显示信号
time_ns=nsl;
nsl_jcw=0; //清标志位
wel_jcw=0;
we_jcw=0;
ns_green=0;
nsl_green=1;
while(time_ns>5)
{
if(we_jc==0)we_jcw=1; //SN左拐绿灯亮,EW红灯
if(wel_jc==0)wel_jcw=1; //违规检测
if(ns_jc==0)ns_jcw=1;
display();}
/*******S3状态**********/
while(time_ns>0)
{flag_ns_yellow=1; //SN开黄灯信号位
if(we_jc==0)we_jcw=1;
if(wel_jc==0)wel_jcw=1;
if(ns_jc==0)ns_jcw=1;
display();}
if(we_jcw==1)SBUF='w'; while (TI== 0); //检测通信
if(wel_jcw==1)SBUF='x'; while (TI== 0);
if(ns_jcw==1)SBUF='y'; while (TI== 0);
if(nsl_jcw==1)SBUF='z'; while (TI== 0);
/***********赋值**********/
we=we1;
ns=ns1;
wel=wel1;
nsl=nsl1;
/*******S4状态**********/
flag_ns_yellow=0; //SN关黄灯显示信号
time_we=ns;
time_ns=we;
ns_jcw=0; //清标志位
wel_jcw=0;
we_jcw=0;
nsl_green=0;
we_green=1;
while(time_we>5)
{ //EW通行,SN红灯
if(ns_jc==0)ns_jcw=1; //违规检测
if(wel_jc==0)wel_jcw=1;
if(nsl_jc==0)nsl_jcw=1;
display();}
/*******S5状态**********/
while(time_we>0)
{flag_we_yellow=1; //EW开黄灯信号位
if(ns_jc==0)ns_jcw=1;
if(wel_jc==0)wel_jcw=1; //违规检测
if(nsl_jc==0)nsl_jcw=1;
display();}
if(we_jcw==1)SBUF='w'; while (TI== 0); //检测通信
if(wel_jcw==1)SBUF='x'; while (TI== 0);
if(ns_jcw==1)SBUF='y'; while (TI== 0);
if(nsl_jcw==1)SBUF='z'; while (TI== 0);
/*******S6状态**********/
flag_we_yellow=0; //EW关黄灯显示信号
time_we=wel;
nsl_jcw=0; //清标志位
wel_jcw=0;
ns_jcw=0;
we_green=0;
wel_green=1;
while(time_we>5)
{ //EW左拐绿灯亮,SN红灯
if(we_jc==0)we_jcw=1;
if(ns_jc==0)ns_jcw=1; //违规检测
if(nsl_jc==0)nsl_jcw=1;
display();}
/*******S7状态**********/
while(time_we>0)
{flag_we_yellow=1; //EN开黄灯信号位
if(we_jc==0)we_jcw=1;
if(ns_jc==0)ns_jcw=1; //违规检测
if(nsl_jc==0)nsl_jcw=1;
display();}
if(we_jcw==1)SBUF='w'; while (TI== 0); //检测通信
if(wel_jcw==1)SBUF='x'; while (TI== 0);
if(ns_jcw==1)SBUF='y'; while (TI== 0);
if(nsl_jcw==1)SBUF='z'; while (TI== 0);
/***********赋值**********/
nsl_jcw=0; //清标志位
we_jcw=0;
ns_jcw=0;
wel_green=0;
ns_green=1;
we=we1;
ns=ns1;
wel=wel1;
nsl=nsl1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -