该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
bit flag1s=1;
unsigned char T0RH,T0RL;
unsigned char ledchar[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
unsigned char ledbuff[7]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
unsigned char color = 0;//0-绿灯1-黄灯2-红灯
void configtimer0(unsigned char ms);
void trafficlight();
void main()
{
EA = 1;
ADDR3 = 1;
ENLED = 0;
configtimer0(1);
while(1)
{
if(flag1s)
{
flag1s = 0;
trafficlight();
}
}
}
void configtimer0(unsigned char ms)
{
unsigned long tmp;
tmp = 11059200/12;
tmp = tmp*ms/1000;
tmp = 65536 - tmp + 12;
T0RH = (unsigned char)(tmp>>8);
T0RL = (unsigned char)tmp;
TMOD &= 0xF0;
TMOD |= 0x01;
TH0 = T0RH;
TL0 = T0RL;
ET0 = 1;
TR0 = 1;
}
void trafficlight()
{
static unsigned char timer = 0;
if(timer==0)
{
switch(color)
{
case 0:color = 1;timer = 2;ledbuff[6] = 0xE7;break;
case 1:color = 2;timer = 29;ledbuff[6] = 0xFC;break;
case 2:color = 0;timer = 39;ledbuff[6] = 0x3F;break;
default :break;
}
}
else
{
timer--;
}
ledbuff[0] = ledchar[timer%10];
ledbuff[1] = ledchar[timer/10];
}
void ledscan()
{
static unsigned char i = 0;
P0 = 0xFF;
P1 = (P1 & 0xF8)|i;
P0 = ledbuff[i];
i++;
if(i>=7)
i=0;
}
void interrupttimer0() interrupt 1
{
static unsigned int tmr1s = 0;
static unsigned int cnt = 0;
static bit dir=1;
TH0 = T0RH;
TL0 = T0RL;
tmr1s++;
if(tmr1s>=1000)
{
tmr1s = 0;
flag1s = 1;
}
if(color!=1)
ledscan();
else
{
if(dir) //dir=1,刷新数码管
{
ledscan();
cnt++;
if(cnt>=600)
{
cnt = 0;
dir = 0;
}
}
else //dir=0,不刷新数码管
{
cnt++;
if(cnt>=600)
{
cnt = 0;
dir = 1;
}
}
}
}