Go Down

Topic: found bug in delayMicroSeconds when called with 0; (Read 2 times) previous topic - next topic

retrolefty

#5
Nov 18, 2012, 06:14 pm Last Edit: Nov 18, 2012, 06:30 pm by retrolefty Reason: 1

Hi Lefty,

Rationale for that statement is the fact that a function call takes approx 1 usec. Checking the param takes some clock cycles too. So before one can start to count down, too much time may have passed already (for small values). The root cause is that one is near the cpu speed.

But mapping 0 usec on 16K usec is a serious Bug - with capital B ;)

For the Due which is 84(?) Mhz I expect a re-implementation for this function. However no experience yet with the Due.


I believe there is also a minimum 4 usec step size resolution for the delayMicroseconds function as implemented in the arduio AVR platform? So it's not capable of 1 usec resolution timing delays as one might assume otherwise even at values greater then 3?

Lefty

robtillaart

The delayMicroseconds() implementation ends with
Code: [Select]

// busy wait
__asm__ __volatile__ (
"1: sbiw %0,1" "\n\t" // 2 cycles
"brne 1b" : "=w" (us) : "0" (us) // 2 cycles

which is a tight loop decrementing the var us. So I think the timing is quite precise for values > 2.  This loop takes 4 cycles so it is executed 4 times per usec (16Mhz assumed).

The stepsize of 4 usec is the precision of the micros() function so if you want to measure time smaller than 4 usec you need a HW timer solution. Can't find the link now.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

retrolefty


The delayMicroseconds() implementation ends with
Code: [Select]

// busy wait
__asm__ __volatile__ (
"1: sbiw %0,1" "\n\t" // 2 cycles
"brne 1b" : "=w" (us) : "0" (us) // 2 cycles

which is a tight loop decrementing the var us. So I think the timing is quite precise for values > 2.  This loop takes 4 cycles so it is executed 4 times per usec (16Mhz assumed).

The stepsize of 4 usec is the precision of the micros() function so if you want to measure time smaller than 4 usec you need a HW timer solution. Can't find the link now.


Thanks for that.
Yes, I was mixing up micros() with delayMicroseconds(). Put then again I'm old, so I'm allowed a certain amount of 'brain farts' per fortnight, as I continue to chase kids off my lawn.  ;)

Lefty

Coding Badly

Find test program and patch below : NOTE the patch does not include the 1.0.2 20Mhz addition as I cannot test it .


The what?

robtillaart

#9
Nov 20, 2012, 06:59 pm Last Edit: Nov 20, 2012, 07:44 pm by robtillaart Reason: 1
@CB
Quote
The what?

The 1.0.2 version of wiring.c has additional code to implement this delayMicroseconds() function correctly(?) for a 20Mhz arduino.
As I do not have such a thing I cannot test it.

Clear now?
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up