#include<reg51.h>
unsigned char keyval,keynum=0;
unsigned char distab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xff};
void t1isr() interrupt 3
{
unsigned char sccode,recode;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
P3=0x0f; //发0扫描,列线输入
if ((P3 & 0x0f) != 0x0f)keynum++; //有键按下
else keynum=0;
if(keynum>5) //按键保持在5次中断以上
{
keynum=0;
sccode = 0xef; //逐行扫描初值
while((sccode&0x01)!=0)
{
P3=sccode;
if((P3&0x0f)!=0x0f)
{
recode=(P3&0x0f)|0xf0;
getkey((~sccode)+(~recode));
}
else
sccode=(sccode<<1)|0x01;
}
}
getkey(0);
}
void getkey(unsigned char key)
{
if(key==0){keyval=0xff;return;}
switch(key)
{
case 0x11:keyval=7;break;
case 0x12:keyval=4;break;
case 0x14:keyval=1;break;
case 0x18:keyval=10;break;
case 0x21:keyval=8;break;
case 0x22:keyval=5;break;
case 0x24:keyval=2;break;
case 0x28:keyval=0;break;
case 0x41:keyval=9;break;
case 0x42:keyval=6;break;
case 0x44:keyval=3;break;
case 0x48:keyval=11;break;
case 0x81:keyval=12;break;
case 0x82:keyval=13;break;
case 0x84:keyval=14;break;
case 0x88:keyval=15;break;
default:keyval=0xff;break;
}
}
main()
{
TMOD=0x01;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
TR0=1;
ET0=1;
EA=1;
while(1)
{
if(keyval!=0xff)P0=distab[keyval];
}
}来自:求助得到的回答
温馨提示:内容为网友见解,仅供参考