问题
利用单片机设计一个十字路口交通灯控制器。
用单片机控制LED灯模拟指示。模拟东西方向的十字路口交通灯控制情况。东西向通行时间为80S,
南北向通行时间为60S,缓冲时间为3S。
要求:东西和南北方向均用红绿黄模拟交通灯
东西和南北方向均用数码管显示剩余时间
使用中断定时。
代码:
#include<reg51.h>
#include<stdio.h>
#define uchar unsigned char
uchar seg[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar stateFlag;//当前工作模式的标志 为1时80s 为0 时 60s
uchar flag;//统计一秒钟的标志
uchar num1=0;//数码管1显示的数
uchar num2=0;//数码管2显示的数
sbit Led1_G = P1^0;
sbit Led1_Y = P1^1;
sbit Led1_R = P1^2;
sbit Led2_G = P1^3;
sbit Led2_Y = P1^4;
sbit Led2_R = P1^5;
void delay(int a);
void showNum();
void main(){
Led1_R = 0;
Led1_G = 0;
Led1_Y = 0;
Led2_R = 0;
Led2_G = 0;
Led2_Y = 0;
stateFlag = 0;
flag = 0;
TMOD=0x10;
TH1= 55536/256;
TL1=55536%256;
num1=60;
num2=63;
Led1_R = 0;
Led1_G = 1;
Led1_Y = 0;
Led2_R = 1;
Led2_G = 0;
Led2_Y = 0;
EA=1;
ET1=1;
TR1=1;
while(1){
showNum();
}
}
void int_T1(void) interrupt 3 {
flag++;
if(flag==100){
num1--;
num2--;
flag=0;
}
if(stateFlag==0){
if(num1==0){
Led1_R = 0;
Led1_G = 0;
Led1_Y = 1;
Led2_R = 1;
Led2_G = 0;
Led2_Y = 0;
num1=3;
}
if(num2==0){
num1=83;
num2=80;
Led1_R = 1;
Led1_G = 0;
Led1_Y = 0;
Led2_R = 0;
Led2_G = 1;
Led2_Y = 0;
stateFlag=1;
}
}
else{
if(num2==0){
Led1_R = 1;
Led1_G = 0;
Led1_Y = 0;
Led2_R = 0;
Led2_G = 0;
Led2_Y = 1;
num2=3;
}
if(num1==0){
num1=60;
num2=63;
Led1_R = 0;
Led1_G = 1;
Led1_Y = 0;
Led2_R = 1;
Led2_G = 0;
Led2_Y = 0;
stateFlag=0;
}
}
TH1= 55536/256;
TL1=55536%256;
}
void delay(int a){
uchar b;
for(;a>0;a--){
for(b=0;b<255;b++);
}
}
void showNum(){
P2=0x0a;
P0=seg[num1/10];
P3=seg[num2/10];
delay(10);
P2=0x05;
P0=seg[num1%10];
P3=seg[num2%10];
delay(10);
}
电路图地址:
1.https://download.csdn.net/download/qq_38313674/10495840
2.https://pan.baidu.com/s/1XR4NOPL25qx7hdqSDoWsJw
电路图: