文档介绍:基于51单片机的交通灯
基于51单片机的交通灯设计,并使用数码管显示倒计时。经过调试验证。
源代码:
#include""
#define uchar unsigned char
#define uint unsigned int
#define dua P0
#define wei P2
sbit ALE_dua=P1^0;
sbit ALE_wei=P1^1;
sbit Red=P1^2;
sbit Gre=P1^3;
sbit Yel=P1^4;
sbit Red1=P1^5;
sbit Gre1=P1^6;
sbit Yel1=P1^7;
uchar TH0_shu,TL0_shu;
uchar Tcount;
uchar time;
uchar code table_wei[ ]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
uchar code table_dua[ ]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void led_display(uint xian1,uint xian2);
void Traficlight();
void delay(int time)
{
int count1,count2;
for(count1=time;count1>0;count1--)
for(count2=20;count2>0;count2--)
;
}
void Init_timer()
{
TMOD=0x01;
TH0_shu=(65536-50000)/256;
TL0_shu=(65536-50000)%256;
TH0=TH0_shu;
TL0=TL0_shu;
EA=1;
ET0=1;
TR0=1;
}
void main()
{
Tcount=0;
Init_timer();
for(;;)
{
Traficlight();
if(Tcount>=20)
{
time++;
Tcount=0;
}
}
}
void led_display(uint xian1,uint xian2)
{
uchar w[8]={0,0,0,0,0,0,0,0};
uchar b1,b2;
w[0]=xian1/1000;
w[1]=xian1%1000/100;
w[2]=xian1%100/10;
w[3]=xian1%10;
w[4]=xian2/1000;
w[5]=xian2%1000/100;
w[6]=xian2%100/10;
w[7]=xian2%10;
for(b1=0;b1<8;b1++)
{
wei=0xff;
dua=0x00;
ALE_wei=1;
ALE_dua=1;
wei=table_wei[b1];
b2=w[b1];
dua=table_dua[b2];
d