Yahoo Groups archive

AVR-Chat

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

Message

Re: [AVR-Chat] Re: Volatile modifier

2012-04-18 by R E Purcella

I found this statement very helpful.

"No, this is not true. If a "global" variable (one with external 
linkage) is not declared volatile, then the compiler will assume that it 
cannot change during the execution of the code being compiled other than 
by an assignment it can see within that code."

Thank you for verifying what i was trying to say. This is the problem, 
and the solution. 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.

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.

(simple explanation can be found at efundies.com not my site fun site or 
not)


Perhaps, global means something different between us? I use global to 
mean variables in my code declared before MAIN, declared outside of 
other functions, that are changeable/changed during execution. It is 
understood that variables are changed by assignment. and without an 
assignment they will not be changed. Generally code can assign a new 
value to a variable at any time. Except as you say, when a linkage is 
needed.

My word "otherwise" draws a contrast between volatile variables and 
other variables that are not volatile.



rep

On 4/17/2012 5:06 AM, bayramdavies wrote:
>
> R E Purcella wrote:
>
> > As I understand it, Volatile variables are
> > used when the variable is expected to be
> > changed in Interrupt routines.
>
> That is one situation. See Don Kinzer's post of Apr 13 for others.
>
> > Otherwise most global variables are by
> > nature changeable at any time.
>
>
>
> > But, interrupt routines do not normally allow
> > outside variables to be changed unless they are
> > volatile. Otherwise pointers must be used.
>
> I can't make any sense of this. You can do whatever you want in an 
> interrupt service routine, there is no issue of being "not allowed" to 
> do certain things.
>
> > Further, volatile variables need to be declared
> > in your global section.
>
> No, you can write the declaration of a global variable anywhere you 
> want to, according to your style and needs.
>
> > i suggest this link efundies.com
>
> I suggest that for complex issues such as this you avoid sites that 
> describe themselves as "a collection of fun, free, electronics 
> projects and advice" and instead stick to places visited by a good 
> number of professionals who are likely to know what they are talking 
> about.
>
> 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.