低电平有效的按键
#define KEY1 GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_10)//读取按键0
#define KEY2 GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_13)//读取按键1
#define KEY3 GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_2)//读取按键2
#define KEY4 GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_11)//读取按键3(WK_UP)
#define KEY5 GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_1)//读取按键3(WK_UP)
void KEY_Init(void) //IO初始化
{
GPIO_InitTypeDef GPIO_InitStructure;
/*开启按键端口(PC)的时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin= GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_13;//KEY0-KEY2;
// GPIO_InitStructure.GPIO_Speed= GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_IPU;
GPIO_Init(GPIOC,&GPIO_InitStructure);
}
void KEY_Scan(u8 mode)
{
staticu8 key_up=1;//按键按松开标志
if(mode)
key_up=1; //支持连按
if((KEY1==0||KEY2==0||KEY3==0||KEY4==0||KEY5==0))
{
delay_ms(40);//去抖动
key_up=0;
if(KEY1==0)
KEY1_flag=1;
if(KEY2==0)
KEY2_flag=1;
if(KEY3==0)
KEY3_flag=1;
if(KEY4==0)
KEY4_flag=1;
if(KEY5==0)
KEY5_flag=1;
}
if((KEY1==1||KEY2==1||KEY3==1||KEY4==1||KEY5==1))
{
delay_ms(10);//去抖动
key_up=0;
if(KEY1==1)
KEY1_flag=0;
if(KEY2==1)
KEY2_flag=0;
if(KEY3==1)
KEY3_flag=0;
if(KEY4==1)
KEY4_flag=0;
if(KEY5==1)
KEY5_flag=0;
}
}
按键,弹起有效
KEY_Scan函数修改为
void KEY_Scan(u8 mode)
{
staticu8 key_up=1;//按键按松开标志
if(mode)
key_up=1; //支持连按
if((KEY1==0||KEY2==0||KEY3==0||KEY4==0||KEY5==0))
{
delay_ms(10);//去抖动
key_up=0;
if(KEY1==0)
{
KEY1_flag=1;
if(0== KEY1_press_flag)
{
KEY1_Event_exec_flag= 0;
KEY1_press_flag= 1;
}
}
if(KEY2==0)
{
KEY2_flag=1;
if(0== KEY2_press_flag)
{
KEY2_Event_exec_flag= 0;
KEY2_press_flag= 1;
}
}
if(KEY3==0)
{
KEY3_flag=1;
if(0== KEY3_press_flag)
{
KEY3_Event_exec_flag= 0;
KEY3_press_flag= 1;
}
}
if(KEY4==0)
{
if(0== KEY4_flag)
{
KEY4_flag=1;
}
if(0== KEY4_press_flag)
{
KEY4_Event_exec_flag= 0;
KEY4_press_flag= 1;
}
}
if(KEY5==0)
KEY5_flag=1;
}
if((KEY1==1||KEY2==1||KEY3==1||KEY4==1||KEY5==1))
{
delay_ms(10);//去抖动
key_up=0;
if(KEY1==1)
{
KEY1_flag=0;
if(KEY1_press_flag == 1)
{
KEY1_press_flag= 0;
if(0== KEY1_Event_exec_flag)
KEY1_Event_exec_flag= 1;
}
}
if(KEY2==1)
{
KEY2_flag=0;
if(KEY2_press_flag== 1)
{
KEY2_press_flag= 0;
if(0== KEY2_Event_exec_flag)
KEY2_Event_exec_flag= 1;
}
}
if(KEY3==1)
{
KEY3_flag=0;
if(KEY3_press_flag== 1)
{
KEY3_press_flag= 0;
if(0== KEY3_Event_exec_flag)
KEY3_Event_exec_flag= 1;
}
}
if(KEY4==1)
{
if(1== KEY4_flag)
{
KEY4_flag=0;
}
if(KEY4_press_flag== 1)
{
KEY4_press_flag= 0;
if(0== KEY4_Event_exec_flag)
KEY4_Event_exec_flag= 1;
}
}
if(KEY5==1)
KEY5_flag=0;
}
}
然后,后面响应按键的操作,不再以KEY_flag为标志,而是以KEY3_Event_exec_flag为标志。