分享

MPU-6050 Accelerometer + Gyro

 dwlinux_gs 2014-08-25

MPU-6050 Accelerometer + Gyro

Navigation


Introduction

The InvenSense MPU-6050 sensor contains a MEMS accelerometer and a MEMS gyro in a single chip. It is very accurate, as it contains 16-bits analog to digital conversion hardware for each channel. Therefor it captures the x, y, and z channel at the same time. The sensor uses the I2C-bus to interface with the Arduino.

The MPU-6050 is not expensive, especially given the fact that it combines both an accelerometer and a gyro.

Photo: GY-521 breakout board

Also note that Invensense has combined the MPU-6050 with a magnetometer (compass) in a single chip called MPU-9150.


Reading raw values is easy, the rest is not

Reading the raw values for the accelerometer and gyro is easy. The sleep mode has to be disabled, and then the registers for the accelerometer and gyro can be read.

But the sensor also contains a 1024 byte FIFO buffer. The sensor values can be programmed to be placed in the FIFO buffer. And the buffer can be read by the Arduino.

The FIFO buffer is used together with the interrupt signal. If the MPU-6050 places data in the FIFO buffer, it signals the Arduino with the interrupt signal so the Arduino knows that there is data in the FIFO buffer waiting to be read.

A little more complicated is the ability to control a second I2C-device.
The MPU-6050 always acts as a slave to the Arduino with the SDA and SCL pins connected to the I2C-bus.
But beside the normal I2C-bus, it has it's own I2C controller to be a master on a second (sub)-I2C-bus. It uses the pins AUX_DA and AUX_CL for that second (sub)-I2C-bus.
It can control, for example, a magnetometer. The values of the magnetometer can be passed on to the Arduino.

Things get really complex with the "DMP".
The sensor has a "Digital Motion Processor" (DMP), also called a "Digital Motion Processing Unit". This DMP can be programmed with firmware and is able to do complex calculations with the sensor values.
For this DMP, InvenSense has a discouragement policy, by not supplying enough information how to program the DMP. However, some have used reverse engineering to capture firmware.

The DMP ("Digital Motion Processor") can do fast calculations directy on the chip. This reduces the load for the microcontroller (like the Arduino). The DMP is even able to do calculations with the sensor values of another chip, for example a magnetometer connected to the second (sub)-I2C-bus.


Code

The accelerometer and gyro values are called the "raw" values. This is just as with other accelerometer and gyro sensors. A more sophisticated application is using the DMP to retrieve specific computed values from the sensor.

The Short example sketch on this page is a very short sketch that shows all the raw values. Click "Get code" at right, below the sketch, and copy it into a sketch.

The Example sketch (base code) on this page is also just showing the raw values, but it is an attempt to be a complete base for more functions.

For serious use of the MPU-6050, Jeff Rowberg has done an excellent job.
See his I2C lib: http://www./devices/mpu6050
Or the latest code on github: https://github.com/jrowberg/i2cdevlib/tree/master/Arduino/MPU6050

The FreeIMU library includes the MPU-6050 code from Jeff Rowberg.
The FreeIMU library: http://www./projects/hardware/FreeIMU

To start with the MPU-6050, see the page of InvenSense: http://www./mems/gyro/mpu6050.html

For other programs and sensors, see the Degrees Of Freedom, 6DOF, 9DOF, 10DOF, 11DOF-section in the Playground index.


Multiple sensors

The pin "AD0" selects between I2C address 0x68 and 0x69. That makes it possible to have two of these sensors in a project. Most breakout boards have a pullup or pulldown resistor to make AD0 default low or high. Connect AD0 to GND or 3.3V for the other I2C address.

When more MPU-6050 sensors are needed in a project, the I2C-bus can be extended with multiplexers. However, someone in the forum mentioned a nice trick:

Trick
Using more than two MPU-6050 sensors can be achieved by connecting each of the AD0 pins to a seperate output of the Arduino. If the number of pins is a problem, then a shift register or a port expander can be used.

The output of a 5V Arduino can not be used. In that case a voltage divider or level shifter for 3.3 volts on each of the outputs is needed. The 5V output pins can also be converted in 3.3V open collector outputs by using transistors or an open-collector driver. Pullup resistors to 3.3V should be added for a high level of 3.3V.

Suppose all AD0 lines are default high (3.3V), so every MPU-6050 is I2C address 0x69. That I2C address is however never used ! The Arduino makes one of the AD0 lines low, and uses that sensor at I2C address 0x68. After that is finished, the Arduino selects another AD0 line, and can use that sensor.
So every sensor is used at I2C address 0x68 (one by one) and 0x69 is never used.

This should make it possible to have many MPU-6050 sensors in a project. Even more than 10 sensor should be possible.
Note that requesting data from many MPU-6050 sensors is slow, because the I2C-bus is slow. A sensor with SPI interface is faster.

At this moment (15 July 2014) it is not known if this trick works for the MPU-6050.


Breakout boards

There are a number of "breakout boards" or "sensor boards" with the MPU-6050. The price dropped fast, only 2.60 dollars in August 2014 on Ebay.

Sparkfun SEN-11028

http://www./products/11028
With schematic and full information. This breakout board must be used with 3.3V. There is no voltage regulator and no I2C-level shifter on the board. The pull-up resistors for the I2C-bus are 10k.

Drotek IMU 10DOF - MPU6050 + HMC5883 + MS5611

http://www./shop/en/62-imu-10dof-mpu6050-hmc5883-ms5611.html
This sensor board contains three sensors. A schematic is not provided. The interrupt ('INT') of the MPU-6050 is not made available. Therefor the FIFO and the Jeff Rowberg library can not be used.

Drotek MPU-6050 Invensense

http://www./shop/en/42-mpu6050-gyro-accelerometer.html
This breakout board contains a voltage regulator. It can be used with 3.3V and with 5V. A schematic is not provided.

GY-521

This sensor board has a voltage regulator. When using 3.3V to the VCC the resulting voltage (after the onboard voltage regulator) might be too low for a good working I2C bus. It is preferred to apply 5V to the VCC pin of the sensor board. The board has pull-up resistors on the I2C-bus. The value of those pull-up resistors are sometimes 10k and sometimes 2k2. The 2k2 is rather low. If it is combined with other sensor board which have also pull-up resistors, the total pull-up impedance might be too low.
This schematic is hard to find, so here is a copy: http://playground./uploads/Main/MPU6050-V1-SCH.jpg
This part is designed in Fritzing: http:///projects/mpu-6050-board-gy-521-acelerometro-y-giroscopio

GY-52

This sensor board has a voltage regulator. So it can be used with 3.3V and with 5V. The board was originally designed for the MPU-3050, therefor the text "MPU-3050" or "MPU-X050" is sometimes printed on the board. The pull-up resistors are sometimes 10k and sometimes 4k7. If they are 10k, two extra pull-up resistors of 10k to the 3.3V could be added (from the SDA and SCL to the 3.3V of the sensor board or the 3.3V of the Arduino).

Flyduino MPU6050 Break Out onboard 3.3V reg

http:///MPU6050-Break-Out-onboard-33V-reg_1
This sensor board contains a voltage regulator, so it can also be used with 5V. The pull-up resistors of the I2C-bus are 4k7. It is actually a GY-52 breakout board.

Flyduino 10DOF IMU GY-86 MPU6050+HMC5883l+MS5611

A sensor board with the MPU-6050 and a magnetometer and barometer. The sensor board contains a voltage regulator, so it can be used with 5V. There seems to be also a level shifter on the board for the I2C-bus. The pull-up resistors for the I2C-bus seems to be 2k2, which is rather low.

no name breakout board

In august 2012 the cheapest breakout board with the MPU-6050 was a breakout board (for about 12 dollars/ 10euros) without any name or code.
The header is on the right with the pins in this order: "5V", "3V3", "GND", "SCL", "SDA", "INT", "SYNC", "CLK", "ASCL", "ADSA".
There are two pull-up resistors for the SCL and SDA, but the value is unknown. On the back are three solder jumpers, one of them for AD0.

no name breakout board 2

In 2014 a new MPU-6050 appeared without any name or code.
http://www./forums/topic/8-mpu6050-connection-failed/?p=347
The header is on the left with the pins in this order: "VCC", "GND", "SCL", "SDA", "XDA", "XCL", "AD0", "INT".
It is almost equal to the GY-521 board. There is a voltage regulator on the board for 3.3V. There are two 10k pull-up resistors for the SCL and SDA, and also 330 ohm resistors in the SCL and SDA signal. Because of the voltage regulator, connect 5V to the VCC of this sensor board.

