我不断在串行监视器中获得以下内容:
AcX = -1 | AcY = -1 | AcZ = -1 | Tmp = 36.53 | GyX = -1 | GyY = -1 | GyZ = -1

我正在使用以下从http://playground.arduino.cc/Main/MPU-6050#sketch下车的代码:

// MPU-6050 Short Example Sketch
// By Arduino User JohnChi
// August 17, 2014
// Public Domain
#include<Wire.h>
const int MPU=0x68;  // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
void setup(){
  Wire.begin();
  Wire.beginTransmission(MPU);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);
  Serial.begin(9600);
}
void loop(){
  Wire.beginTransmission(MPU);
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU,14,true);  // request a total of 14 registers
  AcX=Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
  AcY=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  AcZ=Wire.read()<<8|Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  Tmp=Wire.read()<<8|Wire.read();  // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
  GyX=Wire.read()<<8|Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  GyY=Wire.read()<<8|Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  GyZ=Wire.read()<<8|Wire.read();  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
  Serial.print("AcX = "); Serial.print(AcX);
  Serial.print(" | AcY = "); Serial.print(AcY);
  Serial.print(" | AcZ = "); Serial.print(AcZ);
  Serial.print(" | Tmp = "); Serial.print(Tmp/340.00+36.53);  //equation for temperature in degrees C from datasheet
  Serial.print(" | GyX = "); Serial.print(GyX);
  Serial.print(" | GyY = "); Serial.print(GyY);
  Serial.print(" | GyZ = "); Serial.println(GyZ);
  delay(333);
}


这是我正在使用的接线:http://jamesmpoe.com/roboticswiki/images/4/45/MPU6050-Arduino-Uno-Connections.jpg

我不知道怎么了。

谢谢您的帮助。

最佳答案

首先,我将时钟设置为X陀螺仪振荡器,而不是内部振荡器(如MPU6050数据表上建议的那样),因此

Wire.beginTransmission(MPU);
Wire.write(0x6B);  // PWR_MGMT_1 register
Wire.write(1);     // set to one to turn on the Xgyro oscillator
Wire.endTransmission(true);


但是,在我的项目中,我编写了更多的初始化...我建议您


编写一些函数来写入/读取I2C接口上的位,字节和字,因为您将需要它们
编写函数以获取和设置MPU6050中寄存器的重要部分
编写更复杂的函数。


例子:

// I2C interface
bool i2c_writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t* data)
{
    Wire.beginTransmission(devAddr);
    Wire.write((uint8_t) regAddr); // send address

    for (uint8_t i = 0; i < length; i++)
    {
        Wire.write((uint8_t) data[i]);
    }
    return (Wire.endTransmission() == 0);
}

// Simple function
int16_t mpu6050_getAccelerationX() {
    i2c_readBytes(MPU6050_ADDRESS, MPU6050_RA_ACCEL_XOUT_H, 2, rwBuffer);
    return (((int16_t)rwBuffer[0]) << 8) | rwBuffer[1];
}

// Complex function
void mpu6050_initialize()
{
    mpu6050_reset();
    delay(100);
    mpu6050_setClockSource(MPU6050_CLOCK_PLL_XGYRO);
    mpu6050_setSleepEnabled(false);
    mpu6050_setWakeCycleEnabled(false);
    mpu6050_switchSPIEnabled(false);
    mpu6050_setI2CMasterModeEnabled(false);
    mpu6050_setFIFOEnabled(false);
    delay(100);

    mpu6050_setRate(7); // 1kHz scan freq
    mpu6050_setDLPFMode(MPU6050_DLPF_BW_256);
    mpu6050_setFullScaleGyroRange(MPU6050_GYRO_FS_250);
    mpu6050_setFullScaleAccelRange(MPU6050_ACCEL_FS_2);
    mpu6050_setInterruptMode(MPU6050_INTMODE_ACTIVEHIGH);
    mpu6050_setInterruptDrive(MPU6050_INTDRV_PUSHPULL);
    mpu6050_setInterruptLatch(MPU6050_INTLATCH_WAITCLEAR);
    mpu6050_setInterruptLatchClear(MPU6050_INTCLEAR_ANYREAD);
    mpu6050_setFSyncInterruptEnabled(0);
    mpu6050_setI2CBypassEnabled(0);
    mpu6050_setClockOutputEnabled(0);
    mpu6050_setIntEnabled(MPU6050_INTERRUPT_MASK_DATA_RDY_BIT); // Interrupt when data ready
}


MPU6050寄存器映射是有用的资源(您可以在here中找到它)。

09-12 17:32