3

我正在开发一个使用联合的 C 程序。联合定义在 FILE_A 头文件中,如下所示...

// FILE_A.h****************************************************
xdata union  
{
long position;
char bytes[4];
}CurrentPosition;

如果我在 FILE_A.c 中设置 CurrentPosition.position 的值,然后在 FILE_B.c 中调用一个使用联合的函数,联合中的数据将归零。这在下面演示。

// 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];
}

现在,如果我将 long 传递给 SomeFunctionInFileB(long temp) 然后将其存储到该函数中的 CurrentPosition.bytes 中,最后调用 WriteBytesToFlash(CurrentPosition.bytes[n]... 它工作得很好。

CurrentPosition Union 似乎不是全球性的。所以我尝试更改头文件中的联合定义以包含这样的 extern 关键字......

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

那么我做错了什么?如何让工会全球化?

4

4 回答 4

7

FILE_A.h真的吗MotionControl.h?如果是这样,我认为解决方法是在标题中定义一个联合类型:

typedef
union xdata
{
    long position;
    char bytes[4];
} xdata;

并在头文件的其他地方声明该类型的全局变量(可能是同一个):

extern xdata CurrentPosition;   // in a header file

最后在 C 文件中只定义一次全局变量。也许在file_a.c

xdata CurrentPosition;

当然,更好的解决方法可能是将xdata要写出的变量传递给闪存,SomeFunctionInFileB()这样您就不必依赖全局变量,众所周知,在不非常非常小心使用时会出现问题。似乎没有充分的理由不将数据作为参数传递:

// in a header file
void SomeFunctionInFileB( xdata const* pPosition);


void SomeFunctionInFileB( xdata const* pPosition)
{
    // After the following lines execute I see all zeros in the flash memory.
    WriteByteToFlash(pPosition->bytes[0];
    WriteByteToFlash(pPosition->bytes[1];
    WriteByteToFlash(pPosition->bytes[2];
    WriteByteToFlash(pPosition->bytes[3];
}

并这样称呼它:

int main.c(void)
{
    CurrentPosition.position = 12345;
    SomeFunctionInFileB( &CurrentPosition);
}
于 2010-05-04T19:55:57.230 回答
2

理想情况下,您需要联合的 typedef 和 FILE_A.h 中的 extern声明以及 FILE_A.c 中联合的实际定义

-

// FILE_A.h

typedef union  
{
    long position;
    char bytes[4];
} Position;

extern Position CurrentPosition; // declaration

-

// FILE_A.c

#include "FILE_A.h"

Position CurrentPosition; // definition

int main(void)
{
    CurrentPosition.position = 12345;
    SomeFunctionInFileB();
    return 0;
}

-

// FILE_B.c

#include "FILE_A.h"

void SomeFunctionInFileB(void)
{
    // now there will be valid data in the flash memory.
    WriteByteToFlash(cp.bytes[0];
    WriteByteToFlash(cp.bytes[1];
    WriteByteToFlash(cp.bytes[2];
    WriteByteToFlash(cp.bytes[3];
}

-

于 2010-05-04T19:52:23.613 回答
1

你还没有实例化联合。
你需要 :

// FILE_A.c****************************************************

#include "File_a.h"
CurrentPosition cp;
int main(void)
{
    cp.position = 12345;
    SomeFunctionInFileB();
}

// FILE_B.c****************************************************
#include "File_a.h"
extern CurrentPosition cp;
void SomeFunctionInFileB(void)
{
    // now there will be valid data in the flash memory.
    WriteByteToFlash(cp.bytes[0];
    WriteByteToFlash(cp.bytes[1];
    WriteByteToFlash(cp.bytes[2];
    WriteByteToFlash(cp.bytes[3];
}
于 2010-05-04T19:54:04.857 回答
0

如果sizeof(long)不是 4,那么字节序就起作用了……

考虑

union{
   long position
   char bytes[sizeof long];
}
于 2010-05-04T20:21:57.320 回答