//p1口外接3*4矩陣鍵盤(pán),其中p1^0不用置1,常用讀出鍵值方法有//“查表法”和“反轉(zhuǎn)法”。查表法程序最為簡(jiǎn)短,但是稍多占用//一點(diǎn)存儲(chǔ)空間;反轉(zhuǎn)法的程序執(zhí)行速度最快,只是要求接口是//雙向的。#includereg52.h>#includeintrins.h>#define uint unsigned int#define uchar unsigned charuchar code table[]={~0x3F,~0x06,~0x5B,~0x4F,~0x66,~0x6D,~0x7D,~0x07,~0x7F,~0x6F,~0x77,~0x7C,~0x39,~0x5E,~0x79,~0x71};//共陽(yáng)極LED代碼與共陰極正好相反,所以陰極碼取反uchar num;uchar keyscan();/*void delay(uint z){uint x,y;for(x=z;x>0;x--)for(y=110;y>0;y--); }*/void main(){ P0=table[0];while(1){ num=keyscan();if(num!=16)P0=table[num];} }uchar keyscan(){uchar code table1[3][4]={//0xee,0xde,0xbe,0x7e,0xed,0xdd,0xbd,0x7d,0xeb,0xdb,0xbb,0x7b,0xe7,0xd7,0xb7,0x77};uchar temp1=0xfd,temp2,i,j; //如果是3*4則只需將初值temp1改變p1^0不用置1并改變表值for(i=0;i4;i++)//掃描低四位//如果3*4鍵盤(pán)i3也可{ P1=temp1; //輸出一行0temp2=P1; //馬上讀入P1口狀態(tài)if((temp20xf0)!=0xf0)//如果有鍵按下{for(j=0;j4;j++)//就掃描高四位 j=0 j=1 j=2 j=3 {if(temp2==table1[i][j])//查表 i=0 0 1 2 3return i*4+j; //查到了就返回鍵的值 i=1 4 5 6 7} // i=2 8 9 10 11} // i=3 12 13 14 15else temp1=_crol_(temp1,1); //// i*4+j}return 16; //沒(méi)有查到,返回鍵松開(kāi)的代碼}
評(píng)論