0
0
Embedded Cprogramming~20 mins

Interrupt priority levels in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Interrupt Priority Master
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 priority code?

Consider the following embedded C code snippet that sets interrupt priorities and triggers interrupts. What will be the printed output?

Embedded C
#include <stdio.h>

volatile int interrupt_flag = 0;

void ISR_high_priority() {
    interrupt_flag = 1;
    printf("High priority interrupt handled\n");
}

void ISR_low_priority() {
    interrupt_flag = 2;
    printf("Low priority interrupt handled\n");
}

int main() {
    int priority_high = 1; // Higher priority
    int priority_low = 2;  // Lower priority

    // Simulate interrupts
    if (priority_low > priority_high) {
        ISR_high_priority();
        ISR_low_priority();
    } else {
        ISR_low_priority();
        ISR_high_priority();
    }

    printf("Interrupt flag value: %d\n", interrupt_flag);
    return 0;
}
A
High priority interrupt handled
Low priority interrupt handled
Interrupt flag value: 2
B
Low priority interrupt handled
High priority interrupt handled
Interrupt flag value: 1
C
High priority interrupt handled
Interrupt flag value: 1
D
Low priority interrupt handled
Interrupt flag value: 2
Attempts:
2 left
💡 Hint

Think about the order in which the interrupt service routines are called and how the interrupt_flag variable changes.

🧠 Conceptual
intermediate
1:30remaining
Which statement correctly describes interrupt priority levels?

In embedded systems, what does a lower numerical interrupt priority level usually mean?

AIt means the interrupt priority is dynamic and changes at runtime.
BIt means the interrupt has a lower priority and will be serviced last.
CIt means the interrupt is disabled and will never be serviced.
DIt means the interrupt has a higher priority and will be serviced first.
Attempts:
2 left
💡 Hint

Think about how priority numbers are assigned in many microcontrollers.

🔧 Debug
advanced
1:30remaining
What error does this interrupt priority code cause?

Examine the following code snippet. What error will occur when compiling or running it?

Embedded C
void ISR() {
    // Interrupt service routine
    int priority = 1;
    if(priority < 0) {
        printf("Invalid priority\n");
    }
}

int main() {
    ISR();
    return 0;
}
ACompilation error: missing #include <stdio.h>.
BCompilation error: 'printf' not declared.
CRuntime error: priority variable uninitialized.
DNo error, code runs and prints nothing.
Attempts:
2 left
💡 Hint

Check if all required headers are included for used functions.

📝 Syntax
advanced
1:30remaining
Which option correctly sets interrupt priority in this embedded C code?

Given the following code snippet, which option correctly sets the interrupt priority to 3?

Embedded C
void set_interrupt_priority(int priority);

int main() {
    // Set priority here
    
    return 0;
}
Aset_interrupt_priority = 3;
Bset_interrupt_priority(3);
Cset_interrupt_priority( priority = 3 );
Dset_interrupt_priority[3];
Attempts:
2 left
💡 Hint

Remember how to call functions with arguments in C.

🚀 Application
expert
2:00remaining
How many interrupts will be serviced given these priorities and mask settings?

In an embedded system, three interrupts have priorities 1, 2, and 3 (1 is highest). The system mask level is set to 2, which blocks interrupts with priority 2 and lower. How many interrupts will be serviced?

ANo interrupts will be serviced.
BInterrupts with priorities 1 and 2 will be serviced.
COnly the interrupt with priority 1 will be serviced.
DAll three interrupts will be serviced.
Attempts:
2 left
💡 Hint

Consider which priorities are blocked by the mask level.