Measurements

The raw values raises questions in the forums, since the raw values might seem unstable. Below are the raw values of the sensor that I measured, so you can compare them with your own raw values.

The raw values changes a lot due to a number of reasons. The default sensitivity is high, and the sensor returnes 16 bits, but the actual valid number of bits is less than 16 bits. Since they are 16 bits, a variation of 50 is just a very small variation.

The next measurement were done in these conditions:

  • The sensor was placed as horizontal as possible.
  • It was placed on concreet, not a wooden table.
  • During the measurements, there was no traffic in the street.
  • An battery of 12V was used, not the less stable voltage from the USB bus. I used a battery instead of an adapter to avoid any mains noise.
  • The circuit was on for 15 minutes, to stabalize any temperature influence.
  • The room temperature was 25 degrees Celcius.
MPU-6050
Read accel, temp and gyro, error = 0
accel x,y,z: 184, -484, 14992
temperature: 29.635 degrees Celsius
gyro x,y,z : 367, 220, -812, 

MPU-6050
Read accel, temp and gyro, error = 0
accel x,y,z: 116, -364, 15056
temperature: 29.635 degrees Celsius
gyro x,y,z : 373, 226, -766, 

MPU-6050
Read accel, temp and gyro, error = 0
accel x,y,z: 232, -432, 15100
temperature: 29.682 degrees Celsius
gyro x,y,z : 382, 232, -790, 

MPU-6050
Read accel, temp and gyro, error = 0
accel x,y,z: 280, -468, 15136
temperature: 29.635 degrees Celsius
gyro x,y,z : 368, 211, -820, 

MPU-6050
Read accel, temp and gyro, error = 0
accel x,y,z: 140, -432, 15108
temperature: 29.588 degrees Celsius
gyro x,y,z : 388, 203, -806, 

MPU-6050
Read accel, temp and gyro, error = 0
accel x,y,z: 220, -464, 14920
temperature: 29.541 degrees Celsius
gyro x,y,z : 374, 196, -774, 

MPU-6050
Read accel, temp and gyro, error = 0
accel x,y,z: 172, -440, 15100
temperature: 29.588 degrees Celsius
gyro x,y,z : 363, 200, -769, 

Short example sketch

The short example sketch is a very short sketch and it shows all the raw values (accelerometer, gyro and temperature).

  1. // Short example sketch
  2. // By Arduino user JohnChi
  3. // august 12, 2014.
  4. // public domain
  5. #include<Wire.h>
  6. const int GyroA=0x68;  // I2C address of the MPU-6050
  7. float AcX,AcY,AcZ,Temp,GyX,GyY,GyZ;
  8. void setup(){
  9.   Wire.begin();
  10.   Wire.beginTransmission(GyroA);
  11.   Wire.write(0x6B);  // MPU6050_PWR_MGMT_1 register
  12.   Wire.write(0);     // set to zero (wakes up the MPU-6050)
  13.   Wire.endTransmission(true);
  14.   Serial.begin(9600);
  15. }
  16. void loop(){
  17.   Wire.beginTransmission(GyroA);
  18.   Wire.write(0x3B);  // starting with register 0x3B (MPU6050_ACCEL_XOUT_H)
  19.   Wire.endTransmission(false);
  20.   Wire.requestFrom(GyroA,14,true);  // read a total of 14 registers
  21.   AcX=Wire.read()*256+Wire.read();  // 0x3B (MPU6050_ACCEL_XOUT_H) & 0x3C (MPU6050_ACCEL_XOUT_L)    
  22.   AcY=Wire.read()*256+Wire.read();  // 0x3D (MPU6050_ACCEL_YOUT_H) & 0x3E (MPU6050_ACCEL_YOUT_L)
  23.   AcZ=Wire.read()*256+Wire.read();  // 0x3F (MPU6050_ACCEL_ZOUT_H) & 0x40 (MPU6050_ACCEL_ZOUT_L)
  24.   Temp=(Wire.read()*256+Wire.read())/340.00+36.53;  // 0x41 (MPU6050_TEMP_OUT_H) & 0x42 (MPU6050_TEMP_OUT_L)
  25.   GyX=Wire.read()*256+Wire.read();  // 0x43 (MPU6050_GYRO_XOUT_H) & 0x44 (MPU6050_GYRO_XOUT_L)
  26.   GyY=Wire.read()*256+Wire.read();  // 0x45 (MPU6050_GYRO_YOUT_H) & 0x46 (MPU6050_GYRO_YOUT_L)
  27.   GyZ=Wire.read()*256+Wire.read();  // 0x47 (MPU6050_GYRO_ZOUT_H) & 0x48 (MPU6050_GYRO_ZOUT_L)
  28.   Serial.print(" | AcX = "); Serial.print(AcX);
  29.   Serial.print(" | AcY = "); Serial.print(AcY);
  30.   Serial.print(" | AcZ = "); Serial.print(AcZ);
  31.   Serial.print(" | Temp = "); Serial.print(Temp);
  32.   Serial.print(" | GyX = "); Serial.print(GyX);
  33.   Serial.print(" | GyY = "); Serial.print(GyY);
  34.   Serial.print(" | GyZ = "); Serial.println(GyZ);
  35.   delay(333);
  36. }


Example sketch (base code)

The sketch below is ment to be a base for interfacing the MPU-6050.

The sketch uses the Arduino functions as much as possible. For now, it is just a simple and basic sketch to get the MPU-6050 working.

The I2C-address depends on the AD0 pin of the sensor. If it is connected to ground, the address is 0x68. If it is connected to VLOGIC (+3.3V) it is 0x69. There are a few sensor boards with the MPU-6050 sensor already soldered on it. Some of those boards have a pull-down resistor at AD0 (address = 0x68), others have a pull-up resistor (address = 0x69).

Search in the sketch for "MPU6050_I2C_ADDRESS" and set that to your own I2C address.
A i2c_scanner can be used to check if the device is connected to the i2c bus.

The acceleration and gyro values of the sketch are raw values, which are not yet compensated for offset. The very first acceleration and gyro values after power up are sometimes not valid.

