cancel
Showing results for 
Search instead for 
Did you mean: 

May I ask how to modify this code to replace the HAL_GPIO_EXTI_Callback () callback function?

空楼醉雨
Visitor

The chip I use is STM32G031G8U6, and there seems to be no HAL_GPIO_EXTI_Callback () function in STM 32G00xx _ HAL _ GPIO. C file. What's the matter? The general function of my code is to eliminate the jitter generated when the rotary encoder rotates. Please take the trouble to answer your questions. If there is anything wrong with the following code, please correct it!  ;)

 

 

 

/*注意!
 单片机上电后A、B相默认输出高电平,第一次转动后A相先产生下降沿再产生上升沿,所以应当先判断A相的下降沿,
 再判断A相的上升沿。如果先判断上升沿并检测这时候的B相电平就会导致第一次向新方向旋转时无效!
 */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) // 通用EXTI回调用于判断旋钮左转or右转
{
  static uint8_t B_last_level = 0;

  // 检查中断源是否为 KEY3_EXIT15_Pin
  if (GPIO_Pin == KEY3_EXIT15_Pin)
  {
    // 获取当前A相电平
    uint8_t currentA = HAL_GPIO_ReadPin(KEY3_EXIT15_GPIO_Port, KEY3_EXIT15_Pin);

    if (currentA == GPIO_PIN_RESET)    /* A相先下降,触发第一次中断,检测并记录B相的电平状态 last */
    { 
      // 记录B相旧的电平状态last
      B_last_level = HAL_GPIO_ReadPin(KEY5_Input_GPIO_Port, KEY5_Input_Pin);
    }

    else if (currentA == GPIO_PIN_SET)  /* A相后上升,检测此时B相的电平状态 now */
    { 
      uint8_t B_now_level = HAL_GPIO_ReadPin(KEY5_Input_GPIO_Port, KEY5_Input_Pin);

      /*判断旋转方向——只有当B相电平发生变化时才能说明发生了一次旋转,因为A相波形抖动时B相的电平保持不变,从而能够实现忽略抖动*/
      if (B_last_level == GPIO_PIN_SET && B_now_level == GPIO_PIN_RESET) 
      {
        // B相last为高、now为低,则即顺时针旋转
        printf("ROT Turn Right!\r\n");
      } 
      else if (B_last_level == GPIO_PIN_RESET && B_now_level == GPIO_PIN_SET) 
      {
        // B相last为低、now为高,则即逆时针旋转
        printf("ROT Turn Left!\r\n");
      }

      // 清除中断挂起标志(重要!)
      __HAL_GPIO_EXTI_CLEAR_IT(KEY3_EXIT15_Pin);  // 清除中断挂起标志,作用是防止中断一直触发
    }

  }
  
}

 

 

 

 

4 REPLIES 4
Pavel A.
Evangelist III

there seems to be no HAL_GPIO_EXTI_Callback () function

Correct. This STM32 family (and some others) has  HAL_GPIO_EXTI_Rising_Callback and HAL_GPIO_EXTI_Falling_Callback instead of single HAL_GPIO_EXTI_Callback.

 

TDK
Guru

There is a rising and falling callback function which you can use to know if the interrupt triggered due to a rising or falling edge.

HAL_GPIO_EXTI_Rising_Callback and HAL_GPIO_EXTI_Falling_Callback

stm32g0xx-hal-driver/Src/stm32g0xx_hal_gpio.c at dd4c4604f233e0202b95eeec54ad300392757e85 · STMicroelectronics/stm32g0xx-hal-driver

If you feel a post has answered your question, please click "Accept as Solution".

It seems that I have used this function before, but I can't find it at once today,haha! So it is! Thank you a lot !

Ok, I will try to change the original code with these two functions,thank you very much !