8.4.1 _delay_ms Function

Delay for the specified time.

Include

<util/delay.h>

Prototype

void _delay_ms(double ms);
Arguments
ms
The time in milli seconds to delay.

Remarks

This function delays execution by using the _delay_loop_2() function. The macro F_CPU should be defined as a constant that specifies the CPU clock frequency (in Hertz). The compiler optimizers must be enabled for accurate delay times.

The maximal possible delay is 4294967.295 ms/F_CPU in MHz. Requesting values greater than the maximal possible delay will result in overflow and a delay of 0us.

Conversion of the requested number of milli seconds into clock cycles may not always result in integer value. By default, the number of clock cycles is rounded up to next integer. This ensures that there is at least the requested amount of delay.

By defining the macro __DELAY_ROUND_DOWN__, or __DELAY_ROUND_CLOSEST__, before including this header file, the algorithm can be made to round down, or round to closest integer, respectively.

Example

#define F_CPU 4000000UL
#include <util/delay.h>

int main(void)
{
  _delay_ms(20);  // delay for 20 milli seconds
}