C语言数据结构笔记3:Union联合体+结构体取8位Bool量

本文衔接上文要求,新增8位bool量的获取方式。

目录

问题提出:

Union联合体+struct结构体(方式1):

Union联合体+struct结构体(方式2):

BYTE方式读取:


问题提出:

在STM32单片机的编程中,无法定义Bool或者bit类型

但有时候,比如modbus通信时,需要把bool量八个八个地组合读取,少于8个的部分填充0

Union联合体+struct结构体(方式1):

这里是考虑到超过8位的使用场景,因此定义了超过3字节的定义方式

这种方式适合那种喜欢一个一个列出所有寄存器位名称的情况。

cpp 复制代码
#define BITS_PER_BYTE 8
#define BYTES_FOR_BITS 3  //
 
// 定义3字节的位字段结构
typedef union {
    uint8_t bytes[BYTES_FOR_BITS];  // 整个字节数组访问
    struct {
        // 每个字节单独定义位字段
        struct {
            uint8_t b0 : 1;
            uint8_t b1 : 1;
            uint8_t b2 : 1;
            uint8_t b3 : 1;
            uint8_t b4 : 1;
            uint8_t b5 : 1;
            uint8_t b6 : 1;
            uint8_t b7 : 1;
        } byte0;
        struct {
            uint8_t b0 : 1;
            uint8_t b1 : 1;
            uint8_t b2 : 1;
            uint8_t b3 : 1;
            uint8_t b4 : 1;
            uint8_t b5 : 1;
            uint8_t b6 : 1;
            uint8_t b7 : 1;
        } byte1;
        struct {
            uint8_t b0 : 1;
            uint8_t b1 : 1;
            uint8_t b2 : 1;
            uint8_t b3 : 1;
            uint8_t b4 : 1;
            uint8_t b5 : 1;
            uint8_t b6 : 1;
            uint8_t b7 : 1;
        } byte2;
    } bits;
} LargeBitField;
cpp 复制代码
#include <stdio.h>

typedef unsigned char uint8_t;
typedef unsigned short int    uint16_t;
typedef signed short int      int16_t;

#pragma pack(push, 1) //:将结构体的对齐方式设置为 1 字节,并将当前对齐设置保存到堆栈中。

//0x03e8 - 0x03ee
typedef struct  ABC_regs1
{
    uint16_t  A1;
    uint16_t  B1;
    int16_t   C1;
    int16_t   D1;
    uint16_t  E1;
    int16_t   F1;
    int16_t   G1;
}ABC_regs_1;

//0x177B - 0x1781
typedef struct  ABC_regs2
{
    uint16_t  A2;
    uint16_t  B2;
    int16_t   C2;
    int16_t   D2;
    uint16_t  E2;
    int16_t   F2;
    int16_t   G2;
}ABC_regs_2;

#define BITS_PER_BYTE 8
#define BYTES_FOR_BITS 3  //
 
// 定义15字节的位字段结构
typedef union {
    uint8_t bytes[BYTES_FOR_BITS];  // 整个字节数组访问
    struct {
        // 每个字节单独定义位字段
        struct {
            uint8_t b0 : 1;
            uint8_t b1 : 1;
            uint8_t b2 : 1;
            uint8_t b3 : 1;
            uint8_t b4 : 1;
            uint8_t b5 : 1;
            uint8_t b6 : 1;
            uint8_t b7 : 1;
        } byte0;
        struct {
            uint8_t b0 : 1;
            uint8_t b1 : 1;
            uint8_t b2 : 1;
            uint8_t b3 : 1;
            uint8_t b4 : 1;
            uint8_t b5 : 1;
            uint8_t b6 : 1;
            uint8_t b7 : 1;
        } byte1;
        struct {
            uint8_t b0 : 1;
            uint8_t b1 : 1;
            uint8_t b2 : 1;
            uint8_t b3 : 1;
            uint8_t b4 : 1;
            uint8_t b5 : 1;
            uint8_t b6 : 1;
            uint8_t b7 : 1;
        } byte2;
    } bits;
} LargeBitField;


typedef struct  Letter_regs
{
    ABC_regs_1 ABC1;  //0x03e8 - 0x03f1 //7成员 14字节 //偏移量 0x00- 0x0c
    ABC_regs_2 ABC2;  //0x177B - 0x1781 //7成员 14字节 
    //BOOL_1     Bool1; //
    LargeBitField Bytes;
}letter_regs;



int main(void)
{
    letter_regs reg;


    // 设置位
    reg.Bytes.bits.byte0.b0 = 1;  // 设置byte0的第0位为1
    reg.Bytes.bits.byte1.b7 = 1;  // 设置byte1的第7位为1
 
    // 读取位
    uint8_t bit0 = reg.Bytes.bits.byte0.b0;
    uint8_t bit7 = reg.Bytes.bits.byte1.b7;
 
    printf("Bit 0 of byte0: %d\n", bit0);
    printf("Bit 7 of byte1: %d\n", bit7);
 
    // 通过字节数组访问
    reg.Bytes.bytes[0] = 0x0F;  // 设置byte0为0x0F
    printf("Byte 0: 0x%02X\n", reg.Bytes.bytes[0]);


}

Union联合体+struct结构体(方式2):

这种方式不一个一个列出寄存器名称,直接通过地址偏移操作:

cpp 复制代码
#include <stdio.h>

typedef unsigned char uint8_t;
typedef unsigned short int    uint16_t;
typedef signed short int      int16_t;

#pragma pack(push, 1) //:将结构体的对齐方式设置为 1 字节,并将当前对齐设置保存到堆栈中。

