求89C51单片机C语言用四个74hc595控制四个LED实现交通灯程序

设计要求

1.设计十字路口的交通灯,每个路口用三个LED表示左转、直行、右转三个方向,每个方向用红、绿、黄三种颜色表示,自行设计控制逻辑

2.用键盘输入交通状态模拟道路情况

3.(扩展):为每个路口的红灯加两位数码管显示等待时间

我分不多哦 不好意思 各位大侠帮帮忙呀~~~

第1个回答  推荐于2017-09-14
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int

uint t1oc=20; //20*50000us=1s
uint count;

uchar tp[8];
uchar t1,t2,t3,t4;
uchar d1,d2,d3,d4;

//数码管显示代码
unsigned char code tab[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,
0x40,0x80};
//0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//0 1 2 3 4 5 6 7 8 9 A B C D E F - .

unsigned char code tab_p[]={
0xbf,0x86,0xdb,0xcf,
0xe6,0xed,0xfd,0x87,
0xff,0xef,0xf7,0xfc,
0xb9,0xde,0xf9,0xf1};//带小数点的16个数码

unsigned char code tab_w[]={
0x7F,0xBF,0xDF,0xEF,0xF7,0xfb,0xfd,0xfe};//位选择代码 从右到左

void delay1ms(unsigned int count) //延时1ms
{
unsigned char j;
for(;count>0;count--)
for(j=0;j<120;j++);
}

void led_ex(unsigned char dat,unsigned char N)
{
P0=dat;
P2=0x01<<N;
delay1ms(1);
P2=0x00;
}

void display(unsigned char num,unsigned char wei,bit p)
{
P3=tab_w[wei];
if(!p)
P1=tab_p[num];
else
P1=tab[num];
delay1ms(1);
P1=0x00;
}

//定时器0 50000us 12MHz
void initTimer(void)
{
TMOD=0x1;
TH0=0x3c;
TL0=0xb0;
}

//定时器0 定时中断
void timer0(void) interrupt 1
{
TH0=0x3c;
TL0=0xb0;
t1oc--;
if(t1oc==0)
{
t1oc=20; //20*50000us=1s
t1--;
if(t1==0)
{
t1=d1;
led_ex(0xFF,0);
}
tp[0]=t1%10;
tp[1]=t1/10;

t2--;
if(t2==0)
{
t2=d2;
led_ex(0x00,1);
}
tp[2]=t2%10;
tp[3]=t2/10;

t3--;
if(t3==0)
{
t3=d3;
led_ex(0x00,2);
}
tp[4]=t3%10;
tp[5]=t3/10;

t4--;
if(t4==0)
{
t4=d4;
led_ex(0x00,3);
}
tp[6]=t4%10;
tp[7]=t4/10;
}
}

void main()
{
unsigned char i;
initTimer();
TR0=1;
ET0=1;
EA=1;

d1= 60;
d2= 30;
d3= 50;
d4= 45;

t1=1;
t2=1;
t3=1;
t4=1;

while(1)
{
for(i=0;i<8;i++)
{
display(tp[i],i,i%2);
}
}
}本回答被提问者采纳
第2个回答  2009-04-19
这就毕业了?
相似回答