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:

  1. The MCU is running your main program (called the main loop).

  2. An interrupt event occurs (e.g., a button press, timer overflow, data received).

  3. The MCU pauses the main program.

  4. It jumps to a special function called an Interrupt Service Routine (ISR) or interrupt handler.

  5. Once the ISR is done, it returns to where it left off in the main program.


Types of Interrupts:

  1. External Interrupts – Triggered by changes on input pins (e.g. button press).

  2. Timer Interrupts – Triggered by internal timers reaching a value.

  3. Peripheral Interrupts – Triggered by events in peripherals (UART, ADC, SPI, etc.).

  4. Software Interrupts – Triggered manually in code.


Key Concepts:

ConceptDescription
ISR (Interrupt Service Routine)The function that handles the interrupt
Interrupt Vector TableA lookup table that tells the MCU which ISR to call
Masking/DisablingTemporarily turning off specific interrupts
PrioritySome 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):

c

void main() { setup_button_interrupt(); // Configure external interrupt while(1) { // Main loop doing other stuff } } void EXTI0_IRQHandler() { // This runs when the button is pressed toggle_LED(); }

 Without interrupts:

You’d need to keep checking inputs in a loop (polling), which wastes CPU time and may miss fast events.

评论

此博客中的热门博文

How To Connect Stm32 To PC?

What are the common HDL languages used in FPGA design?

How do you set up ADC (Analog-to-Digital Converter) in STM32?