Skip to main content
KKIM.6
Senior
May 12, 2022 3:46 PM UTC
Question

When I make a timer, 1 ms error happens per every 1 s. How can I revise it?

When I make a timer,

1 ms error happens per every 1 s.

How can I revise it? 

I'm using Necleo-wb55 and "TIM16"

The Prescaler is 31999 / 32000 (both are tested).

This topic has been closed for replies.

12 replies

KKIM.6
KKIM.6Author
Senior
May 12, 2022 4:07 PM UTC

if I give HAL_Delay(100), the 1ms error happens every 100 ms... is it depends on the printf() commend?

Javier1
Principal
May 12, 2022 4:21 PM UTC

HAL_Delay(100) is not the best way to create accurate delays.

Try setting a Timer interruption to be triggered every 1 seconds.

hit me up in https://www.linkedin.com/in/javiermuñoz/
KKIM.6
KKIM.6Author
Senior
May 13, 2022 5:03 PM UTC

I agree but I want to see the detailed speed of my script instantly using USART communication

Javier1
Principal
May 12, 2022 4:14 PM UTC

Best way to debugg this is by toggling on/off a LED or a gpio.

Measure the 1Hz signal with your favourite oscilloscope and voila!

No need for non deterministic printf()

hit me up in https://www.linkedin.com/in/javiermuñoz/
KKIM.6
KKIM.6Author
Senior
May 13, 2022 2:31 AM UTC

My system should work within 1 ms.....so, I'm using a timer to evaluate the speed of the script. but it has 300 us delay for each print() for USB USART communication. So, I'm confused about which speed I should believe or whether the system can be faster.

Javier1
Principal
May 13, 2022 7:32 AM UTC

The fact that you called your code "script" makes me think you come from high level programming.

>My system should work within 1 ms

but it doesnt

>I'm using a timer to evaluate the speed of the script. but it has 300 us delay for each print()

Dont use a printf, use a HAL_gpio_toggle() inside the 1Hz timer ISR function.

>which speed I should believe or whether the system can be faster.

believe the speed you see in your oscilloscope, toggle gpios not printf to measure speed

hit me up in https://www.linkedin.com/in/javiermuñoz/
MM..1
Super User
May 12, 2022 5:08 PM UTC

How is your clock setup HSE or HSI ? Too HAL_Delay is sw implement , can have error.

Next HAL_Delay is based on tick int (systick or TIM) usw.

KKIM.6
KKIM.6Author
Senior
May 13, 2022 2:29 AM UTC

The current version is using 32 MHz HSE. I found that the interval of printf() is about 300 us. but I don't know whether it is due to the printf() or other scripts.

MM..1
Super User
May 13, 2022 1:54 PM UTC

1ms plus or minus ???

Read HAL_Delay code

__weak void HAL_Delay(uint32_t Delay)
{
 uint32_t tickstart = HAL_GetTick();
 uint32_t wait = Delay;
 
 /* Add a freq to guarantee minimum wait */
 if (wait < HAL_MAX_DELAY)
 {
 wait += (uint32_t)(uwTickFreq);
 }
 
 while ((HAL_GetTick() - tickstart) < wait)
 {
 }
}

write your own code ...

Muhammed Güler
Senior III
May 13, 2022 6:54 AM UTC

There are 3 600 000 milliseconds in an hour. Even if your crystal has a tolerance of 1ppm, 3.6ms error can be considered normal.

https://www.digikey.com/en/products/detail/connor-winfield/DOCSC012F-010-0M/13422056

for example if you use this oscillator there is a possibility of 1ms error in 27.8 hours

or

https://www.digikey.com/en/products/detail/connor-winfield/OH320-CC-700503CF-010-0M/13687442

If you use this oscillator, there is a possibility of 1ms error in 555.6 hours.

The real question is, is it worth it?

KKIM.6
KKIM.6Author
Senior
May 13, 2022 5:05 PM UTC

my situation is more serious than physical tolerance. I think some parts cause problems but I cannot find the reason because STM32 is very complex.