改为4*3,原理图去掉一行机可以了。软件可以不变,只是最后一行的值不予理会就可以了。这个程序就好
uchar kbscan(void)
{
unsigned char sccode,recode;
P1=0x0f; //发0扫描,列线输入
if ((P2 & 0x0f) != 0x0f) //有键按下
{
delay(20); //延时去抖动
if ((P1&0x0f)!= 0x0f)
{
sccode = 0xef; //逐行扫描初值
while((sccode&0x01)!=0)
{
P1=sccode;
if((P1&0x0f)!=0x0f)
{
recode=(P1&0x0f)|0xf0;
while((P1&0x0f)!=0x0f);//等待键抬起
return((~sccode)+(~recode));
}
else
sccode=(sccode<<1)|0x01;
}
}
}
return 0; //无键按下,返回0
}