#include<reg52.h>
sbit ADDR3=P1^3;
sbit ENLED=P1^4;
sbit KEYIN4=P2^7;
sbit KEYIN3=P2^6;
sbit KEYIN2=P2^5;
sbit KEYIN1=P2^4;
sbit KEYOUT1=P2^3;
sbit KEYOUT2=P2^2;
sbit KEYOUT3=P2^1;
sbit KEYOUT4=P2^0;
unsigned char code keymodel[4][4]={
{0x31,0x32,0x33,0x20},{0x34,0x35,0x36,0x22},{0x37,0x38,0x39,0x21},{0x30,0x24,0x25,0x23}
};//0x20表示上,0x22表示左,0x21表示下,0x24表示esc,0x25表示回车,0x23表示右
unsigned char code LedChar[] = { //数码管显示字符转换表
0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};
unsigned char pdata keysta[4][4]={
{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}
};//按键当前状态。
unsigned char ledbuf[7]={
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
};
unsigned char rh=0;
unsigned char rl=0;
unsigned char result=0;//最大只能设置100.
unsigned char keytime=0;
bit flagreturn=0;
unsigned int cnt=0;
void configtime(unsigned char ms);
void keydriver();
void retruntime();
void keyaction(unsigned char number);
void shownumber();
void main()
{ EA=1;
ADDR3=1;
ENLED=0;
TMOD&=0xF0;
TMOD|=0x01;
configtime(1);
while(1)
{ keydriver();
if((cnt>=2)&&(flagreturn==1))
{ retruntime();
cnt=0;
}
}
}
void configtime(unsigned char ms)
{ unsigned long tmp;
tmp=11059200/12*ms/1000;
tmp=65536-tmp+18;
rh=(unsigned char)(tmp>>8);
rl=(unsigned char)tmp;
TH0=rh;
TL0=rl;
TR0=1;
ET0=1;
}
void keyaction(unsigned char number)
{ if(number==0x20)
{
if(result<99)
result++;
}
else if(number==0x21)
{ if(result>0)
result--;
}
else if(number==0x24)
{ result=0;
ledbuf[6]=0xFF;
}
else if(number==0x25)
{ flagreturn=1;
}
}
void retruntime()
{
if(result==0)
{ ledbuf[6]=0x00;
flagreturn=0;
}
else
result--;
}
void shownumber()
{ static unsigned char i=0;
ledbuf[0]=LedChar[result%10];
ledbuf[1]=LedChar[result/10];
P0=0xFF;
P1=(P1&0xF8)|i;
P0=ledbuf[i];
i++;
if(i>6)
i=0;
}
void keydriver()
{ unsigned char i=0;
unsigned char j=0;
static unsigned char keyback[4][4]={
{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}
};
static unsigned int keytimemax[4][4]={
{1000,1000,1000,1000},{1000,1000,1000,1000},{1000,1000,1000,1000},{1000,1000,1000,1000}
};
for(i=0;i<4;i++)
{ for(j=0;j<4;j++)
{ if(keyback[i][j]!=keysta[i][j])
{ if(keysta[i][j]==0)
{keyaction(keymodel[i][j]);}
keyback[i][j]=keysta[i][j];
}
else
{ if((keysta[i][j]==0)&&(keytime>=keytimemax[i][j]))
{ keyaction(keymodel[i][j]);
keytimemax[i][j]+=200;
}
else
{ keytimemax[i][j]=1000;
keytime=0;
}
}
}
}
}
void keyscan()
{ static unsigned char keybuf[4][4]={
{0xFF,0xFF,0xFF,0xFF},{0xFF,0xFF,0xFF,0xFF},{0xFF,0xFF,0xFF,0xFF},{0xFF,0xFF,0xFF,0xFF}
};
static unsigned char keyout=0;
unsigned char i=0;
keybuf[keyout][0]=(keybuf[keyout][0]<<1)|KEYIN1;
keybuf[keyout][1]=(keybuf[keyout][1]<<1)|KEYIN2;
keybuf[keyout][2]=(keybuf[keyout][2]<<1)|KEYIN3;
keybuf[keyout][3]=(keybuf[keyout][3]<<1)|KEYIN4;
for(i=0;i<4;i++)
{ if(keybuf[keyout][i]&0x0F==0x00)
{ keysta[keyout][i]=0;
keytime+=4;
}
else if(keybuf[keyout][i]&0x0F==0x0F)
keysta[keyout][i]=1;
}
keyout++;
keyout=keyout&0x03;
switch(keyout)
{ case 0: KEYOUT4=1; KEYOUT1=0;break;
case 1: KEYOUT1=1; KEYOUT2=0;break;
case 2: KEYOUT2=1; KEYOUT3=0;break;
case 3: KEYOUT3=1; KEYOUT4=0;break;
default :break;
}
}
void interrupttmie0()interrupt 1
{
TH0=rh;
TL0=rl;
cnt++;
keyscan();
shownumber();
}
不知道哪里有问题?大神能帮我调试下???
|