下面是在P1口接4X3矩阵键盘的驱动程序,供你参考。上拉电阻也可以不用.
code U8 kcode[]={0xb7,0xbe,0xde,0xee,0xbd,0xdd,0xed,0xbb,0xdb,0xeb,0xd7,0xe7};
//反转码对应键值 0 1 2 3 4 5 6 7 8 9 A B
U8 key(void)
{ U8 a,r = 0xff;
delay_ms(20); // 延时20毫秒,消除按键抖动
if(P1 == 0xf0) goto rn;
a = P1;
P1 = a | 0x0f;
delay_ms(20);
a = P1;
for (r = 0;r <= 11;r++)
{ if(a == kcode[r]) break;
};
rn: P1 = 0xf0;
return r;
}