单片机控制的人行道红绿灯
#include<reg51.h>
#define uchar unsigned char
uchar code arr[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};//共阴
uchar time1=0;
uchar time2=0;
uchar second1=12;
uchar second2;
void main()
{
P0=arr[second1/10]; //初始化红灯
P2=arr[second1%10];
P1=0x00; //绿灯熄灭
P3=0x00; //绿灯熄灭
TMOD=0x01; //初始设置T0定时器
TH0=0x3c; //50ms
TL0=0xb0;
EA=1; //开总中断
ET0=1; //T0允许中断
TR0=1; //初始T0开始计时
TH1=0x3c;
TL1=0xb0;
ET1=1; //T1允许中断
while(1);
}
void int0() interrupt 1
{
TR0=0;
TH0=0x3c;
TL0=0xb0;
time1++;
if(time1==20)
{
second1--;
time1=0;
P0=arr[second1/10];
P2=arr[second1%10];
}
if(second1==0)
{
P0=0x00; //红灯熄灭
P2=0x00;
second2=12;
P1=arr[second2/10]; //绿灯亮
P3=arr[second2%10];
TMOD=0x10;
TR1=1; //开启T1
}
else
TR0=1;
}
void int1() interrupt 3
{
TR1=0;
TH1=0x3c;
TL1=0xb0;
time2++;
if(time2==20)
{
second2--;
time2=0;
P1=arr[second2/10];
P3=arr[second2%10];
}
if(second2==0)
{
P1=0x00; //绿灯熄灭
P3=0x00;
second1=12;
P0=arr[second1/10];
P2=arr[second1%10];
TMOD=0x01;
TR0=1; //开启T0
}
else
TR1=1;
}
单片机人行道红绿灯