#define F_CPU 1000000UL // 1 MHz //#define F_CPU 14.7456E6 #include <avr/delay.h>
The functions in this header file implement simple delay loops that perform a busy-waiting. They are typically used to facilitate short delays in the program execution. They are implemented as count-down loops with a well-known CPU cycle count per loop iteration. As such, no other processing can occur simultaneously. It should be kept in mind that the functions described here do not disable interrupts.
In general, for long delays, the use of hardware timers is much preferrable, as they free the CPU, and allow for concurrent processing of other events while the timer is running. However, in particular for very short delays, the overhead of setting up a hardware timer is too much compared to the overall delay time.
Two inline functions are provided for the actual delay algorithms.
Two wrapper functions allow the specification of microsecond, and millisecond delays directly, using the application-supplied macro F_CPU as the CPU clock frequency (in Hertz). These functions operate on double typed arguments, however when optimization is turned on, the entire floating-point calculation will be done at compile-time.
Functions | |
static __inline__ void | _delay_loop_1 (uint8_t __count) |
static __inline__ void | _delay_loop_2 (uint16_t __count) |
static __inline__ void | _delay_us (double __us) |
static __inline__ void | _delay_ms (double __ms) |
|
Delay loop using an 8-bit counter Thus, at a CPU speed of 1 MHz, delays of up to 768 microseconds can be achieved. |
|
Delay loop using a 16-bit counter Thus, at a CPU speed of 1 MHz, delays of up to about 262.1 milliseconds can be achieved. |
|
Perform a delay of The macro F_CPU is supposed to be defined to a constant defining the CPU clock frequency (in Hertz). The maximal possible delay is 262.14 ms / F_CPU in MHz. |
|
Perform a delay of The macro F_CPU is supposed to be defined to a constant defining the CPU clock frequency (in Hertz). The maximal possible delay is 768 us / F_CPU in MHz. |