Skip to main content
This topic has been closed for replies.
Best answer by waclawek.jan

> it looks like with each delay, it lasts 1ms more!

This is how HAL_Delay() works - it provides you a *minimum* N ms delay.

It looks at a timer which ticks continuously once in 1ms. So, when you call it, it looks at that timer's value, say it's K, and then waits until that timer rolls over from K+N to K+N+1.

If you happen to call it just moments before it rolls over from K to K+1, the delay will be just slightly longer than N ms. If you happen to call it slightly after it rolls over from K-1 to K, the delay will be just slightly shorter than N+1 ms.

JW

8 replies

oeliks
Associate III
December 22, 2019

Well its worst thing to do if you want to generate pulse. For me it produced waveform with randomly changing period.

Set scope to capture single event, and set and reset pin once, not in loop. Also check clocks.​

Try to use timer as pwm or just to toggle pin.​

Tesla DeLorean
Guru
December 22, 2019

Make sure HSE_VALUE reports 8 MHz and not 25, and that the PLL and clock settings are correct.​

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Ala
AlaAuthor
Senior
December 23, 2019

Dear clive1

can you tell me in details what should I do?

should I set the HSE to Crystal/Ceramic Resonator?

what should I do in the CubeMX clock settings? like in the below image?0690X00000Buu7xQAB.png

oeliks
Associate III
December 23, 2019

First enable HSE, in cubemx system RCC->HSE crystal resonator. Then go to clocks menu (image above). Change input frequency from 25 to 8, and check​ HSE option. Change HCLK to desired core clock and allow cubemx to recalculate all prescalers.

Ala
AlaAuthor
Senior
December 23, 2019

Dear Oeliks

I did as you said and I set the HCLK to 16, still it doesn't work

it looks like with each delay, it lasts 1ms more!

for example when I set HAL_Delay(2), for the pin PD12 to remain SET, it remains SET for 3ms!!!

WHY GUYS????

oeliks
Associate III
December 23, 2019

Sorry, cant help more.

For precise time base I use timers and interrupts. For no critical things I use delay. Mabey Clive1 can help more.​

waclawek.jan
waclawek.janBest answer
Super User
December 23, 2019

> it looks like with each delay, it lasts 1ms more!

This is how HAL_Delay() works - it provides you a *minimum* N ms delay.

It looks at a timer which ticks continuously once in 1ms. So, when you call it, it looks at that timer's value, say it's K, and then waits until that timer rolls over from K+N to K+N+1.

If you happen to call it just moments before it rolls over from K to K+1, the delay will be just slightly longer than N ms. If you happen to call it slightly after it rolls over from K-1 to K, the delay will be just slightly shorter than N+1 ms.

JW

Ala
AlaAuthor
Senior
December 23, 2019

THANKS Dear waclawek.jan!

It was a big help!:folded_hands: