打印
[PIC®/AVR®/dsPIC®产品]

PIC单片机位数组定义问题

[复制链接]
583|9
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
Kingstom8MB|  楼主 | 2019-11-19 13:01 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
是这样的,我在写c程序时想定义一个位数组,但是苦于不知道如何定义及使用...纳闷好半天,有没有大佬知道要如何用呢?可不可以教小弟一下

使用特权

评论回复
沙发
Latin_newday| | 2019-11-19 14:18 | 只看该作者
typedef union
{
   unsigned char Byte;
   struct
   {
           unsigned char Bit0:1;
           unsigned char Bit1:1;
           unsigned char Bit2:1;
           unsigned char Bit3:1;
           unsigned char Bit4:1;
           unsigned char Bit5:1;
           unsigned char Bit6:1;
           unsigned char Bit7:1;
   }bits;
}BYTE;

使用特权

评论回复
板凳
CoolSilicon| | 2019-11-19 14:33 | 只看该作者
有很多问题,其实baidu一下也会有答案的

使用特权

评论回复
地板
幸福小强| | 2019-11-22 13:23 | 只看该作者
定义一个位数组
联合体啊

使用特权

评论回复
5
幸福小强| | 2019-11-22 13:23 | 只看该作者
你可以看,很多头文件里面就有这种用法。

使用特权

评论回复
6
幸福小强| | 2019-11-22 13:26 | 只看该作者
位段,C语言允许在一个结构体中以位为单位来指定其成员所占内存长度,这种以位为单位的成员称为“位段”或称“位域”( bit field) 。利用位段能够用较少的位数存储数据。

使用特权

评论回复
7
幸福小强| | 2019-11-22 13:26 | 只看该作者
struct CHAR
{
    unsigned int ch   : 8;    //8位
    unsigned int font : 6;    //6位
    unsigned int size : 18;   //18位
};
struct CHAR ch1;

使用特权

评论回复
8
幸福小强| | 2019-11-22 13:27 | 只看该作者
通常, 我们用下面的语句来说明这些变量: 
char f1,f2,f3;
unsigned int type;
unsigned int index;

但是, 实际上标志 f1, f2, f3 分别只需要 1 位。变量 type 只需要 4 位, 而变量 index 只需要 9 位。 总共是 16位 ---- 2 个字节。我们用两个字节就够了。

我们可这样来做:
struct packed_struct
{
    unsigned int f1 :1;
    unsigned int f2 :1;
    unsigned int f3 :1;
    unsigned int type :4;
    unsigned int index :9;
};

使用特权

评论回复
9
幸福小强| | 2019-11-22 13:28 | 只看该作者
10
coshi| | 2019-12-16 13:32 | 只看该作者
这个 度娘就能解决吧

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

3

主题

11

帖子

0

粉丝