[应用相关] STM32中位变量的实现

[复制链接]
606|0
 楼主| 木木guainv 发表于 2021-9-7 08:04 | 显示全部楼层 |阅读模式
由于STM32没有位处理,所以STM32不像51单片机一样支持BIT变量,下面教大家怎么在STM32中实现位变量
1、首先定义一个结构体

typedef struct
{
    unsigned char  b0:1;
    unsigned char  b1:1;
    unsigned char  b2:1;
    unsigned char  b3:1;
    unsigned char  b4:1;
    unsigned char  b5:1;
    unsigned char  b6:1;
    unsigned char  b7:1;
} BIT8;


2、然后宏定义就行

unsigned char addrNewCode;

#define         addrNewBit0        (((BIT8*) & addrNewCode) -> b0)
#define         addrNewBit1        (((BIT8*) & addrNewCode) -> b1)
#define         addrNewBit2        (((BIT8*) & addrNewCode) -> b2)
#define         addrNewBit3        (((BIT8*) & addrNewCode) -> b3)
#define            addrNewBit4        (((BIT8*) & addrNewCode) -> b4)
#define         addrNewBit5        (((BIT8*) & addrNewCode) -> b5)
#define         addrNewBit6        (((BIT8*) & addrNewCode) -> b6)
#define         addrNewBit7        (((BIT8*) & addrNewCode) -> b7)


3、这样的话addrNewBit0 等等就能当成位变量使用了

您需要登录后才可以回帖 登录 | 注册

本版积分规则

190

主题

4344

帖子

5

粉丝
快速回复 在线客服 返回列表 返回顶部