Skip to content

关于i2c中的demo mpu6050例程原始数据处理 #15

Description

@perseverance51
  • 例程路径:FwLib_STC8\FwLib_STC8\demo\i2c\mpu6050:
  • mpu6050.c内容:
uint16_t swap(uint16_t num)
{
    return (num >> 8) | (num << 8);
}

void MPU6050_ReadAll(uint16_t *buf)
{
    uint8_t i;
    I2C_Read(MPU6050_ADDR, MPU6050_REG_ACCEL_XOUT_H, (uint8_t *)buf, 14);
    for (i = 0; i < 7; i++)
    {
        *(buf + i) = swap(*(buf + i));
    }
}

MPU6050 设备是以大端模式存储的,即高字节在前,低字节在后,在读取原始数据出来后,将高字节的数据左移8位,再或上低字节的数据。

  • 是否应该这样处理才合理:
uint16_t swap(uint16_t num)
{
	 return (num << 8);
}

void MPU6050_ReadAll(uint16_t *buf)
{
    uint8_t i;
    I2C_Read(MPU6050_ADDR, MPU6050_REG_ACCEL_XOUT_H, (uint8_t *)buf, 14);
    for (i = 0; i < 7; i++)
    {
        *(buf + i) = swap(*(buf + 2*i))|(*(buf + 2*i+1));
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions