Skip to main content
Associate III
September 29, 2024
Solved

Timer isn't accurate

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.
DavidL_Best answer
Associate III
September 29, 2024

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

2 replies

DavidL_AuthorBest answer
Associate III
September 29, 2024

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

Karl Yamashita
Principal
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.

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source