0
0
Embedded Cprogramming~20 mins

Enabling and disabling interrupts in Embedded C - Practice Problems & Coding Challenges

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!
Predict Output
intermediate
2:00remaining
What is the output of this interrupt enable code?

Consider this embedded C code snippet that enables global interrupts. What will be the value of the status variable after execution?

Embedded C
volatile unsigned char status = 0;
void enable_interrupts() {
    __asm__("sei"); // Set Interrupt Enable
    status = 1;
}

int main() {
    enable_interrupts();
    return status;
}
AUndefined behavior
B0
C1
DCompilation error
Attempts:
2 left
💡 Hint

The sei assembly instruction sets the global interrupt enable flag. The status variable is set right after.

Predict Output
intermediate
2:00remaining
What error occurs when disabling interrupts incorrectly?

What error will this code produce when compiled?

Embedded C
void disable_interrupts() {
    __asm__("cli"); // Missing semicolon fixed
}
ASyntaxError: missing semicolon
BNo error, runs fine
CRuntime error: interrupt disable failed
DCompilation error: expected ';' before '}' token
Attempts:
2 left
💡 Hint

Check the syntax carefully after the inline assembly statement.

🧠 Conceptual
advanced
2:00remaining
Which statement about interrupt enabling is true?

Which of the following statements about enabling interrupts in embedded C is correct?

AThe <code>sei</code> instruction enables global interrupts immediately.
BInterrupts can only be enabled in the main function.
CEnabling interrupts globally disables all interrupts.
DDisabling interrupts is done by the <code>sei</code> instruction.
Attempts:
2 left
💡 Hint

Recall what sei and cli instructions do.

Predict Output
advanced
2:00remaining
What is the value of flag after this interrupt disable sequence?

Given this code snippet, what is the value of flag after disable_interrupts() is called?

Embedded C
volatile int flag = 0;
void disable_interrupts() {
    __asm__("cli");
    flag = 42;
}

int main() {
    disable_interrupts();
    return flag;
}
A42
BUndefined behavior
CCompilation error
D0
Attempts:
2 left
💡 Hint

The cli instruction disables interrupts, but does not affect variable assignments.

🚀 Application
expert
3:00remaining
Which option correctly toggles interrupts safely?

You want to temporarily disable interrupts, perform a critical task, then restore the previous interrupt state. Which code snippet correctly does this?

A
cli();
// critical code
sei();
B
unsigned char sreg = SREG;
cli();
// critical code
SREG = sreg;
C
sei();
// critical code
cli();
D
unsigned char sreg = SREG;
sei();
// critical code
SREG = sreg;
Attempts:
2 left
💡 Hint

Think about saving and restoring the interrupt state to avoid unintended side effects.