Why interrupts are needed in Embedded C - Performance Analysis
We want to understand how the use of interrupts affects the time a program spends waiting or checking for events.
How does using interrupts change the way the program spends its time compared to checking repeatedly?
Analyze the time complexity of the following code snippet.
// Polling example without interrupts
while(1) {
if (button_pressed()) {
handle_button();
}
}
This code keeps checking if a button is pressed in a continuous loop, handling it when detected.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The infinite loop that checks the button state repeatedly.
- How many times: This check happens continuously, as fast as the processor can run.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 (events) | Many checks, mostly waiting, until 10 events happen |
| 100 (events) | Many more checks, still mostly waiting |
| 1000 (events) | Even more checks, time spent mostly on waiting |
Pattern observation: The program spends a lot of time checking even when nothing happens, so the work grows with time, not just events.
Time Complexity: O(n)
This means the program spends time checking repeatedly, growing linearly with the number of checks, even if events are rare.
[X] Wrong: "Checking repeatedly doesn't cost much time because the processor is fast."
[OK] Correct: Even if fast, the processor wastes time checking instead of doing other useful work or sleeping.
Understanding how interrupts reduce wasted time helps you explain efficient program design, a key skill in embedded systems.
"What if we replaced the polling loop with an interrupt-driven approach? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of interrupts
Interrupts let the program stop what it is doing and handle urgent tasks immediately.Step 2: Compare options
Only To allow the program to respond quickly to important events correctly states that interrupts help respond quickly to important events.Final Answer:
To allow the program to respond quickly to important events -> Option AQuick Check:
Interrupts = Quick response [OK]
- Thinking interrupts slow down the program
- Confusing interrupts with normal function calls
- Believing interrupts increase program size unnecessarily
Solution
Step 1: Recall ISR declaration syntax
In embedded C, ISRs are declared with the __interrupt keyword and the interrupt vector number in parentheses.Step 2: Identify correct option
void ISR_name(void) __interrupt(0); uses __interrupt(0), which is the correct form for declaring ISR with vector 0.Final Answer:
void ISR_name(void) __interrupt(0); -> Option CQuick Check:
ISR syntax = __interrupt(vector) [OK]
- Omitting the vector number in __interrupt
- Using interrupt without __
- Missing void parameter list
volatile int flag = 0;
void ISR_timer(void) __interrupt(1) {
flag = 1;
}
int main() {
while(flag == 0) {
// wait for interrupt
}
return 0;
}
What is the role of the interrupt here?Solution
Step 1: Analyze ISR function
The ISR sets the variable flag to 1 when the interrupt occurs.Step 2: Understand main loop behavior
Main waits in a loop until flag becomes 1, meaning it waits for the interrupt to happen.Final Answer:
To set flag to 1 when timer interrupt occurs -> Option AQuick Check:
ISR sets flag = 1 [OK]
- Thinking flag resets to 0 automatically
- Assuming interrupt stops program
- Confusing syntax error with correct code
void ISR_button() interrupt 2 {
// handle button press
}Solution
Step 1: Check ISR declaration syntax
In embedded C, the interrupt keyword must be preceded by double underscores: __interrupt.Step 2: Verify other parts
ISR functions return void and have no parameters, which is correct here. Interrupt number 2 is valid.Final Answer:
Missing __ before interrupt keyword -> Option DQuick Check:
ISR syntax needs __interrupt [OK]
- Using single underscore or no underscore
- Changing return type from void
- Adding parameters to ISR
Solution
Step 1: Understand multitasking with interrupts
Interrupts allow the program to stop current tasks and quickly handle sensor events.Step 2: Compare options
Interrupts pause other tasks and immediately run sensor code when triggered correctly describes how interrupts improve responsiveness by pausing other tasks to run sensor code immediately.Final Answer:
Interrupts pause other tasks and immediately run sensor code when triggered -> Option BQuick Check:
Interrupts = immediate sensor handling [OK]
- Thinking interrupts slow down program
- Believing interrupts disable sensors
- Assuming manual waiting is needed
Ready to prove it?
Pass the test and this pattern moves unknown
