cancel
Showing results for 
Search instead for 
Did you mean: 

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

KKIM.6
Senior II

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).

12 REPLIES 12
KKIM.6
Senior II

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

Javier1
Principal

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()

we dont need to firmware by ourselves, lets talk

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

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

we dont need to firmware by ourselves, lets talk
MM..1
Chief III

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.

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.

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.

Muhammed Güler
Senior III

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?

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

we dont need to firmware by ourselves, lets talk

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 ...