本人想用P1口做个3*4键盘,由于P1.2,P1.3 要用于串口2,
故P1.0,P1.1,P3.5作行线 P1.4,P1.5,P1.6,P1.7作列线
仿真过好像不行,不知是什么原因,请大侠们给我改动一下
还有ltkey这个定义,不知这样处理可行不?
bit amled = 0; //状态标志位
bit ltkey = 0; //防止长按键标志位
uchar pic = 0; //用于定时器计数
void KeyScan(void)
{
uchar NowTemp;
if(pic == 10) //每40ms首次扫描
{
P1 = 0xfc;
temp = P1;
temp = temp & 0xfc;
}
if(pic == 13) //12ms后再次扫描判断
{
P1 = 0xfc;
NowTemp = P1;
NowTemp = NowTemp & 0xfc;
if(temp != NowTemp) //如果不同,视为抖动,不作处理
{
P1 = 0xfc;
}
if((temp == NowTemp)&&(NowTemp != 0xfc)) //如果相同,且不为fc,判为有按键按下
{
//以下为逐行扫描
P1_0 = 0;
switch(temp)
{
case 0x7c:
if(ltkey == 0)break;
{
amled = ~amled;
KeyValue = 1;
ltkey = 1
}
case 0xdc: KeyValue = 2;break;
case 0xec: KeyValue = 3;break;
default:break;
}
P1_0 = 1; //置位 ?
P1_1 = 0;
switch(temp)
{
case 0x7c: KeyValue = 4;break;
case 0xbc: KeyValue = 5;break;
case 0xdc: KeyValue = 6;break;
case 0xec: KeyValue = 7;break;
default:break;
}
P1_1 = 1;
P3_5 = 0;
switch(temp)
{
case 0x7c: KeyValue = 8;break;
case 0xbc: KeyValue = 9;break;
default:break;
}
P3_5 = 1;
}
else
{
ltkey = 0;
}
}
}
void KeyControl(void)
{
switch(KeyValue)
{
case 1:
LEDAM1 = amled;
LEDAM2 = ~amled;
break;
case 2:
if(amled == 0)
{
LED1 = ~LED1; //灯闪烁
delay0_5s();
}
else
{
LED1 = 1; //灯长亮
}
break;
case 3:
if(amled == 0)
{
LED2 = ~LED2; //灯闪烁
delay0_5s();
}
else
{
LED2 = 1; //灯长亮
}
break;
.
.
.
.
default :break;
}
} |