Skip to content

Commit 0d70c72

Browse files
committed
Fixing delayMicroseconds() timing for 20 MHz clocks. (Erdem U. Altinyurt)
http://code.google.com/p/arduino/issues/detail?id=306
1 parent 6a6ed3d commit 0d70c72

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

hardware/arduino/cores/arduino/wiring.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,26 @@ void delayMicroseconds(unsigned int us)
124124
// calling avrlib's delay_us() function with low values (e.g. 1 or
125125
// 2 microseconds) gives delays longer than desired.
126126
//delay_us(us);
127+
#if F_CPU >= 20000000L
128+
// for the 20 MHz clock on rare Arduino boards
127129

128-
#if F_CPU >= 16000000L
130+
// for a one-microsecond delay, simply wait 2 cycle and return. The overhead
131+
// of the function call yields a delay of exactly a one microsecond.
132+
__asm__ __volatile__ (
133+
"nop" "\n\t"
134+
"nop"); //just waiting 2 cycle
135+
if (--us == 0)
136+
return;
137+
138+
// the following loop takes a 1/5 of a microsecond (4 cycles)
139+
// per iteration, so execute it five times for each microsecond of
140+
// delay requested.
141+
us = (us<<2) + us; // x5 us
142+
143+
// account for the time taken in the preceeding commands.
144+
us -= 2;
145+
146+
#elif F_CPU >= 16000000L
129147
// for the 16 MHz clock on most Arduino boards
130148

131149
// for a one-microsecond delay, simply return. the overhead

0 commit comments

Comments
 (0)