Volatile qualifier

Hey guys,

First time poster here, so i'll give a little bit of my background and goals: I'm a senior EE student with a decent bit of programming (mostly C++) experience and zero microcontroller experience. The arduino was recommended to me for some DIY/learning resources so i ordered my first board today! 8-)

Anyway, i'm looking to start off by doing some simple things... switching relays, reading encoders, 7segment displays, etc.

I'm just doing some research on the encoder bit and i'm having a little trouble understanding what declaring a variable as 'volatile' will get you... I have looked through the reference/example and i understand the situations WHEN you would need to use the qualifier. But i don't understand why. That is, why is it more advantageous to have certain variables declared in RAM rather than in a storage register.

Hopefully that makes sense and someone can shed some light on the topic! Thanks alot for your patience and assistance.

Essentially, volatile turns off a form of optimization performed by the compiler. I'll try to describe what happens using an example...

int i;

void DoIt( void )
{
  int j;
  int k;

  j = i + 37;  // ref #1

  // bunch of code here that does not modify i

  k = i + 41;  // ref #2

  // more code here
}

The variable i is used twice in DoIt. Because the value of i was loaded into a register at ref #1, i was not modified between ref #1 and ref #2, and the value of i was again used at ref #2, the compiler does not bother to reload i from memory; the value already in a register is used. Adding volatile prevents the compiler from performing this optimization. The value of i is always loaded from memory. This ensures data shared with an interrupt service routine is fresh and correct.

Does that make sense?

guitarguy,

As I understand it, the ‘volatile’ qualifier is primarily used in conjunction with interrupt routines.

Ordinarily the compiler will optimize access to variables. If the value of a variable has been loaded into a register, your program will use the value in the register instead of re-fetching it from memory every time.

However, if there is an interrupt routine that can change the value of that variable, then the value in the register may be out of date. Declaring it as ‘volatile’ causes the compiler to generate code that reloads the variable from its memory location each time it is used, rather using a value that is in a register. This way, your code will be operating on the most up-to-date value of the variable.

Regards,

-Mike

Volatile isn't just useful for normal C variables that you define and change in interrupt routines, but also for hardware registers that also change without your program's knowledge. In some architectures including ATMELs, you can make a pointer to a specific address to "read" from I/O ports as if they were memory locations. You'd need to specify volatile if your routine wanted to get updated versions in every calculation.

Oooh i see. Thanks alot guys, this makes alot of sense. Thanks for clearing that up.