cembeddedavr

Meaning of empty loops in timer code using C


Consider:

void Tmr_Wait1us (uint16_t delay) {
    uint16_t i;
    TCNT0 = 0; // Will start to count from 0 up to 255 (if 8-bit timer)
    for (i = 0; i < delay / 256; i++)
        while (TCNT0 < 255)
            ;

    while (TCNT0 <= delay % 256)
        ;
}

This code is used to set a timer to wait a precise amount of time, to be applied to an AVR ATmega32A. What do the empty loops do here? Also, what does the second loop do?


Solution

  • They look like busy waits. They block until the condition is met.