what this volatile means

Discussion in 'Embedded Systems and Microcontrollers' started by ep.hobbyiest, Oct 21, 2014.

  1. ep.hobbyiest

    Thread Starter Member

    Aug 26, 2014
    hello i m confuse in volatile type of data types?
    basically i read about volatile but after go through led blinking code of MSP430 then i m confuse.
    If i put Volatile the code works but if i erased it then it doesn't work.
    here is code

    Code (Text):
    2. #include <msp430.h>              
    4. int main(void) {
    5.     WDTCTL = WDTPW | WDTHOLD;        // Stop watchdog timer
    6.     P1DIR |= 0x01;                    // Set P1.0 to output directionz
    8.     for(;;) {
    9.         unsigned int i;    // volatile to prevent optimization
    11.         P1OUT ^= 0x01;                // Toggle P1.0 using exclusive-OR
    13.         i = 10000;                    // SW Delay
    14.         do i--;
    15.         while(i != 0);
    16.     }
    18.     return 0;
    19. }
  2. MikeML

    AAC Fanatic!

    Oct 2, 2009
    Better asked in the Microcontroller Forum
  3. tshuck

    Well-Known Member

    Oct 18, 2012
    Volatile tells the compiler that the value may be changed outside of the normal program and to not optimize that portion of code out.

    In this case, it is used to tell the compiler to use i (not optimize it out, since it doesn't change the functionality of the program) and perform the blocking delay.
    ep.hobbyiest likes this.
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    The term "volatile" means "don't make any assumptions about this variable".

    Here the compiler is assuming since the value of i is never assigned to any other variable it is safe to remove all lines that set or test it. Thus using the volatile keyword the compiler does not remove these lines.
  5. ep.hobbyiest

    Thread Starter Member

    Aug 26, 2014
    OK means if i used all variable as volatile then is it ok?
    Means all the global declare variable i have to change it anywhere in code. then i have to declare it as volatile.Right?
  6. Papabravo


    Feb 24, 2006
    No that is not correct.

    In a computer architecture a memory location has one very important property. If you write a value to it, that value will be unchanged if you read that location at any future time, assuming the power remains on continuously. If you cannot depend on this property then the memory is essentially useless.

    Once upon a time the registers used for I/O devices existed in a different address space than memory. A clever designer decided that there should be no difference between memory and I/O as far as addressing reading and writing are concerned. At the hardware level this is all well and good. At the compiler level there is a problem. If you write a value to a hardware register and the external hardware can change the value that is read back then you better use the volatile keyword. If you don't chaos will ensue. One of the things the compiler must do is to always assume that any value read from the register could be different from the last value written.

    My guideline on using the volatile keyword is to use it for any variable that can be modified outside of the program you are writing. It might be the hardware that modifies the variable or it might be an interrupt routine, or the operating system. You don't have to use it for any variables that are known and accessed only by the program you are writing. They can be global or local or static.

    Don't use volatile on any variable that behaves like a traditional memory. That is it always contains the last value that you wrote.
    Do use volatile on any variable that does not behave like traditional memory. That is you can't depend on the value it contains to be equal to the last value that you wrote.
    Last edited: Nov 6, 2014
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    Well... yes you could and yes it will probably work for you but you are removing some subtle power from the better C compilers that will clean up your code in ways you may not suspect.

    Your original example is he classic example of why volatile is needed... but what if you did not intend to introduce a long delay? Would it not be a great thing for the compiler to snip out an unintended delay that has no other effects on he rest of the code?