AVR Libc Home Page AVRs AVR Libc Development Pages
Main Page User Manual Library Reference FAQ Alphabetical Index Example Projects

Functions
<util/delay.h>: Convenience functions for busy-wait delay loops

Functions

void _delay_ms (double __ms)
 
void _delay_us (double __us)
 

Detailed Description

#define F_CPU 1000000UL // 1 MHz
//#define F_CPU 14.7456E6
#include <util/delay.h>
Note
As an alternative method, it is possible to pass the F_CPU macro down to the compiler from the Makefile. Obviously, in that case, no #define statement should be used.

The functions in this header file are wrappers around the basic busy-wait functions from <util/delay_basic.h>. They are meant as convenience functions where actual time values can be specified rather than a number of cycles to wait for. The idea behind is that compile-time constant expressions will be eliminated by compiler optimization so floating-point expressions can be used to calculate the number of delay cycles needed based on the CPU frequency passed by the macro F_CPU.

Note
In order for these functions to work as intended, compiler optimizations must be enabled, and the delay time must be an expression that is a known constant at compile-time. If these requirements are not met, the resulting delay will be much longer (and basically unpredictable), and applications that otherwise do not use floating-point calculations will experience severe code bloat by the floating-point library routines linked into the application.

The functions available allow the specification of microsecond, and millisecond delays directly, using the application-supplied macro F_CPU as the CPU clock frequency (in Hertz).

Function Documentation

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.

When the user request delay which exceed the maximum possible one, _delay_ms() provides a decreased resolution functionality. In this mode _delay_ms() will work with a resolution of 1/10 ms, providing delays up to 6.5535 seconds (independent from CPU frequency). The user will not be informed about decreased resolution.

If the avr-gcc toolchain has __builtin_avr_delay_cycles(unsigned long) support, maximal possible delay is 4294967.295 ms/ F_CPU in MHz. For values greater than the maximal possible delay, overflows results in no delay i.e., 0ms.

Conversion of __us into clock cycles may not always result in integer. By default, the clock cycles rounded up to next integer. This ensures that the user gets atleast __us microseconds of delay.

Alternatively, user can define DELAY_ROUND_DOWN and DELAY_ROUND_CLOSEST to round down and round to closest integer.

Note: The new implementation of _delay_ms(double <strong>ms) with __builtin_avr_delay_cycles(unsigned long) support is not backward compatible. User can define __DELAY_BACKWARD_COMPATIBLE to get a backward compatible delay. Also, the backward compatible algorithm will be chosen if the code is compiled in a freestanding environment (GCC option -ffreestanding), as the math functions required for rounding are not available to the compiler then.

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.

If the user requests a delay greater than the maximal possible one, _delay_us() will automatically call _delay_ms() instead. The user will not be informed about this case.

If the avr-gcc toolchain has __builtin_avr_delay_cycles(unsigned long) support, maximal possible delay is 4294967.295 us/ F_CPU in MHz. For values greater than the maximal possible delay, overflow results in no delay i.e., 0us.

Conversion of __us into clock cycles may not always result in integer. By default, the clock cycles rounded up to next integer. This ensures that the user gets atleast __us microseconds of delay.

Alternatively, user can define DELAY_ROUND_DOWN and DELAY_ROUND_CLOSEST to round down and round to closest integer.

Note: The new implementation of _delay_us(double <strong>us) with __builtin_avr_delay_cycles(unsigned long) support is not backward compatible. User can define __DELAY_BACKWARD_COMPATIBLE to get a backward compatible delay. Also, the backward compatible algorithm will be chosen if the code is compiled in a freestanding environment (GCC option -ffreestanding), as the math functions required for rounding are not available to the compiler then.


Automatically generated by Doxygen 1.8.7 on Tue Aug 12 2014.