Challenge - 5 Problems
Timer Overflow Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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);
Attempts:
2 left
💡 Hint
Think about how an 8-bit unsigned integer behaves when it exceeds its max value.
✗ Incorrect
An 8-bit unsigned integer can hold values from 0 to 255. After 255, it wraps around to 0. Incrementing 260 times means it overflows once (256 counts) and then counts 4 more, so the timer value is 4.
🧠 Conceptual
intermediate1:30remaining
Why does a timer overflow cause an interrupt?
In embedded systems, why is a timer overflow often used to trigger an interrupt?
Attempts:
2 left
💡 Hint
Think about what happens when the timer reaches its max count and why that event is useful.
✗ Incorrect
Timer overflow signals that a fixed time period has passed. This event can trigger an interrupt to run code periodically without constant CPU checking.
🔧 Debug
advanced2: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++; }
Attempts:
2 left
💡 Hint
Consider the order of increment and overflow check.
✗ Incorrect
The timer is checked for 0xFFFF before incrementing, so it never reaches 0xFFFF inside the ISR. The increment should happen before the check or the condition adjusted.
📝 Syntax
advanced2: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.
Attempts:
2 left
💡 Hint
Remember that an 8-bit unsigned integer overflows from 255 to 0.
✗ Incorrect
Incrementing timer first and then checking if it is zero detects overflow correctly. Option A increments timer, then checks if it wrapped to zero.
🚀 Application
expert3: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?
Attempts:
2 left
💡 Hint
Calculate the time for 65536 counts at 1 MHz clock.
✗ Incorrect
Timer counts from 0 to 65535 (65536 counts). At 1 MHz, each count is 1 microsecond. So overflow time = 65536 * 1 us = 65.536 ms.