只有一种可能的情况:
宏的定义:
#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 */
宏定义在程序当中是使用非常多的,所以需要弄清楚每一种宏定义的具体含义,如何去使用宏定义在什么情况使用使用什么样得宏定义是非常重要的。