cancel
Showing results for 
Search instead for 
Did you mean: 

accurate delay_ms function for the STM32F0 processor

piyush2
Associate II
Posted on September 28, 2014 at 10:38

Hi

I was doing a small project using the STM32F0 processor and IAR Embedded Workbench toolset.

There I was trying to customize the example source code available in the IAR examples section.

The path is to that example on my system:

C:\Program Files\IAR Systems\Embedded Workbench 6.5\arm\examples\ST\STM32F05x\STM32F0-DISCOVERY\Project\src\EvalTest

 

 

In this source code example there are two functions defined in the main.c file for creating the delay in millisecond.

One is loose type and another one is accurate.

While the working of loose type of the delay is pretty clear but the accurate one is not that mush clear to me.

I mean I am not able to get the idea that how it is going to work instead it must end in an

infinite while loop.

I am pasting the body of that delay routine over here:

void delay_ms(uint32_t n_ms)

 

{

 

  // SysTick interrupt each 1000 Hz with HCLK equal to 32MHz

 

  // - 30 to compensate the overhead of this sub routine

 

  SysTick_Config(8000*PLL_MUL_X - 30);

 

  // Enable the SysTick Counter

  TickValue = n_ms;

 

  while(TickValue == n_ms)

 

    ;

  // SysTick interrupt each 1000 Hz with HCLK equal to 32MHz

 

  SysTick_Config(8000*PLL_MUL_X);

 

  while(TickValue != 0)

 

    ;

}

 

 

 

Also in my code its not working and get stuck in the first while loop.

So please tell me if I am missing something.

Thanks

#delay-function-loophole
1 REPLY 1
Posted on September 28, 2014 at 14:20

One might consider using a free-running timer, running at micro-seconds (1 MHz), and doing a delta measurement of clock ticks.

When using SysTick, I'd just set it up once for 1 milli-second (1 KHz), and increment a tick count with that, other delays would not reinitialize this. Generally you're going to want to avoid these kinds of pointless software delay loops, use scheduling timers. For accurate signal placement, consider using timer hardware.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..