I am working on a C program that uses Union. The join definition is in the FILE_A header file and looks like this:
// FILE_A.h****************************************************
xdata union
{
long position;
char bytes[4];
}CurrentPosition;
If I set CurrentPosition.position to FILE_A.c and then call a function in FILE_B.c that uses a join, the data in the join will return to zero. This is shown below.
// FILE_A.c****************************************************
int main.c(void)
{
CurrentPosition.position = 12345;
SomeFunctionInFileB();
}
// FILE_B.c****************************************************
void SomeFunctionInFileB(void)
{
// After the following lines execute I see all zeros in the flash memory.
WriteByteToFlash(CurrentPosition.bytes[0];
WriteByteToFlash(CurrentPosition.bytes[1];
WriteByteToFlash(CurrentPosition.bytes[2];
WriteByteToFlash(CurrentPosition.bytes[3];
}
Now, if I go to SomeFunctionInFileB (long pace) and then save it in CurrentPosition.bytes inside this function and finally call WriteBytesToFlash (CurrentPosition.bytes [n] ... it works fine.
It seems that Union CurrentPosition is not global. So I tried to change the join definition in the header file to include an extern keyword like this ...
extern xdata union
{
long position;
char bytes[4];
}CurrentPosition;
(.c)...
xdata union
{
long position;
char bytes[4];
}CurrentPosition;
:
C:\SiLabs\Optec Programs\AgosRot\MotionControl.c:76: error 91: extern definition for 'CurrentPosition' mismatches with declaration.
C:\SiLabs\Optec Programs\AgosRot\/MotionControl.h:48: error 177: previously defined here
? ?