How to Use External Interrupt in Embedded C: Simple Guide
To use an
external interrupt in Embedded C, configure the interrupt pin and enable the interrupt in the microcontroller's registers, then write an interrupt service routine (ISR) to handle the event. This allows your program to respond immediately when an external signal changes without constantly checking the pin.Syntax
Using an external interrupt involves three main steps:
- Configure the interrupt pin: Set the pin as input and enable interrupt triggering on a specific edge (rising, falling, or both).
- Enable the interrupt: Turn on the interrupt in the microcontroller's interrupt control registers.
- Write the ISR: Define a special function that runs automatically when the interrupt occurs.
Below is a generic syntax example for an ISR in Embedded C:
c
void __interrupt() ISR(void) { if (INTF) { // Check if external interrupt flag is set INTF = 0; // Clear the interrupt flag // Your interrupt handling code here } }
Example
This example shows how to configure an external interrupt on a PIC microcontroller's INT0 pin. When the interrupt triggers, it toggles an LED connected to PORTB pin 0.
c
#include <xc.h> // Configuration bits here (device specific) void __interrupt() ISR(void) { if (INTF) { // Check external interrupt flag INTF = 0; // Clear interrupt flag PORTBbits.RB0 = ~PORTBbits.RB0; // Toggle LED } } void main(void) { TRISBbits.TRISB0 = 0; // Set RB0 as output (LED) PORTBbits.RB0 = 0; // LED off initially TRISBbits.TRISB1 = 1; // Set RB1 as input (INT0 pin) INTCONbits.INTE = 1; // Enable external interrupt INT0 INTCONbits.INTF = 0; // Clear interrupt flag INTCONbits.GIE = 1; // Enable global interrupts while(1) { // Main loop does nothing, waiting for interrupt } }
Output
When the external interrupt pin (RB1) detects a falling edge, the LED on RB0 toggles on or off.
Common Pitfalls
- Not enabling global interrupts: The ISR won't run if global interrupts are disabled.
- Forgetting to clear the interrupt flag: The interrupt will keep triggering repeatedly.
- Incorrect pin configuration: The interrupt pin must be set as input.
- Long or blocking code inside ISR: Keep ISR short to avoid missing other interrupts.
c
/* Wrong: Missing flag clear and global interrupt enable */ void __interrupt() ISR(void) { if (INTF) { // No INTF = 0; so interrupt repeats PORTBbits.RB0 = ~PORTBbits.RB0; } } /* Correct: Clear flag and enable global interrupt */ void __interrupt() ISR(void) { if (INTF) { INTF = 0; PORTBbits.RB0 = ~PORTBbits.RB0; } }
Quick Reference
Remember these key points when using external interrupts:
- Set interrupt pin as input.
- Enable interrupt and global interrupt bits.
- Write a short ISR that clears the interrupt flag.
- Test with simple actions like toggling an LED.
Key Takeaways
Always enable global interrupts to allow ISR execution.
Clear the interrupt flag inside the ISR to prevent repeated triggers.
Configure the interrupt pin as input and set the correct edge trigger.
Keep ISR code short and efficient to avoid blocking other interrupts.
Test external interrupts with simple outputs like LED toggling first.