On Sun, 28 Aug 2005, Bob Curley wrote:
> I am having difficultly declaring a global variable that can be seen by the
> interrupt routine and main. The program compiles correctly but each
> function has a unique copy of the variable. The only work around that I
> have found it to fix the variable to a specific address location.
> xdata unsigned int interruptcount (unique variables for each
> function, unless they are in same file)
> xdata at 0x2000 unsigned int interruptcount (all functions use same
> I would appreciate any assistance.
If you have a variable that is used by both an interrupt handler and a
regular function, you should always use the "volatile" keyword:
xdata volatile unsigned int interruptcount;
This lets the compiler know that the value may change spontaneously (like
when an interrupt occurs) so it will avoid reusing copies of the
variable's value kept in registers or spill locations. Although not
officially defined behavour, the "at" keyword is also implying "volatile"
at the moment, so this may be why the second case is working.
If the problem is just between functions in different files, you might try
adding the "extern" keyword to all of this variable's declaractions except
for one. I believe it is a linker implementation issue as to how
declarations of a variable with the same name in seperate files are
resolved when "extern" has not been used, but I don't know offhand what
the ASXXXX linker does.