#include "key.h"
#include "delay.h"
u8 lastKey=0,currentKey=0;
u16 rowPin[4] = {GPIO_Pin_0,GPIO_Pin_1,GPIO_Pin_2,GPIO_Pin_3};
u16 colPin[4] = {GPIO_Pin_4,GPIO_Pin_5,GPIO_Pin_6,GPIO_Pin_7};
u8 otherPin[4] = {14,13,11,7};//GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3,GPIO_Pin_0|GPIO_Pin_2|GPIO_Pin_3,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_3,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2};
//按键初始化函数
void KEY_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//使能PORTC时钟
GPIO_InitStructure.GPIO_Pin = ROWPINS;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //设置成推挽输出
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOC 0-3
GPIO_ResetBits(GPIOB,ROWPINS);
GPIO_InitStructure.GPIO_Pin = COLPINS;//PC5
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置成上拉输入
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOC 10-13
GPIO_SetBits(GPIOB,COLPINS);
}
u8 ReScan(u16 colPin,u8 colIndex)
{
u8 i;
for(i = 0;i<4;i++){
GPIO_ResetBits(GPIOB,rowPin);
GPIO_Write(GPIOB,otherPin);
delay_ms(10);//据说是消除抖动,但是去掉工作也正常
if((GPIO_ReadInputDataBit(GPIOB, colPin) == 0)){
GPIO_ResetBits(GPIOB,ROWPINS);
return (i-1)*4+colIndex+1;//返回的数据为1-16 对应4x4键盘的16个键
}
}
GPIO_ResetBits(GPIOB,ROWPINS);
return 0;
}
u8 KEY_Scan(void)
{
u8 i;
for(i = 0;i<4;i++)
{
if(GPIO_ReadInputDataBit(GPIOB,colPin) == 0){
currentKey = ReScan(colPin,i);
if(currentKey == 0)
{
lastKey = 0;
return 0;
}
else
{
if(currentKey != lastKey)
{
lastKey = currentKey;
return 1;
}
else
{
lastKey = currentKey;
return 0;
}
}
}
}
lastKey = 0;
return 0;
}
为什么只有个别按键可以用?求指教
|