util_delay_basic(3avr)
π util_delay_basic β bestie's guide to busy-wait delay loops π
okay so this is util_delay_basic from the AVR-LibC manual (section 3avr, if you're keeping track), and it's lowkey one of those tiny headers that does exactly one thing and does it slayfully. we're talking <util/delay_basic.h>, we're talking spinning the CPU on purpose, we're talking delays. let's go π¦
β¨ NAME β¨
util_delay_basic β <util/delay_basic.h>: basic busy-wait delay loops, aka "make the chip sit there and twiddle its thumbs for a hot second" π«Ά
β¨ SYNOPSIS β¨
π Functions
void _delay_loop_1 (uint8_t __count)
void _delay_loop_2 (uint16_t __count)
two functions, that's the whole menu, and honestly? iconic.
β¨ Detailed Description β¨
#include <util/delay_basic.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.
fr fr, read that again: these are busy-wait loops. your CPU is not doing anything else while it counts down. no multitasking, no vibes, just counting. and no, they do not disable interrupts β interrupts can still jump in and mess with your timing, so don't come for me when your delay is longer than you planned π
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.
the tea β long delay? use a hardware timer, periodt β it frees the CPU and lets other events get processed while the timer does its thing. but for very short delays, setting up a whole timer costs more than the delay itself, so the busy-wait loop is the move. it's giving "right tool for the right job" π
Two inline functions are provided for the actual delay algorithms.
β¨ Function Documentation β¨
πΈ 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.
so: 8-bit counter, __count is a uint8_t, and you get up to 256 iterations out of it β except a uint8_t can't hold 256, so if you want 256 iterations you pass 0 and let it wrap around. cute, right? π
each iteration eats three CPU cycles, and that's not counting whatever overhead your compiler burns setting up the counter register β that's on top, and it depends on your compiler, so no, i can't give you an exact number, nobody can, it's a vibe thing.
Thus, at a CPU speed of 1 MHz, delays of up to 768 microseconds can be achieved.
at 1 MHz β up to 768 microseconds. β¨
πΈ 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.
now the glow-up version: __count is a uint16_t, so you get up to 65536 iterations, and yeah β same trick, 65536 doesn't fit in 16 bits, so you pass 0 to get the full count.
this one runs four CPU cycles per iteration (one more than loop 1, she's fancier), again not counting the compiler's overhead to set up the counter register pair. that setup tax is extra and compiler-dependent, iykyk.
Thus, at a CPU speed of 1 MHz, delays of up to about 262.1 milliseconds can be achieved.
at 1 MHz β up to about 262.1 milliseconds. we love a long-ish delay that still hits different π₯
β¨ Author β¨
Generated automatically by Doxygen for AVR-LibC from the source code.
(yes bestie, a robot wrote this section. doxygen said "i got it" and we respect that π
and that's the whole page!! this was AVR-LibC, Version 2.2.1, from the util_delay_basic(3avr) page β go forth and busy-wait responsibly, bestie πβ¨ππ¦πΈπ
π«Ά
π¦ where this page comes from
- ubuntu resolute: avr-libc 1:2.2.1-1build1 Β· BSD-3-Clause
- ubuntu stonking: avr-libc 1:2.2.1-1build1 Β· BSD-3-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms