|
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit wela =P2^3;
sbit dula =P2^4;
sbit red = P1^4;
sbit yellow = P1^3;
sbit green=P1^2;
uchar shi,ge;
uchar code table[] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07,0x7f, 0x6f};
bit flag1s = 1;
void Timer0_init();
void light_crol();
void delay(uint x);
void main()
{
Timer0_init();
while (1)
{
if (flag1s)
{
flag1s = 0;
light_crol();
}
}
}
void Timer0_init()
{
TMOD = 0x01;
TH0 = (65536-50000)/256 ;
TL0 = (65536-50000)%256 ;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void light_crol()
{uchar num=1;
static unsigned char color = 2;
static unsigned char timer = 0;
if (timer == 0)
{
switch (color)
{
case 0:
red=1;
green=1;
if(num%2)
color = 1;
else color = 2;
timer = 2;
yellow=0;
if(num==11)
num=1;
break;
case 1:
yellow=1;
green=1;
color = 0;
timer = 29;
red=0;
num++;
break;
case 2:
red=1;
yellow=1;
color = 0;
timer = 39;
green=0;
num++;
break;
default:
break;
}
}
else
{
timer--;
}
ge =timer%10;
shi =timer/10;
}
void LedScan()
{
dula=1;
P0=table[shi];
dula=0;
P0 = 0xFF;
wela=1;
P0=0xfe;
wela=0;
delay(50);
P0=0xff;
dula=1;
P0=table[ge];
dula=0;
P0 = 0xFF;
wela=1;
P0=0xfd;
wela=0;
delay(50);
}
void InterruptTimer0() interrupt 1
{
static unsigned int tmr1s = 0;
TH0 =(65536-50000)/256 ;
TL0 = (65536-50000)/256 ;
LedScan();
tmr1s++;
if (tmr1s >= 20)
{
tmr1s = 0;
flag1s = 1;
}
}
void delay(uint x)
{uchar i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
|
|