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.

[SOLVED] What is volatile related with microcontroller?

Status
Not open for further replies.

agg_mayur

Member level 3
Joined
Feb 25, 2010
Messages
66
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,288
Location
India
Activity points
1,715
How can we used volatile in embedded systems.
 

Kindly rephrase your question as it doesn't mean any thing "How can we used volatile in embedded systems."

One should use grammatically correct english as no one has time to break their heads over language of a question!!!

"volatile" is an adjaective used for memory like ram which loose its content once power is removed from the system!!!!


Asimov
 

@Mayur

Did u mean the volatile keyword ?? or volatile memory ??
 

@Natraj
Volatile Keyword.

---------- Post added at 09:57 ---------- Previous post was at 09:53 ----------

And i got the answer from other source, here is the answer
FACTS:

» A volatile qualifier is important to be used for auto-storage variables within setjmp and longjmp.
» A volatile qualifier must be used when reading the contents of a memory location whose value can change unknown to the current program.
» A volatile qualifier must be used for shared data modified in signal handlers or interrupt service routines.
MYTHS:

» All shared data in multi-threaded programs must be declared volatile.

The only information/directive that volatile keyword means to the compiler is to not apply any implementation specific optimization to the object to which this qualifier has been attached. e.g. One of the most general optimizations that a compiler can apply to a variable is that if it cannot find a variable’s value being changed within a certain scope, it will cache the value of that variable, probably in a register, and then refer to that cached value whenever that variable is accessed instead of fetching it from the main memory.

#include "stdio.h"
#include "setjmp.h"
static jmp_buf buf;
int main( )
{
int a;
a = 0;
if (setjmp(buf)!=0)
{
printf("%d n", a) ;
exit(0);
}
a = 1;
longjmp(buf , 1);
return 0;
}
If you were to compile the above program without any optimization, you would see the output as 1, which is correct. But if you were to compile it with optimization turned on (e.g. by using -O2 option with gcc), the output would be 0. This is because the compiler saw that “a” was not used after setting it to one and hence optimized out that statement. Then, if you try it out by declaring “a” as volatile, you’d see the output as 1 even with optimization turned on, because the volatile qualifier would tell the compiler not to optimize any accesses related to a. For the 3 situations stated under facts, compiler cannot determine that a particular variable is needed or not because the accesses happen unknown to the compiler. Hence, we need to declare such variables as volatile.
Now, let’s turn towards the myths. Many people say that since in multi-threaded environment, a shared variable’s value can be changed by a thread before the other thread reads it, such variable should be declared as a volatile. But this is completely false. This is because a volatile qualifier just promises us that the accesses won’t be optimized and would be read from main memory each time but a multi-threaded program needs much more, namely:

1.Atomicity – The read/write access to the shared variables should be atomic in nature but access to volatile can be non-atomic and are completly implementation defined.
2.Preserve order of access – A volatile variable does not mandate this, especially when mixed with non-volatile variables.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top