|

楼主 |
发表于 2015-1-26 19:06:47
|
显示全部楼层
5.1的库,就是看库里边的函数,大概看看明白是什么意思就行了,初始化就是用inv_mpu.c里边的那个初始化,就是对照着代码一步一步查看阅读,MDK编译器的GO TO功能挺好用
int mpu_init(void)
{
unsigned char data[6], rev;
/* Reset device. */
data[0] = 0x80;//BIT_RESET; // 这里写0x80就是复位MPU,可以直接查MPU_PWR_MGMT1_REG寄存器的设置,电源管理1
if (i2c_write(st.hw->addr, st.reg->pwr_mgmt_1, 1, &(data[0])))
return -1;
delay_ms(100);
/* Wake up chip. */
data[0] = 0x00; // 这里写0x00就是唤醒MPU,可以直接查MPU_PWR_MGMT1_REG寄存器的设置,电源管理1
if (i2c_write(st.hw->addr, st.reg->pwr_mgmt_1, 1, &(data[0])))
return -1;
#if defined MPU6050
/* Check product revision. */
if (i2c_read(st.hw->addr, st.reg->accel_offs, 6, data))
return -1;
rev = ((data[5] & 0x01) << 2) | ((data[3] & 0x01) << 1) |
(data[1] & 0x01);
if (rev) {
/* Congrats, these parts are better. */
if (rev == 1)
st.chip_cfg.accel_half = 1;
else if (rev == 2)
st.chip_cfg.accel_half = 0;
else {
log_e("Unsupported software product rev %d.\n");
return -1;
}
} else {
if (i2c_read(st.hw->addr, st.reg->prod_id, 1, &(data[0])))
return -1;
rev = data[0] & 0x0F;
if (!rev) {
log_e("Product ID read as 0 indicates device is either "
"incompatible or an MPU3050.\n");
return -1;
} else if (rev == 4) {
log_i("Half sensitivity part found.\n");
st.chip_cfg.accel_half = 1;
} else
st.chip_cfg.accel_half = 0;
}
#elif defined MPU6500
#define MPU6500_MEM_REV_ADDR (0x17)
if (mpu_read_mem(MPU6500_MEM_REV_ADDR, 1, &rev))
return -1;
if (rev == 0x1)
st.chip_cfg.accel_half = 0;
else {
log_e("Unsupported software product rev %d.\n", rev);
return -1;
}
/* MPU6500 shares 4kB of memory between the DMP and the FIFO. Since the
* first 3kB are needed by the DMP, we'll use the last 1kB for the FIFO.
*/
data[0] = BIT_FIFO_SIZE_1024 | 0x8;//设置FIFO
if (i2c_write(st.hw->addr, st.reg->accel_cfg2, 1, data))
return -1;
#endif
/* Set to invalid values to ensure no I2C writes are skipped. */
st.chip_cfg.sensors = 0xFF;
st.chip_cfg.gyro_fsr = 0xFF;
st.chip_cfg.accel_fsr = 0xFF;
st.chip_cfg.lpf = 0xFF;
st.chip_cfg.sample_rate = 0xFFFF;
st.chip_cfg.fifo_enable = 0xFF;
st.chip_cfg.bypass_mode = 0xFF;
#ifdef AK89xx_SECONDARY
st.chip_cfg.compass_sample_rate = 0xFFFF;
#endif
/* mpu_set_sensors always preserves this setting. */
st.chip_cfg.clk_src = INV_CLK_PLL;
/* Handled in next call to mpu_set_bypass. */
st.chip_cfg.active_low_int = 1;
st.chip_cfg.latched_int = 0;
st.chip_cfg.int_motion_only = 0;
st.chip_cfg.lp_accel_mode = 0;
memset(&st.chip_cfg.cache, 0, sizeof(st.chip_cfg.cache));
st.chip_cfg.dmp_on = 0;
st.chip_cfg.dmp_loaded = 0;
st.chip_cfg.dmp_sample_rate = 0;
if (mpu_set_gyro_fsr(2000))//设置陀螺仪量程2000dps
return -1;
if (mpu_set_accel_fsr(2))//设置加速度量程2g
return -1;
if (mpu_set_lpf(42))//设置滤波器
return -1;
if (mpu_set_sample_rate(50))//设置采样频率
return -1;
if (mpu_configure_fifo(0))//设置FIFO
return -1;
/*if (int_param)
reg_int_cb(int_param);*/
#ifdef AK89xx_SECONDARY
setup_compass();
if (mpu_set_compass_sample_rate(10))
return -1;
#else
/* Already disabled by setup_compass. */
if (mpu_set_bypass(0))
return -1;
#endif
mpu_set_sensors(0);
return 0;
} |
|