强帮我做c语言版51芯片十字路口交通灯程序

2025-04-06 01:59:02
推荐回答(1个)
回答1:

#include
#include
#define uint unsigned int
#define uchar unsigned char
unsigned char code dis_7[10]={0x03,0x9F,0x25,0x0D,0x99,0x49,0x41,0x1F,0x01,0x09};
char i;
sfr p0=0x80;
sfr p1=0x90;
sbit red1=p1^0;
sbit green1=p1^1;
sbit yellow1=p1^2;
sbit red2=p1^3;
sbit green2=p1^4;
sbit yellow2=p1^5;

uchar temp,aa,num,num1;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void delay(uint z);
void main()
{
num=60;
num1=30;
aa=0;
SCON=0x00; //设置串行口的工作方式
PCON=0x00;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
temp=0xfe;
P1=temp;
P0=table[0];
P0=0xfe;
while(1);

}

void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
aa++;
if(aa==20)
{
aa=0;
num--;
num1--;

if(num==0){num=60;num1=30;}
if(num>35)
{red1=0;green1=1;yellow1=1;
red2=1;green2=0;yellow2=1;

if(num1<10)
{for(i=0;i<3;i++)
{SBUF=dis_7[0];
while(!TI);
TI=0;}
SBUF=dis_7[num1];
while(!TI);
TI=0;

}
else
{for(i=0;i<2;i++)
{SBUF=dis_7[0];
while(!TI);
TI=0;}
SBUF=dis_7[num1%10];
while(!TI);
TI=0;
SBUF=dis_7[num1/10];
while(!TI);
TI=0;
}
}
}

else if(num<=35&&num>30)
{red1=0;green1=1;yellow1=1;
red2=1;green2=1;yellow2=0;
for(i=0;i<3;i++)
{SBUF=dis_7[0];
while(!TI);
TI=0;}
SBUF=dis_7[num1];
while(!TI);
TI=0;}
else if(num<=30&&num>5)
{red1=1;green1=0;yellow1=1;
red2=0;green2=1;yellow2=1;
if(num<10)
{for(i=0;i<3;i++)
{SBUF=dis_7[0];
while(!TI);
TI=0;}
SBUF=dis_7[num];
while(!TI);
TI=0;

}
else
{for(i=0;i<2;i++)
{SBUF=dis_7[0];
while(!TI);
TI=0;}
SBUF=dis_7[num%10];
while(!TI);
TI=0;
SBUF=dis_7[num/10];
while(!TI);
TI=0;
}
}
else if(num<=5)
{red1=1;green1=1;yellow1=0;
red2=0;green2=1;yellow2=1;
for(i=0;i<3;i++)
{SBUF=dis_7[0];
while(!TI);
TI=0;}
SBUF=dis_7[num];
while(!TI);
TI=0;}

}