Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Re: Volatile modifier

2012-04-18 by bayramdavies

R E Purcella wrote (although not in this order):

> Perhaps, global means something different between us?
> I use global to mean variables in my code declared
> before MAIN, declared outside of other functions

OK, let's straighten this out.  "Global" means in scope (and thus able to be accessed) everywhere in a program.  There is, strictly speaking, no global scope in the C language.  Variables declared
outside of a function have "file scope".  If they have the "static"
storage class, they are given internal linkage and so they cannot be referenced outside of the unit in which they are defined (the linker will error).  If they don't have static, they have external linkage and can be accessed in other units.  So, to produce the effect of a global variable, declare the variable in a header file that is included in all source files, don't use static, and define the variable in the source file of your choice.  Many (most?) professionals will avoid this whenever possible so as to limit the "connectedness" of the program.  The more global variables you have, the more difficult it becomes to reason about a program and maintain its correctness.

> An ISR will change volatile variables. In my 
> experience and from reading the winavr manual,
> ISR routines will not change non-volatile
> variables. That is to say, non-volatile variables 
> will not change on the outside of the ISR
> function; with or without an assignment.
> the variable must be volatile.

This is so wrong that I am forced to assume I misunderstand you.  There is nothing in the C language, the compiler or the hardware that prevents an ISR from changing non-volatile variables.  If this is really what you are saying, I suggest that you completely discard your present understanding of volatile and re-read this thread.

> But, in other functions I can change any variable
> simply by giving it a new value by assignment.
> So long as it is global to my function.

You can access a variable whenever it is in scope, whether that is local or file scope.  The compiler will allow this.  The problem we are discussing is how the accesses that you have written in your source code map to the generated machine code.

> (simple explanation can be found at efundies.com ...)

Again, you would do better to hang with professionals than fun-seekers for complex issues such as this.  The only reference to volatile on the efundies.com Web site that I can find using Google is a brief example of where you should use it, which Don covered in this thread days ago, followed by a fantastically garbled explanation of why you need to do this.  If this is where you got your understanding of volatile, again, I suggest you start again.  An explanation is not necessarily good because it is simple.  Sometimes you just have to suck it up and wade into the complexity if you want to properly understand something.

Graham Davies
ECROS Technology
www.ecrostech.com

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.