写入加速度计配置寄存器时,MPU6050的SLEEP位置1

问题描述

这是我的STM32的I2C配置

static void I2C_Config(void)
{
  I2CHandle.Instance = I2C1;
  I2CHandle.Init.Timing= 0x2000090E;
  I2CHandle.Init.OwnAddress1= 0;
  I2CHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  I2CHandle.Init.DualAddressMode = I2C_DUALADDRESS_disABLE;
  I2CHandle.Init.OwnAddress2 = 0;
  I2CHandle.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  I2CHandle.Init.GeneralCallMode = I2C_GENERALCALL_disABLE;
  I2CHandle.Init.NoStretchMode = I2C_NOSTRETCH_disABLE;
  if (HAL_I2C_Init(&I2CHandle) != HAL_OK)
  {
    Error_Handler();
  }

}

在这里配置mpu6050单元

uint8_t check;
uint8_t Data;

    Data = 0x80;
    if (HAL_I2C_Mem_Write(&I2CHandle,mpu6050_ADDR,PWR_MGMT_1_REG,1,&Data,1000)!= HAL_OK)
    {       
        while(1)
        {

        }

    }

    if (HAL_I2C_Mem_Read(&I2CHandle,WHO_AM_I_REG,&check,1000)!=HAL_OK)
    {
        while(1)
        {

        }
    }

    if ((check) == 0x68)  // 0x68 will be returned by the sensor if everything goes well
    {
        //reset the unit 

        // power management register 0X6B we should write all 0's to wake the sensor up
        Data = 0x09;
        check = 0x00;
        if(HAL_I2C_Mem_Write(&I2CHandle,1000)!=HAL_OK)
        {   
            while(1)
            {

            }

        }
        HAL_I2C_Mem_Read(&I2CHandle,1000);
        if(check!=Data)
        {
            while(1)
            {

            }
        }

        //setting range of accel to += 2g,disabling self test
        Data = 0x08;
        check =0x00;
        if(HAL_I2C_Mem_Write(&I2CHandle,ACCEL_CONfig_REG,1000)!=HAL_OK)
         {
            while(1)
            {

            }

         }
        HAL_I2C_Mem_Read(&I2CHandle,1000);
        if(check!=0x09)
        {
            while(1)
            {

            }
        }

在最后一次read()调用中,PWR_MGMT_1_REG寄存器的SLEEP位置位。成功将0x09写入PWR_MGMT_1_REG,读取时返回正确的值。当我去写ACCEL_CONfig_REG时,SLEEP位置1。当我调试并逐行浏览代码时,它似乎正常工作。有什么想法吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)