十字路口交通灯程序
时间:10-02
整理:3721RD
点击:
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
void display(uchar); //数码管显示函数
void delay(uint x);//延时函数
sbit duan=P3^6;
sbit wei=P3^7;
sbit hong1=P1^0;
sbit huang1=P1^1;
sbit lu1 =P1^2;
sbit hong2=P1^3;
sbit huang2=P1^4;
sbit lu2 =P1^5;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0xef};
uchar a,b,shi=35;
void delay(uint x)//延时子函数
{
uint i,j;
for(i=x;i>0;i--)
for(j=200;j>0;j--);
}
void display(uchar shi) //数码管显示子函数
{
wei=1;
P0=0xfe;
wei=0;
P0=0xff;
duan=1;
P0=table[shi%10];
duan=0;
delay(1);
wei=1;
P0=0xfd;
wei=0;
P0=0xff;
duan=1;
P0=table[shi/10];
duan=0;
P0=0xff;
delay(1);//
wei=1;
P0=0xfb;
wei=0;
P0=0xff;
duan=1;
P0=table[shi/10];
duan=0;
P0=0xff;
delay(1);
wei=1;
P0=0xf7;
wei=0;
P0=0xff;
duan=1;
P0=table[shi%10];
duan=0;
P0=0xff;
delay(1);//
wei=1;
P0=0xef;
wei=0;
P0=0xff;
duan=1;
P0=table[shi/10];
duan=0;
P0=0xff;
delay(1);
wei=1;
P0=0xdf;
wei=0;
P0=0xff;
duan=1;
P0=table[shi%10];
duan=0;
P0=0xff;
delay(1);//
wei=1;
P0=0xbf;
wei=0;
P0=0xff;
duan=1;
P0=table[shi/10];
duan=0;
P0=0xff;
delay(1);
wei=1;
P0=0x7f;
wei=0;
P0=0xff;
duan=1;
P0=table[shi%10];
duan=0;
P0=0xff;
delay(2);//
}
void mie()
{
hong1=1;huang1=1;lu1=1;
hong2=1;huang2=1;lu2=1;
}
void xian()
{
if(b)
{
mie();
if(shi>5)
{
lu1=0;
hong2=0;
}
if(shi<=5)
{
lu1=1;
huang1=0;
hong2=0;
}
}
if(!b)
{
mie();
if(shi>5)
{
lu2=0;
hong1=0;
}
if(shi<=5)
{
lu2=1;
huang2=0;
hong1=0;
}
}
}
void main()
{
TMOD=0x01;
TL0=(65536-45872)%256;
TH0=(65536-45872)/256;
EA=1;
ET0=1;
TR0=1;
while(1)
{
xian();
display(shi);
}
}
void T0_time() interrupt 1
{
TMOD=0X01;
TL0=(65536-45872)%256;
TH0=(65536-45872)/256;
a++;
if(a==20)
{
a=0;
shi--;
if(shi==0)
{
shi=30;
b=~b;
}
}
}
#define uchar unsigned char
#define uint unsigned int
void display(uchar); //数码管显示函数
void delay(uint x);//延时函数
sbit duan=P3^6;
sbit wei=P3^7;
sbit hong1=P1^0;
sbit huang1=P1^1;
sbit lu1 =P1^2;
sbit hong2=P1^3;
sbit huang2=P1^4;
sbit lu2 =P1^5;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0xef};
uchar a,b,shi=35;
void delay(uint x)//延时子函数
{
uint i,j;
for(i=x;i>0;i--)
for(j=200;j>0;j--);
}
void display(uchar shi) //数码管显示子函数
{
wei=1;
P0=0xfe;
wei=0;
P0=0xff;
duan=1;
P0=table[shi%10];
duan=0;
delay(1);
wei=1;
P0=0xfd;
wei=0;
P0=0xff;
duan=1;
P0=table[shi/10];
duan=0;
P0=0xff;
delay(1);//
wei=1;
P0=0xfb;
wei=0;
P0=0xff;
duan=1;
P0=table[shi/10];
duan=0;
P0=0xff;
delay(1);
wei=1;
P0=0xf7;
wei=0;
P0=0xff;
duan=1;
P0=table[shi%10];
duan=0;
P0=0xff;
delay(1);//
wei=1;
P0=0xef;
wei=0;
P0=0xff;
duan=1;
P0=table[shi/10];
duan=0;
P0=0xff;
delay(1);
wei=1;
P0=0xdf;
wei=0;
P0=0xff;
duan=1;
P0=table[shi%10];
duan=0;
P0=0xff;
delay(1);//
wei=1;
P0=0xbf;
wei=0;
P0=0xff;
duan=1;
P0=table[shi/10];
duan=0;
P0=0xff;
delay(1);
wei=1;
P0=0x7f;
wei=0;
P0=0xff;
duan=1;
P0=table[shi%10];
duan=0;
P0=0xff;
delay(2);//
}
void mie()
{
hong1=1;huang1=1;lu1=1;
hong2=1;huang2=1;lu2=1;
}
void xian()
{
if(b)
{
mie();
if(shi>5)
{
lu1=0;
hong2=0;
}
if(shi<=5)
{
lu1=1;
huang1=0;
hong2=0;
}
}
if(!b)
{
mie();
if(shi>5)
{
lu2=0;
hong1=0;
}
if(shi<=5)
{
lu2=1;
huang2=0;
hong1=0;
}
}
}
void main()
{
TMOD=0x01;
TL0=(65536-45872)%256;
TH0=(65536-45872)/256;
EA=1;
ET0=1;
TR0=1;
while(1)
{
xian();
display(shi);
}
}
void T0_time() interrupt 1
{
TMOD=0X01;
TL0=(65536-45872)%256;
TH0=(65536-45872)/256;
a++;
if(a==20)
{
a=0;
shi--;
if(shi==0)
{
shi=30;
b=~b;
}
}
}