// For boards with I2C bus power control, may need to delay to allow // MPU6050 to come up after initial power. bool mpu_found = false; for (uint8_t tries = 0; tries < 5; tries++) { mpu_found = i2c_dev->begin(); if (mpu_found) break; delay(10); } if (!mpu_found) returnfalse; // 读取MPU6050_WHO_AM_I Adafruit_BusIO_Register chip_id = Adafruit_BusIO_Register(i2c_dev, MPU6050_WHO_AM_I, 1); // 检查MPU6050_WHO_AM_I是否等于MPU6050_DEVICE_ID // make sure we're talking to the right chip if (chip_id.read() != MPU6050_DEVICE_ID) { returnfalse; }
voidMPU6050_Read(int address, uint8_t *data){ // Read from MPU6050. Needs register address, data array int size = sizeof(*data); // Wire.beginTransmission(MPU6050_I2C_ADDRESS); // Begin talking to MPU6050 Wire.write(address); // Set register address Wire.endTransmission(false); // Hold the I2C-bus Wire.requestFrom(MPU6050_I2C_ADDRESS, size, true); // Request bytes, release I2C-bus after data read int i = 0; // while (Wire.available()) { // data[i++] = Wire.read(); // Add data to array } }
voidWhoAmI(){ uint8_t waiByte; // Data will go here MPU6050_Read(MPU6050_WHO_AM_I, &waiByte); // Get data Serial.print(F("Device WhoAmI reports as: 0x")); // Serial.println(waiByte, HEX); // Report WhoAmI data }