#include <reg52.h>
#include <function.h>
u8 code LedChar[16]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};
u8 LedBuff[6]={0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
void delay_ms(u16 x)
{
u16 i,j;
if(x==1000)
{
for(i=0;i<19601;i++)
{
for(j=5;j>0;j--);
}
}
else while(x--)for(j=115;j>0;j--);
}
void LED_Init()
{
P1|=0x0E;
P1&=0xEE;
}
void KEY_Init()
{
P2=0X7F;
}
u8 KEY_Scan(u8 mode,u16 TIMES)
{
static u8 key_up=1;
static u16 times;
if(mode)key_up=1;
if(key_up&&(KEY4==0||KEY8==0||KEY12==0||KEY16==0))
{
times++;
if(times>=TIMES)
{
times=0;
key_up=0;
if(KEY4==0)return 4;
else if(KEY8==0)return 8;
else if(KEY12==0)return 12;
else if(KEY16==0)return 16;
}
}
else if(KEY4==1&&KEY8==1&&KEY12==1&&KEY16==1)key_up=1;
return 0;
}
void SEG_Scan()
{
static u8 i = 0;
P0 = 0xFF;
P1 = (P1 & 0xF8) | i;
P0 = LedBuff[i];
i++;
if(i>=6)i=0;
}
void ShowNumber(u32 num)
{
char i;
u8 buf[6];
for (i=0; i<6; i++)
{
buf[i] = num % 10;
num = num / 10;
}
for (i=5; i>=1; i--)
{
if (buf[i] == 0)
LedBuff[i] = 0xFF;
else
break;
}
for ( ; i>=0; i--)
{
LedBuff[i] = LedChar[buf[i]];
}
}