前三行,检测到了,但是,没有及时返回。
修改如下:
uchar query_key()
{
uchar temp;
P3 = 0xef; temp=P3; temp &=0x0f;
switch (temp) {
case 0x0e: return 0;
case 0x0d: return 4;
case 0x0b: return 8;
case 0x07: return 12;
}
P3 = 0xdf; temp=P3; temp &=0x0f;
switch (temp) {
case 0x0e: return 1;
case 0x0d: return 5;
case 0x0b: return 9;
case 0x07: return 13;
}
P3 = 0xbf; temp=P3; temp &=0x0f;
switch (temp) {
case 0x0e: return 2;
case 0x0d: return 6;
case 0x0b: return 10;
case 0x07: return 14;
}
P3 = 0x7f; temp=P3; temp &=0x0f;
switch (temp) {
case 0x0e: return 3;
case 0x0d: return 7;
case 0x0b: return 11;
case 0x07: return 15;
default: return 255;
}
}
你最后的一个switch……case语句中default:后面已经把keycode改成255了,所以在前面几个switch语句中产生的keycode值,在执行到最后一个switch语句时,由于最后一个switch语句没有符合的case值,便执行default:后面的语句,也就是keycode=255;当你按最后一列的按键时,最后一个switch语句中有符合的case值,所以会执行case后面相应的语句,遇到break;后直接退出switch语句。
你把default: 和keycode=255;这两条语句去掉就可以了。
你这样当然是只有最后一行有效了,从第一行扫描开始,当扫描到有效键值时就退出返回就OK了。