資源描述:
《單片機(jī)矩陣鍵盤行列掃描程序.doc》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、//行列掃描程序,可以自己定義端口和掃描方式,這里做簡單介紹#include//包含頭文件#defineucharunsignedchar#defineuintunsignedintunsignedcharconstdofly[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//0-Fucharkeyscan(void);voiddelay(uinti);voidmain(){ucharke
2、y;P2=0x00;//1數(shù)碼管亮按相應(yīng)的按鍵,會(huì)顯示按鍵上的字符while(1){key=keyscan();//調(diào)用鍵盤掃描,switch(key){case0x7e:P0=dofly[0];break;//0按下相應(yīng)的鍵顯示相對應(yīng)的碼值case0x7d:P0=dofly[1];break;//1case0x7b:P0=dofly[2];break;//2case0x77:P0=dofly[3];break;//3case0xbe:P0=dofly[4];break;//4case0xbd:P0=dofly[5];brea
3、k;//5case0xbb:P0=dofly[6];break;//6case0xb7:P0=dofly[7];break;//7case0xde:P0=dofly[8];break;//8case0xdd:P0=dofly[9];break;//9case0xdb:P0=dofly[10];break;//acase0xd7:P0=dofly[11];break;//bcase0xee:P0=dofly[12];break;//ccase0xed:P0=dofly[13];break;//dcase0xeb:P0=dofly
4、[14];break;//ecase0xe7:P0=dofly[15];break;//f}}}ucharkeyscan(void)//鍵盤掃描函數(shù),使用行列反轉(zhuǎn)掃描法{ucharcord_h,cord_l;//行列值P3=0x0f;//行線輸出全為0cord_h=P3&0x0f;//讀入列線值if(cord_h!=0x0f)//先檢測有無按鍵按下{delay(100);//去抖if(cord_h!=0x0f){cord_h=P3&0x0f;//讀入列線值P3=cord_h
5、0xf0;//輸出當(dāng)前列線值cord_l=P3&0x
6、f0;//讀入行線值return(cord_h+cord_l);//鍵盤最后組合碼值}}return(0xff);//返回該值}voiddelay(uinti)//延時(shí)函數(shù){while(i--);}