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?
Indeed I'm using USB to communicate with pc, so maybe the usb interrupts affects the software delay. I'm more encouraged to use timers or systick I'll tell you the result as soon as I finish
The important thing here is that you can almost always use a timer for creating delays without losing the ability of it to solve other problems too. A free-running timer can be used for polled delays and still constantly reprogram match registers on every match interrupt to let the match registers generate zero-jitter pin toggling. And the same timer could be both a PWM device and a fixed-frequency "uptime" generator.
Best of all? Most timers are quite easy to program, despite their capabilities.