Interrupt priority levels help decide which interrupt to handle first when many happen at the same time. This keeps your program running smoothly and quickly.
Interrupt priority levels in Embedded C
/* Example syntax to set interrupt priority in embedded C */ void set_interrupt_priority(int interrupt_number, int priority_level) { // platform-specific code to assign priority }
Priority levels are usually numbers; lower or higher numbers mean higher priority depending on the system.
Setting priorities is hardware and compiler specific; check your microcontroller's manual.
/* Set Timer interrupt to highest priority */ set_interrupt_priority(TIMER_INTERRUPT, 1);
/* Set UART interrupt to lower priority */ set_interrupt_priority(UART_INTERRUPT, 5);
This program simulates setting interrupt priorities for 3 interrupts and finds which one has the highest priority (lowest number). It then prints the interrupt number with the highest priority.
#include <stdio.h> // Simulated interrupt priority levels int interrupt_priorities[10]; void set_interrupt_priority(int interrupt_number, int priority_level) { if (interrupt_number >= 0 && interrupt_number < 10) { interrupt_priorities[interrupt_number] = priority_level; } } int get_highest_priority_interrupt() { int highest_priority = 1000; // large number int interrupt_num = -1; for (int i = 0; i < 10; i++) { if (interrupt_priorities[i] != 0 && interrupt_priorities[i] < highest_priority) { highest_priority = interrupt_priorities[i]; interrupt_num = i; } } return interrupt_num; } int main() { // Set priorities set_interrupt_priority(2, 3); // Interrupt 2 priority 3 set_interrupt_priority(5, 1); // Interrupt 5 priority 1 (highest) set_interrupt_priority(7, 4); // Interrupt 7 priority 4 int highest = get_highest_priority_interrupt(); printf("Highest priority interrupt is: %d\n", highest); return 0; }
Interrupt priority numbers can be different depending on your hardware; always check your device's datasheet.
Higher priority interrupts can interrupt lower priority ones, but not vice versa.
Be careful to avoid priority inversion, where a low priority interrupt blocks a higher priority one.
Interrupt priority levels help manage multiple interrupts by deciding which to handle first.
Lower numbers usually mean higher priority, but check your system.
Setting priorities correctly helps your embedded system respond quickly and reliably.
Practice
What does a lower interrupt priority number mean in embedded C?
Solution
Step 1: Understand priority numbering
In embedded C, interrupt priority levels are numbered so that lower numbers mean higher priority.Step 2: Interpret priority effect
Therefore, an interrupt with a lower number runs before interrupts with higher numbers.Final Answer:
It has higher priority and runs before others -> Option CQuick Check:
Lower number = higher priority [OK]
- Thinking higher number means higher priority
- Confusing priority with interrupt enable/disable
- Assuming priority affects CPU idle time
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
}Solution
Step 1: Identify correct function to set priority
NVIC_SetPriority is the standard function to set interrupt priority in embedded C.Step 2: Check correct priority value
Passing 2 as the priority sets the interrupt priority level to 2 correctly.Final Answer:
NVIC_SetPriority(IRQn, 2); -> Option BQuick Check:
Use NVIC_SetPriority with numeric level [OK]
- Using NVIC_EnableIRQ to set priority
- Passing symbolic constants instead of numbers
- Setting priority to a value outside allowed range
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 timeSolution
Step 1: Identify priority levels assigned
UART interrupt priority is set to 3, TIMER interrupt priority is set to 1.Step 2: Determine which priority is higher
Lower number means higher priority, so TIMER (1) runs before UART (3).Final Answer:
TIMER interrupt runs first, then UART -> Option AQuick Check:
Lower priority number runs first [OK]
- Assuming higher number runs first
- Thinking interrupts run simultaneously
- Ignoring priority settings
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?
Solution
Step 1: Analyze priority assignments
The code sets priority 5 first, then overwrites it with priority 0 for the same interrupt.Step 2: Understand priority effect
Priority 0 is higher priority than 5, so the last setting (0) is effective.Final Answer:
Priority 0 is higher than priority 5, so last setting wins -> Option DQuick Check:
Last priority set is used [OK]
- Thinking priority 0 is invalid
- Assuming multiple priorities combine
- Ignoring order of calls
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?
Solution
Step 1: Understand priority order needed
IRQn_B should run first, then IRQn_C, then IRQn_A last.Step 2: Assign priorities accordingly
Lower number means higher priority, so IRQn_B = 1, IRQn_C = 2, IRQn_A = 3 matches the desired order.Final Answer:
IRQn_B = 1, IRQn_C = 2, IRQn_A = 3 -> Option AQuick Check:
Lower number runs first [OK]
- Assigning higher numbers for higher priority
- Mixing up interrupt names and priorities
- Not following the required order
Ready to prove it?
Pass the test and this pattern moves unknown
