2

基本上我正在从 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 个字符,称为端口和引脚。

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->PulsePort->DR.BYTE 而无需为其创建新变量?我希望有人能帮助我!

4

1 回答 1

2

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

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

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

于 2012-05-24T13:53:12.797 回答