矿石收音机论坛

 找回密码
 加入会员

QQ登录

只需一步,快速开始

搜索
查看: 1744|回复: 8

用union操作不可位操作的寄存器

[复制链接]
     
发表于 2020-4-7 07:59:27 | 显示全部楼层 |阅读模式
本帖最后由 peiguoqing 于 2020-4-7 08:13 编辑

先定义个共用体;

typedef union
{
  unsigned char v;
  struct _a{
  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;
  }b;
}BITCHAR ;

举个栗子:
union BITCHAR t;

t.v = P0;

t.b.b0 = 1;

P0 =t.v;
     
发表于 2020-4-7 08:35:10 | 显示全部楼层
完美,虽然底层机器码不一定那么漂亮
回复 支持 反对

使用道具 举报

     
发表于 2020-4-7 08:46:36 | 显示全部楼层
struct a
不需要定义。直接都写到union里面去就OK了。
回复 支持 反对

使用道具 举报

     
 楼主| 发表于 2020-4-7 08:50:54 | 显示全部楼层
ssffzz1 发表于 2020-4-7 08:46
struct a
不需要定义。直接都写到union里面去就OK了。

举个例子吧
回复 支持 反对

使用道具 举报

     
 楼主| 发表于 2020-4-7 08:54:51 | 显示全部楼层



struct _a{
  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;
  }  bc;
bc = P0;
bc.b1 = 1;
bc.b2=0;
P0 = bc;
这样吗?我试试
回复 支持 反对

使用道具 举报

     
 楼主| 发表于 2020-4-7 08:57:43 | 显示全部楼层
bc = P0;不行的。
回复 支持 反对

使用道具 举报

     
发表于 2020-4-7 09:04:05 | 显示全部楼层
本帖最后由 ssffzz1 于 2020-4-7 09:19 编辑

抱歉我看错了,还是要加那个struct a的。但这样访问比较麻烦。我提供了另外一种方法:

#include <stdio.h>
#include <stdlib.h>

typedef union{
    unsigned char v;
    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;
    }a;
#define b0 a.b0
#define b1 a.b1
#define b2 a.b2
#define b3 a.b3
#define b4 a.b4
#define b5 a.b5
#define b6 a.b6
#define b7 a.b7
}BITCHAR;

int
main(void)
{
    BITCHAR x;

    x.v=0x53;

    printf("xv=%x\n",x.v);
    printf("b0=%d,b1=%d,b2=%d,b3=%d\n",x.b0,x.b1,x.b2,x.b3);
    printf("b4=%d,b5=%d,b6=%d,b7=%d\n",x.b4,x.b5,x.b6,x.b7);

    return(EXIT_SUCCESS);
}
回复 支持 反对

使用道具 举报

     
 楼主| 发表于 2020-4-7 09:32:11 | 显示全部楼层
ssffzz1 发表于 2020-4-7 09:04
抱歉我看错了,还是要加那个struct a的。但这样访问比较麻烦。我提供了另外一种方法:

#include


typedef union{
    unsigned char v;//整体操作一个字节
    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;
    }a;//这一结构操作位;
    struct{
        unsigned char hi:4;
        unsigned char lo:4;
    }b;//这一结构操作BCD码

#define b0 a.b0
#define b1 a.b1
#define b2 a.b2
#define b3 a.b3
#define b4 a.b4
#define b5 a.b5
#define b6 a.b6
#define b7 a.b7

#define high b.hi
#define low b.lo

}BITCHAR;
回复 支持 反对

使用道具 举报

     
发表于 2020-4-7 11:34:29 | 显示全部楼层
peiguoqing 发表于 2020-4-7 09:32
typedef union{
    unsigned char v;//整体操作一个字节
    struct{

你厉害,没问题的,这个完全可以。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 加入会员

本版积分规则

小黑屋|手机版|矿石收音机 ( 蒙ICP备05000029号-1 )

蒙公网安备 15040402000005号

GMT+8, 2025-4-29 07:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表