cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_Delay() is not working properly!!!

Ala
Senior
1 ACCEPTED SOLUTION

Accepted Solutions

> 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

View solution in original post

8 REPLIES 8
oeliks
Senior

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

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
Up vote any posts that you find helpful, it shows what's working..

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
Senior

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.

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
Senior

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

> 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

THANKS Dear waclawek.jan!

It was a big help!:folded_hands: