标题:
stm32矩阵按键扫描代码
[打印本页]
作者:
阿拉听听的天涯
时间:
2017-8-7 08:08
标题:
stm32矩阵按键扫描代码
//直接将代码贴出来了,就不发附件了,仅供参考
u16 Get_KeyValue(void)//使用PF0~PF7
{
GPIO_InitTypeDef GPIO_InitStructure;
u8 i=5,j=5;
u16 temp1,temp2;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);
GPIO_DeInit(GPIOF);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_ResetBits(GPIOF,GPIO_Pin_L);//扫描列值
if((GPIO_ReadInputData(GPIOF)&0x00f0)==0x00f0)
return 0;
else
{
Delay_nms(70);//按键消抖
if((GPIO_ReadInputData(GPIOF)&0x00f0)==0x00f0)
return 0;
else
temp1=GPIO_ReadInputData(GPIOF)&0x00f0;
}
switch(temp1)
{
case 0x00e0:j=0;break;
case 0x00d0:j=1;break;
case 0x00b0:j=2;break;
case 0x0070:j=3;break;
default:break;
}
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_ResetBits(GPIOF,GPIO_Pin_R);//扫描行值
if((GPIO_ReadInputData(GPIOF)&0x000f)==0x000f)
return 0;
else
{//这里不再延时再扫描,因为已经确定了不是抖动才会进入本步操作
temp2=GPIO_ReadInputData(GPIOF)&0x000f;
}
switch(temp2)
{
case 0x000e:i=0;break;
case 0x000d:i=1;break;
case 0x000b:i=2;break;
case 0x0007:i=3;break;
default:break;
}
if((i==5)||(j==5))
return 0;
else
return (Key_Tab[i][j]);
}
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1