|
下面程序中间在列检测和行检测前面都会有一个赋值GPIO_KEY=0x0f(这是列检测的)
为什么要再赋值一次呢?求大神帮忙解释一下。个人感觉如果再switch语句前给io口赋值的话,那么按键检测就没有用处了。求大神指点迷津
#include
#define uchar unsigned char
#define uint unsigned int
#define GPIO_KEY P1
#define GPIO_DIG P0
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar Keyvalue;
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void Keydown()
{
char a=0;
GPIO_KEY=0x0f;
if(GPIO_KEY!=0x0f) //检测按键按下
{
delayms(10); //消抖
if(GPIO_KEY!=0x0f) //再次检测按键按下
{
GPIO_KEY=0x0f; //测试列
switch(GPIO_KEY)
{
case(0x07):Keyvalue=0;break;
case(0x0b):Keyvalue=1;break;
case(0x0d):Keyvalue=2;break;
case(0x0e):Keyvalue=3;break;
}
GPIO_KEY=0xf0;
switch(GPIO_KEY) //测试行
{
case(0x70):Keyvalue=Keyvalue+0;break;
case(0xb0):Keyvalue=Keyvalue+4;break;
case(0xd0):Keyvalue=Keyvalue+8;break;
case(0xe0):Keyvalue=Keyvalue+12;break;
}
}
while((a<50) && (GPIO_KEY!=0xf0)) //检测按键松手检测
{
delayms(10);
a++;
}
}
}
void main(void)
{
while(1)
{ Keydown();
GPIO_DIG=~table[Keyvalue];
}
}
|
|