Bird
Raised Fist0
Embedded Cprogramming~20 mins

Writing an ISR (Interrupt Service Routine) in Embedded C - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of an ISR (Interrupt Service Routine) in embedded C?
easy
A. To allocate memory dynamically
B. To handle hardware events immediately when they occur
C. To run the main program loop continuously
D. To compile the program code

Solution

  1. Step 1: Understand ISR role

    An ISR is a special function that runs automatically when a hardware event happens, like a button press or timer overflow.
  2. Step 2: Compare options

    Only To handle hardware events immediately when they occur describes this immediate handling of hardware events. Other options describe unrelated tasks.
  3. Final Answer:

    To handle hardware events immediately when they occur -> Option B
  4. Quick Check:

    ISR = handle hardware events [OK]
Hint: ISRs respond instantly to hardware signals [OK]
Common Mistakes:
  • Thinking ISR runs like normal functions
  • Confusing ISR with main program loop
  • Assuming ISR manages memory
2. Which of the following is the correct way to declare an ISR function in embedded C?
easy
A. void ISR_Handler() __interrupt { /* code */ }
B. void ISR_Handler() interrupt { /* code */ }
C. void ISR_Handler() __interrupt(0) { /* code */ }
D. void ISR_Handler() __isr { /* code */ }

Solution

  1. Step 1: Recall ISR syntax in embedded C

    ISRs are declared with the __interrupt keyword followed by the interrupt vector number in parentheses.
  2. Step 2: Check options

    void ISR_Handler() __interrupt(0) { /* code */ } uses __interrupt(0), which is the correct syntax. The others either miss the vector number, use the wrong keyword ('interrupt'), or a non-standard keyword ('__isr').
  3. Final Answer:

    void ISR_Handler() __interrupt(0) { /* code */ } -> Option C
  4. Quick Check:

    ISR syntax = __interrupt(vector) [OK]
Hint: Use __interrupt(vector_number) to declare ISR [OK]
Common Mistakes:
  • Omitting interrupt vector number
  • Using 'interrupt' instead of '__interrupt'
  • Using non-standard keywords
3. Given the following ISR code snippet, what will be the value of flag after the interrupt occurs?
volatile int flag = 0;
void ISR_Timer() __interrupt(1) {
    flag = 1;
    // Interrupt flag cleared here
}

int main() {
    flag = 0;
    // Interrupt occurs here
    // ISR_Timer runs
    return flag;
}
medium
A. 1
B. 0
C. Undefined behavior
D. Compilation error

Solution

  1. Step 1: Understand ISR effect on flag

    The ISR sets the variable 'flag' to 1 when the interrupt occurs.
  2. Step 2: Check main function return

    Main sets flag to 0, then interrupt triggers ISR which sets flag to 1, so return value is 1.
  3. Final Answer:

    1 -> Option A
  4. Quick Check:

    ISR sets flag = 1, so return = 1 [OK]
Hint: ISR changes flag to 1 before main returns [OK]
Common Mistakes:
  • Ignoring ISR effect on flag
  • Assuming flag stays 0
  • Confusing volatile usage
4. Identify the error in this ISR code snippet:
void ISR_UART() __interrupt(4) {
    if (UART_Interrupt_Flag) {
        // Handle UART interrupt
        UART_Interrupt_Flag = 0;
    }
}
medium
A. ISR should not clear interrupt flags inside it
B. ISR function must return an int value
C. ISR must be declared with 'interrupt' keyword, not '__interrupt'
D. Missing volatile keyword for UART_Interrupt_Flag variable

Solution

  1. Step 1: Check variable usage in ISR

    Interrupt flags accessed in ISR should be declared volatile to prevent compiler optimization issues.
  2. Step 2: Analyze other options

    Clearing flags inside ISR is correct practice; __interrupt is correct keyword; ISR functions return void, not int.
  3. Final Answer:

    Missing volatile keyword for UART_Interrupt_Flag variable -> Option D
  4. Quick Check:

    Interrupt flags must be volatile [OK]
Hint: Declare interrupt flags volatile to avoid optimization bugs [OK]
Common Mistakes:
  • Not declaring flags volatile
  • Wrong ISR declaration keyword
  • Expecting ISR to return a value
5. You want to write an ISR for a timer interrupt that increments a counter and clears the interrupt flag. Which of the following is the best practice to ensure correct ISR behavior?
hard
A. Increment counter, then clear interrupt flag inside ISR
B. Clear interrupt flag, then increment counter inside ISR
C. Increment counter outside ISR and clear flag inside ISR
D. Do not clear interrupt flag; let main program clear it

Solution

  1. Step 1: Understand ISR order for flag and counter

    Best practice is to handle the event (increment counter) first, then clear the interrupt flag to acknowledge the interrupt.
  2. Step 2: Evaluate options

    Increment counter, then clear interrupt flag inside ISR follows this order. Clear interrupt flag, then increment counter inside ISR clears flag before handling event, which may cause missed interrupts. Increment counter outside ISR and clear flag inside ISR splits logic incorrectly. Do not clear interrupt flag; let main program clear it risks repeated interrupts.
  3. Final Answer:

    Increment counter, then clear interrupt flag inside ISR -> Option A
  4. Quick Check:

    Handle event first, then clear flag [OK]
Hint: Process event before clearing interrupt flag [OK]
Common Mistakes:
  • Clearing flag before handling event
  • Leaving flag uncleared causing repeated interrupts
  • Handling event outside ISR
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard