基于51单片机控制交通灯的电路图与C语言程序
#include <reg51.h>
#include <stdio.h>
#define uchar unsigned char
#define uint unsigned int
uchar h;
uint r=0;
uint s;
uchar code dis[]={ 0Xc0,0Xf9, 0Xa4,0Xb0,0X99,0X92,0X82,0Xf8,0X80,0X90,0Xff,};
/*0*//*1*//*2*//*3*//*4*//*5*//*6*//*7*//*8*//*9*//*NULL*/
void delay(uint c)
{
uint i,j;
for(i=0;i<c;i++)
for(j=0;j<100;j++);
}
void t0(void) interrupt 0 using 1 //interrupt 1
{
delay(2);
if(INT0==0)
{
r++;
if(r==2)
{
r=0;
}
r%=2;
if(r==0)
{
P0=0xeb;
P1=0x00;
P2=0xff;
}while(1)
{
if(INT0==0|INT1==0|T0==0)
{
delay(2);
if(INT0==0|INT1==0|T0==0)
break;
}
};
}
}
void t1(void) interrupt 2 using 1 //interrupt 2
{
delay(2);
if(INT1==0)
{
r++;
if(r==2)
{
r=0;
}
r%=2;
if(r==0)
{
P0=0xdd;
P1=0x00;
P2=0xff;
}while(1)
{
if(INT1==0|INT0==0|T0==0)
{
delay(2);
if(INT1==0|INT0==0|T0==0)
break;
}
};
}
}
void t2(void) interrupt 1 using 1 //interrupt 3
{
TH0=0xff;
TL0=0xff;
delay(2);
if(T0==0)
{
P0=0xdb;
P1=0x00;
P2=0xff;
}while(1)
{
if(INT0==0|INT1==0)
{
delay(2);
if(INT0==0|INT1==0)
break;
}
};
}
void main(void)
{
TMOD=0x06;
TH0=0xff;
TL0=0xff;
TR0=1;
EA=1;
EX0=1;
EX1=1;
ET0=1;
while(1)
{
P0=0xeb;
for(h=28;h>0;h--)
{
for(s=248;s>0;s--)
{
P1=0x00;
P2=0xff;
P1=0x01;
P2=dis[h/10];
delay(1);
P1=0x00;
P2=0xff;
P1=0x02;
P2=dis[h%10];
delay(1);
P1=0x00;
P2=0xff;
P1=0x04;
P2=dis[(h+2)/10];
delay(1);
P1=0x00;
P2=0xff;
P1=0x08;
P2=dis[(h+2)%10];
delay(1);
}
}
delay(248);
P0=0xf3;
for(h=3;h>0;h--)
{
delay(392);
P0=0xf3;
delay(1);
P1=0x00;
P2=0xff;
P1=0xaa;
P2=dis[(h-1)%10];
delay(600);
P0=0xfb;
}
delay(248);
P0=0xdd;
for(h=38;h>0;h--)
{
for(s=248;s>0;s--)
{
P1=0x00;
P2=0xff;
P1=0x01;
P2=dis[(h+2)/10];
delay(1);
P1=0x00;
P2=0xff;
P1=0x02;
P2=dis[(h+2)%10];
delay(1);
P1=0x00;
P2=0xff;
P1=0x04;
P2=dis[h/10];
delay(1);
P1=0x00;
P2=0xff;
P1=0x08;
P2=dis[h%10];
delay(1);
}
}
delay(248);
P0=0xde;
for(h=3;h>0;h--)
{
delay(392);
P0=0xde;
delay(1);
P1=0x00;
P2=0xff;
P1=0xaa;
P2=dis[(h-1)%10];
delay(600);
P0=0xdf;
}
delay(248);
}
}