c语言存储缝隙,干货分享 | C语言的联合体

#include struct  bit_struct {  char  bit1:3;  char  bit2:5;  char  bit3:7;}data;int main(){   data.bit1 = 0xAA;  data.bit2 = 0xAA;  data.bit3 = 0xAA; printf("sizeof(data) = %x \n data.bit1 = %x \n data.bit2 = %x \n data.bit3 = %x \n",sizeof(bit_struct),data.bit1,data.bit2,data.bit3);  system("pause");  return 0;}

运行结果:https://www.forlinx.com/file.php?f=202103/f_0c94766b2ab3f1af37a979b0f91b1b11&t=png&o=&s=&v=16148515843、联合使用举一个MCP2518FD芯片的例子:先看一下CAN帧格式:https://www.forlinx.com/file.php?f=202103/f_91c23e7a3bbff95c2016f56a199ed2fc&t=png&o=&s=&v=1614851584

//占用4个字节typedef struct _CAN_MSGOBJ_ID {uint32_t SID : 11;uint32_t EID : 18;uint32_t SID11 : 1;uint32_t unimplemented1 : 2;} CAN_MSGOBJ_ID;//占用4个字节typedef struct _CAN_TX_MSGOBJ_CTRL {uint32_t DLC : 4;uint32_t IDE : 1;uint32_t RTR : 1;uint32_t BRS : 1;uint32_t FDF : 1;uint32_t ESI : 1;#ifdef MCP2517FDuint32_t SEQ : 7;uint32_t unimplemented1 : 16;#elseuint32_t SEQ : 23;#endif} CAN_TX_MSGOBJ_CTRL;//占用4个字节typedef uint32_t CAN_MSG_tiMESTAMP;//没有用到typedef union _CAN_TX_MSGOBJ {struct {CAN_MSGOBJ_ID id; //占4个字节CAN_TX_MSGOBJ_CTRL ctrl; //占4个字节CAN_MSG_TIMESTAMP timeStamp;//占4个字节} bF; //共享12个字节uint32_t word;//共享12个字节uint8_t byte;//共享12个字节} CAN_TX_MSGOBJ;txObj.bF.id.SID = CAN_TX_ID;txObj.bF.ctrl.DLC = CAN_DLC_4;//发送的数据长度txObj.bF.ctrl.IDE = 0;//标识符扩展位,在扩展帧中恒为隐性1,在标准帧中,IDE位于控制段,且恒为显性0txObj.bF.ctrl.BRS = 0;//BRS(Bit Rate Switch)位速率转换开关,当BRS为显性位时数据段的位速率与仲裁段的位速率一致,当BRS为隐性位时数据段的位速率高于仲裁段的位速率txObj.bF.ctrl.FDF = 0;//扩展数据长度,在标准的CAN帧中,控制场包含的保留位被指定为显性位发送,但是在CAN-FD帧中以隐性位发送,主要用于区分标准CAN帧格式和CAN-FD的帧格式n = DRV_CANFDSPI_DlcToDataBytes(CAN_DLC_4);for (i = 0; i < n; i++){txd = Count;Count++;}uint8_t txBuffer;txBuffer = txObj->byte; //not using 'for' to reduce no of instructionstxBuffer = txObj->byte;txBuffer = txObj->byte;txBuffer = txObj->byte;txBuffer = txObj->byte;txBuffer = txObj->byte;txBuffer = txObj->byte;txBuffer = txObj->byte;uint8_t i;for (i = 0; i < txdNumBytes; i++){txBuffer = txd;}// Make sure we write a multiple of 4 bytes to RAMuint16_t n = 0;uint8_t j = 0;if (txdNumBytes % 4){// Need to add bytesn = 4 - (txdNumBytes % 4);i = txdNumBytes + 8;for (j = 0; j < n; j++){txBuffer = 0;}}spiTransferError = DRV_CANFDSPI_WriteByteArray(index, a, txBuffer,txdNumBytes + 8 + n);if (spiTransferError){return -4;}// Set UINC and TXREQspiTransferError = DRV_CANFDSPI_TransmitChannelUpdate(index, channel,flush);if (spiTransferError){return -5;}return spiTransferError;原文链接:https://www.forlinx.com/article_view_660.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值