0
0
Power-electronicsConceptBeginner · 3 min read

Interrupt Priority in Embedded C: What It Is and How It Works

In embedded C, interrupt priority is a system that ranks interrupts so the microcontroller knows which interrupt to handle first when multiple occur simultaneously. Higher priority interrupts can pause lower priority ones, ensuring critical tasks get immediate attention.
⚙️

How It Works

Imagine you are at a help desk where multiple people ask for help at the same time. Interrupt priority is like deciding who gets helped first based on how urgent their request is. In embedded systems, when several interrupts happen together, the microcontroller uses interrupt priority to decide which interrupt to handle first.

Each interrupt source is assigned a priority level. If a higher priority interrupt occurs while a lower priority interrupt is being processed, the microcontroller pauses the current task and switches to the higher priority one. This ensures important events are handled quickly without losing track of less urgent tasks.

💻

Example

This example shows how to set interrupt priorities in embedded C for a microcontroller that supports priority levels. It configures two interrupts with different priorities and demonstrates which one runs first.

embedded_c
#include <xc.h>  // Example for PIC microcontroller

void __interrupt() ISR(void) {
    if (INT0IF) {  // Interrupt 0 flag
        // Handle INT0 interrupt
        LATBbits.LATB0 = 1;  // Turn on LED 1
        INT0IF = 0;  // Clear interrupt flag
    } else if (INT1IF) {  // Interrupt 1 flag
        // Handle INT1 interrupt
        LATBbits.LATB1 = 1;  // Turn on LED 2
        INT1IF = 0;  // Clear interrupt flag
    }
}

void main(void) {
    // Configure INT0 and INT1 pins as inputs
    TRISBbits.TRISB0 = 1;
    TRISBbits.TRISB1 = 1;

    // Enable interrupts
    INTCONbits.INT0IE = 1;  // Enable INT0 interrupt
    INTCON3bits.INT1IE = 1; // Enable INT1 interrupt

    // Set priorities (assuming microcontroller supports it)
    INTCON2bits.INT0IP = 1;  // INT0 high priority
    INTCON3bits.INT1IP = 0;  // INT1 low priority

    INTCONbits.GIE = 1;  // Enable global interrupts

    while(1) {
        // Main loop does other work
    }
}
Output
When both INT0 and INT1 interrupts occur simultaneously, the INT0 interrupt (higher priority) is handled first, turning on LED 1 before LED 2.
🎯

When to Use

Use interrupt priority when your embedded system handles multiple interrupt sources that can happen at the same time. It helps ensure that the most critical tasks, like emergency stop signals or sensor alerts, get immediate attention.

For example, in a robot, a collision sensor interrupt should have higher priority than a routine status update interrupt. This way, the robot can stop immediately if it detects an obstacle, even if other tasks are running.

Key Points

  • Interrupt priority ranks interrupts to decide which to handle first.
  • Higher priority interrupts can interrupt lower priority ones.
  • Setting priorities prevents missing critical events in embedded systems.
  • Not all microcontrollers support multiple priority levels.
  • Proper priority assignment improves system reliability and responsiveness.

Key Takeaways

Interrupt priority ensures critical interrupts are handled before less important ones.
Assign priorities based on the urgency of tasks in your embedded system.
Higher priority interrupts can interrupt lower priority interrupt handlers.
Not all microcontrollers support interrupt priority; check your hardware documentation.
Proper use of interrupt priority improves system responsiveness and safety.