0
0
Embedded Cprogramming~20 mins

Why interrupts are needed in Embedded C - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Interrupt Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use interrupts instead of polling?

In embedded systems, why are interrupts preferred over polling for handling external events?

AInterrupts allow the CPU to perform other tasks and respond immediately when an event occurs.
BInterrupts slow down the CPU because they require constant checking of event flags.
CPolling uses less power than interrupts because it checks events continuously.
DPolling is better because it guarantees no events are missed.
Attempts:
2 left
💡 Hint

Think about how the CPU can do other work while waiting for an event.

Predict Output
intermediate
2:00remaining
Output of interrupt-driven vs polling code

What will be the output of the following embedded C code snippets?

Assume an external button press triggers an interrupt.

Embedded C
/* 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");
        }
    }
}
ABoth print 'Button pressed!' repeatedly while button is pressed.
BInterrupt-driven never prints; polling prints once per press.
CBoth print 'Button pressed!' only once per press.
DInterrupt-driven prints 'Button pressed!' once per press; polling prints repeatedly while pressed.
Attempts:
2 left
💡 Hint

Consider how often the print statement runs in each method.

🔧 Debug
advanced
2:00remaining
Identify the problem with this interrupt handler

What issue does this interrupt handler code have?

Embedded C
volatile int count = 0;
void ISR_timer() {
    count++;
    delay(1000); // Delay inside interrupt
}
int main() {
    setup_timer_interrupt(ISR_timer);
    while(1) {
        // main loop
    }
}
AInterrupt handler is missing a return statement.
BIncrementing count inside interrupt causes data corruption.
CDelay inside interrupt blocks other interrupts and main code, causing system lag.
DTimer interrupt is not enabled properly.
Attempts:
2 left
💡 Hint

Think about what happens if the interrupt takes too long.

📝 Syntax
advanced
2:00remaining
Correct syntax for declaring an interrupt service routine (ISR)

Which option shows the correct way to declare an ISR for a button press in embedded C?

Avoid ISR_button(void) __interrupt { /* code */ }
Bvoid __interrupt() ISR_button(void) { /* code */ }
Cvoid ISR_button() interrupt { /* code */ }
Dinterrupt void ISR_button() { /* code */ }
Attempts:
2 left
💡 Hint

Look for the correct placement of the interrupt keyword and function signature.

🚀 Application
expert
3:00remaining
Design a system using interrupts to handle multiple sensors

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?

AUse separate ISRs for each sensor, set flags in each ISR, and process all flags in the main loop.
BUse one ISR for all sensors and process all sensor data inside the ISR directly.
CDisable interrupts and poll sensors continuously in the main loop instead of using ISRs.
DUse one ISR that ignores sensor events and processes data only in the main loop.
Attempts:
2 left
💡 Hint

Consider how to keep ISRs short and avoid blocking.