What if your program could stop wasting time waiting and only act exactly when needed?
Polling vs interrupt-driven execution in Embedded C - When to Use Which
Imagine you are watching a pot on the stove, checking every few seconds if the water is boiling. You keep staring at the pot, unable to do anything else until it boils.
This constant checking wastes your time and attention. You might miss other important tasks or slow down your work because you are stuck waiting and watching.
Interrupt-driven execution is like having a timer that rings when the water boils. You can do other things freely, and only when the timer rings do you stop and check the pot. This saves time and makes your program more efficient.
while(1) { if (device_ready()) { handle_device(); } }
void ISR_device() {
handle_device();
}
// main program continues without waitingIt allows your program to respond immediately to important events without wasting time constantly checking.
In a traffic light system, instead of constantly checking if a car is waiting, sensors trigger an interrupt to change the light only when needed, saving energy and improving traffic flow.
Polling wastes time by constantly checking for events.
Interrupts let the system react only when needed.
This improves efficiency and responsiveness in embedded systems.
Practice
Solution
Step 1: Understand polling concept
Polling means the program continuously checks if an event has happened by running a loop.Step 2: Compare options
Polling repeatedly checks for an event in a loop. correctly states polling as repeated checking. Options A, B, and C describe interrupts or incorrect facts.Final Answer:
Polling repeatedly checks for an event in a loop. -> Option AQuick Check:
Polling = repeated checking [OK]
- Confusing polling with interrupts
- Thinking polling is automatic
- Assuming polling disables interrupts
Solution
Step 1: Recall ISR declaration syntax
In many embedded C compilers, the keyword __interrupt before the function declares it as an ISR.Step 2: Check options
__interrupt void ISR() { /* code */ } uses __interrupt correctly. Options A, B, and D use incorrect or incomplete syntax.Final Answer:
__interrupt void ISR() { /* code */ } -> Option BQuick Check:
ISR declaration = __interrupt keyword [OK]
- Omitting __interrupt keyword
- Placing interrupt keyword after void
- Using plain void without interrupt
while(1) {
if (button_pressed()) {
toggle_led();
}
}
What happens when the button is pressed?Solution
Step 1: Analyze the polling loop
The loop continuously checks if button_pressed() returns true, then calls toggle_led() immediately.Step 2: Understand behavior on button press
When the button is pressed, button_pressed() becomes true, so toggle_led() runs right away.Final Answer:
The LED toggles immediately when the button is pressed. -> Option CQuick Check:
Polling triggers action immediately on event [OK]
- Thinking toggle waits for loop end
- Confusing polling with interrupts
- Assuming interrupts are required
void ISR() {
toggle_led();
}
int main() {
enable_interrupts();
while(1) {}
}
What is the main problem?Solution
Step 1: Check ISR declaration
The ISR function lacks the __interrupt keyword, so it won't be recognized as an interrupt handler.Step 2: Review other code parts
enable_interrupts() is correctly called once; toggle_led() can be called in ISR; infinite loop is normal in embedded main.Final Answer:
ISR is missing the __interrupt keyword. -> Option DQuick Check:
ISR needs __interrupt keyword [OK]
- Forgetting __interrupt keyword
- Thinking infinite loop is wrong
- Assuming toggle_led() is invalid in ISR
Solution
Step 1: Understand the requirement
The program should react only when the sensor changes and avoid wasting CPU time on constant checking.Step 2: Compare methods
Polling (options B, C, D) wastes CPU by checking repeatedly. Interrupt (Use an interrupt triggered by the sensor change.) triggers only on change, saving CPU.Final Answer:
Use an interrupt triggered by the sensor change. -> Option AQuick Check:
Interrupts save CPU by reacting only on events [OK]
- Choosing polling for efficiency
- Using delays wastes CPU
- Confusing timer polling with interrupts
Ready to prove it?
Pass the test and this pattern moves unknown
