C 语言:字节/位的获取,置位,清位操作,头文件宏定义,大小端转换的宏操作,函数切换

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

///防止头文件重复包含
#ifdef _HEAD_H_
#define _HEAD_H_

///如果C++ 引用,提醒C++编译器用C语言的方式去编译
#ifdef __cplusplus
extern "C"{
#endif


///对字节操作
#define    GET_LOW_BYTE0(x)    ((x >>  0) & 0x000000ff)    /* 获取第0个字节 */
#define    GET_LOW_BYTE1(x)    ((x >>  8) & 0x000000ff)    /* 获取第1个字节 */
#define    GET_LOW_BYTE2(x)    ((x >> 16) & 0x000000ff)    /* 获取第2个字节 */
#define    GET_LOW_BYTE3(x)    ((x >> 24) & 0x000000ff)    /* 获取第3个字节 */
 
#define    CLEAR_LOW_BYTE0(x)    (x &= 0xffffff00)    /* 清零第0个字节 */
#define    CLEAR_LOW_BYTE1(x)    (x &= 0xffff00ff)    /* 清零第1个字节 */
#define    CLEAR_LOW_BYTE2(x)    (x &= 0xff00ffff)    /* 清零第2个字节 */
#define    CLEAR_LOW_BYTE3(x)    (x &= 0x00ffffff)    /* 清零第3个字节 */

#define    SET_LOW_BYTE0(x)  (x |= 0x000000ff)    /* 第0个字节置1 */
#define    SET_LOW_BYTE1(x)  (x |= 0x0000ff00)    /* 第1个字节置1 */
#define    SET_LOW_BYTE2(x)  (x |= 0x00ff0000)    /* 第2个字节置1 */
#define    SET_LOW_BYTE3(x)  (x |= 0xff000000)    /* 第3个字节置1 */


///对位节操作
#define    GET_BIT(x, bit)    ((x & (1 << bit)) >> bit)    /* 获取第bit位 */
#define    CLEAR_BIT(x, bit)    (x &= ~(1 << bit))        /* 清零第bit位 */
#define    SET_BIT(x, bit)    (x |= (1 << bit))            /* 置位第bit位 */
#define BIT_M_TO_N(x, m, n)  ((unsigned int)(x << (31-(n))) >> ((31 - (n)) + (m)))   /* 获取第[n:m]位的值  eg 第二位到第三位:[2:3] */

///大小端转换
// 4bytes、32bit数据大小端转化
#define L2B32(Little) (((Little & 0xff) << 24) | (((Little) & 0xff00) << 8) | (((Little) & 0xff0000) >> 8) | ((Little >> 24) & 0xff))
// 2bytes、16bit数据大小端转化
#define L2B16(Little) (((Little & 0xff) << 8) | ((Little >> 8) & 0xff))



///   u0_Printf/u1_Printf/u2_Printf 三个串口的打印函数,可以自由切打印函数
void (*Printf)(char* fmt,...);
///选择串口
#define  UART   2

///选择打印内容
#define  DEBUGALL    0
#define  DEBUGTIME   0
#define  NOTHING     0

#if UART == 0
#define Printf   u0_Printf

#elif UART == 1
#define Printf   u1_Printf

#else
#define Printf   u2_Printf

#endif

#if DEBUGALL
  #define DBG_PRINTF(fmt, args...)  \
  {\
    Printf("<<File:%s  Line:%d  Function:%s>> ", __FILE__, __LINE__, __FUNCTION__);\
    Printf(fmt, ##args);\
  }
#elif DEBUGTIME
    #define DBG_PRINTF(fmt, args...)  \
    {\
        Printf("<<Time:%s>> ", __TIME__);\
        Printf(fmt, ##args);\
    }
#elif NOTHING
    #define DBG_PRINTF(fmt, args...)  \
    {\
        NOP();\
    }
#else
  #define DBG_PRINTF(fmt, args...)\
    {\
        Printf(fmt, ##args);\
    }
#endif

///函数外声明
extern  void DeBug_Init(void);
extern  void DeBug_Task(void);


#ifdef __cplusplus
};
 #endif

#endif _HEAD_H_

  • 4
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值