Part 5 — Chapter 24

Chapter 24: Interrupt Basics in C

Introduction

In embedded systems, hardware events often occur unexpectedly. A button press, timer overflow, UART data arrival, or sensor signal may require immediate attention from the processor. Continuously checking for these events using loops is inefficient and wastes CPU time.

Interrupts provide a mechanism for hardware or software events to temporarily pause normal program execution and immediately execute a special function called an Interrupt Service Routine (ISR).

What is an Interrupt?

An interrupt is a signal that temporarily stops the normal execution flow of the processor so that a high-priority event can be handled immediately.

After the interrupt handling completes, the processor resumes execution from where it stopped.

Interrupt Flow
Main Program Running
⚡ Hardware Event Triggered
(Timer Overflow / UART / Button)
CPU saves current state
Interrupt Service Routine (ISR)
CPU restores previous state
Main Program Resumes
Interrupt Service Routine (ISR)

An ISR is a special function automatically executed when an interrupt occurs.

Example

void Timer_ISR()
{
    timer_flag = 1;
}

The ISR should execute quickly and avoid unnecessary processing.

Why Interrupts Are Used

Interrupts help:

  • respond quickly to hardware events
  • avoid constant polling loops
  • improve CPU efficiency
  • support real-time behavior
  • handle asynchronous events
Polling vs Interrupts
Polling Interrupts
CPU continuously checks events Hardware notifies CPU automatically
Wastes CPU cycles More efficient
Slower response possible Faster event response
Simpler but inefficient Efficient real-time handling
Example Concept
while(1)
{
    if(button_pressed)
    {
        handle_button();
    }
}

This polling approach continuously checks the button state.

With interrupts, the hardware automatically triggers an ISR when the button is pressed.

Common Interrupt Sources

Interrupts may be generated by:

  • timers
  • GPIO pins
  • UART communication
  • ADC conversion completion
  • external sensors
  • communication peripherals

Important Points

  • Interrupts temporarily pause normal execution
  • ISRs execute automatically when interrupts occur
  • Interrupt handling should remain short and fast
  • Interrupts improve responsiveness and CPU efficiency
  • Frequently used in real-time embedded systems

Why This Matters in Embedded Systems

Interrupts are one of the most fundamental concepts in embedded systems programming. Most real-time embedded applications depend heavily on interrupts for handling hardware events efficiently.

volatile int uart_data_ready = 0;

Interrupts are widely used in device drivers, communication protocols, operating systems, timers, sensor interfaces, and real-time control systems. Understanding interrupts is essential for building responsive and efficient embedded firmware.

← Chapter 23: volatile Keyword Chapter 25: Bare Metal Embedded C →