Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Volatile variable - request for resources

Status
Not open for further replies.

sivamit

Full Member level 4
Joined
Dec 1, 2005
Messages
201
Helped
20
Reputation
40
Reaction score
14
Trophy points
1,298
Activity points
2,651
Volatile variable....

Hi please can anyone upload or give some details on volatile keyword and its applications..?

Thanks
 

Re: Volatile variable....

The volatile Keywords is used to specify that a variable (memory location) could be subject to changes of its value due to causes out of control of the running SW.
A typical use is to define interfaces with memory mapped peripherals, where you have a set of memory locations used to exchange data with the HW.
Another use of volatile specifier is when you want to avoid that the compiler performs optimizations on some variables. As an example define a variable but do not use it in the SW: when you compile the file the compiler recognizes that the variable is not used and cut it off from the executable, on the contrary if you specify the volatile keyword the compiler is not able to do any assumption on its use (it could be changed by external factors) and leave it in the code. Typically you can use the volatile specifier to define dummy variables to avoid compiler optimization on little portion of the code (e.g to place breakpoint into a loop).

Regards
Mowgli
 

Re: Volatile variable....

Another reason to use volatile variables is when you are using an interrupt to update a global variable that is used outside of the interrupt. For example it might be a variable used to keep seconds. Consider the following:

X = seconds;
other code;
other code;
other code;
y = seconds;

If seconds is not declared as volatile the compiler will most likely keep the value of seconds in a register after setting the value of x and it will use the same value to set y. This will result in an error if the interrupt updated seconds in between the to lines of code. Setting seconds to volatile will force the compiler to assume that seconds might have been changed by something external to the current code section. It will therefore read the seconds variable again when setting y.

There are of course better ways to write this type of code :) this is just to illustrate the point
 
  • Like
Reactions: MRAMA

    MRAMA

    Points: 2
    Helpful Answer Positive Rating
Volatile variable....

This keyword is mainly used in optimization purpose.

A volatile variable is one that can change unexpectedly. Consequently, the compiler can make no assumptions about the value of the variable. In particular, the optimizer must be careful to reload the variable every time it is used instead of holding a copy in a register. Examples of volatile variables are:

* Hardware registers in peripherals (for example, status registers)
* Non-automatic variables referenced within an interrupt service routine
* Variables shared by multiple tasks in a multi-threaded application

Embedded systems deal with hardware, interrupts, RTOSes, and the like. All of these require volatile variables. Failure to understand the concept of volatile will lead to disaster.
 

Volatile variable....

Best explanation for volatile is available @ **broken link removed**
 

Re: Volatile variable....

In multi threaded applications also the volatile keyword is used to update the golabal variables that are used in both the threads.

To be clear Suppose two threda are running. if first thread is using variable X but during the execution the time slice may shift to second thread and the X value may chage in that thread. So again when time slice is back to first thread the varible should contain the latest value.

So if u declare the variable volatile it will referesh the memory location ASAP with the latest value.

This is my practical experience.
 

Re: Volatile variable....

Short description:
During runtime for every use of the value of a volatile variable, the value holder for the variable is reloaded from actual physical memory.
 

Re: Volatile variable....

features of volatile variable.

1. Volatile is a global variable.
2. takes 1 byte of instruction
3. Compiler cannot optimise the variable.
4. type casting is not allowed.
5. Value can be modified by ports, Hardware, interrupts,serial ports only.
6. Always allocated in Heap memory.
7. Permanent memory location is allocated for it.

Its a very good topic to discuss. I can add more points for this. if you can understand the above points properly.
 
  • Like
Reactions: MRAMA

    MRAMA

    Points: 2
    Helpful Answer Positive Rating
Re: Volatile variable....

ckshivaram said:
features of volatile variable.

1. Volatile is a global variable.
2. takes 1 byte of instruction
3. Compiler cannot optimise the variable.
4. type casting is not allowed.
5. Value can be modified by ports, Hardware, interrupts,serial ports only.
6. Always allocated in Heap memory.
7. Permanent memory location is allocated for it.

Its a very good topic to discuss. I can add more points for this. if you can understand the above points properly.

Please do it... This is for all embedded engrs not only for me..:!:
 

Re: Volatile variable....

When we use the word Optimise, we mean that the variable in the memory can be changed only by the compiler whenever the code is executed. Volatile modifiers cannot be optimised by the compiler, During linking process the code is allocated physical memory in the internal memory. so during the link process( generation of .lnk file during compilation) these variables are placed in the heap instead of main memory. Compiler cannot modify the variable until unless a copy is copied to RAM for execution. So the compiler allocates a different memory location for the variable. These are called un-optimised location. the variables in this location are not dependent on the compiler to change the value. instead interrupt, ports, serial, hardware are given permission to access these variables when ever they raise a request.
When a memory is optimised, then the life of that variable is limited only till the function executes then it is destroyed, but volatile are not destroyed, but keep value in it till there is any change done by external entities. Whenever these variables are accessed only the last updated value is seen in the register.

Uploading a example after this. the best example is RTC(Real Time Clock). in the PC. even when the PC is shut down , and later restarted, the clock updates the latest current time.

Added after 9 minutes:

eg:
volatile char time;
void update(void)
{
time =time+1;
}

void main()
{
time=0;
while(time<100);
}


without volatile modifier the compiler looks at this as 2 different statements.
1. time =0;
2. while(time<100);

since time =0; 0<100, so the loop always stay in the same line till the condition is true.
Time never reach 100. So when memory is optimised then it can be changed only through execution of explicit statement which modify the value. in the above case it does not change.

if we use volatile modifier, this disables optimisation, forcing the program to fetch a new value from the variable every time variable is accessed.

hope this answers your question. if not then i can still continue my explaination in my next post. let me know if you got anything from this.

Always remember you cannot simulate the condition of volatile in c51 or any compiler as Heap memory cannot be simulated in keil. it can be seen only on hardware.

thanks
shivaram
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top