#include <reg52.h>
sbit ADDR2 = P1^2;
sbit ADDR1 = P1^1;
sbit ADDR0 = P1^0;
sbit ENLED = P1^4;
sbit ADDR3 = P1^3;
sbit LED2 = P0^0;
sbit KEY4 = P2^7;
sbit KEY3 = P2^6;
unsigned char code LedChar[16]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};
unsigned char LedBuff[6]={0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
unsigned char cnt;
void SEG_task()
{
static unsigned char i=0;
LedBuff[0] = LedChar[cnt%10];
if(cnt>=10) LedBuff[1]= LedChar[(cnt/10)%10];
if(cnt>=100) LedBuff[2]= LedChar[(cnt/100)%10];
if(cnt==0){ LedBuff[1]=0xFF;LedBuff[2]=0XFF; }
P0=0xFF;
switch(i)
{
case 0:
ADDR2 = 0;ADDR1 = 0;ADDR0 = 0;P0=LedBuff[0];i++;break;
case 1:
ADDR2 = 0;ADDR1 = 0;ADDR0 = 1;P0=LedBuff[1];i++;break;
case 2:
ADDR2 = 0;ADDR1 = 1;ADDR0 = 0;P0=LedBuff[2];i++;break;
case 3:
ADDR2 = 1;ADDR1 = 0;ADDR0 = 1;P0=LedBuff[5];i=0;break;
}
}
void KEY_task()
{
cnt++;
}
void KEY_mode(unsigned char mode)
{
static unsigned char key_up=1;
static unsigned int times=0;
if(key_up==0)
{
times++;
if(mode==1 && times>=1000)
{
times=0;
KEY_task();
}
else if(mode==0 && times>=500)
{
if(KEY4==1)
{
times=0;
KEY_task();
}
}
}
key_up=KEY4;
}
void main()
{
unsigned char mode=0;
unsigned char key_up=1;
unsigned int times=0;
ADDR3 = 1;
ENLED = 0;
P2 = 0xF7;
LedBuff[5]=LedChar[mode];
while(1)
{
SEG_task();
KEY_mode(mode);
if(key_up==0)
{
times++;
if(times>=500&&KEY3==1)
{
times=0;
mode=!mode;
LedBuff[5]=LedChar[mode];
}
}
key_up=KEY3;
}
}