Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
perseverance51 opened this issue Apr 6, 2023 · 1 comment
Open

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

perseverance51 opened this issue Apr 6, 2023 · 1 comment

Comments

@perseverance51
Copy link

perseverance51 commented Apr 6, 2023

  • 例程路径: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));
    }
}
@perseverance51 perseverance51 changed the title 关于i2c中的demo例程原始数据处理 关于i2c中的demo mpu6050例程原始数据处理 Apr 6, 2023
@IOsetting
Copy link
Owner

IOsetting commented Apr 10, 2023

原代码是按16bit读出然后交换高低8位成为新的16bit, 你的代码是按8bit读出, 然后两两组合成16bit, 我理解这两种方式是否都可以, 实际运行的输出是否有区别?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants