cancel
Showing results for 
Search instead for 
Did you mean: 

Micro-second delay function for STM32F0 ?

Vu.Andy
Associate III
Posted on September 30, 2016 at 18:22

Since it seems like the DWT is not available for the F0 series, is the only way is to have a long loop for micro-second delay?

For example, if the F0 runs at 48MHz, if I have a loop like this:

for (uint32_t idx = 0; idx<max_count; idx++) {}

So approximately, what would be the value of max_count to get 1 micro-second?

Thanks.
3 REPLIES 3
Posted on September 30, 2016 at 20:02

You'd want the variable as a volatile so the compiler doesn't optimize it away. You'd need to benchmark the code, perhaps by toggling a GPIO, or counting cycles for the generated assembler.

You can program one of the TIM to clock at 1MHz and use that, or frankly use the TIM at 48 MHz, to calibrate the loop.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Vu.Andy
Associate III
Posted on September 30, 2016 at 20:29

Thanks for the reply.

Based on the assembly codes below, the loop takes 8 instructions.

So assuming in a pipeline, each instruction takes (1/48MHz),

8 instructions in the loop would take 1.67e-7 sec.  So 1usec would take

6 loop iterations. 

But as you suggested, using a timer is probably better.

I guess the bottom line is with a 48MHz clock, there is

not much resolution so it won't be that accurate anyway.

   211:         for (idx=0; idx < max_count; idx++) {};

0x0800082C 9001      STR      r0,[sp,#0x04]

0x0800082E 2000      MOVS     r0,#0x00

0x08000830 9000      STR      r0,[sp,#0x00]

0x08000832 1C40      ADDS     r0,r0,#1

0x08000834 9901      LDR      r1,[sp,#0x04]

0x08000836 9000      STR      r0,[sp,#0x00]

0x08000838 4288      CMP      r0,r1

0x0800083A D3FA      BCC      0x08000832

Posted on September 30, 2016 at 20:50

while(count--); // might have finer granularity

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..