Bird
Raised Fist0
Embedded Cprogramming~20 mins

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

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

Practice

(1/5)
1.

What does a lower interrupt priority number mean in embedded C?

easy
A. It has lower priority and runs after others
B. It disables all other interrupts
C. It has higher priority and runs before others
D. It runs only when the CPU is idle

Solution

  1. Step 1: Understand priority numbering

    In embedded C, interrupt priority levels are numbered so that lower numbers mean higher priority.
  2. Step 2: Interpret priority effect

    Therefore, an interrupt with a lower number runs before interrupts with higher numbers.
  3. Final Answer:

    It has higher priority and runs before others -> Option C
  4. Quick Check:

    Lower number = higher priority [OK]
Hint: Lower number means higher priority, runs first [OK]
Common Mistakes:
  • Thinking higher number means higher priority
  • Confusing priority with interrupt enable/disable
  • Assuming priority affects CPU idle time
2.

Which of the following is the correct way to set an interrupt priority level to 2 in embedded C?

void setup_interrupt() {
    // Set priority here
}
easy
A. NVIC_SetPriority(IRQn, HIGH);
B. NVIC_SetPriority(IRQn, 2);
C. NVIC_EnableIRQ(2);
D. NVIC_SetPriority(IRQn, 10);

Solution

  1. Step 1: Identify correct function to set priority

    NVIC_SetPriority is the standard function to set interrupt priority in embedded C.
  2. Step 2: Check correct priority value

    Passing 2 as the priority sets the interrupt priority level to 2 correctly.
  3. Final Answer:

    NVIC_SetPriority(IRQn, 2); -> Option B
  4. Quick Check:

    Use NVIC_SetPriority with numeric level [OK]
Hint: Use NVIC_SetPriority with number for priority [OK]
Common Mistakes:
  • Using NVIC_EnableIRQ to set priority
  • Passing symbolic constants instead of numbers
  • Setting priority to a value outside allowed range
3.

Given the following code snippet, what will be the order of interrupt execution if both interrupts occur simultaneously?

NVIC_SetPriority(IRQn_UART, 3);
NVIC_SetPriority(IRQn_TIMER, 1);
// Both UART and TIMER interrupts triggered at the same time
medium
A. TIMER interrupt runs first, then UART
B. Both run simultaneously
C. UART interrupt runs first, then TIMER
D. No interrupt runs due to conflict

Solution

  1. Step 1: Identify priority levels assigned

    UART interrupt priority is set to 3, TIMER interrupt priority is set to 1.
  2. Step 2: Determine which priority is higher

    Lower number means higher priority, so TIMER (1) runs before UART (3).
  3. Final Answer:

    TIMER interrupt runs first, then UART -> Option A
  4. Quick Check:

    Lower priority number runs first [OK]
Hint: Lower priority number runs before higher [OK]
Common Mistakes:
  • Assuming higher number runs first
  • Thinking interrupts run simultaneously
  • Ignoring priority settings
4.

Identify the error in this code snippet that sets interrupt priorities:

NVIC_SetPriority(IRQn_ADC, 5);
NVIC_SetPriority(IRQn_ADC, 0);
NVIC_EnableIRQ(IRQn_ADC);

What is the problem?

medium
A. Priority 0 is invalid and causes error
B. Setting priority twice causes conflict
C. No error, code works fine
D. Priority 0 is higher than priority 5, so last setting wins

Solution

  1. Step 1: Analyze priority assignments

    The code sets priority 5 first, then overwrites it with priority 0 for the same interrupt.
  2. Step 2: Understand priority effect

    Priority 0 is higher priority than 5, so the last setting (0) is effective.
  3. Final Answer:

    Priority 0 is higher than priority 5, so last setting wins -> Option D
  4. Quick Check:

    Last priority set is used [OK]
Hint: Last priority set is effective [OK]
Common Mistakes:
  • Thinking priority 0 is invalid
  • Assuming multiple priorities combine
  • Ignoring order of calls
5.

You have three interrupts: IRQn_A, IRQn_B, and IRQn_C. You want IRQn_B to run first, then IRQn_C, and finally IRQn_A. Which priority settings achieve this?

hard
A. IRQn_B = 1, IRQn_C = 2, IRQn_A = 3
B. IRQn_A = 1, IRQn_B = 2, IRQn_C = 3
C. IRQn_C = 1, IRQn_A = 2, IRQn_B = 3
D. IRQn_B = 3, IRQn_C = 2, IRQn_A = 1

Solution

  1. Step 1: Understand priority order needed

    IRQn_B should run first, then IRQn_C, then IRQn_A last.
  2. Step 2: Assign priorities accordingly

    Lower number means higher priority, so IRQn_B = 1, IRQn_C = 2, IRQn_A = 3 matches the desired order.
  3. Final Answer:

    IRQn_B = 1, IRQn_C = 2, IRQn_A = 3 -> Option A
  4. Quick Check:

    Lower number runs first [OK]
Hint: Assign lower numbers to interrupts needing higher priority [OK]
Common Mistakes:
  • Assigning higher numbers for higher priority
  • Mixing up interrupt names and priorities
  • Not following the required order
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