I know I have to use "volatile" to variable "aaa" but do I also need to declare as volatile all the variables that are used inside the function "bbb"?
I know volatile makes sure the value of the variable gets updated correctly in case of an interrupt. To me it's intuitive that the variables inside "bbb" should be volatile but I did some tests and my code worked fine without "volatile" in any of the variables used inside the function bbb, I mean: the expected values of variable were correctly.
I also ask you: should I declare the function "bbb" as volatile? Is it possible to declare a function as volatile?
A variable needs to be declared volatile if it is used by two or more functions and one of them is an interrupt handler. Since your code only has one variable, and one function, none of the variables need to be volatile.
Of course, your code won't even compile and link, but that's a different problem.
@PaulS I think your assumption is wrong. I use the variable aaa and the function bbb inside my "loop" function. I update the values of several variables inside "bbb". So should I use volatile in this case?
KeithRB:
It depends whether they are global or not, which you have declined to tell us.
Not entirely true. Variable scope can range from local to a small block of code within a function, all the way up to global to the entire program, with many levels in-between (such as local to a function, local to one file, etc.). What matters is whether the variable is modified by an interrupt handler, and also read by other code outside the interrupt handler. The variable absolutely does NOT have to be global.
But only variables with global scope can be seen in the interrupt handler. How do you get a variable with function scope to share a variable in an ISR?
No thanks. Exposing a pointer to a static local buffer is best described as poorly structured code. (The other example that comes to mind is already predefined with volatile.)
In any case, this thread has already drifted too far into the weeds. @batata004 is working with a global variable so that should be the focus of our attention.
@Coding Badly is correctly. I am dealing with global variable. So what is the veredict? I am using global variables that are updated/accessed inside the loop function. Should I still declare those global variables as volatile if they are used inside a function (in this case "bbb" function) that is inside an interrupt?
Should all the variables inside the bbb() function be declared as volatile? What is the risk of not doing so? Do I have the risk that some value setted inside the bbb function does not ovewrite the value of the variable when I retrive it later on the loop function?
If I have a variable that is not volatile inside the bbb() funtion and the value of that variable is always false (I initialize that global variable as false and I nver change its value inside my code except in the interrup) and assume that the interrupt is triggered and it changes the value of the variable to true, will this new value be seen by my code inside loop function? Or I have a risk that in some cases this variable will not be setted to true because of the lack of volatile?
I appreciate your help and this question may look stupid but it's not. Sometime ago I was incrementing a variable inside an interrupt and the variable WAS NOT volatile. I discovered later, after LONG time studying, that sometimes the interrupt was called the value of the variable didnt increment and other times it did. Of course, I was incrementing the variable, the interrupt should get the current value of the variable, sum one unit and set it again. But in the paragraph above you see that I say I SET the value to TRUE. The chip will not have to get the value (which was false) and set it to TRUE, it can only set it to TRUE and that's it, correct?