完成以下任务:运用89C51单片机十字路口的交通灯控制系统,四组灯(红黄绿)对应十字路口的四个方向,用两位的数码管显示剩余的时间。
仿真视频:
https://www.bilibili.com/video/BV1qg411o71b/
#define uchar unsigned char
#define uint unsigned int
#include<reg51.h>
sbit dx_LED1=P2^2;
sbit dx_LED2=P2^3;
sbit dx_Red=P1^3;
sbit dx_Yellow=P1^2;
sbit nb_LED1=P2^0;
sbit nb_LED2=P2^1;
sbit nb_Red=P1^7;
sbit nb_Yellow=P1^6;
bit Flag_nb_Yellow;
bit Flag_dx_Yellow;
char Time_dx;
char Time_nb;
uchar dx=10,nb=10,dxL=5,nbL=5;
uchar dx1=10,nb1=10,dxL1=3,nbL1=3;
uchar code table[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar code a[9]={0X28,0X48,0X18,0X48,0X82,0X84,0X81,0X84,0x88};
void delay_ms(unsigned int x)
{
unsigned int i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
void Display(void)
{
char t1,t2;
t1=Time_dx/10;
t2=Time_dx%10;
P0=table[t2];
dx_LED2=1;
delay_ms(1);
dx_LED2=0;
P0=table[t1];
dx_LED1=1;
delay_ms(1);
dx_LED1=0;
t1=Time_nb/10;
t2=Time_nb%10;
P0=table[t2];
nb_LED2=1;
delay_ms(1);
nb_LED2=0;
P0=table[t1];
nb_LED1=1;
delay_ms(1);
nb_LED1=0;
}
void timer0(void)interrupt 1
{
static uchar count;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==10)
{
if(Flag_nb_Yellow==1)
{nb_Yellow=~nb_Yellow;}
if(Flag_dx_Yellow==1)
{dx_Yellow=~dx_Yellow;}
}
if(count==20)
{
Time_dx--;
Time_nb--;
if(Flag_nb_Yellow==1)
{nb_Yellow=~nb_Yellow;}
if(Flag_dx_Yellow==1)
{dx_Yellow=~dx_Yellow;}
count=0;
}
}
void main(void)
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
Time_dx=3;
Time_nb=3;
while(Time_nb>=0||Time_dx>=0)
{
Flag_dx_Yellow=0;
P1=a[8];
Display();
}
while(1)
{
Time_dx=dx;
Time_nb=nb;
while(Time_nb>=4)
{
Flag_dx_Yellow=0;
P1=a[0];
Display();
}
P1=0x00;
while(Time_nb>=0)
{
Flag_nb_Yellow=1;
dx_Red=1;
Display();
}
dx=dx1;
nb=nb1;
dxL=dxL1;
nbL=nbL1;
Time_dx=nb;
Time_nb=dx;
while(Time_dx>=4)
{
Flag_nb_Yellow=0;
P1=a[4];
Display();
}
P1=0X00;
while(Time_dx>=0)
{
Flag_dx_Yellow=1;
nb_Red=1;
Display();
}
dx=dx1;
nb=nb1;
dxL=dxL1;
nbL=nbL1;
}
}