0
0
Embedded Cprogramming~20 mins

Timer overflow behavior in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Timer Overflow Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output when a 8-bit timer overflows?
Consider an 8-bit timer that increments every millisecond and resets to 0 after reaching 255. What will be the value of the timer after 260 milliseconds?
Embedded C
uint8_t timer = 0;
for (int i = 0; i < 260; i++) {
    timer++;
}
printf("%u", timer);
A260
B4
C0
D255
Attempts:
2 left
💡 Hint
Think about how an 8-bit unsigned integer behaves when it exceeds its max value.
🧠 Conceptual
intermediate
1:30remaining
Why does a timer overflow cause an interrupt?
In embedded systems, why is a timer overflow often used to trigger an interrupt?
ABecause the timer overflow indicates the timer reached its maximum count and can be used to perform periodic tasks.
BBecause the timer overflow resets the CPU clock speed automatically.
CBecause the timer overflow disables all other interrupts.
DBecause the timer overflow clears the memory registers.
Attempts:
2 left
💡 Hint
Think about what happens when the timer reaches its max count and why that event is useful.
🔧 Debug
advanced
2:30remaining
Identify the cause of incorrect timer overflow handling
This code is intended to toggle an LED every time the 16-bit timer overflows. What is the bug causing the LED not to toggle?
Embedded C
volatile uint16_t timer = 0;
volatile uint8_t led_state = 0;

void timer_overflow_isr() {
    if (timer == 0xFFFF) {
        led_state = !led_state;
        timer = 0;
    }
    timer++;
}
AThe ISR should not modify the timer variable.
BThe led_state variable should be uint16_t, not uint8_t.
CThe timer should be reset to 0xFFFF instead of 0 after overflow.
DThe timer increments after checking for overflow, so the overflow condition is never true.
Attempts:
2 left
💡 Hint
Consider the order of increment and overflow check.
📝 Syntax
advanced
2:00remaining
Which code snippet correctly handles 8-bit timer overflow?
Select the code snippet that correctly detects an 8-bit timer overflow and toggles an LED.
Atimer++; if (timer == 0) { led_state = !led_state; }
Bif (timer++ == 255) { led_state = !led_state; timer = 0; }
Cif (++timer == 255) { led_state = !led_state; timer = 0; }
Dif (timer == 255) { led_state = !led_state; timer = 0; } timer++;
Attempts:
2 left
💡 Hint
Remember that an 8-bit unsigned integer overflows from 255 to 0.
🚀 Application
expert
3:00remaining
Calculate timer overflow interval for a 16-bit timer
A 16-bit timer runs at a clock frequency of 1 MHz and increments every clock cycle. How long in milliseconds does it take for the timer to overflow?
A0.065536 ms
B65536 ms
C65.536 ms
D1 ms
Attempts:
2 left
💡 Hint
Calculate the time for 65536 counts at 1 MHz clock.