My code is becoming a bit hard to follow so I'm moving parts of it in to tabs..
Got an interrupt on timer and want to move it's contents into a tab too.
So have to declare the
void function(){..code..}
But since it will be in an interrupt do I have to declare it as volatile void? Timer value will be getting changed based on main code.
IDE allowed me to declare a volatile void, it compiled and seems to be running fine..
But whole idea seems a bit odd..
No. The volatile keyword is used for variables so the compiler will not optimize them out when it appears the variable is not being modified in the main code.
volatile tells the compiler it should not optimize a memory access away, because you know something the compiler does not.
C compilers do not have a concept of memory changing by itself or having an effect in the physical world. So, when you read or write twice to the same memory location the compiler is allowed to assume one of the operations was not necessary.
So, when you have a variable that can be changed during interrupts you need to declare the variable volatile. This will tell the compiler to always read the variable from memory and not use a "copy" in the CPU registers.
An interrupt service function cannot have any parameters by definition and cannot return any parameters. A volatile void parameter does not make any sense. The compiler likely gives you a warning but not an error. Because void cannot be volatile so it can be ignored.
Klaus_K:
A volatile void does not make any sense. The compiler likely gives you a warning but not an error. Because voids cannot be volatile so it can be ignored.
They are FUNCTIONS, not VOIDS! void is simply the return type == it returns nothing.
Regards,
Ray L.
I was talking about the parameters as you can see from the sentence before, but I see how it can be misunderstood. Modified it. I called them voids because there are two. Nothing in, Nothing out. Thanks
In other words no need to declare functions as volatile.
Cool, thank you for explaining! 