/*P1口外接3*4矩阵键盘,P1^0不必,回转法读键值
本程序尽管稍多几行,可是没有循环, 还能够提早回来,所以履行的速度最快*/#include#include #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 temh,teml,key;P1=0xf0;//低四位先输出0temh=P1;//读入temh,高四位含有按键信息P1=0x0f;//回转输出0,即高四位输出0teml=P1;//读入teml,低四位含有按键信息//------------------//两次读入的时刻距离,有必要尽量小,以尽量防止按键状况发生变化//要重视这些,有些程序乃至还有距离几个毫秒的!//按键会颤动,时刻长,有或许读出其他//------------------switch(temh){case 0xe0: key=0;break;case 0xd0: key=1;break;case 0xb0: key=2;break; case 0x70: key=3;break;default :return 16;//按下不是上述键,就当没有键} switch(teml){//case 0x0e: return key; 假如P1^0不接,此句不履行,key值稍加改动case 0x0d: return key;case 0x0b: return key+4; case 0x07: return key+8;default :return 16;//按下不是上述键,就当没有键}}