This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Delay function takes the doubel of time

Hello,
I'm using an stm32f103C8 and trying to produce a precise delay funcion.
I'm using a HSE of 72Mhz so a I supposed the clock cycle is equal to 13.8ns.
The delay function is simply a while(count--) where count has the specific number of times for looping to gain the requested time.
I made an hypothese that while(count--) takes 3 cycles to execute.
So when I need to make 1µs delay I put 24 in count.

But I found that I make 4us with count =24 that means 4 times the desired time.
Please can you help identify this problem?

Parents
  • Change your controller if it doesn't have a TIMER.

    "Despite the capability of most timers, we still see people totally ignoring them, trying to do everything in software - including basic delays as counted number of iterations of a busy loop." - Per Westermark

Reply
  • Change your controller if it doesn't have a TIMER.

    "Despite the capability of most timers, we still see people totally ignoring them, trying to do everything in software - including basic delays as counted number of iterations of a busy loop." - Per Westermark

Children
  • Hello Sir,
    Do you suggest that I merge to Timer or systick?
    Did you encounter problems with software delay before and can you please tell me what are the causes of this problem

  • Use whichever meets your requirements!

    SysTick, as the name suggests, is specifically intended for providing a System timing "Tick" to software.

    Other timers tend to have far more flexibility for many more applications - so it may be a "waste" to use one where SysTick would do...

    Only you know the specific requirements & constraints of your particular project - so only you can decide.