Generating precise delays with timers in Embedded C - Time & Space Complexity
When using timers to create delays in embedded C, it's important to understand how the time the program takes grows as the delay length changes.
We want to know how the program's running time changes when we ask for longer or shorter delays.
Analyze the time complexity of the following code snippet.
void delay_ms(int ms) {
for (int i = 0; i < ms; i++) {
for (int j = 0; j < 1000; j++) {
// empty loop to waste time
}
}
}
This code creates a delay by running two nested loops: the outer loop runs once per millisecond, and the inner loop runs 1000 times to waste time.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The inner empty loop that runs 1000 times.
- How many times: The inner loop runs 1000 times for each iteration of the outer loop, which runs ms times.
As the delay time ms increases, the total number of loop operations grows proportionally.
| Input Size (ms) | Approx. Operations |
|---|---|
| 10 | 10 x 1000 = 10,000 |
| 100 | 100 x 1000 = 100,000 |
| 1000 | 1000 x 1000 = 1,000,000 |
Pattern observation: The total operations increase directly with the delay time requested.
Time Complexity: O(n)
This means the time the program takes grows in a straight line with the delay length you ask for.
[X] Wrong: "The delay time does not affect how long the program runs because the loops are fixed."
[OK] Correct: The outer loop runs once per millisecond, so increasing the delay increases the total loops and time proportionally.
Understanding how delay loops scale helps you reason about timing and efficiency in embedded systems, a key skill for writing reliable code.
"What if we replaced the inner loop with a hardware timer interrupt instead? How would the time complexity change?"
Practice
Solution
Step 1: Understand timer functionality
Timers count clock pulses to measure time intervals precisely without busy-waiting.Step 2: Identify timer use for delays
Using timers allows the program to wait exact times while freeing CPU for other tasks.Final Answer:
To create precise and accurate wait times without blocking the CPU -> Option AQuick Check:
Timers = precise delays [OK]
- Thinking timers speed up the CPU
- Confusing timers with memory storage
- Assuming timers handle user input
Solution
Step 1: Recognize common timer function names
Standard embedded C libraries often usetimer_start()to begin timers.Step 2: Check syntax correctness
timer_start();is a valid function call matching typical timer APIs.Final Answer:
timer_start(); -> Option CQuick Check:
Correct timer start syntax = timer_start() [OK]
- Using incorrect function names like start_timer()
- Confusing initialization with starting
- Missing parentheses in function calls
timer_set(1000); // Set timer for 1000 ms
timer_start();
while(!timer_expired()) {
// wait
}
LED_toggle();What happens after this code runs?
Solution
Step 1: Understand timer_set and timer_start
The timer is set for 1000 milliseconds and then started to count down.Step 2: Analyze the while loop waiting for timer expiration
The loop waits untiltimer_expired()returns true, which happens after ~1 second.Final Answer:
LED toggles after approximately 1 second delay -> Option BQuick Check:
timer_set(1000) = 1 second delay [OK]
- Assuming timer_expired() never becomes true
- Confusing CPU cycles with milliseconds
- Thinking LED toggles immediately
timer_set(500);
while(timer_expired()) {
// wait
}
LED_on();Solution
Step 1: Analyze the while loop condition
The loop waits whiletimer_expired()is true, but timer just set, so expired is false initially.Step 2: Correct the condition to wait until timer expires
It should wait while timer is NOT expired, so condition must be negated:!timer_expired().Final Answer:
The while condition should be !timer_expired() to wait correctly -> Option DQuick Check:
Wait while not expired = !timer_expired() [OK]
- Using timer_expired() without negation in wait loop
- Calling timer_set() after waiting
- Assuming LED_on() order affects delay
Solution
Step 1: Understand precise timing with interrupts
Using timer interrupts allows exact timing without blocking CPU.Step 2: Identify best place to toggle LED
Toggling LED inside the timer ISR ensures it happens exactly every 250 ms.Final Answer:
Use a timer interrupt set for 250 ms to toggle LED inside ISR -> Option AQuick Check:
Timer ISR toggles LED precisely every 250 ms [OK]
- Using busy-wait loops causing CPU blocking
- Toggling LED outside ISR losing timing precision
- Not using timer interrupts for periodic tasks
Ready to prove it?
Pass the test and this pattern moves unknown
