类型转换为非类型定义的结构

时间:2012-05-24 13:45:47

标签: c struct io rtos micrium

基本上我从RTOS中的另一个线程获取数据。该数据是板上的引脚。所有IO端口都在iodefine.h文件中的结构/联合中。请参阅此示例,了解Micrium如何制作它:

struct st_portd {
union {
    unsigned char BYTE;
    struct {
        unsigned char B0:1;
        unsigned char B1:1;
        unsigned char B2:1;
        unsigned char B3:1;
        unsigned char B4:1;
        unsigned char B5:1;
        unsigned char B6:1;
        unsigned char B7:1;
    } BIT;
} DDR;
unsigned char wk0[31];
union {
    unsigned char BYTE;
    struct {
        unsigned char B0:1;
        unsigned char B1:1;
        unsigned char B2:1;
        unsigned char B3:1;
        unsigned char B4:1;
        unsigned char B5:1;
        unsigned char B6:1;
        unsigned char B7:1;
    } BIT;
} DR;
unsigned char wk1[31];
union {
    unsigned char BYTE;
    struct {
        unsigned char B0:1;
        unsigned char B1:1;
        unsigned char B2:1;
        unsigned char B3:1;
        unsigned char B4:1;
        unsigned char B5:1;
        unsigned char B6:1;
        unsigned char B7:1;
    } BIT;
} PORT;
unsigned char wk2[31];
union {
    unsigned char BYTE;
    struct {
        unsigned char B0:1;
        unsigned char B1:1;
        unsigned char B2:1;
        unsigned char B3:1;
        unsigned char B4:1;
        unsigned char B5:1;
        unsigned char B6:1;
        unsigned char B7:1;
    } BIT;
} ICR;
unsigned char wk3[95];
union {
    unsigned char BYTE;
    struct {
        unsigned char B0:1;
        unsigned char B1:1;
        unsigned char B2:1;
        unsigned char B3:1;
        unsigned char B4:1;
        unsigned char B5:1;
        unsigned char B6:1;
        unsigned char B7:1;
    } BIT;
} PCR;
};

如果你问我,这是非常聪明的方式。 所以我将这个引脚保存为结构中的2个字符,称为Port和Pin。

struct StepperMotor {
  CPU_INT32U ID;
  CPU_CHAR *EnablePort;
  CPU_CHAR EnablePin;
  CPU_CHAR *DirectionPort;
  CPU_CHAR DirectionPin;
  CPU_CHAR *PulsePort;
  CPU_CHAR PulsePin;
};

我想以这种方式使用引脚:

(struct st_portd)(steppermotor->PulsePort)->DR.BYTE ^= (1 << steppermotor->PulsePin);

steppermotor是结构。 只有这样我才会收到错误

  

请求成员'DR'不是结构或联合

我如何才能使用steppermotor-&gt; PulsePort-&gt; DR.BYTE而不为其创建新变量? 我希望有人能帮助我!

1 个答案:

答案 0 :(得分:2)

由于您正在构建指针,因此您应该将其转换为指针到结构,而不是结构本身,如下所示:

((struct st_portd*)steppermotor->PulsePort)->DR.BYTE ^= (1 << steppermotor->PulsePin);

另请注意,您的括号位于错误的位置。