|
Re: volatile keyword
Quote:
|
Originally Posted by mightywombat
What does the volatile declaration mean?
ie (from kevin.org/frc timer demo)
volatile unsigned long msClock =0;
|
Volatile is an ANSI C storage qualifier that tells the compiler that a value in that storage location can not be trusted.
From the MPLAB® C18 C Compiler User’s Guide:
2.9.2.2 INTERRUPT SERVICE ROUTINES
...
Global variables that are accessed by both an ISR and mainline functions should be declared volatile.
What does this mean? Let's assume you are doing a simple math operation. One operand is in a register and the other is a memory location. The compiler will not use the memory location for a volatile variable. This becomes very important for a complex mathematical line such as:
x = x * m + b; // where x is a variable modifiable by an ISR.
In this example, if x is stored with x*m and the ISR modifies x before x+b occurs, you get the wrong answer... The volatile keyword prevents this from happening.
Regards,
__________________
Mike Betts
Alumnus, Team 3518, Panthrobots, 2011
Alumnus, Team 177, Bobcat Robotics, 1995 - 2010
LRI, Connecticut Regional, 2007-2010
LRI, WPI Regional, 2009 - 2010
RI, South Florida Regional, 2012 - 2013
As easy as 355/113...
|