//p1口外接3*4矩阵键盘,其中p1^0不用置1,常用读出键值方法有//“查表法”和“反转法”。查表法程序最为简短,但是稍多占用//一点存储空间;反转法的程序执行速度最快,只是要求接口是//双向的。#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};//共阳极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键盘i3也可{ P1=temp1; //输出一行0temp2=P1; //马上读入P1口状态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; //没有查到,返回键松开的代码}
评论