0
0
Embedded Cprogramming~20 mins

Timer interrupt for periodic tasks in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Timer Interrupt Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output 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;
}
ACounter: 5
BCounter: 0
CCounter: 4
DCounter: 1
Attempts:
2 left
💡 Hint
Think about how many times the interrupt function is called.
🧠 Conceptual
intermediate
1:30remaining
Purpose 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?
AIt automatically initializes the variable to zero before use.
BIt makes the variable read-only to prevent accidental changes.
CIt prevents the compiler from optimizing out accesses to the variable, ensuring the latest value is always read.
DIt allows the variable to be stored in faster CPU registers.
Attempts:
2 left
💡 Hint
Think about how the compiler might optimize code when it doesn't expect a variable to change unexpectedly.
🔧 Debug
advanced
2:30remaining
Identify 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
    }
}
Asei() should be called before timer_init() to enable interrupts.
Btick variable should not be volatile.
CInterrupt vector name TIMER0_OVF_vect is incorrect for this MCU.
DPrescaler bit combination is invalid; valid prescalers are powers of two or specific values like 1024.
Attempts:
2 left
💡 Hint
Check the prescaler bits and their valid values for the timer control register.
📝 Syntax
advanced
1:30remaining
Syntax error in timer interrupt declaration
Which option correctly declares a timer interrupt service routine (ISR) for Timer1 overflow in AVR C?
Avoid TIMER1_OVF_vect() { /* handler code */ }
BISR(TIMER1_OVF_vect) { /* handler code */ }
Cinterrupt TIMER1_OVF_vect() { /* handler code */ }
Dvoid ISR_TIMER1_OVF() { /* handler code */ }
Attempts:
2 left
💡 Hint
Look for the special macro used to define ISRs in AVR GCC.
🚀 Application
expert
3:00remaining
Calculate 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?
AApproximately 244 Hz
BApproximately 62500 Hz
CApproximately 16000000 Hz
DApproximately 256 Hz
Attempts:
2 left
💡 Hint
Calculate timer tick frequency first, then find overflow frequency using 256 counts for 8-bit timer.