0
0
Embedded Cprogramming~20 mins

ISR best practices (keep it short) in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ISR Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Why keep ISRs short?

Why is it important to keep Interrupt Service Routines (ISRs) short in embedded C programming?

ATo enable ISRs to run in parallel with main code without synchronization
BTo minimize interrupt latency and avoid blocking other interrupts
CTo increase the stack size used by the ISR
DTo allow ISRs to perform complex calculations without delay
Attempts:
2 left
💡 Hint

Think about how long the CPU is busy inside an ISR and how it affects other tasks.

Predict Output
intermediate
1:30remaining
Output of ISR flag clearing code

What is the output of this embedded C code snippet after the ISR runs?

Embedded C
volatile int flag = 0;

void ISR_Handler() {
    flag = 1;
    // Clear interrupt flag here
}

int main() {
    flag = 0;
    ISR_Handler();
    if(flag == 1) {
        printf("Interrupt handled\n");
    } else {
        printf("No interrupt\n");
    }
    return 0;
}
ARuntime error due to flag not being volatile
BNo interrupt
CCompilation error due to missing interrupt attribute
DInterrupt handled
Attempts:
2 left
💡 Hint

Consider what the ISR does to the flag variable and how main checks it.

🔧 Debug
advanced
2:00remaining
Identify the ISR bug causing missed interrupts

Which option shows the bug that can cause missed interrupts in this ISR code?

Embedded C
volatile int count = 0;

void ISR_Handler() {
    count++;
    // Missing interrupt flag clear
}
AISR does not clear the interrupt flag, causing missed interrupts
BISR uses a global variable, which is not allowed
CISR increments count without disabling interrupts
DISR uses a volatile variable, which is incorrect
Attempts:
2 left
💡 Hint

Think about what happens if the interrupt flag is not cleared inside the ISR.

📝 Syntax
advanced
1:30remaining
Correct ISR declaration syntax

Which option shows the correct way to declare an ISR function in embedded C?

Avoid ISR_Handler() interrupt { /* code */ }
Binterrupt void ISR_Handler() { /* code */ }
Cvoid __interrupt() ISR_Handler(void) { /* code */ }
Dvoid ISR_Handler(void) __interrupt { /* code */ }
Attempts:
2 left
💡 Hint

Check the placement of the __interrupt keyword and function signature.

🚀 Application
expert
2:00remaining
Determine the number of ISR executions

Given this embedded C code, how many times will the ISR increment the counter if the interrupt triggers 5 times rapidly?

Embedded C
volatile int counter = 0;

void ISR_Handler() {
    counter++;
    // Interrupt flag cleared here
}

int main() {
    // Simulate 5 rapid interrupts
    for(int i=0; i<5; i++) {
        ISR_Handler();
    }
    printf("Counter: %d\n", counter);
    return 0;
}
A5
B1
C0
DUndefined behavior
Attempts:
2 left
💡 Hint

Consider how many times the ISR is called and how it increments the counter.