0
0
Embedded Cprogramming~20 mins

Writing an ISR (Interrupt Service Routine) 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!
Predict Output
intermediate
2:00remaining
What is the output of this ISR increment?

Consider this embedded C code snippet where an ISR increments a global counter each time an interrupt occurs. What will be the value of counter after the ISR is called 3 times?

Embedded C
volatile int counter = 0;

void __attribute__((interrupt)) Timer_ISR(void) {
    counter++;
}

int main() {
    // Simulate 3 interrupts
    Timer_ISR();
    Timer_ISR();
    Timer_ISR();
    return counter;
}
A3
B1
C0
DUndefined behavior
Attempts:
2 left
💡 Hint

Each call to the ISR increases the counter by one.

🧠 Conceptual
intermediate
1:30remaining
Which keyword ensures variable consistency in an ISR?

In embedded C, which keyword is essential to declare a variable that is shared between the main program and an ISR to prevent compiler optimization issues?

Aregister
Bstatic
Cvolatile
Dconst
Attempts:
2 left
💡 Hint

This keyword tells the compiler the variable can change unexpectedly.

🔧 Debug
advanced
2:00remaining
What error occurs with this ISR declaration?

Examine this ISR declaration. What error will the compiler produce?

Embedded C
void Timer_ISR() {
    // ISR code
}

int main() {
    // Setup code
    return 0;
}
ANo error, valid ISR declaration
BLinker error: ISR not linked to interrupt vector
CRuntime error: ISR causes stack overflow
DSyntaxError: Missing interrupt attribute
Attempts:
2 left
💡 Hint

ISRs usually need special attributes or linkage to connect to hardware interrupts.

📝 Syntax
advanced
1:30remaining
Which ISR declaration is syntactically correct for GCC?

Choose the correct syntax to declare an ISR for a timer interrupt using GCC compiler extensions.

A
void Timer_ISR() __interrupt {
    // ISR code
}
B
void Timer_ISR(void) interrupt {
    // ISR code
}
C
interrupt void Timer_ISR(void) {
    // ISR code
}
D
void __attribute__((interrupt)) Timer_ISR(void) {
    // ISR code
}
Attempts:
2 left
💡 Hint

GCC uses __attribute__((interrupt)) to mark ISRs.

🚀 Application
expert
2:30remaining
What is the final state of the LED after 5 interrupts?

Given this embedded C code where an ISR toggles an LED state on each interrupt, what will be the final value of led_state after 5 interrupts? (1 = ON, 0 = OFF)

Embedded C
volatile int led_state = 0;

void __attribute__((interrupt)) Timer_ISR(void) {
    led_state = !led_state;
}

int main() {
    for (int i = 0; i < 5; i++) {
        Timer_ISR();
    }
    return led_state;
}
A1 (LED ON)
B0 (LED OFF)
C5 (LED toggled 5 times, final state undefined)
DCompilation error due to volatile usage
Attempts:
2 left
💡 Hint

Each interrupt toggles the LED state from 0 to 1 or 1 to 0.