|
本帖最后由 littleshrimp 于 2017-10-11 17:47 编辑
分析了一下你的代码和测试结果,感觉问题还是出在I2C通信上(或者器件损坏?可能性不大)
你可以重点检查一下 yujie2510说的时序问题,确定通信正确如果有条件可以换个器件试试
另外你在测试时使用LOGI输出提示字符写寄存地名称看起来更直观些,使用“chip_rst2”这样的代号对比起来比较麻烦
如果你确定已经把I2C的通信问题解决了可以试试下边的方法
1、使用简单I2C通信,向寄存器地址0x12写入0x01(CTRL3_C ->SW_RESET = 1)
例如i2c_write_bytes(0x6b,0x12,0x01);器件地址(SLV_ADDRESS)为0x6b时(取决于pin1引脚的上下拉状态)
2、延时一段时间
3、读取全部寄存器数据,对比是否与数据手册声明的一致
4、向0x11写入0xac( CTRL2_G->ODR_G = 1010 = 6.66 kHz,FS_G = 11 = 2000dps)5、回读0x11地址,看数据是否为0xac
6、读取 OUTX_L_G、OUTX_H_G等陀螺仪X\Y\Z轴数据看是否正确
- lsm6dsl_block_data_update_set(&lsm6dsl_ctx, PROPERTY_ENABLE);
- lsm6dsl_read_reg(&lsm6dsl_ctx, LSM6DSL_CTRL3_C, &buffer, 1);
- LOGI("chip_rst1=%d\r\n",buffer);//0x68 01101000 //如果之前没对寄存器操作过,此处应该为0x44 01000100
- lsm6dsl_read_reg(&lsm6dsl_ctx, LSM6DSL_FIFO_CTRL5, &buffer, 1);
- LOGI("chip_rst2=%d\r\n",buffer);//0x00
- hal_gpt_delay_us(100);
- lsm6dsl_reset_set(&lsm6dsl_ctx, PROPERTY_ENABLE);
-
- //复位后最好加一段延时,等待复位完成再读取寄存器数据
- lsm6dsl_read_reg(&lsm6dsl_ctx, LSM6DSL_CTRL3_C, &buffer, 1);
- //hal_gpt_delay_us(100);
- //hal_gpt_delay_ms(100);
- LOGI("chip_rst3=%d\r\n",buffer);//0x04 //复位后CTRL3_C=0x04是正确的,前题是确定在执行lsm6dsl_read_reg(&lsm6dsl_ctx, LSM6DSL_CTRL3_C, &buffer, 1);前器件已经复位完成,最后在前边加一段延时
-
- do {
- lsm6dsl_reset_get(&lsm6dsl_ctx, &rst);
- } while (rst);
- LOGI("chip_rst4=%x\r\n",rst);//0x00
- //或者把读取CTRL3_C的操作放在这里
- /*
- *
- Set xl Full Scale
- */
- hal_gpt_delay_us(100);
- lsm6dsl_xl_full_scale_set(&lsm6dsl_ctx, FS_4g);
- lsm6dsl_read_reg(&lsm6dsl_ctx, LSM6DSL_CTRL1_XL, &buffer, 1);
- LOGI("Read result5:%d\n",buffer);//0x08 00001000 FS_XL=10(+-4g) //数据正确
- /*
- * Set gyro Full Scale
- */
- lsm6dsl_gyro_full_scale_set(&lsm6dsl_ctx, FS_2000dps);//FS_2000dps = (xxxx)110(x)
- lsm6dsl_read_reg(&lsm6dsl_ctx, LSM6DSL_CTRL2_G, &buffer, 1);
- LOGI("Read result6:%d\n",buffer);//0x12 00010010 125Hz 125dps(这个地方是错的,读取和写入不一至)
-
- //以下读取数据都与写入不一至
- /*
- * Enable Acc Block Data Update
- */
-
- hal_gpt_delay_us(100);
- lsm6dsl_block_data_update_set(&lsm6dsl_ctx, PROPERTY_ENABLE);
- lsm6dsl_read_reg(&lsm6dsl_ctx, LSM6DSL_CTRL3_C, &buffer, 1);
- LOGI("chip_rst7=%d\r\n",buffer);//0x00
-
- /*
- * Set xl Output Data Rate
- */
-
- lsm6dsl_xl_data_rate_set(&lsm6dsl_ctx, XL_12Hz5);
- lsm6dsl_read_reg(&lsm6dsl_ctx, LSM6DSL_CTRL1_XL, &buffer, 1);
- LOGI("Read result8:%d\n",buffer);//0x00
-
- /*
- * Set gyro Output Data Rate
- */
- lsm6dsl_gyro_data_rate_set(&lsm6dsl_ctx, GY_26Hz);
- lsm6dsl_read_reg(&lsm6dsl_ctx, LSM6DSL_CTRL2_G, &buffer, 1);
- LOGI("Read result9:%d\n",buffer);//0x00
- //lsm6dsl_gyro_data_rate_get(&lsm6dsl_ctx, testpp);
- //LOGI("Read result4:%d\n",*testpp);
- /*
- * Read samples in polling mode (no int)
- */
-
复制代码
|
|