我是用的是STM32F103ZET芯片,使用的是普中开发板,按键扫描模块使用的是反转扫描,已经可以正常识别按键值,但是想要连续输入两个数却总是出错下面是keypad.c源文件
#include "keypad.h"
#include "systick.h"
void keypad_line_init(void){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_Init(GPIOC,&GPIO_InitStructure);
}
void keypad_col_init(void){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_Init(GPIOC,&GPIO_InitStructure);
}
u8 keypad_scan(void){
u8 line,col;
keypad_line_init();
GPIO_WriteBit(GPIOC,GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11,Bit_SET);
GPIO_WriteBit(GPIOC,GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15,Bit_RESET);
if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8)!=Bit_SET||GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_9)!=Bit_SET||
GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_10)!=Bit_SET||GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_11)!=Bit_SET){
delay_ms(10);
if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8)!=Bit_SET||GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_9)!=Bit_SET||
GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_10)!=Bit_SET||GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_11)!=Bit_SET){
if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8)!=Bit_SET){
line = 1;
}
else if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_9)!=Bit_SET){
line = 2;
}
else if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_10)!=Bit_SET){
line = 3;
}
else{
line = 4;
}
}
}
else{
line = 0;
}
keypad_col_init();
GPIO_WriteBit(GPIOC,GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11,Bit_RESET);
GPIO_WriteBit(GPIOC,GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15,Bit_SET);
if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_12)!=Bit_SET||GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_13)!=Bit_SET||
GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_14)!=Bit_SET||GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_15)!=Bit_SET){
delay_ms(10);
if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_12)!=Bit_SET||GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_13)!=Bit_SET||
GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_14)!=Bit_SET||GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_15)!=Bit_SET){
if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_12)!=Bit_SET){
col = 10;
}
else if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_13)!=Bit_SET){
col = 20;
}
else if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_14)!=Bit_SET){
col = 30;
}
else{
col = 40;
}
}
}
else{
col = 0;
}
return line+col;
}
u8 keypad_value_distinguish(void){
u8 value;
u8 key_velue;
value = keypad_scan();
switch(value){
case 11:key_velue = 1 ;break;
case 12:key_velue = 2 ;break;
case 13:key_velue = 3 ;break;
case 14:key_velue = 10;break;
case 21:key_velue = 4 ;break;
case 22:key_velue = 5 ;break;
case 23:key_velue = 6 ;break;
case 24:key_velue = 11;break;
case 31:key_velue = 7 ;break;
case 32:key_velue = 8 ;break;
case 33:key_velue = 9 ;break;
case 34:key_velue = 12;break;
case 41:key_velue = 13;break;
case 42:key_velue = 14;break;
case 43:key_velue = 15;break;
case 44:key_velue = 16;break;
defult:key_velue = 0;break;
}
return key_velue;
}
下面是main.c模块
while(1)
{
do{
key1 = keypad_value_distinguish();//获取第一个值
}while(key1==0);
LCD_ShowNum(198,163,key1,1,24);//LCD打印输出获取的第一个值
while(key1){
key1 = keypad_value_distinguish();//查询第一个按键是否抬起
};
do{
key2 = keypad_value_distinguish();//获取第二个值
}while(key2==0);
LCD_ShowNum(222,163,key2,1,24);//LCD打印输出获取的第二个值
i++;
if(i%20==0)
{
led1=!led1;
}
delay_ms(10);
}
不知道你这个问题是否已经解决, 如果还没有解决的话: