Technology Sharing

Using a timer to eliminate jitter

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Question: What operation mode do timer interrupt and key interrupt belong to, polling?

How to implement it

Timer interrupt (judgment) Time parameter Key interrupt (modification)

Interrupt vector table .s file

        DCD     SysTick_Handler            ; SysTick Handler

Timer related in interrupt file
stm32f1xx_it.c

  1. void SysTick_Handler(void)
  2. {
  3. HAL_IncTick();//增加计数值,
  4. extern void check_timer(void);
  5. check_timer();
  6. }

stm32f1xxx_hal.c 

  1. __weak void HAL_IncTick(void)
  2. {
  3. uwTick += uwTickFreq;
  4. }
  5. __weak uint32_t HAL_GetTick(void)
  6. {
  7. return uwTick;
  8. }

main.c

Pointer time and clock time

pTimer structureFunctions that contain time and count values

In the systemClock processing functionAdd a clock check function, and the if statement in check_timer limits the pointer time to be less than or equal to the clock time.

If interrupted,Interrupt callback functionLet your pointer time increase by 10ms,If another interruption occurs during this period, the pointer time will be increased by 10.

When no interrupt occurs or the interrupt is within (n*10) ms, the condition to enter the if function body in check_timer is not met;

Until there is no jitter, enter the if function body.Call the function in the structure through the timer function and check_timer function,makeCount valueIncrease

  1. struct soft_timer {
  2. uint32_t timeout;
  3. void * args;
  4. void (*func)(void *);
  5. };
  6. int g_key_cnt = 0;
  7. void key_timeout_func(void *args);
  8. struct soft_timer key_timer = {~0, NULL, key_timeout_func};
  9. void key_timeout_func(void *args)
  10. {
  11. g_key_cnt++;
  12. key_timer.timeout = ~0;
  13. }
  14. void mod_timer(struct soft_timer *pTimer, uint32_t timeout)
  15. {
  16. pTimer->timeout = HAL_GetTick() + timeout;
  17. }
  18. void check_timer(void)
  19. {
  20. if (key_timer.timeout <= HAL_GetTick())
  21. {
  22. key_timer.func(key_timer.args);
  23. }
  24. }
  25. 、、数据处理的源头,发生中断
  26. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  27. {
  28. if (GPIO_Pin == GPIO_PIN_14)
  29. {
  30. mod_timer(&key_timer, 10);
  31. }
  32. }
  1. int main(){
  2. OLED_Init();
  3. OLED_Clear();
  4. OLED_PrintString(0, 4, "Key ISR cnt = ");
  5. while (1)
  6. {
  7. OLED_PrintSignedVal(0, 6, g_key_cnt);
  8. }
  9. }

stm32f1xx_it.c

          DCD     EXTI15_10_IRQHandler       ; EXTI Line 15..10

  1. void EXTI15_10_IRQHandler(void)
  2. {
  3. /* USER CODE BEGIN EXTI15_10_IRQn 0 */
  4. /* USER CODE END EXTI15_10_IRQn 0 */
  5. HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_14);
  6. /* USER CODE BEGIN EXTI15_10_IRQn 1 */
  7. /* USER CODE END EXTI15_10_IRQn 1 */
  8. }

 stm32f1xxx_hal_gpio.c  

The source of data processing, an interruption occurs

  1. void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
  2. {
  3. /* EXTI line interrupt detected */
  4. if (__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != 0x00u)
  5. {
  6. __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
  7. HAL_GPIO_EXTI_Callback(GPIO_Pin);
  8. }
  9. }