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.

Why Volatile keyword in Embedded C?

Status
Not open for further replies.

Surender Reddy

Full Member level 2
Joined
Jan 28, 2012
Messages
129
Helped
7
Reputation
14
Reaction score
7
Trophy points
1,298
Location
Bangalore, India
Activity points
2,186
Hi,
What is the necessity of using volatile keyword for embedded applications?
Can any guide me with some example?
 

Use this keyword within variable that accessed inside and outside interrupt at the same time.
 

Use this keyword within variable that accessed inside and outside interrupt at the same time.

even the global variable also does the same without volatile.
what is the important of volatile?
How volatile keyword avoid optimization techniques by the compiler for a variable?
 

Just lets compilier fully copy the stack including this variable before enter interrupt routine.
 

assume you have an timer interrupt handler which sest a global variable every second, e.g.
Code:
volatile int second=0; 	// set every second 

void __attribute__((__interrupt__, __shadow__)) _T1Interrupt(void)
{
/* Interrupt Service Routine code goes here */
IFS0bits.T1IF = 0; 							//Reset Timer1 interrupt flag														
second=1;
}

int main(void)
{
...
startTimer();
second=0;
while(!second);      // wait 1 second
    ...                // code executed after a second

   
}
in main second is tested in the while() statement which executes every second

the problem is that if second is not declared volatile an optimising compiler will look at
Code:
second=0;
while(!second) ;     // wait 1 second
see that second is initialised to 0 and remove the while() statement - thus whole sections of code can be removed
the volatile keyword tells the compiler that the variable may be altered outside the current context and code should not be optimised
for more detail see
https://en.cppreference.com/w/cpp/language/cv
 
Last edited:

even the global variable also does the same without volatile.
what is the important of volatile?

It is a good practice to put the variables inside the routines where are supposed to be used.
This makes the program more organized, avoiding spread a big list of variables at the main().
 

Thanks for the reply....
Is there any separate memory for these volatile variables? where these are stored?

volatile doesn't allow to make perform optimization by compiler on the volatile variables, which increase the length of the code (some).
This is one drawback of volatile.
I am right?

assume you have an timer interrupt handler which sest a global variable every second, e.g.
Code:
volatile int second=0; 	// set every second 

void __attribute__((__interrupt__, __shadow__)) _T1Interrupt(void)
{
/* Interrupt Service Routine code goes here */
IFS0bits.T1IF = 0; 							//Reset Timer1 interrupt flag														
second=1;
}

int main(void)
{
...
startTimer();
second=0;
while(!second);      // wait 1 second
    ...                // code executed after a second

   
}
in main second is tested in the while() statement which executes every second

the problem is that if second is not declared volatile an optimising compiler will look at
Code:
second=0;
while(!second) ;     // wait 1 second
see that second is initialised to 0 and remove the while() statement - thus whole sections of code can be removed
the volatile keyword tells the compiler that the variable may be altered outside the current context and code should not be optimised
for more detail see
https://en.cppreference.com/w/cpp/language/cv
 

Thanks for the reply....
Is there any separate memory for these volatile variables? where these are stored?

volatile doesn't allow to make perform optimization by compiler on the volatile variables, which increase the length of the code (some).
This is one drawback of volatile.
I am right?
I assume volatile variables would be allocated on the heap along with other global variables - you could always look at memory maps to check
you would only use volatile in situations where you don't want optimisation, e.g. global variables used to pass information between interrupt routines and other parts of the program
 

Global variables are allocated in the data memory but you said heap.
Ok, I will check with memory maps for volatile variables.

Anyway, thanks a lot, you cleared my doubt.....



I assume volatile variables would be allocated on the heap along with other global variables - you could always look at memory maps to check
you would only use volatile in situations where you don't want optimisation, e.g. global variables used to pass information between interrupt routines and other parts of the program
 

Global variables are allocated in the data memory but you said heap.
Ok, I will check with memory maps for volatile variables.

Anyway, thanks a lot, you cleared my doubt.....
both heap and satck are what you would call data memory
1. heap for permanent (during execution of the program) variables such as global external variables and static variables (defined inside a function) - also dynamic storage allocated by functions such as malloc and calloc
2. stack for temporary data such as automatic variables defined inside function and the information pushed onto the stack during a function call

have a look at
https://www-ee.eng.hawaii.edu/~tep/EE160/Book/chap14/subsection2.1.1.8.html
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top