mirror of
https://github.com/esp8266/Arduino.git
synced 2025-08-09 22:24:14 +03:00
Updating delayMicroseconds() to work on an 8 MHz clock as well as a 16 MHz one.
This commit is contained in:
@@ -65,6 +65,9 @@ void delayMicroseconds(unsigned int us)
|
|||||||
// 2 microseconds) gives delays longer than desired.
|
// 2 microseconds) gives delays longer than desired.
|
||||||
//delay_us(us);
|
//delay_us(us);
|
||||||
|
|
||||||
|
#if F_CPU >= 16000000L
|
||||||
|
// for the 16 MHz clock on most Arduino boards
|
||||||
|
|
||||||
// for a one-microsecond delay, simply return. the overhead
|
// for a one-microsecond delay, simply return. the overhead
|
||||||
// of the function call yields a delay of approximately 1 1/8 us.
|
// of the function call yields a delay of approximately 1 1/8 us.
|
||||||
if (--us == 0)
|
if (--us == 0)
|
||||||
@@ -77,6 +80,26 @@ void delayMicroseconds(unsigned int us)
|
|||||||
|
|
||||||
// account for the time taken in the preceeding commands.
|
// account for the time taken in the preceeding commands.
|
||||||
us -= 2;
|
us -= 2;
|
||||||
|
#else
|
||||||
|
// for the 8 MHz internal clock on the ATmega168
|
||||||
|
|
||||||
|
// for a one- or two-microsecond delay, simply return. the overhead of
|
||||||
|
// the function calls takes more than two microseconds. can't just
|
||||||
|
// subtract two, since us is unsigned; we'd overflow.
|
||||||
|
if (--us == 0)
|
||||||
|
return;
|
||||||
|
if (--us == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// the following loop takes half of a microsecond (4 cycles)
|
||||||
|
// per iteration, so execute it twice for each microsecond of
|
||||||
|
// delay requested.
|
||||||
|
us <<= 1;
|
||||||
|
|
||||||
|
// partially compensate for the time taken by the preceeding commands.
|
||||||
|
// we can't subtract any more than this or we'd overflow w/ small delays.
|
||||||
|
us--;
|
||||||
|
#endif
|
||||||
|
|
||||||
// disable interrupts, otherwise the timer 0 overflow interrupt that
|
// disable interrupts, otherwise the timer 0 overflow interrupt that
|
||||||
// tracks milliseconds will make us delay longer than we want.
|
// tracks milliseconds will make us delay longer than we want.
|
||||||
|
Reference in New Issue
Block a user