满意答案
大大宝VS小小宝
推荐于 2017.09.19
采纳率:46% 等级:12
已帮助:5009人
底层涉及到寄存器按位访问的地方,可以使用union设计既可以按位访问,又可以按字节访问的结构
上个DSP芯片的程序给你看看
//底层涉及到寄存器按位访问的地方,可以使用union设计既可以按位访问,又可以按字节访问的结构
//上个DSP芯片的程序给你看看
/* Allow access to the bit fields or entire register */
union CANMD_REG {
Uint32 all;
struct CANMD_BITS bit;
};
/* eCAN Transmit Request Set register (CANTRS) bit definitions */
struct CANTRS_BITS { // bit description
Uint16 TRS0:1; // 0 TRS for Mailbox 0
Uint16 TRS1:1; // 1 TRS for Mailbox 1
Uint16 TRS2:1; // 2 TRS for Mailbox 2
Uint16 TRS3:1; // 3 TRS for Mailbox 3
Uint16 TRS4:1; // 4 TRS for Mailbox 4
Uint16 TRS5:1; // 5 TRS for Mailbox 5
Uint16 TRS6:1; // 6 TRS for Mailbox 6
Uint16 TRS7:1; // 7 TRS for Mailbox 7
Uint16 TRS8:1; // 8 TRS for Mailbox 8
Uint16 TRS9:1; // 9 TRS for Mailbox 9
Uint16 TRS10:1; // 10 TRS for Mailbox 10
Uint16 TRS11:1; // 11 TRS for Mailbox 11
Uint16 TRS12:1; // 12 TRS for Mailbox 12
Uint16 TRS13:1; // 13 TRS for Mailbox 13
Uint16 TRS14:1; // 14 TRS for Mailbox 14
Uint16 TRS15:1; // 15 TRS for Mailbox 15
Uint16 TRS16:1; // 16 TRS for Mailbox 16
Uint16 TRS17:1; // 17 TRS for Mailbox 17
Uint16 TRS18:1; // 18 TRS for Mailbox 18
Uint16 TRS19:1; // 19 TRS for Mailbox 19
Uint16 TRS20:1; // 20 TRS for Mailbox 20
Uint16 TRS21:1; // 21 TRS for Mailbox 21
Uint16 TRS22:1; // 22 TRS for Mailbox 22
Uint16 TRS23:1; // 23 TRS for Mailbox 23
Uint16 TRS24:1; // 24 TRS for Mailbox 24
Uint16 TRS25:1; // 25 TRS for Mailbox 25
Uint16 TRS26:1; // 26 TRS for Mailbox 26
Uint16 TRS27:1; // 27 TRS for Mailbox 27
Uint16 TRS28:1; // 28 TRS for Mailbox 28
Uint16 TRS29:1; // 29 TRS for Mailbox 29
Uint16 TRS30:1; // 30 TRS for Mailbox 30
Uint16 TRS31:1; // 31 TRS for Mailbox 31
};追问: 是的,这个例子确实比较好,我之前看了个例子,比如定义一个数组。如果union的话既可以访问这个数组整体,也可以访问数组的每一个项。确实很方便
00分享举报