Skip to content
University of California, Davis Mechanical and Aerospace Engineering
ARMS Laboratory
EME154 · Interactive Lab

A timer event. A change of flow.

Follow Timer1 from a compare match to an interrupt, then back to the main program.

Step advances to the next teaching phase. Main-program spans show intermediate counter values and pause at TCNT1 = OCR1A, then at the following timer tick. These observation pauses can occur within an instruction. Animation speed does not change simulated time.

Compare the times

Applied settings · 16 MHz clock ·

Timer compare-event interval
ISR body duration

Reaching OCR1A takes OCR1A ticks from zero. The flag is set on the next tick, so compare events are OCR1A + 1 ticks apart. ISR body time excludes interrupt entry, the vector jump, and RETI.

TCNT1 · current count
≠
OCR1A · compare valueFixed until Apply & reset
→
TIFR1.OCF1A
+
OCIE1A & I
→
CPU phase

Main program

The highlighted line is the next instruction. Each counting step summarizes many passes through this loop.

Illustrative work blocks, each modeled as a 4-cycle instruction. Addresses are program words.

Interrupt service routine

ISR output · LED

Return address

Conceptual stack entry holds the saved PC. Hardware stores the return address as bytes.

Simulated time
Compare events
ISR entries
Events while already pending

Timer and CPU share the same clock

Recent time window. Amber marks compare boundaries; green is main work, blue is ISR handling. Empty regions precede the retained trace.

Arduino setup for the applied settings

Register setup excerpt. ISR timing above is an explicit simulation setting, not a compiler timing estimate. This takes ownership of Timer1.

Recent execution trace
    What this draft models

    Timer1 CTC with OCR1A as TOP, a sticky compare A flag, the interrupt mask and global enable, saving the return PC, interrupt entry, vector jump, ISR work, and RETI. A pending flag is one bit, not an event queue: additional matches while it is already set do not accumulate additional requests.

    The default example uses /8 and OCR1A = 12499: (12499 + 1) × 8 / 16 MHz = 6.25 ms. The interval includes counter value zero. Flag clearing occurs when its interrupt is serviced, not merely because interrupts are enabled.

    This is a guided execution model, not an Arduino compiler or a complete AVR emulator. Main work uses illustrative 4-cycle instructions. Equality pauses preserve partial instruction progress; interrupt acceptance waits for the instruction to finish. Entry is modeled as 4 cycles and the vector JMP as 3 cycles. ISR duration is user-specified and includes any intended compiler save/restore overhead before the separately modeled 4-cycle RETI. Exact SEI timing, live timer-register writes, other interrupt sources, nesting, and SREG/general-register preservation are not simulated. Global-enable changes are teaching controls at phase boundaries.

    ATmega328P hardware reference.