各种应用的宏定义define

只有一种可能的情况:

    宏的定义:

    #define MPU6050
    #define AK89xx_BYPASS

    宏变量的定义:

     #define MAXLEN  1024

    函数名替换:

    #define i2c_write   msp430_i2c_write

    函数名加变量的替换

    #define i2c_write(a, b, c, d)   twi_write(a, b, d, c)
    #define i2c_read(a, b, c, d)    twi_read(a, b, d, c)

    宏函数的简单定义: 

    #define fabs(x)     (((x)>0)?(x):-(x))
    #define min(a,b) ((a<b)?a:b)

    宏函数的定义:

     #define handle_error(msg) \
                do {\
                    printk("%s----%d\n",__func__,__LINE__); \
                      perror(msg); \
                    exit(EXIT_FAILURE); \
                  }while (0)

只有两种可能的情况:

    函数外两种情况下的宏开关:

    #ifdef AK89xx_BYPASS
        i2c_write(st.chip_cfg.compass_addr, AKM_REG_CNTL, 1, tmp+8);
    #else
        i2c_read(st.hw->addr, st.reg->raw_compass, 8, tmp);
    #endif

    函数内的宏开关

    int mpu_get_compass_fsr(unsigned short *fsr)
    {
    #ifdef AK89xx_SECONDARY
        fsr[0] = st.hw->compass_fsr;
        return 0;
    #else
        return -1;
    #endif
    }

函数外的多重宏开关

    #if defined MPU6050
         functions......
    #elif defined MPU6500
        functions......
    #elif defined MPU9250
        .....
    #endif

    两重定义

    #if defined AK8975_SECONDARY || defined AK8963_SECONDARY
    #define AK89xx_SECONDARY
    #else
    #warning "No compass = less profit for Invensense. Lame." */
    #endif

    多重嵌套情况1

    #if !defined MPU6050 && !defined MPU9250  &&  (defined  AK8975_SECONDARY || defined AK8963_SECONDARY)
    #error  Which gyro are you using? Define MPUxxxx in your compiler options.
    #endif

    多重嵌套情况2

    #if defined MPU9150

        #ifndef MPU6050
            #define MPU6050
        #endif                          /* #ifndef MPU6050 */

        #if defined AK8963_SECONDARY
            #error "MPU9150 and AK8963_SECONDARY cannot both be defined."
        #elif !defined AK8975_SECONDARY /* #if defined AK8963_SECONDARY */
            #define AK8975_SECONDARY
        #endif                          /* #if defined AK8963_SECONDARY */

    #elif defined MPU9250           /* #if defined MPU9150 */

        #ifndef MPU6500
            #define MPU6500
        #endif                          /* #ifndef MPU6500 */

        #if defined AK8975_SECONDARY
            #error "MPU9250 and AK8975_SECONDARY cannot both be defined."
        #elif !defined AK8963_SECONDARY /* #if defined AK8975_SECONDARY */
            #define AK8963_SECONDARY
        #endif                          /* #if defined AK8975_SECONDARY */
    #endif                          /* #if defined MPU9150 */

        宏定义在程序当中是使用非常多的,所以需要弄清楚每一种宏定义的具体含义,如何去使用宏定义在什么情况使用使用什么样得宏定义是非常重要的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啵啵520520

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值