Challenge - 5 Problems
Timer Interrupt Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Timer Interrupt Handler Increment
What is the output of the following embedded C code snippet simulating a timer interrupt incrementing a counter?
Embedded C
#include <stdio.h> volatile int counter = 0; void Timer_ISR() { counter++; } int main() { for(int i = 0; i < 5; i++) { Timer_ISR(); } printf("Counter: %d\n", counter); return 0; }
Attempts:
2 left
💡 Hint
Think about how many times the interrupt function is called.
✗ Incorrect
The Timer_ISR function is called 5 times in the loop, each time incrementing the counter by 1, so the final value is 5.
🧠 Conceptual
intermediatePurpose of volatile keyword in timer interrupt variables
Why is the
volatile keyword important for variables shared between the main program and a timer interrupt handler?Attempts:
2 left
💡 Hint
Think about how the compiler might optimize code when it doesn't expect a variable to change unexpectedly.
✗ Incorrect
The volatile keyword tells the compiler that the variable can change at any time, such as inside an interrupt, so it must always read the variable from memory and not cache it in a register.
🔧 Debug
advancedIdentify the bug in timer interrupt setup
What is the bug in this timer interrupt setup code that causes the interrupt not to trigger periodically?
Embedded C
#include <avr/io.h> #include <avr/interrupt.h> volatile int tick = 0; void timer_init() { TCCR0A = 0; TCCR0B = (1 << CS02) | (1 << CS01); // Invalid prescaler (incorrect) TIMSK0 = (1 << TOIE0); sei(); } ISR(TIMER0_OVF_vect) { tick++; } int main() { timer_init(); while(1) { // main loop } }
Attempts:
2 left
💡 Hint
Check the prescaler bits and their valid values for the timer control register.
✗ Incorrect
The prescaler bits set in TCCR0B do not correspond to a valid prescaler value. The combination (1 << CS02) | (1 << CS01) sets bits 2 and 1, which is not a valid prescaler setting for AVR timers (selects external clock source). This prevents the timer from counting correctly and the interrupt from triggering.
📝 Syntax
advancedSyntax error in timer interrupt declaration
Which option correctly declares a timer interrupt service routine (ISR) for Timer1 overflow in AVR C?
Attempts:
2 left
💡 Hint
Look for the special macro used to define ISRs in AVR GCC.
✗ Incorrect
In AVR GCC, the ISR macro with the interrupt vector name is used to declare interrupt handlers. Other forms are invalid syntax or not recognized as ISRs.
🚀 Application
expertCalculate timer overflow frequency
Given an 8-bit timer with a 16 MHz clock and a prescaler of 256, what is the frequency (in Hz) at which the TIMER0 overflow interrupt triggers?
Attempts:
2 left
💡 Hint
Calculate timer tick frequency first, then find overflow frequency using 256 counts for 8-bit timer.
✗ Incorrect
Timer tick frequency = CPU frequency / prescaler = 16,000,000 / 256 = 62,500 Hz. The timer overflows after 256 ticks, so overflow frequency = 62,500 / 256 ≈ 244 Hz.
