1

我在配置 MPU-6500 时遇到困难。我不确定如何校准它以获得准确的读数,因为我对此仍然很陌生。我正在使用 MPU-6500 和我的 Arduino Uno。下面是我在参考了我可以在网上找到的资源后编写的代码,并试图对其进行评论以试图理解代码。MPU-6050 有许多可用资源,但 MPU-6500 却没有。

#include<Wire.h>
const int MPU_addr = 0x68; // I2C address of the MPU-6500
int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ; //16-bit integer

void setup() {
  Wire.begin(); //join I2C bus
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6500)
  Wire.endTransmission(true); //sends a stop message after transmission, releasing I2C bus
  Serial.begin(9600);
}

void loop() {
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false); //sends a restart message after transmission
  Wire.requestFrom(MPU_addr, 14, true);
// master requests a total of 14 registers from slave, true = sends a stop message after the request, releasing I2C bus
  AcX = Wire.read() << 8; // 0x3B (ACCEL_XOUT_H) read most significant byte first
  AcX = Wire.read(); // 0x3C (ACCEL_XOUT_L) then least significant byte
  AcY = Wire.read() << 8; // 0x3D (ACCEL_YOUT_H) read most significant byte first
  AcY = Wire.read(); // 0x3E (ACCEL_YOUT_L) then least significant byte
  AcZ = Wire.read() << 8; // 0x3F (ACCEL_ZOUT_H)read most significant byte first
  AcZ = Wire.read(); // 0x40 (ACCEL_ZOUT_L) then least significant byte
  Tmp = Wire.read() << 8; // 0x41 (TEMP_OUT_H) read most significant byte first
  Tmp = Wire.read(); // 0x42 (TEMP_OUT_L) then least significant byte
  GyX = Wire.read() << 8; // 0x43 (GYRO_XOUT_H) read most significant byte first
  GyX = Wire.read(); // 0x44 (GYRO_XOUT_L) then least significant byte
  GyY = Wire.read() << 8; // 0x45 (GYRO_YOUT_H) read most significant byte first
  GyY = Wire.read(); // 0x46 (GYRO_YOUT_L) then least significant byte
  GyZ = Wire.read() << 8; // 0x47 (GYRO_ZOUT_H) read most significant byte first
  GyZ = Wire.read(); // 0x48 (GYRO_ZOUT_L) then least significant byte
  //results
  Serial.print("MPU-6500");
  Serial.println();
  Serial.print("Accelerometer X, Y, Z = "); //print accelerometer values
  Serial.print(AcX);
  Serial.print (", ");
  Serial.print(AcY);
  Serial.print (", ");
  Serial.print(AcZ);
  Serial.println();
  Serial.print("Temperature = "); //print temperature
  Serial.print(Tmp / 333.87 + 21); //equation for temperature in degrees C from datasheet
  Serial.print("°C");
  Serial.println();
  Serial.print("Gyroscope X, Y, Z = "); //print gyroscope values
  Serial.print(GyX);
  Serial.print (", ");
  Serial.print(GyY);
  Serial.print (", ");
  Serial.print(GyZ);
  Serial.println();
  Serial.print("\n");
  delay(2000);
}
4

0 回答 0