#include<reg52.h>
#define SEG_DAT P2
#define SEG_BIT P0
unsigned char code DispCode[]={
0x3F,
0x06,
0x5B,
0x4F,
0x66,
0x6D,
0x7D,
0x07,
0x7F,
0x6F,
0x77,
0x7C,
0x39,
0x5E,
0x79,
0x71
};
unsigned char DisBuf[8]={1,2,3,4,5,8,7,8};
unsigned char bitn=0;
bit bf_10ms=0;
unsigned int ui_count_10ms=0;
void T0_IRQ(void)interrupt 1
{
TH0=(0-1000)/256;
TL0=(0-1000)%256;
{
SEG_BIT=0xFF;
SEG_DAT=DispCode[DisBuf[bitn]];
SEG_BIT=~(1<<bitn);
bitn=(bitn+1)%8;
}
ui_count_10ms++;
if(ui_count_10ms>=10)
{
ui_count_10ms=0;
bf_10ms=1;
}
}
void SystemInit(void)
{
TMOD=0x11;
TH0=(0-1000)/256;
TL0=(0-1000)%256;
TR0=1;
ET0=1;
EA=1;
}
void SetDisplay(unsigned long x)
{
DisBuf[0]=x/1%10;
DisBuf[1]=x/10%10;
DisBuf[2]=x/100%10;
DisBuf[3]=x/1000%10;
DisBuf[4]=x/10000%10;
DisBuf[5]=x/100000%10;
DisBuf[6]=x/1000000%10;
DisBuf[7]=x/10000000%10;
}
unsigned char KeyScan(void)
{
static char state=0;
unsigned char key=0xff,i;
switch(state)
{
case 0:P3=0xF0;
if((P3&0xF0)!=0xF0)
state =1;
break;
case 1:
P3=0xF0;
if((P3&0xF0)!=0xF0)
{
for (i=0;i<4;i++)
{
P3=(~(1<<i));
switch(P3&0xF0)
{
case 0xE0:key=i*4+0;break;
case 0xD0:key=i*4+1;break;
case 0xB0:key=i*4+2;break;
case 0x70:key=i*4+3;break;
}
}
state =2;
}
break;
case 2:
P3=0xF0;
if((P3&0xF0)!=0xF0)
state=2;
else state =0;
break;
default:break;
}
return key;
}
|