0
0
Power-electronicsConceptBeginner · 3 min read

What is volatile and why needed in ISR in Embedded C

In Embedded C, volatile tells the compiler a variable can change unexpectedly, so it must always read it from memory. It is needed in ISRs because variables shared between the main program and ISR can change anytime, preventing wrong optimizations.
⚙️

How It Works

Imagine you have a mailbox where you expect letters to arrive only when you check it. Normally, your program assumes a variable stays the same unless it changes it directly. But in embedded systems, some variables can change anytime, like a mailbox where someone can drop a letter without you knowing.

The volatile keyword tells the compiler: "Don't assume this variable stays the same; always check its actual value in memory." This is important because compilers try to optimize code by keeping variables in fast storage (like registers) and not re-reading them every time.

In Interrupt Service Routines (ISRs), the program can jump to a special function when an event happens, changing variables unexpectedly. Without volatile, the compiler might not notice these changes, causing bugs.

💻

Example

This example shows a variable flag changed inside an ISR and checked in the main loop. Marking it volatile ensures the main loop always reads the latest value.

c
#include <stdio.h>
#include <stdbool.h>

volatile bool flag = false; // volatile because changed in ISR

// Simulated ISR function
void ISR(void) {
    flag = true; // ISR sets the flag
}

int main() {
    printf("Starting main loop\n");
    while (!flag) {
        // Waiting for flag to be set by ISR
    }
    printf("Flag detected, ISR was called!\n");
    return 0;
}
Output
Starting main loop Flag detected, ISR was called!
🎯

When to Use

Use volatile for variables that can change outside the normal program flow, such as:

  • Variables modified inside ISRs
  • Hardware registers that can change independently
  • Shared variables in multi-threaded or multi-core systems

This prevents the compiler from optimizing away necessary reads or writes, ensuring your program reacts correctly to hardware or interrupts.

Key Points

  • volatile tells the compiler not to optimize access to a variable.
  • It is essential for variables changed by ISRs to avoid stale values.
  • Without volatile, the program may behave unpredictably.
  • Use it for hardware registers and shared variables in concurrent contexts.

Key Takeaways

Use volatile for variables changed by ISRs to ensure correct memory access.
Without volatile, the compiler may optimize away needed reads, causing bugs.
volatile prevents the compiler from assuming a variable's value is stable.
Apply volatile to hardware registers and shared variables in embedded systems.