What is interrupt in microcontroller?
An interrupt in a microcontroller is a signal or event that temporarily halts the normal execution flow of your program so the microcontroller can respond to an urgent task or event immediately.
Simple Definition:
An interrupt is like someone tapping your shoulder while you're working — you pause what you're doing, handle the urgent matter, then go back to your original task.
How It Works:
-
The MCU is running your main program (called the main loop).
-
An interrupt event occurs (e.g., a button press, timer overflow, data received).
-
The MCU pauses the main program.
-
It jumps to a special function called an Interrupt Service Routine (ISR) or interrupt handler.
-
Once the ISR is done, it returns to where it left off in the main program.
Types of Interrupts:
-
External Interrupts – Triggered by changes on input pins (e.g. button press).
-
Timer Interrupts – Triggered by internal timers reaching a value.
-
Peripheral Interrupts – Triggered by events in peripherals (UART, ADC, SPI, etc.).
-
Software Interrupts – Triggered manually in code.
Key Concepts:
Concept | Description |
---|---|
ISR (Interrupt Service Routine) | The function that handles the interrupt |
Interrupt Vector Table | A lookup table that tells the MCU which ISR to call |
Masking/Disabling | Temporarily turning off specific interrupts |
Priority | Some interrupts are more urgent and can interrupt other ISRs |
Why Use Interrupts?
-
React immediately to real-world events (e.g. sensor input, communication).
-
Improve efficiency (no need to poll constantly).
-
Enable multitasking behavior in real-time systems.
Example (Pseudocode):
Without interrupts:
You’d need to keep checking inputs in a loop (polling), which wastes CPU time and may miss fast events.
评论
发表评论