例如,有一些寄存器,寄存器有一些位,每个位都控制不同的设置,要是想单独设置某一个位的值,用位段就是一个比较好的方法,
寄存器例子:
第一种方法:如果内存小的话可能造成堆栈溢出
union _GSTAT_Register { uint32_t value; struct GSTAT_Register *GSTAT_Register_bit; }; struct GSTAT_Register { uint8_t reset:1; // Indicates that the IC has been reset since the last read access to GSTAT uint8_t drv_err:1; // Indicates that the driver has been shut down due to overtemperature or short circuit detection since the last read access uint8_t uv_cp:1; // Indicates an undervoltage on the charge pump. The driver is disabled in this case. };
第二种方法:比第一种占用的更小
union _GSTAT_Register { uint32_t value; struct { uint8_t reset:1; // Indicates that the IC has been reset since the last read access to GSTAT uint8_t drv_err:1; // Indicates that the driver has been shut down due to overtemperature or short circuit detection since the last read access uint8_t uv_cp:1; // Indicates an undervoltage on the charge pump. The driver is disabled in this case. }GSTAT_Register; };
union _GSTAT_Register gstat = {0};//调用先出事化
gstat.GSTAT_Register.reset = 1;
gstat.GSTAT_Register.uv_cp =1;
sendData(GSTAT, gstat.value);
位段在使用中可能是从第零位开始0,1,2,3,4等,也可能是从最高位开始
我这台是从零开始。
由于是一位一位操作所得到的数就是二进制数,不需要进制转换,与想传入的十进制数或者十六进制数一致。
标签:reset,GSTAT,Register,uint8,C语言,位段,Indicates,联合 From: https://www.cnblogs.com/mokongking/p/16853353.html