一、4*3键盘模块实物分析
说明:
1、横着4排,竖着3列,加起来共7组,所以对外引出7根线。
2、根据排针终端引脚又可分两类。即横排和竖列对应的引脚。
二、代码编写构想:
1、使用7个gpio输入中断,检测7个引脚的输入状态。
2、当横排和竖排引脚对应的io引脚,同时处于一致状态时,即判断按键被按下。
1、gpio初始化....
2、对按键中断的处理
static uint8_t keyval=0;
/* USER CODE BEGIN 2 */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{uint8_t row=0,column=0;if(0!=keyval) return;//横排四组状态编号。if(GPIO_Pin==GPIO_PIN_x){row=0x40;//第四排}else if(GPIO_Pin==GPIO_PIN_y){row=0x30;//第三排}else if(GPIO_Pin==GPIO_PIN_z){row=0x20;//第二排}else if(GPIO_Pin==GPIO_PIN_w){row=0x10;//第一排}//竖排三组状态编号if(GPIO_PIN_SET==HAL_GPIO_ReadPin(GPIOx,GPIO_PIN_a)){column=0x03;//第三列}else if(GPIO_PIN_SET==HAL_GPIO_ReadPin(GPIOx,GPIO_PIN_b)){column=0x02;//第二列}else if(GPIO_PIN_SET==HAL_GPIO_ReadPin(GPIOx,GPIO_PIN_c)){column=0x01;//第一列}if(0!=row&&0!=column){keyval=row|column;//判断按键是否按下的状态//printf("key val:%02x\r\n",keyval);}
}
//对按键按下后的检测处理与判断
char get_key_ch(void)
{char ch=0;if(0!=keyval){if(0x11==keyval)ch='1';else if(0x12==keyval) ch='2';else if(0x13==keyval) ch='3';else if(0x21==keyval) ch='4';else if(0x22==keyval) ch='5';else if(0x23==keyval) ch='6';else if(0x31==keyval) ch='7';else if(0x32==keyval) ch='8';else if(0x33==keyval) ch='9';else if(0x41==keyval) ch='*';else if(0x42==keyval) ch='0';else if(0x43==keyval) ch='#';keyval=0;}return ch;
}