|
#include "REG52.h"http:// #include "intrins.h"http:// sbit Row0 = P1^0; sbit Row1 = P1^1; sbit Row2 = P1^2; sbit Row3 = P1^3; sbit Col0 = P1^4; sbit Col1 = P1^5; sbit Col2 = P1^6; sbit Col3 = P1^7;
unsigned char KeyScan(void); void main(void) { KeyScan(); while(1); } unsigned char KeyScan(void) { unsigned char key, temp; unsigned char code keytab[] = {//鍵碼表 //鍵值,//點,鍵名 鍵號 0x81,//07,key1 00 0x41,//06,key2 01 0x21,//05,key3 02 0x11,//04,key4 03 0x82,//17,key5 04 0x42,//16,key6 05 0x22,//15,key7 06 0x12,//14,key8 07 0x84,//27,key9 08 0x44,//26,key10 09 0x24,//25,key11 0A 0x14,//24,key12 0B 0x88,//37,key13 0C 0x48,//36,key14 0D 0x28,//35,key15 0E 0x18,//34,key16 0F 0x03,//01,key17 10 0x06,//12,key18 11 0x0c,//23,key19 12 0x05,//02,key20 13 0x09,//03,key21 14 0x0d,//13,key22 15 0xc0,//67,key23 16 0x60,//56,key24 17 0x30,//45,key25 18 0xa0,//57,key26 19 0x50,//46,key27 1A 0x90,//47,key28 1B /* 0x01,//G0,key29 1C 0x02,//G1,key30 1D 0x04,//G2,key31 1E 0x08,//G3,key32 1F 0x10,//G4,key33 20 0x20,//G5,key34 21 0x40,//G6,key35 22 0x80,//G7,key36 23 */ 0//退出 }; P1 = 0xff;//釋放鍵盤 _nop_();//延時 key = P1;//測試獨占鍵(與GND連接) key = ~key;//取反,變?yōu)檎壿? if (key) {//有獨占鍵壓下,鍵碼28~35,鍵key29..key36 temp = 35;//最后一個獨占鍵key36 do { if (key >= 0x80) break;//有獨占鍵壓下,退出測試 key <<= 1;//測試下一獨占鍵 temp --;//鍵號-1 } while(key);//未測完繼續(xù) if (key != 0x80) key = 0xff;//多個獨占鍵壓下,出錯鍵碼0xff else key = temp;//得到鍵碼28~35,鍵號key29~key36. } else {//測試組合鍵,鍵碼0~27 temp = 0x01;//實為從P1_1測起,到P1_7測完 do {//只需掃描7次!! temp <<= 1;//繼續(xù)掃描下一位 P1 = ~temp;//發(fā)送某位低電平 _nop_();//延時 key = P1;//接收鍵盤數(shù)據(jù) key = ~key;//取反,變?yōu)檎壿? } while((temp < 0x80) && (key == temp));//測到P1_6或有鍵壓下結(jié)束 if (key == temp) key = 0xff;//無鍵壓下,鍵碼0xff else {//有組合鍵壓下 temp = 0;//初始鍵號 while(keytab[temp] && (key != keytab[temp])) temp ++;//查鍵值表 if (temp >= 28) temp = 0xff;//查無此組合鍵,出錯鍵碼0xff else key = temp;//得到組合鍵碼0~27,鍵key1~key28. } } return key;//返回鍵碼0~35或出錯碼0xff }
|