From 509f7a67724bca9a3222a41c870da747df97f720 Mon Sep 17 00:00:00 2001 From: Stanley Huang Date: Tue, 2 Apr 2013 16:00:54 +0800 Subject: add MPU6050 library --- libraries/MPU6050/MPU6050.cpp | 195 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 195 insertions(+) create mode 100644 libraries/MPU6050/MPU6050.cpp (limited to 'libraries/MPU6050/MPU6050.cpp') diff --git a/libraries/MPU6050/MPU6050.cpp b/libraries/MPU6050/MPU6050.cpp new file mode 100644 index 0000000..86901d9 --- /dev/null +++ b/libraries/MPU6050/MPU6050.cpp @@ -0,0 +1,195 @@ +// MPU-6050 Accelerometer + Gyro +// ----------------------------- +// +// By arduino.cc user "Krodal". +// June 2012 +// Open Source / Public Domain +// +// Using Arduino 1.0.1 +// It will not work with an older version, +// since Wire.endTransmission() uses a parameter +// to hold or release the I2C bus. +// +// Documentation: +// - The InvenSense documents: +// - "MPU-6000 and MPU-6050 Product Specification", +// PS-MPU-6000A.pdf +// - "MPU-6000 and MPU-6050 Register Map and Descriptions", +// RM-MPU-6000A.pdf or RS-MPU-6000A.pdf +// - "MPU-6000/MPU-6050 9-Axis Evaluation Board User Guide" +// AN-MPU-6000EVB.pdf +// +// The accuracy is 16-bits. +// +// Temperature sensor from -40 to +85 degrees Celsius +// 340 per degrees, -512 at 35 degrees. +// +// At power-up, all registers are zero, except these two: +// Register 0x6B (PWR_MGMT_2) = 0x40 (I read zero). +// Register 0x75 (WHO_AM_I) = 0x68. +// + +#include +#include "MPU6050.h" + +// -------------------------------------------------------- +// MPU6050_read +// +// This is a common function to read multiple bytes +// from an I2C device. +// +// It uses the boolean parameter for Wire.endTransMission() +// to be able to hold or release the I2C-bus. +// This is implemented in Arduino 1.0.1. +// +// Only this function is used to read. +// There is no function for a single byte. +// +int MPU6050_read(int start, uint8_t *buffer, int size) +{ + int i, n; + + Wire.beginTransmission(MPU6050_I2C_ADDRESS); + n = Wire.write(start); + if (n != 1) + return (-10); + + n = Wire.endTransmission(false); // hold the I2C-bus + if (n != 0) + return (n); + + // Third parameter is true: relase I2C-bus after data is read. + Wire.requestFrom(MPU6050_I2C_ADDRESS, size, true); + i = 0; + while(Wire.available() && ireg.x_accel_h, accel_t_gyro->reg.x_accel_l); + SWAP (accel_t_gyro->reg.y_accel_h, accel_t_gyro->reg.y_accel_l); + SWAP (accel_t_gyro->reg.z_accel_h, accel_t_gyro->reg.z_accel_l); + SWAP (accel_t_gyro->reg.t_h, accel_t_gyro->reg.t_l); + SWAP (accel_t_gyro->reg.x_gyro_h, accel_t_gyro->reg.x_gyro_l); + SWAP (accel_t_gyro->reg.y_gyro_h, accel_t_gyro->reg.y_gyro_l); + SWAP (accel_t_gyro->reg.z_gyro_h, accel_t_gyro->reg.z_gyro_l); +} -- cgit v1.2.3