<> Discuss it stm32 How to effectively eliminate jitter when external interrupt is pressed

The engineering code is used directly stm32 cubeMX For configuration generation , Let's discuss it together .
1. Comment the statement of clearing interrupt flag in interrupt handling service function , To prevent the key jitter and constantly enter the interrupt service program
2. Advanced delay after entering callback function , It is generally 10ms, Carry out shaking elimination , Then judge the level state of the pin
3. Be sure to delay the end for a period of time , The interrupt flag is then cleared , And it needs longer time to eliminate shaking , The purpose is to release the key when the jitter and again into the interrupt service program , Jitter generated
4.
As a matter of fact 3 The steps are extremely lax . Clear the interrupt flag after a period of delay , If the interrupt flag is cleared . The button is still on , Wait until it's released , There will still be jitters . The correct one is to wait for the pin level to release ,eg
(1 != HAL_GPIO_ReadPin(WK_UP_GPIO_Port,WK_UP_Pin)) . The corresponding interrupt flag is cleared only when the pin is released

notes : This method still belongs to the blocking wait mode , Partners can change the way of blocking and waiting to use timer to detect the time
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); }
As shown in the figure : Shake free


The following is written using the standard library , The program adopts time slice polling ( cycle 5ms) Task processing in the way of , Polling without external interrupt
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); } }
Is there a better way for my friends , Welcome to comment below