首页 > 其他分享 >C语言 结构体、联合、位段

C语言 结构体、联合、位段

时间:2022-11-03 09:46:28浏览次数:62  
标签:reset GSTAT Register uint8 C语言 位段 Indicates 联合

例如,有一些寄存器,寄存器有一些位,每个位都控制不同的设置,要是想单独设置某一个位的值,用位段就是一个比较好的方法,

寄存器例子:

 

 第一种方法:如果内存小的话可能造成堆栈溢出

 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

相关文章

  • 如何判断点是否在多边形内部 (C语言版)
    概述这是图形学中的一个经典问题(point-in-polygon),一种比较简易的判断方法是射线法,就是以判断点作为端点,朝着任意方向绘制一条射线。如果射线与多边形交点为奇数个,就说明此......
  • C语言 模拟实现字符串函数 看着一篇够了
    C语言模拟实现字符串操作的库函数求字符串长度strlen思路1.如果碰到\0就代表字符串已经到了末尾size_tmy_strlen(constchar*str){ assert(str!=NULL); //......
  • 结构体的内存对齐与位段的结构体实现
     求结构体总大小(字节数)的规则结构体第一个成员在结构体变量偏移量从0开始地址数结构体其余成员对齐数的整数倍的地址数对齐数:默认的对齐数与结构体成员大小的较小值......
  • 四舍六入五成双(C语言版)
    四舍五入的小细节计算机的四舍五入与我们数学学的还是有点区别,下面开始讲解吧四舍五入的规则:如果需要约位的数<=4,舍去不进位如果需要约位的数>=6,舍6进1如果需要约......
  • C语言 旋转字符串
    C语言旋转字符串思路:1.循环需要旋转几次2.保存处于第一个的字符3.再将后面的所有字符往前挪动一位4.再把处于第一位的字符放在末尾//字符串旋转第一种解法......
  • C语言中出现[Error] assignment to expression with array type
    1.原因数组不能直接给数组赋值指针不能直接给数组赋值2.解决办法chara[]={'h','e','l','l','o'};charb[5];char*p=NULL;//错误情况charc[5]=a;//......
  • 实验3 C语言控制语句应用编程
    #include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_spaces......
  • C语言学习之写出四位数的各位的数值
    以下为今天学习到的程序的的部分函数解释。(过程参考了本平台博客好友“碟”的《C语言学习记录2(分别计算一个三位数的各位)》的程序)#include<stdio.h>     //此为声......
  • 学习C语言的第3天
    //dengfenfaintmain(){ chararr[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; inti; intsz=sizeof(arr)/sizeof(arr[0]); intk=14; intleft=0;......
  • 嵌入式-C语言基础:字符串比较函数strcmp及其实现
    #include<stdio.h>#include<string.h>intmystrcmp(char*p1,char*p2){intret=0;if(p1!=NULL||p2!=NULL){while(*p1==*p2){......