讨论一下stm32下按键外部中断如何进行有效的消抖
工程的代码是直接使用stm32 cubeMX进行配置生成的,下面就一起讨论吧。
1. 在中断处理服务函数中注释清除中断标志的语句,防止按键抖动而不断的进入中断服务程序中
2. 进入回调函数后先进性延时,一般为10ms,进行消抖,然后再判断引脚的电平状态
3. 在结束时一定注意要延时一段时间,然后才清除中断标志,而且要相对消抖时间要长一些,目的是为了松开按键时产生的抖动而又再次进入中断服务程序中,产生的抖动
4. 其实第3步是极其不严谨的。延时一段时间再清除中断标志,如果在清除中断标志的时候。按键仍未松开呢,等到松开时候,仍会有抖动。正确的应该是等待引脚电平的释放,eg(1 != HAL_GPIO_ReadPin(WK_UP_GPIO_Port,WK_UP_Pin))
。引脚释放了才清除相应的中断标志
注:该处理方式仍属于阻塞等待的方式哦,小伙伴们可以把阻塞等待方式改为使用定时器去定时检测的方式
void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
{
/* EXTI line interrupt detected */
if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != RESET)
{
// __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
HAL_GPIO_EXTI_Callback(GPIO_Pin);
}
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){
if(WK_UP_Pin == GPIO_Pin){
HAL_Delay(10);
if(1 == HAL_GPIO_ReadPin(WK_UP_GPIO_Port,WK_UP_Pin));
myprintf(&huart1, "wake up key pressed\n");
}
else if(KEY0_Pin == GPIO_Pin){
HAL_Delay(10);
if(0 == HAL_GPIO_ReadPin(KEY0_GPIO_Port,KEY0_Pin))
myprintf(&huart1, "key 0 pressed\n");
}
else if(KEY1_Pin == GPIO_Pin){
HAL_Delay(10);
if(0 == HAL_GPIO_ReadPin(KEY1_GPIO_Port,KEY1_Pin))
myprintf(&huart1, "key 1 pressed\n");
}
else if(KEY2_Pin == GPIO_Pin){
HAL_Delay(10);
if(0 == HAL_GPIO_ReadPin(KEY2_GPIO_Port,KEY2_Pin))
myprintf(&huart1, "key 2 pressed\n");
}
HAL_Delay(100);
__HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
}
如图:消抖成功
下面是之前使用标准库写的,程序采用时间片轮询(周期5ms)的方式进行任务处理,不用外部中断的轮询方式
void Key_Config(void){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC,ENABLE);
// Key1 / Key2 / Key3 -> IPU
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_Init(GPIOC,&GPIO_InitStructure);
// SW1 -> IPU
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IPU; //SW°´¼üÉÏÀÊäÈë
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_Init(GPIOB,&GPIO_InitStructure);
// SW2 -> IPU
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IPU; //SW°´¼üÉÏÀÊäÈë
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
u8 Key_Scan(void){
u8 i,temp;
for(i=0;i<5;i++) {
temp=KEY_VAL>>i;
if(0x1f!=KEY_VAL && 0==(temp & 0x01))
return i+1;
}
return 0;
}
u8 Get_Key(void){
static u8 n,status=0,last_val;
u8 key_val;
key_val=Key_Scan();
if(0!=key_val && key_val==last_val){
if(0==status) status=1; //°´¼ü°´Ï±êʶ
else n++;
}
if(1==status && key_val==0){
status=0; //°´¼üÊͷűêʶ
if(n>2 && n<25){ //40ms ~ 500ms //¶Ì°´µ¥»÷ʱ¼ä¿ØÖÆ£¬¸ù¾Ý¿ØÖÆÖÜÆÚʱ¼ä¿ØÖÆ´ÎÊý
n=0; //Çå³ý¼ÆÊý
return last_val; //·µ»Ø¼üÖµ
}
else if(n>=25){ //´óÓÚ 500ms //³¤°´Ê±¼ä¿ØÖÆ
n=0; //Çå³ý¼ÆÊý
return last_val+10; //³¤°´¼üÖµ = (¶Ì°´¼üÖµ+10)
}
else{ //°´¼üʱ¼ä¹ý¶Ì±»ÈÏΪÊǰ´¼ü¶¶¶¯»òÎó°´×´Ì¬
n=0; //Çå³ý¼ÆÊý
return 20; //·µ»ØÎó°´×´Ì¬
}
}
last_val=key_val; //±£´æÉÏÒ»´ÎµÄ¼üÖµ
return 0;
}
void TIM2_Config(void){
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
/* Tout=((arr+1)*(psc+1))/Tclk */
TIM_TimeBaseStructure.TIM_Prescaler=719; //Ô¤·ÖƵ£º72 000 000 / (719+1) = 1 000 00 Hz
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //¼ÆÊý·½Ê½£ºÏòÉϼÆÊý
TIM_TimeBaseStructure.TIM_Period= 1999; //¼ÆÊýÖÜÆÚ£º1 000 00 / (4999 + 1) = 50 Hz = 0.02 s = 20 ms
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; //ʱÖÓ²»·Ö¸î
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
NVIC_Config(0,0,TIM2_IRQn,2);
TIM_ITConfig(TIM2,TIM_IT_Update , ENABLE);
TIM_Cmd(TIM2,ENABLE);
}
void TIM2_IRQHandler(void){
if(TIM_GetFlagStatus(TIM2,TIM_IT_Update)!=RESET) {
Key_Val=Get_Key(); //ÔÚÖжϳÌÐòÖлñÈ¡¼üÖµ
DATA_Task(); //ͨ¹ý°´¼ü»ñȡʹÄÜÄǸöÊý¾Ý´«Ê书ÄÜ£¬Êý¾Ý´«Ê亯ÊýÔÚÖ÷º¯ÊýÖе÷ÓÃ
TIM_ClearFlag(TIM2,TIM_IT_Update);
}
}
小伙伴们有什么更好的方式吗,欢迎下方评论