按键判断的几种方法
1.利switch.....case 语句,由于switch语句可以判断离散值,所以可以用来判断按键
eg:switch(P1)
{
case 0xfe:delay();
if(P1=0xfe)
{}
break;
case 0xfd:delay();
if(P1=0xfd)
{}
break;
default: break;
}
通过对P1值的判断可以确定是哪个独立按键按下了。
2.switch(P1)
{
case 0xfe:delay(); //去抖
if(P1=0xfe) //按键按下
{
for(;;)
{
if(P1=0xfe) //确认按下
continue;
else break;
}
for(;;)
{
while(P1=0xfe); //如为干扰,继续循环
if(P1=0xfe)
continue;
else break; //按键释放
}
disp5(); //执行按键的功能
}
}