#include
sbitADDR0=P1^0;
sbitADDR1=P1^1;
sbitADDR2=P1^2;
sbitADDR3=P1^3;
sbitENLED=P1^4;
sbitKEY_IN_1=P2^4;
sbitKEY_IN_2=P2^5;
sbitKEY_IN_3=P2^6;
sbitKEY_IN_4=P2^7;
sbitKEY_OUT_1=P2^3;
sbitKEY_OUT_2=P2^2;
sbitKEY_OUT_3=P2^1;
sbitKEY_OUT_4=P2^0;
codeunsignedcharLedChar[]={//数码管显现字符转化表
0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E
};
unsignedcharKeySta[4][4]={//悉数矩阵按键的当时状况
{1,1,1,1},
{1,1,1,1},
{1,1,1,1},
{1,1,1,1}
};
voidmain(){
unsignedchari,j;
unsignedcharbackup[4][4]={//按键值备份,保存前一次的值
{1,1,1,1},
{1,1,1,1},
{1,1,1,1},
{1,1,1,1}
};
EA=1;//使能总中止
ENLED=0;//挑选数码管DS1进行显现
ADDR3=1;
ADDR2=0;
ADDR1=0;
ADDR0=0;
TMOD=0x01;//设置T0为形式1
TH0=0xFC;//为T0赋初值0xFC67,守时1ms
TL0=0x67;
ET0=1;//使能T0中止
TR0=1;//发动T0
P0=LedChar[0];//默许显现0
while(1){
for(i=0;i《4;i++){//循环检测4*4的矩阵按键
for(j=0;j《4;j++){
if(KeySta[i][j]!=backup[i][j]){//检测按键动作
if(KeySta[i][j]==0){//按键按下时履行动作
P0=LedChar[i*4+j];//将编号显现到数码管
}
backup[i][j]=KeySta[i][j];//更新前一次的备份值
}
}
}
}
}
/*T0中止服务函数,扫描矩阵按键状况并消抖*/
voidInterruptTImer0()interrupt1{
unsignedcharj;
staTIcunsignedchari=0;//矩阵按键扫描输出索引
staTIcunsignedcharkeybuf[4][4]={//矩阵按键扫描缓冲区
{0xFF,0xFF,0xFF,0xFF},
{0xFF,0xFF,0xFF,0xFF},
{0xFF,0xFF,0xFF,0xFF},
{0xFF,0xFF,0xFF,0xFF}
};
TH0=0xFC;//从头加载初值
TL0=0x67;
//将一行的4个按键值移入缓冲区
keybuf[i][0]=(keybuf[i][0]《《1)|KEY_IN_1;
keybuf[i][1]=(keybuf[i][1]《《1)|KEY_IN_2;
keybuf[i][2]=(keybuf[i][2]《《1)|KEY_IN_3;
keybuf[i][3]=(keybuf[i][3]《《1)|KEY_IN_4;
//消抖后更新按键状况
for(j=0;j《4;j++){//每行4个按键,所以循环4次
if((keybuf[i][j]&0x0F)==0x00){//接连4次扫描值为0,即4*4ms内都是按下状况时,可以为按键已安稳的按下
KeySta[i][j]=0;
}
elseif((keybuf[i][j]&0x0F)==0x0F){//接连4次扫描值为1,即4*4ms内都是弹起状况时,可以为按键已安稳的弹起
KeySta[i][j]=1;
}
}
//履行下一次的扫描输出
switch(i){//依据索引,开释当时输出引脚,拉低下次的输出引脚
case0:KEY_OUT_1=1;KEY_OUT_2=0;break;
case1:KEY_OUT_2=1;KEY_OUT_3=0;break;
case2:KEY_OUT_3=1;KEY_OUT_4=0;break;
case3:KEY_OUT_4=1;KEY_OUT_1=0;break;
default:break;
}
i=++i&0x03;
}