The sketch is about 7kbyte, and will fit in a ATmega8.

  1. // MPU-6050 Accelerometer + Gyro
  2. // -----------------------------
  3. //
  4. // By user "Krodal".
  5. //
  6. // June 2012
  7. //      first version
  8. // July 2013
  9. //      The 'int' in the union for the x,y,z
  10. //      changed into int16_t to be compatible
  11. //      with Arduino Due.
  12. //
  13. // Open Source / Public Domain
  14. //
  15. // Using Arduino 1.0.1
  16. // It will not work with an older version,
  17. // since Wire.endTransmission() uses a parameter
  18. // to hold or release the I2C bus.
  19. //
  20. // Documentation:
  21. // - The InvenSense documents:
  22. //   - "MPU-6000 and MPU-6050 Product Specification",
  23. //     PS-MPU-6000A.pdf
  24. //   - "MPU-6000 and MPU-6050 Register Map and Descriptions",
  25. //     RM-MPU-6000A.pdf or RS-MPU-6000A.pdf
  26. //   - "MPU-6000/MPU-6050 9-Axis Evaluation Board User Guide"
  27. //     AN-MPU-6000EVB.pdf
  28. //
  29. // The accuracy is 16-bits.
  30. //
  31. // Temperature sensor from -40 to +85 degrees Celsius
  32. //   340 per degrees, -512 at 35 degrees.
  33. //
  34. // At power-up, all registers are zero, except these two:
  35. //      Register 0x6B (PWR_MGMT_2) = 0x40  (I read zero).
  36. //      Register 0x75 (WHO_AM_I)   = 0x68.
  37. //
  38. #include <Wire.h>
  39. // The name of the sensor is "MPU-6050".
  40. // For program code, I omit the '-',
  41. // therefor I use the name "MPU6050....".
  42. // Register names according to the datasheet.
  43. // According to the InvenSense document
  44. // "MPU-6000 and MPU-6050 Register Map
  45. // and Descriptions Revision 3.2", there are no registers
  46. // at 0x02 ... 0x18, but according other information
  47. // the registers in that unknown area are for gain
  48. // and offsets.
  49. //
  50. #define MPU6050_AUX_VDDIO          0x01   // R/W
  51. #define MPU6050_SMPLRT_DIV         0x19   // R/W
  52. #define MPU6050_CONFIG             0x1A   // R/W
  53. #define MPU6050_GYRO_CONFIG        0x1B   // R/W
  54. #define MPU6050_ACCEL_CONFIG       0x1C   // R/W
  55. #define MPU6050_FF_THR             0x1D   // R/W
  56. #define MPU6050_FF_DUR             0x1E   // R/W
  57. #define MPU6050_MOT_THR            0x1F   // R/W
  58. #define MPU6050_MOT_DUR            0x20   // R/W
  59. #define MPU6050_ZRMOT_THR          0x21   // R/W
  60. #define MPU6050_ZRMOT_DUR          0x22   // R/W
  61. #define MPU6050_FIFO_EN            0x23   // R/W
  62. #define MPU6050_I2C_MST_CTRL       0x24   // R/W
  63. #define MPU6050_I2C_SLV0_ADDR      0x25   // R/W
  64. #define MPU6050_I2C_SLV0_REG       0x26   // R/W
  65. #define MPU6050_I2C_SLV0_CTRL      0x27   // R/W
  66. #define MPU6050_I2C_SLV1_ADDR      0x28   // R/W
  67. #define MPU6050_I2C_SLV1_REG       0x29   // R/W
  68. #define MPU6050_I2C_SLV1_CTRL      0x2A   // R/W
  69. #define MPU6050_I2C_SLV2_ADDR      0x2B   // R/W
  70. #define MPU6050_I2C_SLV2_REG       0x2C   // R/W
  71. #define MPU6050_I2C_SLV2_CTRL      0x2D   // R/W
  72. #define MPU6050_I2C_SLV3_ADDR      0x2E   // R/W
  73. #define MPU6050_I2C_SLV3_REG       0x2F   // R/W
  74. #define MPU6050_I2C_SLV3_CTRL      0x30   // R/W
  75. #define MPU6050_I2C_SLV4_ADDR      0x31   // R/W
  76. #define MPU6050_I2C_SLV4_REG       0x32   // R/W
  77. #define MPU6050_I2C_SLV4_DO        0x33   // R/W
  78. #define MPU6050_I2C_SLV4_CTRL      0x34   // R/W
  79. #define MPU6050_I2C_SLV4_DI        0x35   // R  
  80. #define MPU6050_I2C_MST_STATUS     0x36   // R
  81. #define MPU6050_INT_PIN_CFG        0x37   // R/W
  82. #define MPU6050_INT_ENABLE         0x38   // R/W
  83. #define MPU6050_INT_STATUS         0x3A   // R  
  84. #define MPU6050_ACCEL_XOUT_H       0x3B   // R  
  85. #define MPU6050_ACCEL_XOUT_L       0x3C   // R  
  86. #define MPU6050_ACCEL_YOUT_H       0x3D   // R  
  87. #define MPU6050_ACCEL_YOUT_L       0x3E   // R  
  88. #define MPU6050_ACCEL_ZOUT_H       0x3F   // R  
  89. #define MPU6050_ACCEL_ZOUT_L       0x40   // R  
  90. #define MPU6050_TEMP_OUT_H         0x41   // R  
  91. #define MPU6050_TEMP_OUT_L         0x42   // R  
  92. #define MPU6050_GYRO_XOUT_H        0x43   // R  
  93. #define MPU6050_GYRO_XOUT_L        0x44   // R  
  94. #define MPU6050_GYRO_YOUT_H        0x45   // R  
  95. #define MPU6050_GYRO_YOUT_L        0x46   // R  
  96. #define MPU6050_GYRO_ZOUT_H        0x47   // R  
  97. #define MPU6050_GYRO_ZOUT_L        0x48   // R  
  98. #define MPU6050_EXT_SENS_DATA_00   0x49   // R  
  99. #define MPU6050_EXT_SENS_DATA_01   0x4A   // R  
  100. #define MPU6050_EXT_SENS_DATA_02   0x4B   // R  
  101. #define MPU6050_EXT_SENS_DATA_03   0x4C   // R  
  102. #define MPU6050_EXT_SENS_DATA_04   0x4D   // R  
  103. #define MPU6050_EXT_SENS_DATA_05   0x4E   // R  
  104. #define MPU6050_EXT_SENS_DATA_06   0x4F   // R  
  105. #define MPU6050_EXT_SENS_DATA_07   0x50   // R  
  106. #define MPU6050_EXT_SENS_DATA_08   0x51   // R  
  107. #define MPU6050_EXT_SENS_DATA_09   0x52   // R  
  108. #define MPU6050_EXT_SENS_DATA_10   0x53   // R  
  109. #define MPU6050_EXT_SENS_DATA_11   0x54   // R  
  110. #define MPU6050_EXT_SENS_DATA_12   0x55   // R  
  111. #define MPU6050_EXT_SENS_DATA_13   0x56   // R  
  112. #define MPU6050_EXT_SENS_DATA_14   0x57   // R  
  113. #define MPU6050_EXT_SENS_DATA_15   0x58   // R  
  114. #define MPU6050_EXT_SENS_DATA_16   0x59   // R  
  115. #define MPU6050_EXT_SENS_DATA_17   0x5A   // R  
  116. #define MPU6050_EXT_SENS_DATA_18   0x5B   // R  
  117. #define MPU6050_EXT_SENS_DATA_19   0x5C   // R  
  118. #define MPU6050_EXT_SENS_DATA_20   0x5D   // R  
  119. #define MPU6050_EXT_SENS_DATA_21   0x5E   // R  
  120. #define MPU6050_EXT_SENS_DATA_22   0x5F   // R  
  121. #define MPU6050_EXT_SENS_DATA_23   0x60   // R  
  122. #define MPU6050_MOT_DETECT_STATUS  0x61   // R  
  123. #define MPU6050_I2C_SLV0_DO        0x63   // R/W
  124. #define MPU6050_I2C_SLV1_DO        0x64   // R/W
  125. #define MPU6050_I2C_SLV2_DO        0x65   // R/W
  126. #define MPU6050_I2C_SLV3_DO        0x66   // R/W
  127. #define MPU6050_I2C_MST_DELAY_CTRL 0x67   // R/W
  128. #define MPU6050_SIGNAL_PATH_RESET  0x68   // R/W
  129. #define MPU6050_MOT_DETECT_CTRL    0x69   // R/W
  130. #define MPU6050_USER_CTRL          0x6A   // R/W
  131. #define MPU6050_PWR_MGMT_1         0x6B   // R/W
  132. #define MPU6050_PWR_MGMT_2         0x6C   // R/W
  133. #define MPU6050_FIFO_COUNTH        0x72   // R/W
  134. #define MPU6050_FIFO_COUNTL        0x73   // R/W
  135. #define MPU6050_FIFO_R_W           0x74   // R/W
  136. #define MPU6050_WHO_AM_I           0x75   // R
  137. // Defines for the bits, to be able to change
  138. // between bit number and binary definition.
  139. // By using the bit number, programming the sensor
  140. // is like programming the AVR microcontroller.
  141. // But instead of using "(1<<X)", or "_BV(X)",
  142. // the Arduino "bit(X)" is used.
  143. #define MPU6050_D0 0
  144. #define MPU6050_D1 1
  145. #define MPU6050_D2 2
  146. #define MPU6050_D3 3
  147. #define MPU6050_D4 4
  148. #define MPU6050_D5 5
  149. #define MPU6050_D6 6
  150. #define MPU6050_D7 7
  151. // AUX_VDDIO Register
  152. #define MPU6050_AUX_VDDIO MPU6050_D7  // I2C high: 1=VDD, 0=VLOGIC
  153. // CONFIG Register
  154. // DLPF is Digital Low Pass Filter for both gyro and accelerometers.
  155. // These are the names for the bits.
  156. // Use these only with the bit() macro.
  157. #define MPU6050_DLPF_CFG0     MPU6050_D0
  158. #define MPU6050_DLPF_CFG1     MPU6050_D1
  159. #define MPU6050_DLPF_CFG2     MPU6050_D2
  160. #define MPU6050_EXT_SYNC_SET0 MPU6050_D3
  161. #define MPU6050_EXT_SYNC_SET1 MPU6050_D4
  162. #define MPU6050_EXT_SYNC_SET2 MPU6050_D5
  163. // Combined definitions for the EXT_SYNC_SET values
  164. #define MPU6050_EXT_SYNC_SET_0 (0)
  165. #define MPU6050_EXT_SYNC_SET_1 (bit(MPU6050_EXT_SYNC_SET0))
  166. #define MPU6050_EXT_SYNC_SET_2 (bit(MPU6050_EXT_SYNC_SET1))
  167. #define MPU6050_EXT_SYNC_SET_3 (bit(MPU6050_EXT_SYNC_SET1)|bit(MPU6050_EXT_SYNC_SET0))
  168. #define MPU6050_EXT_SYNC_SET_4 (bit(MPU6050_EXT_SYNC_SET2))
  169. #define MPU6050_EXT_SYNC_SET_5 (bit(MPU6050_EXT_SYNC_SET2)|bit(MPU6050_EXT_SYNC_SET0))
  170. #define MPU6050_EXT_SYNC_SET_6 (bit(MPU6050_EXT_SYNC_SET2)|bit(MPU6050_EXT_SYNC_SET1))
  171. #define MPU6050_EXT_SYNC_SET_7 (bit(MPU6050_EXT_SYNC_SET2)|bit(MPU6050_EXT_SYNC_SET1)|bit(MPU6050_EXT_SYNC_SET0))
  172. // Alternative names for the combined definitions.
  173. #define MPU6050_EXT_SYNC_DISABLED     MPU6050_EXT_SYNC_SET_0
  174. #define MPU6050_EXT_SYNC_TEMP_OUT_L   MPU6050_EXT_SYNC_SET_1
  175. #define MPU6050_EXT_SYNC_GYRO_XOUT_L  MPU6050_EXT_SYNC_SET_2
  176. #define MPU6050_EXT_SYNC_GYRO_YOUT_L  MPU6050_EXT_SYNC_SET_3
  177. #define MPU6050_EXT_SYNC_GYRO_ZOUT_L  MPU6050_EXT_SYNC_SET_4
  178. #define MPU6050_EXT_SYNC_ACCEL_XOUT_L MPU6050_EXT_SYNC_SET_5
  179. #define MPU6050_EXT_SYNC_ACCEL_YOUT_L MPU6050_EXT_SYNC_SET_6
  180. #define MPU6050_EXT_SYNC_ACCEL_ZOUT_L MPU6050_EXT_SYNC_SET_7
  181. // Combined definitions for the DLPF_CFG values
  182. #define MPU6050_DLPF_CFG_0 (0)
  183. #define MPU6050_DLPF_CFG_1 (bit(MPU6050_DLPF_CFG0))
  184. #define MPU6050_DLPF_CFG_2 (bit(MPU6050_DLPF_CFG1))
  185. #define MPU6050_DLPF_CFG_3 (bit(MPU6050_DLPF_CFG1)|bit(MPU6050_DLPF_CFG0))
  186. #define MPU6050_DLPF_CFG_4 (bit(MPU6050_DLPF_CFG2))
  187. #define MPU6050_DLPF_CFG_5 (bit(MPU6050_DLPF_CFG2)|bit(MPU6050_DLPF_CFG0))
  188. #define MPU6050_DLPF_CFG_6 (bit(MPU6050_DLPF_CFG2)|bit(MPU6050_DLPF_CFG1))
  189. #define MPU6050_DLPF_CFG_7 (bit(MPU6050_DLPF_CFG2)|bit(MPU6050_DLPF_CFG1)|bit(MPU6050_DLPF_CFG0))
  190. // Alternative names for the combined definitions
  191. // This name uses the bandwidth (Hz) for the accelometer,
  192. // for the gyro the bandwidth is almost the same.
  193. #define MPU6050_DLPF_260HZ    MPU6050_DLPF_CFG_0
  194. #define MPU6050_DLPF_184HZ    MPU6050_DLPF_CFG_1
  195. #define MPU6050_DLPF_94HZ     MPU6050_DLPF_CFG_2
  196. #define MPU6050_DLPF_44HZ     MPU6050_DLPF_CFG_3
  197. #define MPU6050_DLPF_21HZ     MPU6050_DLPF_CFG_4
  198. #define MPU6050_DLPF_10HZ     MPU6050_DLPF_CFG_5
  199. #define MPU6050_DLPF_5HZ      MPU6050_DLPF_CFG_6
  200. #define MPU6050_DLPF_RESERVED MPU6050_DLPF_CFG_7
  201. // GYRO_CONFIG Register
  202. // The XG_ST, YG_ST, ZG_ST are bits for selftest.
  203. // The FS_SEL sets the range for the gyro.
  204. // These are the names for the bits.
  205. // Use these only with the bit() macro.
  206. #define MPU6050_FS_SEL0 MPU6050_D3
  207. #define MPU6050_FS_SEL1 MPU6050_D4
  208. #define MPU6050_ZG_ST   MPU6050_D5
  209. #define MPU6050_YG_ST   MPU6050_D6
  210. #define MPU6050_XG_ST   MPU6050_D7
  211. // Combined definitions for the FS_SEL values
  212. #define MPU6050_FS_SEL_0 (0)
  213. #define MPU6050_FS_SEL_1 (bit(MPU6050_FS_SEL0))
  214. #define MPU6050_FS_SEL_2 (bit(MPU6050_FS_SEL1))
  215. #define MPU6050_FS_SEL_3 (bit(MPU6050_FS_SEL1)|bit(MPU6050_FS_SEL0))
  216. // Alternative names for the combined definitions
  217. // The name uses the range in degrees per second.
  218. #define MPU6050_FS_SEL_250  MPU6050_FS_SEL_0
  219. #define MPU6050_FS_SEL_500  MPU6050_FS_SEL_1
  220. #define MPU6050_FS_SEL_1000 MPU6050_FS_SEL_2
  221. #define MPU6050_FS_SEL_2000 MPU6050_FS_SEL_3
  222. // ACCEL_CONFIG Register
  223. // The XA_ST, YA_ST, ZA_ST are bits for selftest.
  224. // The AFS_SEL sets the range for the accelerometer.
  225. // These are the names for the bits.
  226. // Use these only with the bit() macro.
  227. #define MPU6050_ACCEL_HPF0 MPU6050_D0
  228. #define MPU6050_ACCEL_HPF1 MPU6050_D1
  229. #define MPU6050_ACCEL_HPF2 MPU6050_D2
  230. #define MPU6050_AFS_SEL0   MPU6050_D3
  231. #define MPU6050_AFS_SEL1   MPU6050_D4
  232. #define MPU6050_ZA_ST      MPU6050_D5
  233. #define MPU6050_YA_ST      MPU6050_D6
  234. #define MPU6050_XA_ST      MPU6050_D7
  235. // Combined definitions for the ACCEL_HPF values
  236. #define MPU6050_ACCEL_HPF_0 (0)
  237. #define MPU6050_ACCEL_HPF_1 (bit(MPU6050_ACCEL_HPF0))
  238. #define MPU6050_ACCEL_HPF_2 (bit(MPU6050_ACCEL_HPF1))
  239. #define MPU6050_ACCEL_HPF_3 (bit(MPU6050_ACCEL_HPF1)|bit(MPU6050_ACCEL_HPF0))
  240. #define MPU6050_ACCEL_HPF_4 (bit(MPU6050_ACCEL_HPF2))
  241. #define MPU6050_ACCEL_HPF_7 (bit(MPU6050_ACCEL_HPF2)|bit(MPU6050_ACCEL_HPF1)|bit(MPU6050_ACCEL_HPF0))
  242. // Alternative names for the combined definitions
  243. // The name uses the Cut-off frequency.
  244. #define MPU6050_ACCEL_HPF_RESET  MPU6050_ACCEL_HPF_0
  245. #define MPU6050_ACCEL_HPF_5HZ    MPU6050_ACCEL_HPF_1
  246. #define MPU6050_ACCEL_HPF_2_5HZ  MPU6050_ACCEL_HPF_2
  247. #define MPU6050_ACCEL_HPF_1_25HZ MPU6050_ACCEL_HPF_3
  248. #define MPU6050_ACCEL_HPF_0_63HZ MPU6050_ACCEL_HPF_4
  249. #define MPU6050_ACCEL_HPF_HOLD   MPU6050_ACCEL_HPF_7
  250. // Combined definitions for the AFS_SEL values
  251. #define MPU6050_AFS_SEL_0 (0)
  252. #define MPU6050_AFS_SEL_1 (bit(MPU6050_AFS_SEL0))
  253. #define MPU6050_AFS_SEL_2 (bit(MPU6050_AFS_SEL1))
  254. #define MPU6050_AFS_SEL_3 (bit(MPU6050_AFS_SEL1)|bit(MPU6050_AFS_SEL0))
  255. // Alternative names for the combined definitions
  256. // The name uses the full scale range for the accelerometer.
  257. #define MPU6050_AFS_SEL_2G  MPU6050_AFS_SEL_0
  258. #define MPU6050_AFS_SEL_4G  MPU6050_AFS_SEL_1
  259. #define MPU6050_AFS_SEL_8G  MPU6050_AFS_SEL_2
  260. #define MPU6050_AFS_SEL_16G MPU6050_AFS_SEL_3
  261. // FIFO_EN Register
  262. // These are the names for the bits.
  263. // Use these only with the bit() macro.
  264. #define MPU6050_SLV0_FIFO_EN  MPU6050_D0
  265. #define MPU6050_SLV1_FIFO_EN  MPU6050_D1
  266. #define MPU6050_SLV2_FIFO_EN  MPU6050_D2
  267. #define MPU6050_ACCEL_FIFO_EN MPU6050_D3
  268. #define MPU6050_ZG_FIFO_EN    MPU6050_D4
  269. #define MPU6050_YG_FIFO_EN    MPU6050_D5
  270. #define MPU6050_XG_FIFO_EN    MPU6050_D6
  271. #define MPU6050_TEMP_FIFO_EN  MPU6050_D7
  272. // I2C_MST_CTRL Register
  273. // These are the names for the bits.
  274. // Use these only with the bit() macro.
  275. #define MPU6050_I2C_MST_CLK0  MPU6050_D0
  276. #define MPU6050_I2C_MST_CLK1  MPU6050_D1
  277. #define MPU6050_I2C_MST_CLK2  MPU6050_D2
  278. #define MPU6050_I2C_MST_CLK3  MPU6050_D3
  279. #define MPU6050_I2C_MST_P_NSR MPU6050_D4
  280. #define MPU6050_SLV_3_FIFO_EN MPU6050_D5
  281. #define MPU6050_WAIT_FOR_ES   MPU6050_D6
  282. #define MPU6050_MULT_MST_EN   MPU6050_D7
  283. // Combined definitions for the I2C_MST_CLK
  284. #define MPU6050_I2C_MST_CLK_0 (0)
  285. #define MPU6050_I2C_MST_CLK_1  (bit(MPU6050_I2C_MST_CLK0))
  286. #define MPU6050_I2C_MST_CLK_2  (bit(MPU6050_I2C_MST_CLK1))
  287. #define MPU6050_I2C_MST_CLK_3  (bit(MPU6050_I2C_MST_CLK1)|bit(MPU6050_I2C_MST_CLK0))
  288. #define MPU6050_I2C_MST_CLK_4  (bit(MPU6050_I2C_MST_CLK2))
  289. #define MPU6050_I2C_MST_CLK_5  (bit(MPU6050_I2C_MST_CLK2)|bit(MPU6050_I2C_MST_CLK0))
  290. #define MPU6050_I2C_MST_CLK_6  (bit(MPU6050_I2C_MST_CLK2)|bit(MPU6050_I2C_MST_CLK1))
  291. #define MPU6050_I2C_MST_CLK_7  (bit(MPU6050_I2C_MST_CLK2)|bit(MPU6050_I2C_MST_CLK1)|bit(MPU6050_I2C_MST_CLK0))
  292. #define MPU6050_I2C_MST_CLK_8  (bit(MPU6050_I2C_MST_CLK3))
  293. #define MPU6050_I2C_MST_CLK_9  (bit(MPU6050_I2C_MST_CLK3)|bit(MPU6050_I2C_MST_CLK0))
  294. #define MPU6050_I2C_MST_CLK_10 (bit(MPU6050_I2C_MST_CLK3)|bit(MPU6050_I2C_MST_CLK1))
  295. #define MPU6050_I2C_MST_CLK_11 (bit(MPU6050_I2C_MST_CLK3)|bit(MPU6050_I2C_MST_CLK1)|bit(MPU6050_I2C_MST_CLK0))
  296. #define MPU6050_I2C_MST_CLK_12 (bit(MPU6050_I2C_MST_CLK3)|bit(MPU6050_I2C_MST_CLK2))
  297. #define MPU6050_I2C_MST_CLK_13 (bit(MPU6050_I2C_MST_CLK3)|bit(MPU6050_I2C_MST_CLK2)|bit(MPU6050_I2C_MST_CLK0))
  298. #define MPU6050_I2C_MST_CLK_14 (bit(MPU6050_I2C_MST_CLK3)|bit(MPU6050_I2C_MST_CLK2)|bit(MPU6050_I2C_MST_CLK1))
  299. #define MPU6050_I2C_MST_CLK_15 (bit(MPU6050_I2C_MST_CLK3)|bit(MPU6050_I2C_MST_CLK2)|bit(MPU6050_I2C_MST_CLK1)|bit(MPU6050_I2C_MST_CLK0))
  300. // Alternative names for the combined definitions
  301. // The names uses I2C Master Clock Speed in kHz.
  302. #define MPU6050_I2C_MST_CLK_348KHZ MPU6050_I2C_MST_CLK_0
  303. #define MPU6050_I2C_MST_CLK_333KHZ MPU6050_I2C_MST_CLK_1
  304. #define MPU6050_I2C_MST_CLK_320KHZ MPU6050_I2C_MST_CLK_2
  305. #define MPU6050_I2C_MST_CLK_308KHZ MPU6050_I2C_MST_CLK_3
  306. #define MPU6050_I2C_MST_CLK_296KHZ MPU6050_I2C_MST_CLK_4
  307. #define MPU6050_I2C_MST_CLK_286KHZ MPU6050_I2C_MST_CLK_5
  308. #define MPU6050_I2C_MST_CLK_276KHZ MPU6050_I2C_MST_CLK_6
  309. #define MPU6050_I2C_MST_CLK_267KHZ MPU6050_I2C_MST_CLK_7
  310. #define MPU6050_I2C_MST_CLK_258KHZ MPU6050_I2C_MST_CLK_8
  311. #define MPU6050_I2C_MST_CLK_500KHZ MPU6050_I2C_MST_CLK_9
  312. #define MPU6050_I2C_MST_CLK_471KHZ MPU6050_I2C_MST_CLK_10
  313. #define MPU6050_I2C_MST_CLK_444KHZ MPU6050_I2C_MST_CLK_11
  314. #define MPU6050_I2C_MST_CLK_421KHZ MPU6050_I2C_MST_CLK_12
  315. #define MPU6050_I2C_MST_CLK_400KHZ MPU6050_I2C_MST_CLK_13
  316. #define MPU6050_I2C_MST_CLK_381KHZ MPU6050_I2C_MST_CLK_14
  317. #define MPU6050_I2C_MST_CLK_364KHZ MPU6050_I2C_MST_CLK_15
  318. // I2C_SLV0_ADDR Register
  319. // These are the names for the bits.
  320. // Use these only with the bit() macro.
  321. #define MPU6050_I2C_SLV0_RW MPU6050_D7
  322. // I2C_SLV0_CTRL Register
  323. // These are the names for the bits.
  324. // Use these only with the bit() macro.
  325. #define MPU6050_I2C_SLV0_LEN0    MPU6050_D0
  326. #define MPU6050_I2C_SLV0_LEN1    MPU6050_D1
  327. #define MPU6050_I2C_SLV0_LEN2    MPU6050_D2
  328. #define MPU6050_I2C_SLV0_LEN3    MPU6050_D3
  329. #define MPU6050_I2C_SLV0_GRP     MPU6050_D4
  330. #define MPU6050_I2C_SLV0_REG_DIS MPU6050_D5
  331. #define MPU6050_I2C_SLV0_BYTE_SW MPU6050_D6
  332. #define MPU6050_I2C_SLV0_EN      MPU6050_D7
  333. // A mask for the length
  334. #define MPU6050_I2C_SLV0_LEN_MASK 0x0F
  335. // I2C_SLV1_ADDR Register
  336. // These are the names for the bits.
  337. // Use these only with the bit() macro.
  338. #define MPU6050_I2C_SLV1_RW MPU6050_D7
  339. // I2C_SLV1_CTRL Register
  340. // These are the names for the bits.
  341. // Use these only with the bit() macro.
  342. #define MPU6050_I2C_SLV1_LEN0    MPU6050_D0
  343. #define MPU6050_I2C_SLV1_LEN1    MPU6050_D1
  344. #define MPU6050_I2C_SLV1_LEN2    MPU6050_D2
  345. #define MPU6050_I2C_SLV1_LEN3    MPU6050_D3
  346. #define MPU6050_I2C_SLV1_GRP     MPU6050_D4
  347. #define MPU6050_I2C_SLV1_REG_DIS MPU6050_D5
  348. #define MPU6050_I2C_SLV1_BYTE_SW MPU6050_D6
  349. #define MPU6050_I2C_SLV1_EN      MPU6050_D7
  350. // A mask for the length
  351. #define MPU6050_I2C_SLV1_LEN_MASK 0x0F
  352. // I2C_SLV2_ADDR Register
  353. // These are the names for the bits.
  354. // Use these only with the bit() macro.
  355. #define MPU6050_I2C_SLV2_RW MPU6050_D7
  356. // I2C_SLV2_CTRL Register
  357. // These are the names for the bits.
  358. // Use these only with the bit() macro.
  359. #define MPU6050_I2C_SLV2_LEN0    MPU6050_D0
  360. #define MPU6050_I2C_SLV2_LEN1    MPU6050_D1
  361. #define MPU6050_I2C_SLV2_LEN2    MPU6050_D2
  362. #define MPU6050_I2C_SLV2_LEN3    MPU6050_D3
  363. #define MPU6050_I2C_SLV2_GRP     MPU6050_D4
  364. #define MPU6050_I2C_SLV2_REG_DIS MPU6050_D5
  365. #define MPU6050_I2C_SLV2_BYTE_SW MPU6050_D6
  366. #define MPU6050_I2C_SLV2_EN      MPU6050_D7
  367. // A mask for the length
  368. #define MPU6050_I2C_SLV2_LEN_MASK 0x0F
  369. // I2C_SLV3_ADDR Register
  370. // These are the names for the bits.
  371. // Use these only with the bit() macro.
  372. #define MPU6050_I2C_SLV3_RW MPU6050_D7
  373. // I2C_SLV3_CTRL Register
  374. // These are the names for the bits.
  375. // Use these only with the bit() macro.
  376. #define MPU6050_I2C_SLV3_LEN0    MPU6050_D0
  377. #define MPU6050_I2C_SLV3_LEN1    MPU6050_D1
  378. #define MPU6050_I2C_SLV3_LEN2    MPU6050_D2
  379. #define MPU6050_I2C_SLV3_LEN3    MPU6050_D3
  380. #define MPU6050_I2C_SLV3_GRP     MPU6050_D4
  381. #define MPU6050_I2C_SLV3_REG_DIS MPU6050_D5
  382. #define MPU6050_I2C_SLV3_BYTE_SW MPU6050_D6
  383. #define MPU6050_I2C_SLV3_EN      MPU6050_D7
  384. // A mask for the length
  385. #define MPU6050_I2C_SLV3_LEN_MASK 0x0F
  386. // I2C_SLV4_ADDR Register
  387. // These are the names for the bits.
  388. // Use these only with the bit() macro.
  389. #define MPU6050_I2C_SLV4_RW MPU6050_D7
  390. // I2C_SLV4_CTRL Register
  391. // These are the names for the bits.
  392. // Use these only with the bit() macro.
  393. #define MPU6050_I2C_MST_DLY0     MPU6050_D0
  394. #define MPU6050_I2C_MST_DLY1     MPU6050_D1
  395. #define MPU6050_I2C_MST_DLY2     MPU6050_D2
  396. #define MPU6050_I2C_MST_DLY3     MPU6050_D3
  397. #define MPU6050_I2C_MST_DLY4     MPU6050_D4
  398. #define MPU6050_I2C_SLV4_REG_DIS MPU6050_D5
  399. #define MPU6050_I2C_SLV4_INT_EN  MPU6050_D6
  400. #define MPU6050_I2C_SLV4_EN      MPU6050_D7
  401. // A mask for the delay
  402. #define MPU6050_I2C_MST_DLY_MASK 0x1F
  403. // I2C_MST_STATUS Register
  404. // These are the names for the bits.
  405. // Use these only with the bit() macro.
  406. #define MPU6050_I2C_SLV0_NACK MPU6050_D0
  407. #define MPU6050_I2C_SLV1_NACK MPU6050_D1
  408. #define MPU6050_I2C_SLV2_NACK MPU6050_D2
  409. #define MPU6050_I2C_SLV3_NACK MPU6050_D3
  410. #define MPU6050_I2C_SLV4_NACK MPU6050_D4
  411. #define MPU6050_I2C_LOST_ARB  MPU6050_D5
  412. #define MPU6050_I2C_SLV4_DONE MPU6050_D6
  413. #define MPU6050_PASS_THROUGH  MPU6050_D7
  414. // I2C_PIN_CFG Register
  415. // These are the names for the bits.
  416. // Use these only with the bit() macro.
  417. #define MPU6050_CLKOUT_EN       MPU6050_D0
  418. #define MPU6050_I2C_BYPASS_EN   MPU6050_D1
  419. #define MPU6050_FSYNC_INT_EN    MPU6050_D2
  420. #define MPU6050_FSYNC_INT_LEVEL MPU6050_D3
  421. #define MPU6050_INT_RD_CLEAR    MPU6050_D4
  422. #define MPU6050_LATCH_INT_EN    MPU6050_D5
  423. #define MPU6050_INT_OPEN        MPU6050_D6
  424. #define MPU6050_INT_LEVEL       MPU6050_D7
  425. // INT_ENABLE Register
  426. // These are the names for the bits.
  427. // Use these only with the bit() macro.
  428. #define MPU6050_DATA_RDY_EN    MPU6050_D0
  429. #define MPU6050_I2C_MST_INT_EN MPU6050_D3
  430. #define MPU6050_FIFO_OFLOW_EN  MPU6050_D4
  431. #define MPU6050_ZMOT_EN        MPU6050_D5
  432. #define MPU6050_MOT_EN         MPU6050_D6
  433. #define MPU6050_FF_EN          MPU6050_D7
  434. // INT_STATUS Register
  435. // These are the names for the bits.
  436. // Use these only with the bit() macro.
  437. #define MPU6050_DATA_RDY_INT   MPU6050_D0
  438. #define MPU6050_I2C_MST_INT    MPU6050_D3
  439. #define MPU6050_FIFO_OFLOW_INT MPU6050_D4
  440. #define MPU6050_ZMOT_INT       MPU6050_D5
  441. #define MPU6050_MOT_INT        MPU6050_D6
  442. #define MPU6050_FF_INT         MPU6050_D7
  443. // MOT_DETECT_STATUS Register
  444. // These are the names for the bits.
  445. // Use these only with the bit() macro.
  446. #define MPU6050_MOT_ZRMOT MPU6050_D0
  447. #define MPU6050_MOT_ZPOS  MPU6050_D2
  448. #define MPU6050_MOT_ZNEG  MPU6050_D3
  449. #define MPU6050_MOT_YPOS  MPU6050_D4
  450. #define MPU6050_MOT_YNEG  MPU6050_D5
  451. #define MPU6050_MOT_XPOS  MPU6050_D6
  452. #define MPU6050_MOT_XNEG  MPU6050_D7
  453. // IC2_MST_DELAY_CTRL Register
  454. // These are the names for the bits.
  455. // Use these only with the bit() macro.
  456. #define MPU6050_I2C_SLV0_DLY_EN MPU6050_D0
  457. #define MPU6050_I2C_SLV1_DLY_EN MPU6050_D1
  458. #define MPU6050_I2C_SLV2_DLY_EN MPU6050_D2
  459. #define MPU6050_I2C_SLV3_DLY_EN MPU6050_D3
  460. #define MPU6050_I2C_SLV4_DLY_EN MPU6050_D4
  461. #define MPU6050_DELAY_ES_SHADOW MPU6050_D7
  462. // SIGNAL_PATH_RESET Register
  463. // These are the names for the bits.
  464. // Use these only with the bit() macro.
  465. #define MPU6050_TEMP_RESET  MPU6050_D0
  466. #define MPU6050_ACCEL_RESET MPU6050_D1
  467. #define MPU6050_GYRO_RESET  MPU6050_D2
  468. // MOT_DETECT_CTRL Register
  469. // These are the names for the bits.
  470. // Use these only with the bit() macro.
  471. #define MPU6050_MOT_COUNT0      MPU6050_D0
  472. #define MPU6050_MOT_COUNT1      MPU6050_D1
  473. #define MPU6050_FF_COUNT0       MPU6050_D2
  474. #define MPU6050_FF_COUNT1       MPU6050_D3
  475. #define MPU6050_ACCEL_ON_DELAY0 MPU6050_D4
  476. #define MPU6050_ACCEL_ON_DELAY1 MPU6050_D5
  477. // Combined definitions for the MOT_COUNT
  478. #define MPU6050_MOT_COUNT_0 (0)
  479. #define MPU6050_MOT_COUNT_1 (bit(MPU6050_MOT_COUNT0))
  480. #define MPU6050_MOT_COUNT_2 (bit(MPU6050_MOT_COUNT1))
  481. #define MPU6050_MOT_COUNT_3 (bit(MPU6050_MOT_COUNT1)|bit(MPU6050_MOT_COUNT0))
  482. // Alternative names for the combined definitions
  483. #define MPU6050_MOT_COUNT_RESET MPU6050_MOT_COUNT_0
  484. // Combined definitions for the FF_COUNT
  485. #define MPU6050_FF_COUNT_0 (0)
  486. #define MPU6050_FF_COUNT_1 (bit(MPU6050_FF_COUNT0))
  487. #define MPU6050_FF_COUNT_2 (bit(MPU6050_FF_COUNT1))
  488. #define MPU6050_FF_COUNT_3 (bit(MPU6050_FF_COUNT1)|bit(MPU6050_FF_COUNT0))
  489. // Alternative names for the combined definitions
  490. #define MPU6050_FF_COUNT_RESET MPU6050_FF_COUNT_0
  491. // Combined definitions for the ACCEL_ON_DELAY
  492. #define MPU6050_ACCEL_ON_DELAY_0 (0)
  493. #define MPU6050_ACCEL_ON_DELAY_1 (bit(MPU6050_ACCEL_ON_DELAY0))
  494. #define MPU6050_ACCEL_ON_DELAY_2 (bit(MPU6050_ACCEL_ON_DELAY1))
  495. #define MPU6050_ACCEL_ON_DELAY_3 (bit(MPU6050_ACCEL_ON_DELAY1)|bit(MPU6050_ACCEL_ON_DELAY0))
  496. // Alternative names for the ACCEL_ON_DELAY
  497. #define MPU6050_ACCEL_ON_DELAY_0MS MPU6050_ACCEL_ON_DELAY_0
  498. #define MPU6050_ACCEL_ON_DELAY_1MS MPU6050_ACCEL_ON_DELAY_1
  499. #define MPU6050_ACCEL_ON_DELAY_2MS MPU6050_ACCEL_ON_DELAY_2
  500. #define MPU6050_ACCEL_ON_DELAY_3MS MPU6050_ACCEL_ON_DELAY_3
  501. // USER_CTRL Register
  502. // These are the names for the bits.
  503. // Use these only with the bit() macro.
  504. #define MPU6050_SIG_COND_RESET MPU6050_D0
  505. #define MPU6050_I2C_MST_RESET  MPU6050_D1
  506. #define MPU6050_FIFO_RESET     MPU6050_D2
  507. #define MPU6050_I2C_IF_DIS     MPU6050_D4   // must be 0 for MPU-6050
  508. #define MPU6050_I2C_MST_EN     MPU6050_D5
  509. #define MPU6050_FIFO_EN        MPU6050_D6
  510. // PWR_MGMT_1 Register
  511. // These are the names for the bits.
  512. // Use these only with the bit() macro.
  513. #define MPU6050_CLKSEL0      MPU6050_D0
  514. #define MPU6050_CLKSEL1      MPU6050_D1
  515. #define MPU6050_CLKSEL2      MPU6050_D2
  516. #define MPU6050_TEMP_DIS     MPU6050_D3    // 1: disable temperature sensor
  517. #define MPU6050_CYCLE        MPU6050_D5    // 1: sample and sleep
  518. #define MPU6050_SLEEP        MPU6050_D6    // 1: sleep mode
  519. #define MPU6050_DEVICE_RESET MPU6050_D7    // 1: reset to default values
  520. // Combined definitions for the CLKSEL
  521. #define MPU6050_CLKSEL_0 (0)
  522. #define MPU6050_CLKSEL_1 (bit(MPU6050_CLKSEL0))
  523. #define MPU6050_CLKSEL_2 (bit(MPU6050_CLKSEL1))
  524. #define MPU6050_CLKSEL_3 (bit(MPU6050_CLKSEL1)|bit(MPU6050_CLKSEL0))
  525. #define MPU6050_CLKSEL_4 (bit(MPU6050_CLKSEL2))
  526. #define MPU6050_CLKSEL_5 (bit(MPU6050_CLKSEL2)|bit(MPU6050_CLKSEL0))
  527. #define MPU6050_CLKSEL_6 (bit(MPU6050_CLKSEL2)|bit(MPU6050_CLKSEL1))
  528. #define MPU6050_CLKSEL_7 (bit(MPU6050_CLKSEL2)|bit(MPU6050_CLKSEL1)|bit(MPU6050_CLKSEL0))
  529. // Alternative names for the combined definitions
  530. #define MPU6050_CLKSEL_INTERNAL    MPU6050_CLKSEL_0
  531. #define MPU6050_CLKSEL_X           MPU6050_CLKSEL_1
  532. #define MPU6050_CLKSEL_Y           MPU6050_CLKSEL_2
  533. #define MPU6050_CLKSEL_Z           MPU6050_CLKSEL_3
  534. #define MPU6050_CLKSEL_EXT_32KHZ   MPU6050_CLKSEL_4
  535. #define MPU6050_CLKSEL_EXT_19_2MHZ MPU6050_CLKSEL_5
  536. #define MPU6050_CLKSEL_RESERVED    MPU6050_CLKSEL_6
  537. #define MPU6050_CLKSEL_STOP        MPU6050_CLKSEL_7
  538. // PWR_MGMT_2 Register
  539. // These are the names for the bits.
  540. // Use these only with the bit() macro.
  541. #define MPU6050_STBY_ZG       MPU6050_D0
  542. #define MPU6050_STBY_YG       MPU6050_D1
  543. #define MPU6050_STBY_XG       MPU6050_D2
  544. #define MPU6050_STBY_ZA       MPU6050_D3
  545. #define MPU6050_STBY_YA       MPU6050_D4
  546. #define MPU6050_STBY_XA       MPU6050_D5
  547. #define MPU6050_LP_WAKE_CTRL0 MPU6050_D6
  548. #define MPU6050_LP_WAKE_CTRL1 MPU6050_D7
  549. // Combined definitions for the LP_WAKE_CTRL
  550. #define MPU6050_LP_WAKE_CTRL_0 (0)
  551. #define MPU6050_LP_WAKE_CTRL_1 (bit(MPU6050_LP_WAKE_CTRL0))
  552. #define MPU6050_LP_WAKE_CTRL_2 (bit(MPU6050_LP_WAKE_CTRL1))
  553. #define MPU6050_LP_WAKE_CTRL_3 (bit(MPU6050_LP_WAKE_CTRL1)|bit(MPU6050_LP_WAKE_CTRL0))
  554. // Alternative names for the combined definitions
  555. // The names uses the Wake-up Frequency.
  556. #define MPU6050_LP_WAKE_1_25HZ MPU6050_LP_WAKE_CTRL_0
  557. #define MPU6050_LP_WAKE_2_5HZ  MPU6050_LP_WAKE_CTRL_1
  558. #define MPU6050_LP_WAKE_5HZ    MPU6050_LP_WAKE_CTRL_2
  559. #define MPU6050_LP_WAKE_10HZ   MPU6050_LP_WAKE_CTRL_3
  560. // Default I2C address for the MPU-6050 is 0x68.
  561. // But only if the AD0 pin is low.
  562. // Some sensor boards have AD0 high, and the
  563. // I2C address thus becomes 0x69.
  564. #define MPU6050_I2C_ADDRESS 0x68
  565. // Declaring an union for the registers and the axis values.
  566. // The byte order does not match the byte order of
  567. // the compiler and AVR chip.
  568. // The AVR chip (on the Arduino board) has the Low Byte
  569. // at the lower address.
  570. // But the MPU-6050 has a different order: High Byte at
  571. // lower address, so that has to be corrected.
  572. // The register part "reg" is only used internally,
  573. // and are swapped in code.
  574. typedef union accel_t_gyro_union
  575. {
  576.   struct
  577.   {
  578.     uint8_t x_accel_h;
  579.     uint8_t x_accel_l;
  580.     uint8_t y_accel_h;
  581.     uint8_t y_accel_l;
  582.     uint8_t z_accel_h;
  583.     uint8_t z_accel_l;
  584.     uint8_t t_h;
  585.     uint8_t t_l;
  586.     uint8_t x_gyro_h;
  587.     uint8_t x_gyro_l;
  588.     uint8_t y_gyro_h;
  589.     uint8_t y_gyro_l;
  590.     uint8_t z_gyro_h;
  591.     uint8_t z_gyro_l;
  592.   } reg;
  593.   struct
  594.   {
  595.     int16_t x_accel;
  596.     int16_t y_accel;
  597.     int16_t z_accel;
  598.     int16_t temperature;
  599.     int16_t x_gyro;
  600.     int16_t y_gyro;
  601.     int16_t z_gyro;
  602.   } value;
  603. };
  604. void setup()
  605. {      
  606.   int error;
  607.   uint8_t c;
  608.   Serial.begin(9600);
  609.   Serial.println(F("InvenSense MPU-6050"));
  610.   Serial.println(F("June 2012"));
  611.   // Initialize the 'Wire' class for the I2C-bus.
  612.   Wire.begin();
  613.   // default at power-up:
  614.   //    Gyro at 250 degrees second
  615.   //    Acceleration at 2g
  616.   //    Clock source at internal 8MHz
  617.   //    The device is in sleep mode.
  618.   //
  619.   error = MPU6050_read (MPU6050_WHO_AM_I, &c, 1);
  620.   Serial.print(F("WHO_AM_I : "));
  621.   Serial.print(c,HEX);
  622.   Serial.print(F(", error = "));
  623.   Serial.println(error,DEC);
  624.   // According to the datasheet, the 'sleep' bit
  625.   // should read a '1'.
  626.   // That bit has to be cleared, since the sensor
  627.   // is in sleep mode at power-up.
  628.   error = MPU6050_read (MPU6050_PWR_MGMT_1, &c, 1);
  629.   Serial.print(F("PWR_MGMT_1 : "));
  630.   Serial.print(c,HEX);
  631.   Serial.print(F(", error = "));
  632.   Serial.println(error,DEC);
  633.   // Clear the 'sleep' bit to start the sensor.
  634.   MPU6050_write_reg (MPU6050_PWR_MGMT_1, 0);
  635. }
  636. void loop()
  637. {
  638.   int error;
  639.   double dT;
  640.   accel_t_gyro_union accel_t_gyro;
  641.   Serial.println(F(""));
  642.   Serial.println(F("MPU-6050"));
  643.   // Read the raw values.
  644.   // Read 14 bytes at once,
  645.   // containing acceleration, temperature and gyro.
  646.   // With the default settings of the MPU-6050,
  647.   // there is no filter enabled, and the values
  648.   // are not very stable.
  649.   error = MPU6050_read (MPU6050_ACCEL_XOUT_H, (uint8_t *) &accel_t_gyro, sizeof(accel_t_gyro));
  650.   Serial.print(F("Read accel, temp and gyro, error = "));
  651.   Serial.println(error,DEC);
  652.   // Swap all high and low bytes.
  653.   // After this, the registers values are swapped,
  654.   // so the structure name like x_accel_l does no
  655.   // longer contain the lower byte.
  656.   uint8_t swap;
  657.   #define SWAP(x,y) swap = x; x = y; y = swap
  658.   SWAP (accel_t_gyro.reg.x_accel_h, accel_t_gyro.reg.x_accel_l);
  659.   SWAP (accel_t_gyro.reg.y_accel_h, accel_t_gyro.reg.y_accel_l);
  660.   SWAP (accel_t_gyro.reg.z_accel_h, accel_t_gyro.reg.z_accel_l);
  661.   SWAP (accel_t_gyro.reg.t_h, accel_t_gyro.reg.t_l);
  662.   SWAP (accel_t_gyro.reg.x_gyro_h, accel_t_gyro.reg.x_gyro_l);
  663.   SWAP (accel_t_gyro.reg.y_gyro_h, accel_t_gyro.reg.y_gyro_l);
  664.   SWAP (accel_t_gyro.reg.z_gyro_h, accel_t_gyro.reg.z_gyro_l);
  665.   // Print the raw acceleration values
  666.   Serial.print(F("accel x,y,z: "));
  667.   Serial.print(accel_t_gyro.value.x_accel, DEC);
  668.   Serial.print(F(", "));
  669.   Serial.print(accel_t_gyro.value.y_accel, DEC);
  670.   Serial.print(F(", "));
  671.   Serial.print(accel_t_gyro.value.z_accel, DEC);
  672.   Serial.println(F(""));
  673.   // The temperature sensor is -40 to +85 degrees Celsius.
  674.   // It is a signed integer.
  675.   // According to the datasheet:
  676.   //   340 per degrees Celsius, -512 at 35 degrees.
  677.   // At 0 degrees: -512 - (340 * 35) = -12412
  678.   Serial.print(F("temperature: "));
  679.   dT = ( (double) accel_t_gyro.value.temperature + 12412.0) / 340.0;
  680.   Serial.print(dT, 3);
  681.   Serial.print(F(" degrees Celsius"));
  682.   Serial.println(F(""));
  683.   // Print the raw gyro values.
  684.   Serial.print(F("gyro x,y,z : "));
  685.   Serial.print(accel_t_gyro.value.x_gyro, DEC);
  686.   Serial.print(F(", "));
  687.   Serial.print(accel_t_gyro.value.y_gyro, DEC);
  688.   Serial.print(F(", "));
  689.   Serial.print(accel_t_gyro.value.z_gyro, DEC);
  690.   Serial.print(F(", "));
  691.   Serial.println(F(""));
  692.   delay(1000);
  693. }
  694. // --------------------------------------------------------
  695. // MPU6050_read
  696. //
  697. // This is a common function to read multiple bytes
  698. // from an I2C device.
  699. //
  700. // It uses the boolean parameter for Wire.endTransMission()
  701. // to be able to hold or release the I2C-bus.
  702. // This is implemented in Arduino 1.0.1.
  703. //
  704. // Only this function is used to read.
  705. // There is no function for a single byte.
  706. //
  707. int MPU6050_read(int start, uint8_t *buffer, int size)
  708. {
  709.   int i, n, error;
  710.   Wire.beginTransmission(MPU6050_I2C_ADDRESS);
  711.   n = Wire.write(start);
  712.   if (n != 1)
  713.     return (-10);
  714.   n = Wire.endTransmission(false);    // hold the I2C-bus
  715.   if (n != 0)
  716.     return (n);
  717.   // Third parameter is true: relase I2C-bus after data is read.
  718.   Wire.requestFrom(MPU6050_I2C_ADDRESS, size, true);
  719.   i = 0;
  720.   while(Wire.available() && i<size)
  721.   {
  722.     buffer[i++]=Wire.read();
  723.   }
  724.   if ( i != size)
  725.     return (-11);
  726.   return (0);  // return : no error
  727. }
  728. // --------------------------------------------------------
  729. // MPU6050_write
  730. //
  731. // This is a common function to write multiple bytes to an I2C device.
  732. //
  733. // If only a single register is written,
  734. // use the function MPU_6050_write_reg().
  735. //
  736. // Parameters:
  737. //   start : Start address, use a define for the register
  738. //   pData : A pointer to the data to write.
  739. //   size  : The number of bytes to write.
  740. //
  741. // If only a single register is written, a pointer
  742. // to the data has to be used, and the size is
  743. // a single byte:
  744. //   int data = 0;        // the data to write
  745. //   MPU6050_write (MPU6050_PWR_MGMT_1, &c, 1);
  746. //
  747. int MPU6050_write(int start, const uint8_t *pData, int size)
  748. {
  749.   int n, error;
  750.   Wire.beginTransmission(MPU6050_I2C_ADDRESS);
  751.   n = Wire.write(start);        // write the start address
  752.   if (n != 1)
  753.     return (-20);
  754.   n = Wire.write(pData, size);  // write data bytes
  755.   if (n != size)
  756.     return (-21);
  757.   error = Wire.endTransmission(true); // release the I2C-bus
  758.   if (error != 0)
  759.     return (error);
  760.   return (0);         // return : no error
  761. }
  762. // --------------------------------------------------------
  763. // MPU6050_write_reg
  764. //
  765. // An extra function to write a single register.
  766. // It is just a wrapper around the MPU_6050_write()
  767. // function, and it is only a convenient function
  768. // to make it easier to write a single register.
  769. //
  770. int MPU6050_write_reg(int reg, uint8_t data)
  771. {
  772.   int error;
  773.   error = MPU6050_write(reg, &data, 1);
  774.   return (error);
  775. }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多