Skip to main content
Graduate
September 29, 2024
Solved

Timer isn't accurate

  • September 29, 2024
  • 2 replies
  • 996 views

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

    This topic has been closed for replies.
    Best answer by DavidL_

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

    2 replies

    DavidL_AuthorAnswer
    Graduate
    September 29, 2024

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

    Graduate II
    September 29, 2024

    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.