cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F030 Microsecond delay

AMard.1510
Associate II

Hi every body, how can I create a microsecond delay in STM32f0? ( this MCU has not DWT register)

Thank you

5 REPLIES 5

Use any other free-running TIM to delta progress of time.

Clock a 1 MHz or faster to improve resolution. Don't interrupt. Remember most TIM here will be limited to 16-bit.​

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

Thank you, could you please tell me the details or give me an example?

I've posted examples and discussed this before. It really shouldn't be that complex to think through..

Set the TIM up in Timebase mode, set the Period as maximal, ie 0xFFFF

Set the Prescaler as MHz-1, ie 31 if the bus clocks at 32 MHz, the tick time should be 1us

void delay_us(uint16_t us)
{
 uint16_t start = TIM2->CNT;
 while((TIM2->CNT - start) < us); // rebase, then compare
}

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

Thank You, Please check my CubeMx config, is it correct?

my HCLK = 48Mhz

0690X00000As6qBQAR.jpg

Looks reasonable, not using CubeMX here. Likely some timebase examples under the HAL CubeF0 trees also

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