//0x03e8 - 0x03ee
typedef struct  ABC_regs1
{
    uint16_t  A1;
    uint16_t  B1;
    int16_t   C1;
    int16_t   D1;
    uint16_t  E1;
    int16_t   F1;
    int16_t   G1;
}ABC_regs_1;

//0x177B - 0x1781
typedef struct  ABC_regs2
{
    uint16_t  A2;
    uint16_t  B2;
    int16_t   C2;
    int16_t   D2;
    uint16_t  E2;
    int16_t   F2;
    int16_t   G2;
}ABC_regs_2;

// 定义字节的位字段结构
typedef union {
    uint8_t byte;  // 整个字节数组访问
        struct {
            uint8_t b0 : 1;
            uint8_t b1 : 1;
            uint8_t b2 : 1;
            uint8_t b3 : 1;
            uint8_t b4 : 1;
            uint8_t b5 : 1;
            uint8_t b6 : 1;
            uint8_t b7 : 1;
        } bits;
}BitField;


typedef struct  Letter_regs
{
    ABC_regs_1 ABC1;  //0x03e8 - 0x03f1 //7成员 14字节 //偏移量 0x00- 0x0c
    ABC_regs_2 ABC2;  //0x177B - 0x1781 //7成员 14字节 
    BitField Bytes[10];
}letter_regs;



int main(void)
{
    letter_regs reg;
    // 通过直接赋值给联合体的字节
    reg.Bytes[0].byte = 0xA5;  // 0xA5 in hex is 10100101 in binary
 
    // 通过位域赋值
    reg.Bytes[1].bits.b0 = 1;  // Set bit 0 to 1
    reg.Bytes[1].bits.b7 = 1;  // Set bit 7 to 1
 
    // 读取并打印结果
    printf("Byte 0: 0x%02X\n", reg.Bytes[0].byte);  // 输出 0xA5
    printf("Byte 1: 0x%02X\n", reg.Bytes[1].byte);  // 输出 0x81 (因为 b7 和 b0 被设置为1)
 
}

BYTE方式读取:

cpp 复制代码
#include <stdio.h>

typedef unsigned char uint8_t;
typedef unsigned short int    uint16_t;
typedef signed short int      int16_t;
#pragma pack(push, 1) //:将结构体的对齐方式设置为 1 字节,并将当前对齐设置保存到堆栈中。

//0x03e8 - 0x03ee
typedef struct  ABC_regs1
{
    uint16_t  A1;
    uint16_t  B1;
    int16_t   C1;
    int16_t   D1;
    uint16_t  E1;
    int16_t   F1;
    int16_t   G1;
}ABC_regs_1;

//0x177B - 0x1781
typedef struct  ABC_regs2
{
    uint16_t  A2;
    uint16_t  B2;
    int16_t   C2;
    int16_t   D2;
    uint16_t  E2;
    int16_t   F2;
    int16_t   G2;
}ABC_regs_2;

// 定义字节的位字段结构
typedef union {
    uint8_t byte;  // 整个字节数组访问
        struct {
            uint8_t b0 : 1;
            uint8_t b1 : 1;
            uint8_t b2 : 1;
            uint8_t b3 : 1;
            uint8_t b4 : 1;
            uint8_t b5 : 1;
            uint8_t b6 : 1;
            uint8_t b7 : 1;
        } bits;
}BitField;

typedef struct  Letter_regs
{
    ABC_regs_1 ABC1;  //0x03e8 - 0x03f1 //7成员 14字节
    ABC_regs_2 ABC2;  //0x177B - 0x1781 //7成员 14字节 
    BitField Bytes[10]; //0x0400 - 0x0450 //80 成员 
}letter_regs;

int main(void)
{
    letter_regs reg;
    // 通过直接赋值给联合体的字节
    reg.Bytes[0].byte = 0xAA;  // 0xA5 in hex is 10100101 in binary
     reg.Bytes[8].byte = 0x6A;  // 0xA5 in hex is 10100101 in binary
    unsigned char* ptr = (unsigned char*)&reg.Bytes;

    //print_struct_values(&reg);

     //联合体读8个,实际物理地址才偏移1,相对于基地址地偏移量,都一个BYTE才偏移1
     for (int j = 0; j < 10; j++) //读10个BYTE 整个联合体
    {
        unsigned char offset = j; // 计算偏移量
        uint8_t value = *(const uint8_t*)(ptr + offset);
         printf("Byte %d: 0x%02X\n",j,value);
    }
}
相关推荐
蒙奇D索大6 小时前
【数据结构】考研数据结构核心考点:二叉排序树(BST)全方位详解与代码实现
数据结构·笔记·学习·考研·算法·改行学it
玲娜贝儿--努力学习买大鸡腿版6 小时前
推荐算法学习笔记(十九)阿里SIM 模型
笔记·学习·推荐算法
我命由我123458 小时前
Photoshop - Photoshop 工具栏(10)透视裁剪工具
经验分享·笔记·学习·ui·职场和发展·职场发展·photoshop
JJJJ_iii8 小时前
【深度学习03】神经网络基本骨架、卷积、池化、非线性激活、线性层、搭建网络
网络·人工智能·pytorch·笔记·python·深度学习·神经网络
玉石观沧海8 小时前
高压变频器故障代码解析F67 F68
运维·经验分享·笔记·分布式·深度学习
初级炼丹师(爱说实话版)10 小时前
MySql速成笔记5(多表关系)
笔记
iconball10 小时前
个人用云计算学习笔记 --19 (MariaDB服务器)
linux·运维·笔记·学习·云计算
岑梓铭11 小时前
《考研408数据结构》第四章(串和串的算法)复习笔记
数据结构·笔记·考研·算法
冬夜戏雪12 小时前
记录下C盘清理步骤(有效)
经验分享·笔记
我登哥MVP12 小时前
Apache Tomcat 详解
java·笔记·tomcat