51單片機(jī) 矩陣按鍵的掃描、消抖、動(dòng)作分離
掃描二維碼
隨時(shí)隨地手機(jī)看文章
#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[]={//數(shù)碼管顯示字符轉(zhuǎn)換表
0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E
};
unsignedcharKeySta[4][4]={//全部矩陣按鍵的當(dāng)前狀態(tài)
{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;//選擇數(shù)碼管DS1進(jìn)行顯示
ADDR3=1;
ADDR2=0;
ADDR1=0;
ADDR0=0;
TMOD=0x01;//設(shè)置T0為模式1
TH0=0xFC;//為T0賦初值0xFC67,定時(shí)1ms
TL0=0x67;
ET0=1;//使能T0中斷
TR0=1;//啟動(dòng)T0
P0=LedChar[0];//默認(rèn)顯示0
while(1){
for(i=0;i<4;i++){//循環(huán)檢測(cè)4*4的矩陣按鍵
for(j=0;j<4;j++){
if(KeySta[i][j]!=backup[i][j]){//檢測(cè)按鍵動(dòng)作
if(KeySta[i][j]==0){//按鍵按下時(shí)執(zhí)行動(dòng)作
P0=LedChar[i*4+j];//將編號(hào)顯示到數(shù)碼管
}
backup[i][j]=KeySta[i][j];//更新前一次的備份值
}
}
}
}
}
/*T0中斷服務(wù)函數(shù),掃描矩陣按鍵狀態(tài)并消抖*/
voidInterruptTimer0()interrupt1{
unsignedcharj;
staticunsignedchari=0;//矩陣按鍵掃描輸出索引
staticunsignedcharkeybuf[4][4]={//矩陣按鍵掃描緩沖區(qū)
{0xFF,0xFF,0xFF,0xFF},
{0xFF,0xFF,0xFF,0xFF},
{0xFF,0xFF,0xFF,0xFF},
{0xFF,0xFF,0xFF,0xFF}
};
TH0=0xFC;//重新加載初值
TL0=0x67;
//將一行的4個(gè)按鍵值移入緩沖區(qū)
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;
//消抖后更新按鍵狀態(tài)
for(j=0;j<4;j++){//每行4個(gè)按鍵,所以循環(huán)4次
if((keybuf[i][j]&0x0F)==0x00){//連續(xù)4次掃描值為0,即4*4ms內(nèi)都是按下狀態(tài)時(shí),可認(rèn)為按鍵已穩(wěn)定的按下
KeySta[i][j]=0;
}
elseif((keybuf[i][j]&0x0F)==0x0F){//連續(xù)4次掃描值為1,即4*4ms內(nèi)都是彈起狀態(tài)時(shí),可認(rèn)為按鍵已穩(wěn)定的彈起
KeySta[i][j]=1;
}
}
//執(zhí)行下一次的掃描輸出
switch(i){//根據(jù)索引,釋放當(dāng)前輸出引腳,拉低下次的輸出引腳
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;
}