0
0
Embedded Cprogramming~10 mins

Volatile variables in ISR context in Embedded C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Volatile variables in ISR context
Main program starts
Variable declared volatile
Main program reads variable
Interrupt occurs
ISR modifies volatile variable
Main program reads updated variable
Program continues with updated value
The main program and ISR share a volatile variable; the volatile keyword ensures the main program always reads the latest value changed by the ISR.
Execution Sample
Embedded C
volatile int flag = 0;

void ISR() {
  flag = 1;
}

int main() {
  while(flag == 0) {}
  // proceed after ISR sets flag
  return 0;
}
This code shows a volatile flag set by an ISR and checked by the main program to proceed.
Execution Table
StepContextVariable 'flag' ValueActionEffect
1Main0Initialize flag to 0flag is 0, main waits
2Main0Check flag == 0Condition true, loop continues
3Interrupt0ISR runs, sets flag = 1flag updated to 1
4Main1Check flag == 0Condition false, exit loop
5Main1Proceed after ISRMain continues with updated flag
💡 flag becomes 1 in ISR, main loop condition flag==0 becomes false, loop exits
Variable Tracker
VariableStartAfter Step 3Final
flag011
Key Moments - 2 Insights
Why must 'flag' be declared volatile?
Because the ISR changes 'flag' asynchronously, volatile tells the compiler not to optimize away repeated reads, ensuring main sees the updated value (see execution_table step 3 and 4).
What happens if 'flag' is not volatile?
The compiler might cache 'flag' in a register in main, never seeing the ISR update, causing an infinite loop (execution_table step 2 and 4 show the importance).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'flag' right after the ISR runs?
A1
BUndefined
C0
D-1
💡 Hint
Check execution_table row 3 where ISR sets flag to 1
At which step does the main program exit the loop checking 'flag == 0'?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at execution_table step 4 where flag is 1 and condition becomes false
If 'flag' was not declared volatile, what likely happens in the main loop?
AMain sees updated flag and exits loop
BMain never sees update and loops forever
CISR never runs
DProgram crashes
💡 Hint
Refer to key_moments about compiler caching and optimization
Concept Snapshot
volatile keyword tells compiler variable can change anytime
Used for variables shared with ISR or hardware
Prevents compiler optimizations caching variable
Ensures main program reads latest value
Critical for synchronization between ISR and main code
Full Transcript
This example shows a volatile variable 'flag' shared between main program and an interrupt service routine (ISR). The main program waits in a loop checking if 'flag' is zero. When the ISR runs, it sets 'flag' to 1. Because 'flag' is declared volatile, the main program always reads the updated value and exits the loop. Without volatile, the compiler might optimize the loop to never see the change, causing an infinite wait. The execution table traces each step: initialization, main loop checks, ISR update, and main loop exit. The variable tracker shows 'flag' changes from 0 to 1 after ISR. Key moments clarify why volatile is needed and what happens if omitted. The quiz tests understanding of variable values and program flow. This teaches how volatile ensures correct communication between ISR and main code.