How to Use the Volatile Keyword and Its Importance in Embedded C

Embedded C for microcontrollers



In this article, we go over how to use the volatile keyword and its importance in embedded C.

Volatile is a very important qualifier used in embedded systems.

The volatile keyword or qualifier is used when a variable may be constantly changing values.

An embedded system is a system where users, many of the times, can interact.

Think of cell phones, where users can press keys and enter in new key values.

Think of washing machines where users can change settings and start and stop processes.

With embedded systems, there are many times variables that always stay the same and there are at times variables that change constantly.

The definition of volatile is a value that can always be changing. So when you have a variable that can change constantly throughout a program, it should be used with the volatile qualifier.

Why is this necessary?

You may be thinking, aren't all variables by definition items that can potentially change in value?

However, you have to think as a compiler and in terms of compiler optimization?

Compilers have different levels of optimization.

If level is 0 is specified, then a compiler does no type of optimization.

But if you specify you want compiler optimization and specify a level higher than 0, it performs some level of optimization. It does this to speed up the compilation time. Why? Because then it runs faster, so in a way it's more efficient.

Now if you compile some software code and it has some level of optimization attached to it, it may just compile the code as specified during the time of compilation and won't recheck variables to see if any of the values change during a program. For example, say you have a pushbutton that a user can press. If the compiler has a high level of optimization specified, in order to save time, it won't go back and recheck the value of variables because they would slow it down and make the program less efficient and optimized. Therefore, it will try to optimize compilation time and won't recheck values. This is many times not desired at all. We don't want the compiler not to recheck values just to make the compilation faster. We want the compiler to recheck values so that the user input at any given time while the program is running is taken into account, whether is a key press or a button press or anything.

This is where the keyword qualifier volatile comes into play.

Specifying a variable as volatile instructs the compiler not to invoke any optimization on the variable operation.

Therefore, a compiler will always recheck the value of a variable that is declared volatile throughout a program. So it won't invoke any type of optimization on the variable. It won't assume that the variable is the same value when it was first initially compiled.

It tells the compiler that the value of the variable may change at any time with or without the programmer's consent.

Volatile is very important again for embedded systems because most embedded systems take in user input, which can always change at any time.

Take, for example, this code below which uses PORTA pin 0 as an input pin. If the pin is a LOW status, the LED at PORTD pin 12 turns off. If the pin is a HIGH status, the LED at PORTD pin 12 turns on. A user can constantly change this value at any time. Therefore, we specify the input status of this pin as volatile, since we want the compiler to constantly check this input pin status to determine whether the LED should turn on or turn off.





So the variable pinStatus represents the status of the input pin of PORTA pin 0.

Since this value can constantly change, we specify this variable as volatile.

This prevents the compiler from invoking any type of optimization on the variable; thus it will won't try to save on compilation time and will recheck this variable for any value changes constantly throughout the program.

Thus, you can see the importance of the volatile qualifier and when you would use it in an embedded software program.

Related Resources

How to Set Bits of a Number in C

How to Clear Bits of a Number in C



HTML Comment Box is loading comments...