Timer overflow behavior in Embedded C - Time & Space Complexity
We want to understand how the time it takes for a timer to overflow changes as the timer counts up.
How does the timer's counting steps affect when it resets?
Analyze the time complexity of the following code snippet.
volatile unsigned int timer = 0;
void timer_tick() {
timer++;
if (timer == 0) { // overflow occurs
// handle overflow event
}
}
This code increments a timer variable and checks when it overflows back to zero.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Incrementing the timer variable by one.
- How many times: This happens once every timer tick, repeatedly until overflow.
As the timer counts up, the number of increments before overflow grows with the timer's size.
| Input Size (Timer max value) | Approx. Increments before Overflow |
|---|---|
| 10 | 10 increments |
| 100 | 100 increments |
| 1000 | 1000 increments |
Pattern observation: The increments needed to overflow grow linearly with the timer's maximum value.
Time Complexity: O(n)
This means the time until overflow grows directly in proportion to the timer's maximum count.
[X] Wrong: "The timer overflows instantly or after a fixed short time regardless of its size."
[OK] Correct: The timer must count through all values up to its max before overflowing, so bigger timers take longer.
Understanding timer overflow helps you reason about how embedded systems handle time and events, a useful skill in many real projects.
"What if the timer increments by 2 instead of 1 each tick? How would the time complexity change?"
Practice
Solution
Step 1: Understand timer counting behavior
Timers count up from zero to a maximum value based on their bit size.Step 2: Identify what happens at max value
When the timer reaches its max, it resets to zero and continues counting, called overflow.Final Answer:
It resets to zero and continues counting (overflow). -> Option CQuick Check:
Timer overflow means reset to zero [OK]
- Thinking timer stops at max value
- Assuming overflow causes microcontroller reset
- Confusing overflow with syntax errors
Solution
Step 1: Understand syntax for condition check
In C, conditions use double equals '==' to compare values inside parentheses.Step 2: Identify correct overflow flag check
Checking if TIMER_FLAG equals 1 means overflow occurred, so use 'if (TIMER_FLAG == 1)'.Final Answer:
if (TIMER_FLAG == 1) { /* handle overflow */ } -> Option BQuick Check:
Use '==' for comparison in if conditions [OK]
- Using single '=' instead of '==' in if condition
- Checking for zero instead of one for overflow
- Missing parentheses around condition
overflow_count after the timer overflows twice?volatile uint8_t timer = 254;
volatile uint8_t overflow_count = 0;
void timer_ISR() {
timer++;
if (timer == 0) {
overflow_count++;
}
}Solution
Step 1: Understand timer increment and overflow
Timer starts at 254, increments to 255, then overflows to 0, triggering overflow_count++.Step 2: Count overflows after two increments
After two overflows (timer wraps to 0 twice), overflow_count increments twice, so value is 2.Final Answer:
2 -> Option AQuick Check:
Two overflows increment count twice [OK]
- Confusing timer value with overflow count
- Forgetting overflow increments counter
- Assuming overflow_count resets with timer
volatile uint8_t timer = 0;
volatile uint8_t overflow_flag = 0;
void timer_ISR() {
timer++;
if (timer = 0) {
overflow_flag = 1;
}
}Solution
Step 1: Check if condition syntax
The condition uses '=' which assigns 0 to timer instead of comparing it.Step 2: Correct comparison operator
It should use '==' to check if timer equals 0 for overflow detection.Final Answer:
Using assignment '=' instead of comparison '==' in if condition. -> Option DQuick Check:
Use '==' for comparisons, not '=' [OK]
- Confusing '=' with '==' in conditions
- Assuming timer type causes error here
- Ignoring missing flag reset logic
Solution
Step 1: Understand timer max count limitation
An 8-bit timer counts from 0 to 255, then overflows back to 0.Step 2: Use overflow count to extend timing range
Counting overflows lets you track how many times timer wrapped, extending measurable time.Step 3: Calculate total time
Total count = (overflow_count * 256) + current timer value, giving full interval.Final Answer:
Count the number of overflows in an interrupt and multiply by 256 plus current timer value. -> Option AQuick Check:
Overflow count extends timer range [OK]
- Resetting timer manually loses overflow info
- Increasing frequency doesn't extend max count
- Ignoring overflow loses timing accuracy
Ready to prove it?
Pass the test and this pattern moves unknown
