我正在尝试将变量从不同的端口映射到 IO。我能找到的最接近的例子是:
union {
struct
{ // specify each bit in this char byte
unsigned bit0:1 ; // name each member and size
unsigned bit1:1 ;
unsigned bit2:1 ;
unsigned bit3:1 ;
unsigned bit4to6:3 ; // example set to 3 bits
unsigned bit7:1 ;
};
unsigned char allbits; // overall type of union
} Flag ; // name of union = Flag
Flag.allbits = 0x12; // sets value of union/bits to 0x12
Flag. bit2 = 0; // clear the if (Flag. bit2==1), etc
if (Flag. bit2 == 1) etc
是否有可能让 bit0、bit1、bit2 等具有来自不同端口的 IO 位?像这样的东西:
union {
struct
{ // specify each bit in this char byte
LATAbits.LATA5:1 ; // name each member and size
LATAbits.LATA7:1 ;
LATBbits.LATB2:1 ;
LATBbits.LATB4:1 ;
LATBbits.LATB5:1 ;
LATCbits.LATC0:1 ;
LATCbits.LATC1:1 ;
LATCbits.LATC2:1 ;
};
unsigned char allbits; // overall type of union
} Flag ; // name of union = Flag
Flag.allbits = 0x12; // sets value of union/bits to 0x12
对我来说重要的是能够设置整个联合的值,而不必访问单个位。