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.

How to use volilate int in microcontroller project?

Status
Not open for further replies.

Maverickmax

Advanced Member level 1
Joined
Dec 6, 2004
Messages
404
Helped
8
Reputation
16
Reaction score
3
Trophy points
1,298
Activity points
3,689
Hi

How to use to use volilate int in microcontroller project? I never understand this concept or how to use it. Please give me example of using it

MM
 

Re: volilate int

Hi,
I think you mean a variable that is declared in 'c' as say:

volatile int iValue

This type of declaration is usually used to declare a globally accessed variable, the value of which may be changed by an interrupt service routine that is servicing a timer for example. Most microcontroller 'c' compilers are deliberately designed to generate code that is as compact as possible so they optimise out the part of a routine that they consider unnecessry, even though the programmer included it in the c source code they had written intending the code to read the variable but the compiler writer has assumed that if a variable has not had its value changed up to that point within the function body (it had to have been declared as a static variable or its value would not have been remembered at all) it will be the same as it was the last time that the function used it, so they will use the old value in the calculation without reading it from the memory again, the calculation will probably not be carried out either if no other variable in it has been changed within the body of that function. Seems strange I know and it can cause some really wierd bugs when the microcontroller code is tested in harware because the programmer thinks that their code is doing exactly what is required but the compiler has thrown away a chunk of it so it does not do what the programmer intended. That is the way it is with most of the microcontroller c compilers that I have come across brecause a compiler is judged by the smallness of the compiled machine code that it produces, therefore the compiler writer tries very hard to achieve this, Keil for example in their documentation actually use the expression 'the compiler shoots itself in the foot sometimes', hence the use of declaration of a variable as volatile is necessary to prevent this from happening, easy to spot once you are aware of it but it can cause 'programmers hell' :cry: the first time it happens. :D
I hope that helps,
Bob.
 

Re: volilate int

**broken link removed**
then read this
volatile is a keyword, it tells compiler that this variable may change its value due to something not known to compiler.

e.g. considor following segment

unsigned char ch;

unsigned char my_func(void)
{

ch = 0xFF;

....... //some code lines
....... //these lines donot change value of ch directly or indirectly
.......
.......

if(ch == 0)
{
return(0);
}
else
{
return(1);
}

}//end of my_func



in the above segment ch is global variable initialised to 0xFF.
hence while compiling if..else compiler knows value of ch is 0xFF, hence it will omit the if..else statement & returns 1 irrespective of value of ch.
it is possible that some ISR is making ch = 0, which compiler may fail to visualise.


On the other hand if we declare it as volatile compiler will compile if..else though it knows that ch is initialised to 0xFF.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top