Help with keyowrd Volatile... A bit of subject i know.

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have a question about the keyword "Volatile".

Can anyone please offer me a good explanation why and where i would use this for defining a variable ?

Thanks in advance.

P.S. I'm Using Codevision Compiler (FYI)

JIm

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The volatile keyword forces the compiler not to make any assumptions about a variable.

for example suppose there was a flag that gets cleared in an interrupt routine, and in your main code you wrote something like

unsigned char flag;

flag=1;

while(flag)
{
//wait for flag to clear
};

the compiler will think, since there's no way flag can change (not knowing about the interrupt), then this is just the same as

while(1)
{
};

The volatile keyword will force the compiler to evaluate the variable every time, where it thinks it could have changed or not.

j.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the reply. I understand what you are saying, however I would tend to think that almost all global variables should be initialized as volatile. Because a global variable can be used & changed in any function throughout the program. But you hardly see that keyword used in many programs.

I would expect the compiler to evaluate the following code no matter what it thinks I mean and do what I tell it to J

while(flag)
{
//wait for flag to clear
};

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Not exactly, Jim--in fact, nearly the opposite.

The pressure is on compiler writers and vendors to produce smaller and/or faster code. IMO, those would probably follow right behind "reliability"--always generating proper instruction sequences--in the Top Ten list of compiler evaluation.

You can see that in the contrived example posted, eliminating the entire while() loop would make the program both smaller and faster.

Now, let's address your subject of global variables. In the straightforward case a single-threaded (no ISRs, multiple processors, or other outside influences) program is analyzed by the compiler into a set of "basic blocks". Inside a basic block there are no transfers of control into (or also out of? I forget) the block, and the compiler can analyze the "lifetime" of register and other variable contents within the basic block. Thus, it can avoid the expensive reload of the registers with a value that is already present. A contrived example:

if (ptr) *ptr++;

Now, if you call another function you have stepped outside the basic block. thus, the global variable will be reloaded the next time. More sophisticated compilers can do more detailed analysis such as drilling down the call tree to see what exactly the called function(s) might disturb.

The summary:
--for detailed work, you need to know how your compiler treats the AVR environment. For example, I'd suspect that I/O ports are always treated as volatile in AVR compilers, but you'd need to check or experiment with yours to make sure
--for single-threaded work it is usually not critical. Thus, I've got a lot of AVR apps without any "volatile" in them, even with many ISRs.
--Aside from the "volatile" nature of variables written in ISRs, you must also be aware of what is an "atomic" operation inside your compiler and what isn't. In general, I copy all values longer than 8 bits that are written in an ISR with interrupts turned off. A very common example is ADC results; another is 16-bit timer counts.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I understand what volatile means... but I'm not certain why a compiler can't check for itself as to if it should be volatile or not.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The compiler cannot know what events might occor at >>any<< time. When writing an interrupt-driven microcontroller app, >>you<< must consider what might happen if any enabled interrupt fires between any two machine language instructions (not C statements--it typically takes multiple machile instructions for one C statement). In any particular app, I have only a very few pieces of data that need to be protected. But this comes from considerations during system design, and disabling interrupts when needed.

Making sure that operations are atomic takes care of most of it. Having only "one way" writing takes care of much of the rest. that's when the volatile is needed, for those last few operations.

As in the original example given, repeated tests are one of the areas that is a prime volatile candidate.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You might want to find a copy of the standard C "rationale" document(s). A quick Google search uncovered this one with a loop example:
http://www.lysator.liu.se/c/rat/...

There are other parts of the rationale that (paraphrased) say that it is not the responsibility of the C compiler to protect the programmer from him/herself, and the compiler shouldn't interfere with the programmer's attempt to get the job done. The volatile keyword was added to help apply standard C to environments such as our AVR microcontroller apps. Other examples given are things like device drivers in other environments.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is a neat article.

Introduction to the Volatile Keyword
http://www.embedded.com/story/OE...
Category : When to use the volatile keyword in C

/Bingo

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks Guys, Thank you for the links too it sure answerd my questions. Bingo600 that link was exactly what i was looking for it had real nice examples.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jimparisi wrote:
Thanks Guys, Thank you for the links too it sure answerd my questions. Bingo600 that link was exactly what i was looking for it had real nice examples.

Yes, bingo, that professionally-written explanation is great. Thanks.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Bingo600 wrote:
This is a neat article.

Introduction to the Volatile Keyword
http://www.embedded.com/story/OE...
Category : When to use the volatile keyword in C

/Bingo


This link is also in the first post made by Bingo600 in the sticky topic called "Find/Post your Online C Books & Tools here ...." at the top of this forum:
https://www.avrfreaks.net/phpBB2/...
Remember to check out this topic from time to time, to find useful references.