In embedded systems, why are interrupts preferred over polling for handling external events?
Think about how the CPU can do other work while waiting for an event.
Interrupts let the CPU work on other tasks and only stop to handle events when they happen, making the system more efficient.
What will be the output of the following embedded C code snippets?
Assume an external button press triggers an interrupt.
/* Interrupt-driven code snippet */ volatile int button_pressed = 0; void ISR_button() { button_pressed = 1; } int main() { while(1) { if(button_pressed) { printf("Button pressed!\n"); button_pressed = 0; } } } /* Polling code snippet */ int main() { while(1) { if(read_button_pin() == PRESSED) { printf("Button pressed!\n"); } } }
Consider how often the print statement runs in each method.
Interrupt-driven code sets a flag once per press, printing once. Polling checks continuously and prints repeatedly while the button is held.
What issue does this interrupt handler code have?
volatile int count = 0; void ISR_timer() { count++; delay(1000); // Delay inside interrupt } int main() { setup_timer_interrupt(ISR_timer); while(1) { // main loop } }
Think about what happens if the interrupt takes too long.
Delays inside interrupts block the CPU from handling other tasks or interrupts, causing slow or unresponsive behavior.
Which option shows the correct way to declare an ISR for a button press in embedded C?
Look for the correct placement of the interrupt keyword and function signature.
In many embedded C compilers, the ISR is declared with void __interrupt() before the function name.
You have three sensors connected to an embedded system. Each sensor triggers an interrupt when it detects an event. How should you design the interrupt handling to efficiently process events from all sensors?
Consider how to keep ISRs short and avoid blocking.
Separate ISRs set flags quickly, letting the main loop handle processing without blocking interrupts, improving responsiveness.