如果将字节按位带拆分是没问题的,如下
1 typedef union 2 { 3 unsigned char byte; 4 struct 5 { 6 unsigned char bit0:1; 7 unsigned char bit1:1; 8 unsigned char bit2:1; 9 unsigned char bit3:1; 10 unsigned char bit4:1; 11 unsigned char bit5:1; 12 unsigned char bit6:1; 13 unsigned char bit7:1; 14 }Bits; 15 }strbits_TypeDef;
但如果是16位的short数据进行拆分以下编译通不过的:
1 typedef unsigned short int u16; 2 3 Typedef union 4 { 5 u16 Data16; 6 7 struct 8 { // 低字节低位,低字节高位,高字节低位,高字节高位 9 u16 bit0:1; // 10 u16 bit1:1; // 11 u16 bit2:1; // 12 u16 bit3:1; // 13 u16 bit4:1; // 14 u16 bit5:1; // 15 u16 bit6:1; // 16 u16 bit7:1; // 17 u16 bit8:1; // 18 u16 bit9:1; // 19 u16 bit10:1; // 20 u16 bit11:1; // 21 u16 bit12:1; // 22 u16 bit13:1; // 23 u16 bit14:1; // 24 u16 bit15:1;// 25 }Bits; 26 }REG16_TypeDef;
但是可以用下面写法就没问题:
1 typedef union 2 { 3 unsigned short int Data16; 4 struct 5 { 6 unsigned char bit0:1; 7 unsigned char bit1:1; 8 unsigned char bit2:1; 9 unsigned char bit3:1; 10 unsigned char bit4:1; 11 unsigned char bit5:1; 12 unsigned char bit6:1; 13 unsigned char bit7:1; 14 unsigned char bit8:1; 15 unsigned char bit9:1; 16 unsigned char bit10:1; 17 unsigned char bit11:1; 18 unsigned char bit12:1; 19 unsigned char bit13:1; 20 unsigned char bit14:1; 21 unsigned char bit15:1; 22 }Bits; 23 }strbits_TypeDef2;