代码
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
//#define BitPort P0
#define SegPort P1
uchar code segcode[]={0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07,
0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71};
uchar count1,count;
uchar dispbuf[2];
uint num,LED_N,i;
uchar data _1s_flag;
void init(void)
{
TMOD=0x01;
TH0=(65536-2000)/256;
TL0=(65536-2000)%256;
EA=1;
ET0=1;
TR0=1;
LED_N=2;
num=0;
}
void T0_timer0(void) interrupt 1
{
TH0=(65536-2000)/256;
TL0=(65536-2000)%256;
num++;
if(num==500)
{
num=0;
_1s_flag=1;
}
}
void display(uchar count)
{
dispbuf[0]=count%10;
}
void LED_drive(void)
{
SegPort=~segcode[dispbuf[0]];
}
void LED_Exchange(void)
{
LED_N--;
if(LED_N<0)
LED_N=2;
switch(LED_N)
{
case 2:
{
count=9;
for(i=0;i<9;i++)
{
if(_1s_flag==1)
{
_1s_flag=0;
display(count);
count--;
}
}
};break;
case 1:
{
count=7;
for(i=0;i<7;i++)
{
if(_1s_flag==1)
{
_1s_flag=0;
display(count);
count--;
}
}
};break;
default:
{
count=3;
for(i=0;i<3;i++)
{
if(_1s_flag==1)
{
_1s_flag=0;
display(count);
count--;
}
}
} ;break;
}
}
void main(void)
{
init();
while(1)
{
LED_Exchange();
LED_drive();
}
|