AVR Libc Home Page | ![]() |
AVR Libc Development Pages | ||
Main Page | FAQ | Library Reference | Additional Documentation | Example Projects |
#define F_CPU 1000000UL // 1 MHz //#define F_CPU 14.7456E6 #include <util/delay.h>
#define
statement should be used.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 | |
void | _delay_loop_1 (uint8_t __count) |
void | _delay_loop_2 (uint16_t __count) |
void | _delay_us (double __us) |
void | _delay_ms (double __ms) |
void _delay_loop_1 | ( | uint8_t | __count | ) |
Delay loop using an 8-bit counter __count
, so up to 256 iterations are possible. (The value 256 would have to be passed as 0.) The loop executes three CPU cycles per iteration, not including the overhead the compiler needs to setup the counter register.
Thus, at a CPU speed of 1 MHz, delays of up to 768 microseconds can be achieved.
void _delay_loop_2 | ( | uint16_t | __count | ) |
Delay loop using a 16-bit counter __count
, so up to 65536 iterations are possible. (The value 65536 would have to be passed as 0.) The loop executes four CPU cycles per iteration, not including the overhead the compiler requires to setup the counter register pair.
Thus, at a CPU speed of 1 MHz, delays of up to about 262.1 milliseconds can be achieved.
void _delay_ms | ( | double | __ms | ) |
Perform a delay of __ms
milliseconds, using _delay_loop_2().
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.
void _delay_us | ( | double | __us | ) |
Perform a delay of __us
microseconds, using _delay_loop_1().
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.