|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||||
|
|||||
|
volatile keyword
What does the volatile declaration mean?
ie (from kevin.org/frc timer demo) volatile unsigned long msClock =0; |
|
#2
|
|||||
|
|||||
|
Re: volatile keyword
as an addition to my last post, what does the keyword static mean as well? as in:
static unsigned int divisor =0; |
|
#3
|
|||||
|
|||||
|
Re: volatile keyword
Quote:
static tells the compiler to always retain the variable's value. You'll want to use static quite often. In the following example, divisor will increase in value every time My_stuff() is called, but x will always start fresh as 0 each time and increase to 1, before being lost when you exit the routine: Code:
void My_stuff()
{
static unsigned int divisor =0;
unsigned int x=0;
x++;
divisor++;
}
|
|
#4
|
||||||
|
||||||
|
Re: volatile keyword
Quote:
Static means that only one copy is used of the variable in all instances of the function. For example, void foo(){ static int count = 0; count++; printf("%d", count); } int main(int argc, char *argv[]){ while(true) foo(); } would print out 123456789101112, etc whereas void foo(){ int count = 0; count++; printf("%d", count); } would just print out 1 every time since the function can't keep track of things between calls. As for volatile, it basically tells the compiler that it's possible that something other than the program itself could change the value of this variable, so don't try to do any crazy optimization stuff. Chances are that you won't really need it much (if ever), but it's good to know its there just in case. -Rob |
|
#5
|
||||
|
||||
|
Re: volatile keyword
static can also be used outside a function. When used on a global variable, it is then only accessable to the functions in that file. It is impossible for a function out side the file to use it.
|
|
#6
|
|||
|
|||
|
Re: volatile keyword
Quote:
Actually, that is how normal "global" variables work. they have file scope. To increase the scope you need to use extern (see above). |
|
#7
|
||||
|
||||
|
Re: volatile keyword
Quote:
|
|
#8
|
|||
|
|||
|
Re: volatile keyword
Quote:
Ahh, I did not know that. Thanks. |
|
#9
|
||||
|
||||
|
Re: volatile keyword
Actually, here's what really happens. A normal golbal variable is accessible to any file, but in order to use it, the file has to be told it exists. Hence the extern. Static limits the scope to the file. If you know C++, an unnamed namespace does the same thing as static, when used on a global variable.
|
|
#10
|
||||
|
||||
|
Re: volatile keyword
Quote:
When you tell the compiler that a variable is "volatile" it means that it can be changed by something other than the code which the compiler is generating. So, when the compiler sees this, it does not cache the variable in a register and instead reads it directly out of RAM each time it is needed. Where this becomes important is on the digital I/O for example. The digital inputs are memory mapped (meaning to read the digital inputs, you just read a memory location). If these are not declared volatile, each time you access the digital inputs you may not be receiving the newest available data. So, if you go and look at ifi_picdefs.h, you'll see that all the CPU registers are declared volatile for this reason. Hope that made sense, it's pretty tricky to explain in text without a whiteboard . Let me know if you want me to clarify any part of it... |
|
#11
|
||||
|
||||
|
Re: volatile keyword
Quote:
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, |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Using the extern keyword. | D.Viddy | Programming | 7 | 20-01-2004 21:22 |