AVR-LibC  2.3.0
Standard C library for AVR-GCC
 

AVR-LibC Manual

AVR-LibC Sources

Main Page

User Manual

Lib­rary Refe­rence

FAQ

Exam­ple Pro­jects

Index

Loading...
Searching...
No Matches
Functions
<util/delay_basic.h>: Basic busy-wait delay loops

Functions

void _delay_loop_1 (uint8_t __count)
 
void _delay_loop_2 (uint16_t __count)
 

Detailed Description

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 preferable, 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.

Function Documentation

◆ _delay_loop_1()

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, delays of up to 768 / fCPU microseconds can be achieved, where fCPU denotes the CPU speed in units of 1 MHz.

As an alternative, consider __builtin_avr_delay_cycles which allows to specify the cycle count and can implement delays of up to 71.5 / fCPU seconds.

◆ _delay_loop_2()

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, delays of up to 262.1 / fCPU milliseconds can be achieved, where fCPU denotes the CPU speed in units of 1 MHz.

As an alternative, consider __builtin_avr_delay_cycles which allows to specify the cycle count and can implement delays of up to 71.5 / fCPU seconds.