|
#include<reg52.h>
sbit Key1 = P3^3;
unsigned char code LedChar[] = { //數(shù)碼管顯示字符轉(zhuǎn)換表
0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07,
0x7F, 0x6F,
};
unsigned char code LedBuff[] = {
0xFF,0xFF
};
unsigned char cnt = 0;
unsigned char sec = 0;
unsigned char cnt200 = 0;
unsigned int cnt1000 = 0;
void LinghtenLed();
void ShowLed();
void delay();
unsigned char i;
unsigned char buf[2]; //中間轉(zhuǎn)換緩沖區(qū)
void time()
{
TMOD = 0X01;
TH0 = 0xFC;
TL0 = 0x67;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void main()
{
unsigned char buf[2]; //中間轉(zhuǎn)換緩沖區(qū)
static unsigned int j;
while (1)
{
if(Key1 == 0)
{
EA = 0;
for(j=0;j<300;j++);
}
else
{
if (cnt200 >= 200) //0.2s刷新
{
cnt200 = 0;
LinghtenLed();
}
if (cnt1000 >= 1000) //1s刷新
{
cnt1000 = 0;
sec++;
P3 = ~0x03;
ShowLed();
}
for (i=1; i>=0; i--) //從最高為開(kāi)始,遇到0不顯示,遇到非0退出循環(huán)
{
if (buf[i] == 0)
LedBuff[i] = 0x00;
else
break;
}
for ( ; i>=0; i--)
{
LedBuff[i] = LedChar[buf[i]];
}
}
}
}
void LinghtenLed()
{
static unsigned char flag = 0;
if(flag==0)
{
P0=0x24;
ShowLed();
if(sec == 25)
{
sec = 0;
flag = 1;
}
}
else if(flag == 1)
{
P0=0x90;
ShowLed();
if(sec == 20)
{
flag = 2;
sec = 0;
}
}
else if(flag == 2)
{
P0 = 0x48;
ShowLed();
if(sec == 5)
{
flag = 3;
sec = 0;
}
}
else
{
P0 = 0xFF;
ShowLed();
delay();
P0 = ~P0;
flag = 0;
}
}
void ShowLed()
{
char i;
static unsigned long min = 0; //記錄經(jīng)過(guò)的秒數(shù)
min++;
buf[0]=min%10;
buf[1]=min/10%10;
}
void delay()
{
static unsigned int j;
for(j=0;j<2000;j++);
j=0;
}
/* 定時(shí)器0中斷服務(wù)函數(shù) */
void InterruptTimer0() interrupt 1
{
static unsigned char i = 0; //動(dòng)態(tài)掃描的索引
TH0 = 0xFC; //重新加載初值
TL0 = 0x67;
cnt200++;
cnt1000++;
P2 = 0x00;
switch (i)
{
case 0: i++; P2=LedBuff[0]; break;
case 1: i++; P2=LedBuff[1]; break;
default: break;
}
}
|
|