單片機鍵盤C程序
void int1proc() interrupt IE1_VECTOR using 1
{
unsigned char i, key;
code unsigned char PS2TAB[] = {//20鍵PS2小鍵盤鍵碼表
0x70,//0
0x69,//1
0x72,//2
0x7a,//3
0x6b,//4
0x73,//5
0x74,//6
0x6c,//7
0x75,//8
0x7d,//9
0x05,//F1
0x06,//F2
0x04,//F3
0x0c,//F4
0x03,//F5
0x0b,//F6
0x5a,//Enter
0x76,//Esc
0x66,//Bksp
0x71//KP.
};
if (PS2Buffers.PS2KeyCount == 0){//PS2起始位測試
if (!PS2CLOCK !PS2DATA){//低電平是起始位
if (PS2Buffers.PS2KeyExtFlage != 0xf0){
PS2Buffers.PS2KeyTemp = 0;
PS2Buffers.PS2KeyExtFlage = 0;
PS2Buffers.PS2KeyPopError = 0;
}
PS2Buffers.PS2KeyCount ++;//脈沖計數(shù)
}
else PS2Buffers.PS2KeyPopError = 0xeb;//置鍵起始位錯誤號0xeb
}
else if (PS2Buffers.PS2KeyCount 9){//PS2數(shù)據(jù)位
key = PS2Buffers.PS2KeyTemp;//取鍵盤緩沖區(qū)移位數(shù)據(jù)
key >>= 1;
if (PS2DATA) key = 0x80;
PS2Buffers.PS2KeyTemp = key;
PS2Buffers.PS2KeyCount++;//脈沖計數(shù)
}
else if (PS2Buffers.PS2KeyCount == 9){//PS2數(shù)據(jù)奇校驗
ACC = PS2Buffers.PS2KeyTemp;//取鍵盤緩沖區(qū)移位數(shù)據(jù)(C51取偶校驗位)
if (P != PS2DATA) PS2Buffers.PS2KeyCount ++;//脈沖計數(shù)
else{
PS2Buffers.PS2KeyPopError = 0xec;////置鍵奇校驗位錯誤號0xec
PS2Buffers.PS2KeyCount = 0;//脈沖計數(shù)歸零
}
}
else if (PS2Buffers.PS2KeyCount == 10){//停止位
key = PS2Buffers.PS2KeyTemp;//取鍵盤緩沖區(qū)移位數(shù)據(jù)
PS2Buffers.PS2KeyCount = 0;//脈沖計數(shù)歸零
if (PS2DATA){//高電平是停止位
if (key == 0xe0){//本次是擴展鍵
PS2Buffers.PS2KeyExtFlage = 0xe0;//置擴展鍵標(biāo)志(小鍵盤只有回車鍵)
}
else if (key == 0xf0){//本次是鍵斷碼,鍵釋放
PS2Buffers.PS2KeyExtFlage = 0xf0;//置鍵釋放標(biāo)志
}
else{//本次必為鍵值
if ((key == 0xaa) (key == 0xfa)) PS2Buffers.PS2KeyPushCount = 0;//長
壓鍵計數(shù)器清零
else{
PS2CLOCK = 0;//阻止PS2鍵盤立即回送數(shù)據(jù)
PS2Buffers.PS2KeyPopError = 0xaa;//出錯碼
for (i = 0; i 20; i++){
if (key == (PS2TAB[i])){//搜索
key = i + 1;
if (PS2Buffers.PS2KeyExtFlage == 0xf0){
key = 0x80;//鍵釋放
PS2Buffers.PS2KeyPushCount = 0;//長壓鍵計數(shù)器清零
PS2Buffers.PS2KeyMessage = key;//存入當(dāng)前鍵值并執(zhí)行命令
}
else{
if (key != PS2Buffers.PS2KeyVal){//換了一個鍵
PS2Buffers.PS2KeyPushCount = 0;//長壓鍵計數(shù)器清零
PS2Buffers.PS2KeyMessage = key;//存入當(dāng)前鍵值并執(zhí)行命令
}
else{//未換鍵
PS2Buffers.PS2KeyPushCount ++;//長壓鍵計數(shù)器計數(shù)
if (PS2Buffers.PS2KeyPushCount > DEFPS2PUSHCOUNT){//長壓時間到
PS2Buffers.PS2KeyMessage = key 0x40;//存入當(dāng)前鍵值并執(zhí)行命
令
PS2Buffers.PS2KeyPushCount = 0;//長壓鍵計數(shù)器清零
}
}
}
PS2Buffers.PS2KeyVal = key;//存入當(dāng)前鍵值值1~20或0x80+(1~20)
PS2Buffers.PS2KeyTemp = 0;//鍵碼移位記錄器
PS2Buffers.PS2KeyExtFlage = 0;//擴展鍵標(biāo)志
PS2Buffers.PS2KeyPopError = 0;//鍵釋放標(biāo)志或出錯碼
break;
}
}
if (PS2Buffers.PS2KeyPopError) PS2Buffers.PS2KeyPushCount = 0;//長壓鍵
計數(shù)器清零
PS2CLOCK = 1;//釋放PS2時鐘總線
}
}
}
else PS2Buffers.PS2KeyPopError = 0xed;//置停止位錯誤號0xed
}
else PS2Buffers.PS2KeyCount = 0;//PS2鍵盤出錯
}
評論