Bird
Raised Fist0
Embedded Cprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1.

What is the main purpose of a timer interrupt in embedded C programming?

easy
A. To stop the main program permanently
B. To run code automatically at regular time intervals
C. To manually start a function when needed
D. To increase the speed of the processor

Solution

  1. Step 1: Understand timer interrupt functionality

    Timer interrupts trigger code execution automatically at set time intervals without manual intervention.
  2. Step 2: Compare options with timer interrupt purpose

    Only To run code automatically at regular time intervals describes automatic, regular code execution, which matches timer interrupt use.
  3. Final Answer:

    To run code automatically at regular time intervals -> Option B
  4. Quick Check:

    Timer interrupt = automatic periodic code run [OK]
Hint: Timer interrupts run code regularly without manual calls [OK]
Common Mistakes:
  • Thinking timer interrupts stop the main program
  • Confusing manual function calls with interrupts
  • Assuming timer interrupts speed up the processor
2.

Which of the following is the correct way to enable a timer interrupt in embedded C?

void setup() {
    // Enable timer interrupt here
}
easy
A. TIMSK |= (1 << TOIE0);
B. TIMSK = 0;
C. TCCR0 = 0;
D. PORTB |= (1 << PB0);

Solution

  1. Step 1: Identify register for timer interrupt enable

    TIMSK register controls timer interrupt enable bits; setting TOIE0 bit enables Timer0 overflow interrupt.
  2. Step 2: Analyze each option

    TIMSK |= (1 << TOIE0); sets TOIE0 bit correctly. TIMSK = 0; clears TIMSK disabling interrupts. TCCR0 = 0; sets timer control register, not interrupt enable. PORTB |= (1 << PB0); sets port pin, unrelated to interrupts.
  3. Final Answer:

    TIMSK |= (1 << TOIE0); -> Option A
  4. Quick Check:

    Enable timer interrupt = set TIMSK TOIE0 bit [OK]
Hint: Enable timer interrupt by setting TIMSK TOIE0 bit [OK]
Common Mistakes:
  • Clearing TIMSK disables interrupts
  • Confusing timer control register with interrupt enable
  • Setting port pins instead of interrupt bits
3.

What will be the output of the following code snippet if the timer interrupt triggers every 1 second and increments count?

volatile int count = 0;

ISR(TIMER0_OVF_vect) {
    count++;
}

int main() {
    setup_timer_interrupt();
    while(count < 3) {}
    printf("Count: %d", count);
    return 0;
}
medium
A. Count: 5
B. Count: 0
C. Count: 1
D. Count: 3

Solution

  1. Step 1: Understand interrupt increments

    Each timer interrupt increments count by 1 every 1 second.
  2. Step 2: Analyze loop condition and output

    Main loop waits until count reaches 3, then prints count. So output is "Count: 3".
  3. Final Answer:

    Count: 3 -> Option D
  4. Quick Check:

    count increments 3 times before print [OK]
Hint: Loop waits for count 3, so output is Count: 3 [OK]
Common Mistakes:
  • Assuming count stays 0 without interrupt
  • Printing before count reaches 3
  • Confusing count increments with time
4.

Identify the error in the following code that prevents the timer interrupt from working properly:

volatile int flag = 0;

ISR(TIMER1_COMPA_vect) {
    flag = 1;
}

int main() {
    setup_timer1();
    while(flag == 0) {
        // wait
    }
    // do something
    return 0;
}
medium
A. The ISR vector name is incorrect for Timer1 Compare A interrupt
B. The flag variable should not be volatile
C. The setup_timer1() function is missing enabling the interrupt
D. The while loop condition should be flag != 0

Solution

  1. Step 1: Check ISR vector correctness

    ISR vector TIMER1_COMPA_vect is correct for Timer1 Compare A interrupt.
  2. Step 2: Verify interrupt enabling in setup

    If setup_timer1() does not enable the interrupt, ISR never triggers, so flag stays 0.
  3. Final Answer:

    setup_timer1() function is missing enabling the interrupt -> Option C
  4. Quick Check:

    Interrupt must be enabled in setup for ISR to run [OK]
Hint: Always enable interrupt in setup function [OK]
Common Mistakes:
  • Assuming ISR vector name is wrong
  • Removing volatile from flag
  • Changing loop condition incorrectly
5.

You want to toggle an LED every 500ms using a timer interrupt on an 8-bit microcontroller with a 1MHz clock. Which approach correctly sets up the timer interrupt for this periodic task?

Options:
  1. Set timer prescaler to 64, preload timer to 131, enable overflow interrupt.
  2. Set timer prescaler to 256, preload timer to 0, enable compare match interrupt at 500.
  3. Set timer prescaler to 8, preload timer to 0, enable overflow interrupt.
  4. Set timer prescaler to 1024, preload timer to 0, enable overflow interrupt.
hard
A. Set timer prescaler to 64, preload timer to 131, enable overflow interrupt.
B. Set timer prescaler to 256, preload timer to 0, enable compare match interrupt at 500.
C. Set timer prescaler to 8, preload timer to 0, enable overflow interrupt.
D. Set timer prescaler to 1024, preload timer to 0, enable overflow interrupt.

Solution

  1. Step 1: Calculate timer counts for 500ms with 1MHz clock

    Timer tick = prescaler / clock = 64 / 1,000,000 = 64us. Counts needed = 500ms / 64us = 7812.5 counts.
  2. Step 2: Use 8-bit timer overflow with preload

    8-bit timer max count = 256. Number of overflows = 7812.5 / 256 ≈ 30.5. Preload = 256 - (7812.5 % 256) = 256 - 132.5 ≈ 123.5 (131 preload with prescaler 64 is closest practical setup).
  3. Step 3: Analyze options

    Set timer prescaler to 64, preload timer to 131, enable overflow interrupt. uses prescaler 64 and preload 131, which is a common approach to adjust overflow timing. Other options either use wrong prescaler or compare match without correct value.
  4. Final Answer:

    Set timer prescaler to 64, preload timer to 131, enable overflow interrupt. -> Option A
  5. Quick Check:

    Prescaler 64 + preload adjusts overflow for 500ms toggle [OK]
Hint: Use prescaler and preload to match 500ms timer overflow [OK]
Common Mistakes:
  • Ignoring timer resolution and prescaler effect
  • Using wrong interrupt type for timing
  • Not calculating preload for overflow timing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard