cancel
Showing results for 
Search instead for 
Did you mean: 

[STM32F3] How to create microseconds Delay using for?

Felipe Caon
Associate
Posted on November 09, 2017 at 19:45

I'm using STM32F3 with HC-SR04 sensor, already made it work. But not with the accuracy I need, for some reason the output is off by 2-3 centimeter, I'm sure it's because of the delay. 

I tried to create a timer for it but with no success, so?

My board clock is 64 MHz and my Oscillator s 8 MHz. Correct if i`m wrong, but?

1/8MHz = 0.125microseconds, it sends one tick every 0.125us, so if I need to send a 10us ticks I need to send 80 ticks.

Call: pDelay(80);

void pDelay(int i){

static uint32_t j=0;

for(j=0;j<i;j++);

}

Is this right? Is there a better way to do it SINCE I don't even know how to set up timers. I'm probably sending a more than 10us wave with this function.

#stm32f3 #delay #for #stm32 #hc-sr04
1 REPLY 1
Posted on November 10, 2017 at 03:42

I would use DWT_CYCCNT to count off processor cycles at 72 MHz.

You can always configure a free running timer at 1MHz, and delta 1us ticks from the TIM->CNT register.

Don't assume a single C equates to a since micro-processor instruction. The loop variables need to be volatile so the optimizer remove them.

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