cancel
Showing results for 
Search instead for 
Did you mean: 

Timer isn't accurate

DavidL_
Associate III

I am using an stm32 timer calculator ( https://deepbluembedded.com/stm32-timer-calculator/ ) to get the settings for a 20ms timer and got the following values: https://imgur.com/a/oMHS17r which I set for my TIM16 timer.

I am using the following code:

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
    if(htim == &htim16)
    {
      int t = HAL_GetTick();
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, SET);

      HAL_TIM_Base_Start_IT(&htim15);

      char m[30];
      sprintf(m, "1: %d\r\n", t);

      HAL_UART_Transmit(&huart2, (uint8_t*)m, 30, HAL_MAX_DELAY);
    }
}

 

to check if it works as expected every 20ms and it doesn't seem to work as intended, since it triggers every 17-18ms.

Does someone have an idea why its not working correctly?

PS: Here is an image of my clock tree: https://imgur.com/a/7bcML52 and I am using a STM32 Nucleo-32 L432KC

1 ACCEPTED SOLUTION

Accepted Solutions
DavidL_
Associate III

The UART transmission duration was accounting for the lost 2-3 ms

View solution in original post

2 REPLIES 2
DavidL_
Associate III

The UART transmission duration was accounting for the lost 2-3 ms

Karl Yamashita
Lead III

When you're in a interrupt, you want to set a flag and exit right away. Then in main while loop, you check the flag and send your UART data.

If smoke escapes your device, put the smoke back in. It'll still work as a conversation piece. If you find my answers useful, click the Accept as Solution button so that way others can see the solution.