原创 [函数库][AVR]8位单片机常见数据类型位拆分(位段)

2007-11-23 15:37 5535 3 3 分类: MCU/ 嵌入式

RD_UseBits.h


--------------------------------------------------------------------------------


#ifndef _USE_BITS_H_
#define _USE_BITS_H_
/***********************************************************
*   声明库说明:系统位段操作函数库                         *
*   版本:      v1.01                                      *
*   作者:      傻孩子                                     *
*   创建时间:  2006年5月4日                               *
* -------------------------------------------------------- *
*  [支 持 库]                                              *
*   支持库名称:                                           *
*   需要版本:                                             *
*   支持库说明:                                           *
* -------------------------------------------------------- *
*  [版本更新]                                              *
*   修改:      傻孩子                                     *
*   修改日期:  2007年9月3日                               *
*   版本:      v1.01                                      *
* -------------------------------------------------------- *
*  [版本历史]                                              *
*      v1.00   包含了从32位到8位对16位8位4位1位的各种映射  *
*              允许通过功能宏定义中定义的宏拆分任意的地址  *
*      v1.01   修改了一处BUG                               *
* -------------------------------------------------------- *
*  [使用说明]                                              *
*          1、该头文件属于基层头文件。                     *
*          2、兼容32位16位8位单片机系统。                  *
*          3、饱含该头文件以后,可以通过功能宏定义中定义的 *
*             宏拆分任意地址,也可以直接使用后面自定义的位 *
*             段类型定义新的位段变量或者指针。             *
*          4、需要注意的是,使用功能宏定义中的宏只是将指定 *
*             变量的地址强行邦定为指定类型的位段,并不真正 *
*             分配一个地址空间,如果指定类型的位段长度和地 *
*             址实际长度不相同将会导致不可预期的后果。     *
***********************************************************/


/********************
*   系 统 宏 定 义  *
********************/


/*------------------*
*   常 数 宏 定 义  *
*------------------*/
   
/*------------------*
*   功 能 宏 定 义  *
*------------------*/
# define SET_BIT8_FORMAT(Addr)     (*((volatile BYTEBIT *)&Addr))
# define SET_BYTE_DIV_2(Addr)      (*((volatile BYTE2HBYTE *)&Addr))


# define SET_BIT16_FORMAT(Addr)    (*((volatile WORDBIT *)&Addr))
# define SET_WORD_DIV_2(Addr)      (*((volatile WORD2BYTE *)&Addr))
# define SET_WORD_DIV_4(Addr)      (*((volatile WORD2HBYTE *)&Addr))


# define SET_BIT32_FORMAT(Addr)    (*((volatile DWORDBIT *)&Addr))
# define SET_DWORD_DIV_2(Addr)     (*((volatile DWORD2WORD *)&Addr))
# define SET_DWORD_DIV_4(Addr)     (*((volatile DWORD2BYTE *)&Addr))
# define SET_DWORD_DIV_8(Addr)     (*((volatile DWORD2HBYTE *)&Addr))


/********************
* 结 构 体 定 义 区 *
********************/


/*------------------*
*   8位变量位映射   *
*------------------*/
typedef struct BYTE_BIT
{
    unsigned BIT0:1;
    unsigned BIT1:1;
    unsigned BIT2:1;
    unsigned BIT3:1;
    unsigned BIT4:1;
    unsigned BIT5:1;
    unsigned BIT6:1;
    unsigned BIT7:1;
}BYTEBIT;


/*------------------*
*   16位变量位映射  *
*------------------*/
typedef struct WORD_BIT
{
    unsigned BIT0:1;
    unsigned BIT1:1;
    unsigned BIT2:1;
    unsigned BIT3:1;
    unsigned BIT4:1;
    unsigned BIT5:1;
    unsigned BIT6:1;
    unsigned BIT7:1;
   
    unsigned BIT8:1;
    unsigned BIT9:1;
    unsigned BIT10:1;
    unsigned BIT11:1;
    unsigned BIT12:1;
    unsigned BIT13:1;
    unsigned BIT14:1;
    unsigned BIT15:1;
}WORDBIT;


/*------------------*
*  32位变量位映射   *
*------------------*/
typedef struct DWORD_BIT
{
    unsigned BIT0:1;
    unsigned BIT1:1;
    unsigned BIT2:1;
    unsigned BIT3:1;
    unsigned BIT4:1;
    unsigned BIT5:1;
    unsigned BIT6:1;
    unsigned BIT7:1;
   
    unsigned BIT8:1;
    unsigned BIT9:1;
    unsigned BIT10:1;
    unsigned BIT11:1;
    unsigned BIT12:1;
    unsigned BIT13:1;
    unsigned BIT14:1;
    unsigned BIT15:1;
   
    unsigned BIT16:1;
    unsigned BIT17:1;
    unsigned BIT18:1;
    unsigned BIT19:1;
    unsigned BIT20:1;
    unsigned BIT21:1;
    unsigned BIT22:1;
    unsigned BIT23:1;
   
    unsigned BIT24:1;
    unsigned BIT25:1;
    unsigned BIT26:1;
    unsigned BIT27:1;
    unsigned BIT28:1;
    unsigned BIT29:1;
    unsigned BIT30:1;
    unsigned BIT31:1;
}DWORDBIT;


/*------------------*
*  32位变量4分映射  *
*------------------*/
typedef struct DWORD_BYTE
{
    unsigned BYTEA:8;
    unsigned BYTEB:8;
    unsigned BYTEC:8;
    unsigned BYTED:8;
}DWORD2BYTE;


/*------------------*
*  32位变量2分映射  *
*------------------*/
typedef struct DWORD_WORD
{
    unsigned WORDA:16;
    unsigned WORDB:16;
}DWORD2WORD;


/*------------------*
*  32位变量16分映射 *
*------------------*/
typedef struct DWORD_HBYTE
{
    unsigned BYTEAL:4;
  unsigned BYTEAH:4;
  
  unsigned BYTEBL:4;
  unsigned BYTEBH:4;
  
  unsigned BYTECL:4;
  unsigned BYTECH:4;
  
  unsigned BYTEDL:4;
  unsigned BYTEDH:4;
  
}DWORD2HBYTE;


/*------------------*
* 16位变量2分映射   *
*------------------*/
typedef struct WORD_BYTE
{
    unsigned BYTEA:8;
    unsigned BYTEB:8;
}WORD2BYTE;


/*------------------*
*  16位变量4分映射  *
*------------------*/
typedef struct WORD_HBYTE
{
    unsigned BYTEAL:4;
  unsigned BYTEAH:4;
  
  unsigned BYTEBL:4;
  unsigned BYTEBH:4;
}WORD2HBYTE;


/*------------------*
*  8位变量2分映射   *
*------------------*/
typedef struct BYTE_DIV2
{
    unsigned BYTEAL:4;
    unsigned BYTEAH:4;
}BYTE2HBYTE;


#endif

文章评论0条评论)

登录后参与讨论
我要评论
0
3
关闭 站长推荐上一条 /2 下一条