Other Parts Discussed in Thread: CCSTUDIO
I am having an issue that seems similar to
and
I can't find a CCStudio forum off-hand, so please point me there if you deem this a IDE/Complier-linker issue.
For example
I declare a global variable, say
unsigned int Global_A = 1;
and use it as a semaphore flag within my code.
The Problem is that my Global_A variable seems to change value during run time (not debug, Release Run) and I get weird results sometimes. Since it is a flag, I only set it as "1" or "0" in my code, but sometimes (once every ... maybe... 10000 loops of the code) Global_A changes to a value that is not "1" or "0" (checked by turning on LED if Global_A is neither 1 nor 0) and if ever my watchdog triggers a reset, the value of Global_A also goes haywire.
BUT IF i declare it as
volatile unsigned int Global_A = 1;
I seem to not get any problems with the value of Global_A, although restarting due to the Watchdog sometimes does result in an incorrect initailisation value (verified by output of value via UART port), but that can be solved by declaring
Global_A = 1;
within the first few lines of Main(), so that isn't a big problem.
My question is why do I need to declare global variables (or even potentially local variables) as 'volatile' so that the value does not jump wildly during runtime.
Has anyone else encountered this before? Is this a hardware or a complier-linker thing? I can code the same code on other IDEs and not encounter this issue, so I don't think its a code thing.