Skip to content

Handle Timer Events With An Interrupt

Concept preview: Handle Timer Events With An Interrupt

Timer compare event sets a flag that the CPU can poll or service through one interrupt vector

A timer compare flag tells the program that a compare event occurred. If foreground code does not inspect the flag before another event, the same flag can remain set and the number of missed events is lost. Polling works when the foreground has a short, bounded loop; it is not a count of all elapsed compare matches.

Enabling the timer’s local compare interrupt lets the CPU run a handler when the event occurs. On the ATmega328P, the interrupt vector must have one owner. The timer configuration and the ISR are separate decisions: a peripheral driver should not silently claim a vector or enable global interrupts unless its documented contract says so.

The course scheduler’s TIMER0_COMPA_vect handler increments the millisecond tick. Foreground code reads that shared 32-bit value atomically and schedules LED and UART tasks. The ISR does not print, delay, wait on a peripheral or perform lengthy calculations.

If the foreground and ISR share data, use the correct ownership and atomicity rules. volatile makes accesses observable to the compiler; it does not make a multi-byte value atomic on an 8-bit CPU. See volatile and atomic access.

Build the scheduler example with XC8 or AVR GCC. It uses the optional Timer0 tick owner; do not combine it with the Arduino core’s Timer0 timebase. The program is software-build validated, but its physical timing has not been measured.

Why should UART printing happen in foreground code rather than in a timer ISR?

Answer: Sending text can wait for the UART and take many bit periods. That lengthens interrupt execution and delays other event handlers. The ISR should record the event; foreground code can format and transmit it.

Example code on GitHub:

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

The example’s main() configures its peripherals, runs the algorithm and reports or displays the result. These source operations are listed in execution order:

  • (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.
  • (void)avr_adc_init(AVR_ADC_AVCC); - Selects the reference in ADMUX and enables the ADC in ADCSRA with a divide-by-128 prescaler. At 16 MHz, the ADC clock is 125 kHz. The HAL keeps the result right-adjusted, waits for reference settling and leaves ADC interrupts disabled.
  • ADMUX = (uint8_t)(ADMUX & 0xF0U); - Clears the ADMUX channel-selection nibble to choose ADC0 while preserving REFS1:0 reference settings.
  • ADCSRA |= _BV(ADIF); - Writes one to ADIF to clear a pending ADC completion flag; ADIF is write-one-to-clear.
  • ADCSRA |= _BV(ADIE); - Sets ADIE so ADC conversion completion requests the configured ADC interrupt.
  • 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.
  • ADCSRA = (uint8_t)((ADCSRA & (uint8_t)~_BV(ADIF)) | _BV(ADSC)); - Controls ADC enable, prescaling, conversion start and optional interrupt. ADSC starts a conversion; ADIF is write-one-to-clear.
  • uint32_t budget = AVR_POLL_BUDGET; - Stores this intermediate or output value for the next algorithm step. Check the units and range represented by each operand.
  • while (ready == 0U && budget > 0UL) - Waits for this condition to change; inspect the nearby counter or budget to see the bound on blocking.
  • --budget; - This statement advances the operation sequence; follow its expression to see the state change.
  • uint16_t sample = 0U; - Clears the state/counter after its threshold is reached so the next interval starts from zero.
  • bool available = false; - Stores this intermediate or output value for the next algorithm step. Check the units and range represented by each operand.
  • if (ready != 0U) - Tests the stated condition; the following block handles the matching case and the other path is skipped.
  • sample = latest; - Stores this intermediate value for the next step of the algorithm; check the operands and units.
  • ready = 0U; - Clears the state/counter after its threshold is reached so the next interval starts from zero.
  • available = true; - Stores this intermediate or output value for the next algorithm step. Check the units and range represented by each operand.
  • if (available) - Tests the stated condition; the following block handles the matching case and the other path is skipped.
  • (void)avr_uart_u16(sample); - 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.
  • _delay_ms(100); - Waits for the stated interval; the foreground does no other work during this blocking delay.

Algorithm: The interrupt reports completion; it does not make the conversion faster. The two-byte sample and ready flag must be transferred consistently. Expected behaviour: Conversion ISR saves a result; foreground prints it HAL source files in this repository: ATmega328P HAL on GitHub