Use A Timer For System Delays

A Delay Stops Foreground Work
Section titled “A Delay Stops Foreground Work”A busy delay keeps the CPU in a wait loop until time has passed. That is simple for a tiny demonstration, but the foreground cannot scan buttons, update a display or process a serial command during that wait. A timer tick lets the application check elapsed time and continue doing other bounded work between checks.
The course tick driver uses Timer0 compare A. At 16 MHz with a divide-by-64 prescaler, OCR0A=249 produces a nominal 1 ms interrupt. The ISR increments a shared millisecond counter; foreground code reads an atomic snapshot using avr_tick_ms().
Check Elapsed Time Without Waiting
Section titled “Check Elapsed Time Without Waiting”uint32_t now = avr_tick_ms();if ((uint32_t)(now - previous_ms) >= interval_ms){ previous_ms = now; update_output();}Unsigned subtraction continues to work across counter wrap for intervals below half the counter range, provided the foreground checks often enough. previous_ms = now schedules from the time the event was noticed; adding the interval to the previous deadline instead preserves phase but needs a policy for missed deadlines.
Timer Ownership and Accuracy
Section titled “Timer Ownership and Accuracy”The avr_tick_init() driver owns Timer0 and TIMER0_COMPA_vect; it cannot coexist with the Arduino core timebase or another Timer0 owner. Interrupts must be enabled by the application after setup. The tick is a software timebase, not a calibrated real-time clock, and long periods with interrupts disabled can lose compare events.
Build the cooperative scheduler lab with XC8 or AVR GCC. Its LED and UART examples illustrate independent periodic work without a delay loop.
Exercise with Answer
Section titled “Exercise with Answer”Why does the tick ISR only update a counter instead of printing the elapsed time?
Answer: Printing can wait for UART hardware and take much longer than the tick interval. Keeping the ISR short limits interrupt latency; foreground code can format and send the message later.
Code Walkthrough
Section titled “Code Walkthrough”Interrupt tick and cooperative tasks
Section titled “Interrupt tick and cooperative tasks”The example’s main() configures its peripherals, runs the algorithm and reports or displays the result. These source operations are listed in execution order:
uint32_t led_start = 0UL, report_start = 0UL;- Clears the state/counter after its threshold is reached so the next interval starts from zero.(void)avr_gpio_mode(13U, AVR_GPIO_OUTPUT);- Maps the Uno header pin to its AVR port, sets its DDR direction bit and prepares the PORT latch for the requested initial level or pull-up.(void)avr_uart_init(9600UL);- Configures USART0 for asynchronous 8N1. It chooses a baud divisor and normal/double-speed mode, programs UBRR0/UCSR0A/UCSR0C, then enables receive and transmit in UCSR0B.avr_tick_init();- Configures the course millisecond tick interrupt source; it does not enable global interrupts itself.sei();- Enables global interrupts after the example has configured its sources and shared state.for (;;)- Repeats the foreground task indefinitely; each pass checks inputs, advances the calculation or services the peripheral shown below.uint32_t now = avr_tick_ms();- Use the linked public header for this function’s parameters, register ownership, blocking behavior and status values.if (avr_elapsed(now, led_start, 500UL))- This branch runs only after the requested interval has elapsed. Uses unsigned subtraction to test elapsed time safely across a 32-bit tick wraparound.led_start = now;- Stores this intermediate or output value for the next algorithm step. Check the units and range represented by each operand.(void)avr_gpio_toggle(13U);- Atomically toggles the selected pin output latch.if (avr_elapsed(now, report_start, 1000UL))- This branch runs only after the requested interval has elapsed. Uses unsigned subtraction to test elapsed time safely across a 32-bit tick wraparound.report_start = now;- Stores this intermediate or output value for the next algorithm step. Check the units and range represented by each operand.(void)avr_uart_u16((uint16_t)(now / 1000UL));- Formats an unsigned integer as decimal digits and sends them on USART0; it does not add a newline.(void)avr_uart_put('\n', AVR_POLL_BUDGET);- Waits for the USART transmit buffer within its poll budget, then writes the byte to UDR0.
Algorithm: The ISR increments a tick; foreground code snapshots it atomically. Unsigned subtraction remains valid across wraparound for the documented interval range. Expected behaviour: LED toggles twice per second while elapsed seconds print independently HAL source files in this repository: ATmega328P HAL on GitHub
include/avr_hal/gpio.hinclude/avr_hal/tick.hinclude/avr_hal/uart.hlib/interrupts/tick.csrc/gpio.csrc/uart.c