找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2116|回复: 1
打印 上一主题 下一主题
收起左侧

MPU6050资料与arduino源码

[复制链接]
跳转到指定楼层
楼主
MPU6050的资料打包:

mpu6050的arduino源程序如下:
  1. // I2Cdev library collection - MPU6050 I2C device class
  2. // Based on InvenSense MPU-6050 register map document rev. 2.0, 5/19/2011 (RM-MPU-6000A-00)
  3. // 8/24/2011 by Jeff Rowberg <jeff@rowberg.net>
  4. // Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
  5. //
  6. // Changelog:
  7. //     ... - ongoing debug release

  8. // NOTE: THIS IS ONLY A PARIAL RELEASE. THIS DEVICE CLASS IS CURRENTLY UNDERGOING ACTIVE
  9. // DEVELOPMENT AND IS STILL MISSING SOME IMPORTANT FEATURES. PLEASE KEEP THIS IN MIND IF
  10. // YOU DECIDE TO USE THIS PARTICULAR CODE FOR ANYTHING.

  11. /* ============================================
  12. I2Cdev device library code is placed under the MIT license
  13. Copyright (c) 2011 Jeff Rowberg

  14. Permission is hereby granted, free of charge, to any person obtaining a copy
  15. of this software and associated documentation files (the "Software"), to deal
  16. in the Software without restriction, including without limitation the rights
  17. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  18. copies of the Software, and to permit persons to whom the Software is
  19. furnished to do so, subject to the following conditions:

  20. The above copyright notice and this permission notice shall be included in
  21. all copies or substantial portions of the Software.

  22. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. THE SOFTWARE.
  29. ===============================================
  30. */

  31. #include "MPU6050.h"

  32. /** Default constructor, uses default I2C address.
  33. * @see MPU6050_DEFAULT_ADDRESS
  34. */
  35. MPU6050::MPU6050() {
  36.     devAddr = MPU6050_DEFAULT_ADDRESS;
  37. }

  38. /** Specific address constructor.
  39. * @param address I2C address
  40. * @see MPU6050_DEFAULT_ADDRESS
  41. * @see MPU6050_ADDRESS_AD0_LOW
  42. * @see MPU6050_ADDRESS_AD0_HIGH
  43. */
  44. MPU6050::MPU6050(uint8_t address) {
  45.     devAddr = address;
  46. }

  47. /** Power on and prepare for general usage.
  48. * This will activate the device and take it out of sleep mode (which must be done
  49. * after start-up). This function also sets both the accelerometer and the gyroscope
  50. * to their most sensitive settings, namely +/- 2g and +/- 250 degrees/sec, and sets
  51. * the clock source to use the X Gyro for reference, which is slightly better than
  52. * the default internal clock source.
  53. */
  54. void MPU6050::initialize() {
  55.     setClockSource(MPU6050_CLOCK_PLL_XGYRO);
  56.     setFullScaleGyroRange(MPU6050_GYRO_FS_250);
  57.     setFullScaleAccelRange(MPU6050_ACCEL_FS_2);
  58.     setSleepEnabled(false); // thanks to Jack Elston for pointing this one out!
  59. }

  60. /** Verify the I2C connection.
  61. * Make sure the device is connected and responds as expected.
  62. * @return True if connection is valid, false otherwise
  63. */
  64. bool MPU6050::testConnection() {
  65.     return getDeviceID() == 0b110100;
  66. }

  67. // AUX_VDDIO register (InvenSense demo code calls this RA_*G_OFFS_TC)

  68. /** Get the auxiliary I2C supply voltage level.
  69. * When set to 1, the auxiliary I2C bus high logic level is VDD. When cleared to
  70. * 0, the auxiliary I2C bus high logic level is VLOGIC. This does not apply to
  71. * the MPU-6000, which does not have a VLOGIC pin.
  72. * @return I2C supply voltage level (0=VLOGIC, 1=VDD)
  73. */
  74. uint8_t MPU6050::getAuxVDDIOLevel() {
  75.     I2Cdev::readBit(devAddr, MPU6050_RA_YG_OFFS_TC, MPU6050_TC_PWR_MODE_BIT, buffer);
  76.     return buffer[0];
  77. }
  78. /** Set the auxiliary I2C supply voltage level.
  79. * When set to 1, the auxiliary I2C bus high logic level is VDD. When cleared to
  80. * 0, the auxiliary I2C bus high logic level is VLOGIC. This does not apply to
  81. * the MPU-6000, which does not have a VLOGIC pin.
  82. * @param level I2C supply voltage level (0=VLOGIC, 1=VDD)
  83. */
  84. void MPU6050::setAuxVDDIOLevel(uint8_t level) {
  85.     I2Cdev::writeBit(devAddr, MPU6050_RA_YG_OFFS_TC, MPU6050_TC_PWR_MODE_BIT, level);
  86. }

  87. // SMPLRT_DIV register

  88. /** Get gyroscope output rate divider.
  89. * The sensor register output, FIFO output, DMP sampling, Motion detection, Zero
  90. * Motion detection, and Free Fall detection are all based on the Sample Rate.
  91. * The Sample Rate is generated by dividing the gyroscope output rate by
  92. * SMPLRT_DIV:
  93. *
  94. * Sample Rate = Gyroscope Output Rate / (1 + SMPLRT_DIV)
  95. *
  96. * where Gyroscope Output Rate = 8kHz when the DLPF is disabled (DLPF_CFG = 0 or
  97. * 7), and 1kHz when the DLPF is enabled (see Register 26).
  98. *
  99. * Note: The accelerometer output rate is 1kHz. This means that for a Sample
  100. * Rate greater than 1kHz, the same accelerometer sample may be output to the
  101. * FIFO, DMP, and sensor registers more than once.
  102. *
  103. * For a diagram of the gyroscope and accelerometer signal paths, see Section 8
  104. * of the MPU-6000/MPU-6050 Product Specification document.
  105. *
  106. * @return Current sample rate
  107. * @see MPU6050_RA_SMPLRT_DIV
  108. */
  109. uint8_t MPU6050::getRate() {
  110.     return I2Cdev::readByte(devAddr, MPU6050_RA_SMPLRT_DIV, buffer);
  111.     return buffer[0];
  112. }
  113. /** Set gyroscope sample rate divider.
  114. * @param rate New sample rate divider
  115. * @see getRate()
  116. * @see MPU6050_RA_SMPLRT_DIV
  117. */
  118. void MPU6050::setRate(uint8_t rate) {
  119.     I2Cdev::writeByte(devAddr, MPU6050_RA_SMPLRT_DIV, rate);
  120. }

  121. // CONFIG register

  122. /** Get external FSYNC configuration.
  123. * Configures the external Frame Synchronization (FSYNC) pin sampling. An
  124. * external signal connected to the FSYNC pin can be sampled by configuring
  125. * EXT_SYNC_SET. Signal changes to the FSYNC pin are latched so that short
  126. * strobes may be captured. The latched FSYNC signal will be sampled at the
  127. * Sampling Rate, as defined in register 25. After sampling, the latch will
  128. * reset to the current FSYNC signal state.
  129. *
  130. * The sampled value will be reported in place of the least significant bit in
  131. * a sensor data register determined by the value of EXT_SYNC_SET according to
  132. * the following table.
  133. *
  134. * <pre>
  135. * EXT_SYNC_SET | FSYNC Bit Location
  136. * -------------+-------------------
  137. * 0            | Input disabled
  138. * 1            | TEMP_OUT_L[0]
  139. * 2            | GYRO_XOUT_L[0]
  140. * 3            | GYRO_YOUT_L[0]
  141. * 4            | GYRO_ZOUT_L[0]
  142. * 5            | ACCEL_XOUT_L[0]
  143. * 6            | ACCEL_YOUT_L[0]
  144. * 7            | ACCEL_ZOUT_L[0]
  145. * </pre>
  146. *
  147. * @return FSYNC configuration value
  148. */
  149. uint8_t MPU6050::getExternalFrameSync() {
  150.     I2Cdev::readBits(devAddr, MPU6050_RA_CONFIG, MPU6050_CFG_EXT_SYNC_SET_BIT, MPU6050_CFG_EXT_SYNC_SET_LENGTH, buffer);
  151.     return buffer[0];
  152. }
  153. /** Set external FSYNC configuration.
  154. * @see getExternalFrameSync()
  155. * @see MPU6050_RA_CONFIG
  156. * @param sync New FSYNC configuration value
  157. */
  158. void MPU6050::setExternalFrameSync(uint8_t sync) {
  159.     I2Cdev::writeBits(devAddr, MPU6050_RA_CONFIG, MPU6050_CFG_EXT_SYNC_SET_BIT, MPU6050_CFG_EXT_SYNC_SET_LENGTH, sync);
  160. }
  161. /** Get digital low-pass filter configuration.
  162. * The DLPF_CFG parameter sets the digital low pass filter configuration. It
  163. * also determines the internal sampling rate used by the device as shown in
  164. * the table below.
  165. *
  166. * Note: The accelerometer output rate is 1kHz. This means that for a Sample
  167. * Rate greater than 1kHz, the same accelerometer sample may be output to the
  168. * FIFO, DMP, and sensor registers more than once.
  169. *
  170. * <pre>
  171. *          |   ACCELEROMETER    |           GYROSCOPE
  172. * DLPF_CFG | Bandwidth | Delay  | Bandwidth | Delay  | Sample Rate
  173. * ---------+-----------+--------+-----------+--------+-------------
  174. * 0        | 260Hz     | 0ms    | 256Hz     | 0.98ms | 8kHz
  175. * 1        | 184Hz     | 2.0ms  | 188Hz     | 1.9ms  | 1kHz
  176. * 2        | 94Hz      | 3.0ms  | 98Hz      | 2.8ms  | 1kHz
  177. * 3        | 44Hz      | 4.9ms  | 42Hz      | 4.8ms  | 1kHz
  178. * 4        | 21Hz      | 8.5ms  | 20Hz      | 8.3ms  | 1kHz
  179. * 5        | 10Hz      | 13.8ms | 10Hz      | 13.4ms | 1kHz
  180. * 6        | 5Hz       | 19.0ms | 5Hz       | 18.6ms | 1kHz
  181. * 7        |   -- Reserved --   |   -- Reserved --   | Reserved
  182. * </pre>
  183. *
  184. * @return DLFP configuration
  185. * @see MPU6050_RA_CONFIG
  186. * @see MPU6050_CFG_DLPF_CFG_BIT
  187. * @see MPU6050_CFG_DLPF_CFG_LENGTH
  188. */
  189. uint8_t MPU6050::getDLPFMode() {
  190.     I2Cdev::readBits(devAddr, MPU6050_RA_CONFIG, MPU6050_CFG_DLPF_CFG_BIT, MPU6050_CFG_DLPF_CFG_LENGTH, buffer);
  191.     return buffer[0];
  192. }
  193. /** Set digital low-pass filter configuration.
  194. * @param mode New DLFP configuration setting
  195. * @see getDLPFBandwidth()
  196. * @see MPU6050_DLPF_BW_256
  197. * @see MPU6050_RA_CONFIG
  198. * @see MPU6050_CFG_DLPF_CFG_BIT
  199. * @see MPU6050_CFG_DLPF_CFG_LENGTH
  200. */
  201. void MPU6050::setDLPFMode(uint8_t mode) {
  202.     I2Cdev::writeBits(devAddr, MPU6050_RA_CONFIG, MPU6050_CFG_DLPF_CFG_BIT, MPU6050_CFG_DLPF_CFG_LENGTH, mode);
  203. }

  204. // GYRO_CONFIG register

  205. /** Get full-scale gyroscope range.
  206. * The FS_SEL parameter allows setting the full-scale range of the gyro sensors,
  207. * as described in the table below.
  208. *
  209. * <pre>
  210. * 0 = +/- 250 degrees/sec
  211. * 1 = +/- 500 degrees/sec
  212. * 2 = +/- 1000 degrees/sec
  213. * 3 = +/- 2000 degrees/sec
  214. * </pre>
  215. *
  216. * @return Current full-scale gyroscope range setting
  217. * @see MPU6050_GYRO_FS_250
  218. * @see MPU6050_RA_GYRO_CONFIG
  219. * @see MPU6050_GCONFIG_FS_SEL_BIT
  220. * @see MPU6050_GCONFIG_FS_SEL_LENGTH
  221. */
  222. uint8_t MPU6050::getFullScaleGyroRange() {
  223.     I2Cdev::readBits(devAddr, MPU6050_RA_GYRO_CONFIG, MPU6050_GCONFIG_FS_SEL_BIT, MPU6050_GCONFIG_FS_SEL_LENGTH, buffer);
  224.     return buffer[0];
  225. }
  226. /** Set full-scale gyroscope range.
  227. * @param range New full-scale gyroscope range value
  228. * @see getFullScaleRange()
  229. * @see MPU6050_GYRO_FS_250
  230. * @see MPU6050_RA_GYRO_CONFIG
  231. * @see MPU6050_GCONFIG_FS_SEL_BIT
  232. * @see MPU6050_GCONFIG_FS_SEL_LENGTH
  233. */
  234. void MPU6050::setFullScaleGyroRange(uint8_t range) {
  235.     I2Cdev::writeBits(devAddr, MPU6050_RA_GYRO_CONFIG, MPU6050_GCONFIG_FS_SEL_BIT, MPU6050_GCONFIG_FS_SEL_LENGTH, range);
  236. }

  237. // ACCEL_CONFIG register

  238. /** Get self-test enabled setting for accelerometer X axis.
  239. * @return Self-test enabled value
  240. * @see MPU6050_RA_ACCEL_CONFIG
  241. */
  242. bool MPU6050::getAccelXSelfTest() {
  243.     I2Cdev::readBit(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_XA_ST_BIT, buffer);
  244.     return buffer[0];
  245. }
  246. /** Get self-test enabled setting for accelerometer X axis.
  247. * @param enabled Self-test enabled value
  248. * @see MPU6050_RA_ACCEL_CONFIG
  249. */
  250. void MPU6050::setAccelXSelfTest(bool enabled) {
  251.     I2Cdev::writeBit(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_XA_ST_BIT, enabled);
  252. }
  253. /** Get self-test enabled value for accelerometer Y axis.
  254. * @return Self-test enabled value
  255. * @see MPU6050_RA_ACCEL_CONFIG
  256. */
  257. bool MPU6050::getAccelYSelfTest() {
  258.     I2Cdev::readBit(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_YA_ST_BIT, buffer);
  259.     return buffer[0];
  260. }
  261. /** Get self-test enabled value for accelerometer Y axis.
  262. * @param enabled Self-test enabled value
  263. * @see MPU6050_RA_ACCEL_CONFIG
  264. */
  265. void MPU6050::setAccelYSelfTest(bool enabled) {
  266.     I2Cdev::writeBit(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_YA_ST_BIT, enabled);
  267. }
  268. /** Get self-test enabled value for accelerometer Z axis.
  269. * @return Self-test enabled value
  270. * @see MPU6050_RA_ACCEL_CONFIG
  271. */
  272. bool MPU6050::getAccelZSelfTest() {
  273.     I2Cdev::readBit(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_ZA_ST_BIT, buffer);
  274.     return buffer[0];
  275. }
  276. /** Set self-test enabled value for accelerometer Z axis.
  277. * @param enabled Self-test enabled value
  278. * @see MPU6050_RA_ACCEL_CONFIG
  279. */
  280. void MPU6050::setAccelZSelfTest(bool enabled) {
  281.     I2Cdev::writeBit(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_ZA_ST_BIT, enabled);
  282. }
  283. /** Get full-scale accelerometer range.
  284. * The FS_SEL parameter allows setting the full-scale range of the accelerometer
  285. * sensors, as described in the table below.
  286. *
  287. * <pre>
  288. * 0 = +/- 2g
  289. * 1 = +/- 4g
  290. * 2 = +/- 8g
  291. * 3 = +/- 16g
  292. * </pre>
  293. *
  294. * @return Current full-scale accelerometer range setting
  295. * @see MPU6050_ACCEL_FS_2
  296. * @see MPU6050_RA_ACCEL_CONFIG
  297. * @see MPU6050_ACONFIG_AFS_SEL_BIT
  298. * @see MPU6050_ACONFIG_AFS_SEL_LENGTH
  299. */
  300. uint8_t MPU6050::getFullScaleAccelRange() {
  301.     I2Cdev::readBits(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_AFS_SEL_BIT, MPU6050_ACONFIG_AFS_SEL_LENGTH, buffer);
  302.     return buffer[0];
  303. }
  304. /** Set full-scale accelerometer range.
  305. * @param range New full-scale accelerometer range setting
  306. * @see getFullScaleAccelRange()
  307. */
  308. void MPU6050::setFullScaleAccelRange(uint8_t range) {
  309.     I2Cdev::writeBits(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_AFS_SEL_BIT, MPU6050_ACONFIG_AFS_SEL_LENGTH, range);
  310. }
  311. /** Get the high-pass filter configuration.
  312. * The DHPF is a filter module in the path leading to motion detectors (Free
  313. * Fall, Motion threshold, and Zero Motion). The high pass filter output is not
  314. * available to the data registers (see Figure in Section 8 of the MPU-6000/
  315. * MPU-6050 Product Specification document).
  316. *
  317. * The high pass filter has three modes:
  318. *
  319. * <pre>
  320. *    Reset: The filter output settles to zero within one sample. This
  321. *           effectively disables the high pass filter. This mode may be toggled
  322. *           to quickly settle the filter.
  323. *
  324. *    On:    The high pass filter will pass signals above the cut off frequency.
  325. *
  326. *    Hold:  When triggered, the filter holds the present sample. The filter
  327. *           output will be the difference between the input sample and the held
  328. *           sample.
  329. * </pre>
  330. *
  331. * <pre>
  332. * ACCEL_HPF | Filter Mode | Cut-off Frequency
  333. * ----------+-------------+------------------
  334. * 0         | Reset       | None
  335. * 1         | On          | 5Hz
  336. * 2         | On          | 2.5Hz
  337. * 3         | On          | 1.25Hz
  338. * 4         | On          | 0.63Hz
  339. * 7         | Hold        | None
  340. * </pre>
  341. *
  342. * @return Current high-pass filter configuration
  343. * @see MPU6050_DHPF_RESET
  344. * @see MPU6050_RA_ACCEL_CONFIG
  345. */
  346. uint8_t MPU6050::getDHPFMode() {
  347.     I2Cdev::readBits(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_ACCEL_HPF_BIT, MPU6050_ACONFIG_ACCEL_HPF_LENGTH, buffer);
  348.     return buffer[0];
  349. }
  350. /** Set the high-pass filter configuration.
  351. * @param bandwidth New high-pass filter configuration
  352. * @see setDHPFMode()
  353. * @see MPU6050_DHPF_RESET
  354. * @see MPU6050_RA_ACCEL_CONFIG
  355. */
  356. void MPU6050::setDHPFMode(uint8_t bandwidth) {
  357.     I2Cdev::writeBits(devAddr, MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_ACCEL_HPF_BIT, MPU6050_ACONFIG_ACCEL_HPF_LENGTH, bandwidth);
  358. }

  359. // FF_THR register

  360. /** Get free-fall event acceleration threshold.
  361. * This register configures the detection threshold for Free Fall event
  362. * detection. The unit of FF_THR is 1LSB = 2mg. Free Fall is detected when the
  363. * absolute value of the accelerometer measurements for the three axes are each
  364. * less than the detection threshold. This condition increments the Free Fall
  365. * duration counter (Register 30). The Free Fall interrupt is triggered when the
  366. * Free Fall duration counter reaches the time specified in FF_DUR.
  367. *
  368. * For more details on the Free Fall detection interrupt, see Section 8.2 of the
  369. * MPU-6000/MPU-6050 Product Specification document as well as Registers 56 and
  370. * 58 of this document.
  371. *
  372. * @return Current free-fall acceleration threshold value (LSB = 2mg)
  373. * @see MPU6050_RA_FF_THR
  374. */
  375. uint8_t MPU6050::getFreefallDetectionThreshold() {
  376.     I2Cdev::readByte(devAddr, MPU6050_RA_FF_THR, buffer);
  377.     return buffer[0];
  378. }
  379. /** Get free-fall event acceleration threshold.
  380. * @param threshold New free-fall acceleration threshold value (LSB = 2mg)
  381. * @see getFreefallDetectionThreshold()
  382. * @see MPU6050_RA_FF_THR
  383. */
  384. void MPU6050::setFreefallDetectionThreshold(uint8_t threshold) {
  385.     I2Cdev::writeByte(devAddr, MPU6050_RA_FF_THR, threshold);
  386. }

  387. // FF_DUR register

  388. /** Get free-fall event duration threshold.
  389. * This register configures the duration counter threshold for Free Fall event
  390. * detection. The duration counter ticks at 1kHz, therefore FF_DUR has a unit
  391. * of 1 LSB = 1 ms.
  392. *
  393. * The Free Fall duration counter increments while the absolute value of the
  394. * accelerometer measurements are each less than the detection threshold
  395. * (Register 29). The Free Fall interrupt is triggered when the Free Fall
  396. * duration counter reaches the time specified in this register.
  397. *
  398. * For more details on the Free Fall detection interrupt, see Section 8.2 of
  399. * the MPU-6000/MPU-6050 Product Specification document as well as Registers 56
  400. * and 58 of this document.
  401. *
  402. * @return Current free-fall duration threshold value (LSB = 1ms)
  403. * @see MPU6050_RA_FF_DUR
  404. */
  405. uint8_t MPU6050::getFreefallDetectionDuration() {
  406.     I2Cdev::readByte(devAddr, MPU6050_RA_FF_DUR, buffer);
  407.     return buffer[0];
  408. }
  409. /** Get free-fall event duration threshold.
  410. * @param duration New free-fall duration threshold value (LSB = 1ms)
  411. * @see getFreefallDetectionDuration()
  412. * @see MPU6050_RA_FF_DUR
  413. */
  414. void MPU6050::setFreefallDetectionDuration(uint8_t duration) {
  415.     I2Cdev::writeByte(devAddr, MPU6050_RA_FF_DUR, duration);
  416. }

  417. // MOT_THR register

  418. /** Get motion detection event acceleration threshold.
  419. * This register configures the detection threshold for Motion interrupt
  420. * generation. The unit of MOT_THR is 1LSB = 2mg. Motion is detected when the
  421. * absolute value of any of the accelerometer measurements exceeds this Motion
  422. * detection threshold. This condition increments the Motion detection duration
  423. * counter (Register 32). The Motion detection interrupt is triggered when the
  424. * Motion Detection counter reaches the time count specified in MOT_DUR
  425. * (Register 32).
  426. *
  427. * The Motion interrupt will indicate the axis and polarity of detected motion
  428. * in MOT_DETECT_STATUS (Register 97).
  429. *
  430. * For more details on the Motion detection interrupt, see Section 8.3 of the
  431. * MPU-6000/MPU-6050 Product Specification document as well as Registers 56 and
  432. * 58 of this document.
  433. *
  434. * @return Current motion detection acceleration threshold value (LSB = 2mg)
  435. * @see MPU6050_RA_MOT_THR
  436. */
  437. uint8_t MPU6050::getMotionDetectionThreshold() {
  438.     I2Cdev::readByte(devAddr, MPU6050_RA_MOT_THR, buffer);
  439.     return buffer[0];
  440. }
  441. /** Set free-fall event acceleration threshold.
  442. * @param threshold New motion detection acceleration threshold value (LSB = 2mg)
  443. * @see getMotionDetectionThreshold()
  444. * @see MPU6050_RA_MOT_THR
  445. */
  446. void MPU6050::setMotionDetectionThreshold(uint8_t threshold) {
  447.     I2Cdev::writeByte(devAddr, MPU6050_RA_MOT_THR, threshold);
  448. }

  449. // MOT_DUR register

  450. /** Get motion detection event duration threshold.
  451. * This register configures the duration counter threshold for Motion interrupt
  452. * generation. The duration counter ticks at 1 kHz, therefore MOT_DUR has a unit
  453. * of 1LSB = 1ms. The Motion detection duration counter increments when the
  454. * absolute value of any of the accelerometer measurements exceeds the Motion
  455. * detection threshold (Register 31). The Motion detection interrupt is
  456. * triggered when the Motion detection counter reaches the time count specified
  457. * in this register.
  458. *
  459. * For more details on the Motion detection interrupt, see Section 8.3 of the
  460. * MPU-6000/MPU-6050 Product Specification document.
  461. *
  462. * @return Current motion detection duration threshold value (LSB = 1ms)
  463. * @see MPU6050_RA_MOT_DUR
  464. */
  465. uint8_t MPU6050::getMotionDetectionDuration() {
  466.     I2Cdev::readByte(devAddr, MPU6050_RA_MOT_DUR, buffer);
  467.     return buffer[0];
  468. }
  469. /** Set motion detection event duration threshold.
  470. * @param duration New motion detection duration threshold value (LSB = 1ms)
  471. * @see getMotionDetectionDuration()
  472. * @see MPU6050_RA_MOT_DUR
  473. */
  474. void MPU6050::setMotionDetectionDuration(uint8_t duration) {
  475.     I2Cdev::writeByte(devAddr, MPU6050_RA_MOT_DUR, duration);
  476. }

  477. // ZRMOT_THR register

  478. /** Get zero motion detection event acceleration threshold.
  479. * This register configures the detection threshold for Zero Motion interrupt
  480. * generation. The unit of ZRMOT_THR is 1LSB = 2mg. Zero Motion is detected when
  481. * the absolute value of the accelerometer measurements for the 3 axes are each
  482. * less than the detection threshold. This condition increments the Zero Motion
  483. * duration counter (Register 34). The Zero Motion interrupt is triggered when
  484. * the Zero Motion duration counter reaches the time count specified in
  485. * ZRMOT_DUR (Register 34).
  486. *
  487. * Unlike Free Fall or Motion detection, Zero Motion detection triggers an
  488. * interrupt both when Zero Motion is first detected and when Zero Motion is no
  489. * longer detected.
  490. *
  491. * When a zero motion event is detected, a Zero Motion Status will be indicated
  492. * in the MOT_DETECT_STATUS register (Register 97). When a motion-to-zero-motion
  493. * condition is detected, the status bit is set to 1. When a zero-motion-to-
  494. * motion condition is detected, the status bit is set to 0.
  495. *
  496. * For more details on the Zero Motion detection interrupt, see Section 8.4 of
  497. * the MPU-6000/MPU-6050 Product Specification document as well as Registers 56
  498. * and 58 of this document.
  499. *
  500. * @return Current zero motion detection acceleration threshold value (LSB = 2mg)
  501. * @see MPU6050_RA_ZRMOT_THR
  502. */
  503. uint8_t MPU6050::getZeroMotionDetectionThreshold() {
  504.     I2Cdev::readByte(devAddr, MPU6050_RA_ZRMOT_THR, buffer);
  505.     return buffer[0];
  506. }
  507. /** Set zero motion detection event acceleration threshold.
  508. * @param threshold New zero motion detection acceleration threshold value (LSB = 2mg)
  509. * @see getZeroMotionDetectionThreshold()
  510. * @see MPU6050_RA_ZRMOT_THR
  511. */
  512. void MPU6050::setZeroMotionDetectionThreshold(uint8_t threshold) {
  513.     I2Cdev::writeByte(devAddr, MPU6050_RA_ZRMOT_THR, threshold);
  514. }

  515. // ZRMOT_DUR register

  516. /** Get zero motion detection event duration threshold.
  517. * This register configures the duration counter threshold for Zero Motion
  518. * interrupt generation. The duration counter ticks at 16 Hz, therefore
  519. * ZRMOT_DUR has a unit of 1 LSB = 64 ms. The Zero Motion duration counter
  520. * increments while the absolute value of the accelerometer measurements are
  521. * each less than the detection threshold (Register 33). The Zero Motion
  522. * interrupt is triggered when the Zero Motion duration counter reaches the time
  523. * count specified in this register.
  524. *
  525. * For more details on the Zero Motion detection interrupt, see Section 8.4 of
  526. * the MPU-6000/MPU-6050 Product Specification document, as well as Registers 56
  527. * and 58 of this document.
  528. *
  529. * @return Current zero motion detection duration threshold value (LSB = 64ms)
  530. * @see MPU6050_RA_ZRMOT_DUR
  531. */
  532. uint8_t MPU6050::getZeroMotionDetectionDuration() {
  533.     I2Cdev::readByte(devAddr, MPU6050_RA_ZRMOT_DUR, buffer);
  534.     return buffer[0];
  535. }
  536. /** Set zero motion detection event duration threshold.
  537. * @param duration New zero motion detection duration threshold value (LSB = 1ms)
  538. * @see getZeroMotionDetectionDuration()
  539. * @see MPU6050_RA_ZRMOT_DUR
  540. */
  541. void MPU6050::setZeroMotionDetectionDuration(uint8_t duration) {
  542.     I2Cdev::writeByte(devAddr, MPU6050_RA_ZRMOT_DUR, duration);
  543. }

  544. // FIFO_EN register

  545. /** Get temperature FIFO enabled value.
  546. * When set to 1, this bit enables TEMP_OUT_H and TEMP_OUT_L (Registers 65 and
  547. * 66) to be written into the FIFO buffer.
  548. * @return Current temperature FIFO enabled value
  549. * @see MPU6050_RA_FIFO_EN
  550. */
  551. bool MPU6050::getTempFIFOEnabled() {
  552.     I2Cdev::readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_TEMP_FIFO_EN_BIT, buffer);
  553.     return buffer[0];
  554. }
  555. /** Set temperature FIFO enabled value.
  556. * @param enabled New temperature FIFO enabled value
  557. * @see getTempFIFOEnabled()
  558. * @see MPU6050_RA_FIFO_EN
  559. */
  560. void MPU6050::setTempFIFOEnabled(bool enabled) {
  561.     I2Cdev::writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_TEMP_FIFO_EN_BIT, enabled);
  562. }
  563. /** Get gyroscope X-axis FIFO enabled value.
  564. * When set to 1, this bit enables GYRO_XOUT_H and GYRO_XOUT_L (Registers 67 and
  565. * 68) to be written into the FIFO buffer.
  566. * @return Current gyroscope X-axis FIFO enabled value
  567. * @see MPU6050_RA_FIFO_EN
  568. */
  569. bool MPU6050::getXGyroFIFOEnabled() {
  570.     I2Cdev::readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_XG_FIFO_EN_BIT, buffer);
  571.     return buffer[0];
  572. }
  573. /** Set gyroscope X-axis FIFO enabled value.
  574. * @param enabled New gyroscope X-axis FIFO enabled value
  575. * @see getXGyroFIFOEnabled()
  576. * @see MPU6050_RA_FIFO_EN
  577. */
  578. void MPU6050::setXGyroFIFOEnabled(bool enabled) {
  579.     I2Cdev::writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_XG_FIFO_EN_BIT, enabled);
  580. }
  581. /** Get gyroscope Y-axis FIFO enabled value.
  582. * When set to 1, this bit enables GYRO_YOUT_H and GYRO_YOUT_L (Registers 69 and
  583. * 70) to be written into the FIFO buffer.
  584. * @return Current gyroscope Y-axis FIFO enabled value
  585. * @see MPU6050_RA_FIFO_EN
  586. */
  587. bool MPU6050::getYGyroFIFOEnabled() {
  588.     I2Cdev::readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_YG_FIFO_EN_BIT, buffer);
  589.     return buffer[0];
  590. }
  591. /** Set gyroscope Y-axis FIFO enabled value.
  592. * @param enabled New gyroscope Y-axis FIFO enabled value
  593. * @see getYGyroFIFOEnabled()
  594. * @see MPU6050_RA_FIFO_EN
  595. */
  596. void MPU6050::setYGyroFIFOEnabled(bool enabled) {
  597.     I2Cdev::writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_YG_FIFO_EN_BIT, enabled);
  598. }
  599. /** Get gyroscope Z-axis FIFO enabled value.
  600. * When set to 1, this bit enables GYRO_ZOUT_H and GYRO_ZOUT_L (Registers 71 and
  601. * 72) to be written into the FIFO buffer.
  602. * @return Current gyroscope Z-axis FIFO enabled value
  603. * @see MPU6050_RA_FIFO_EN
  604. */
  605. bool MPU6050::getZGyroFIFOEnabled() {
  606.     I2Cdev::readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_ZG_FIFO_EN_BIT, buffer);
  607.     return buffer[0];
  608. }
  609. /** Set gyroscope Z-axis FIFO enabled value.
  610. * @param enabled New gyroscope Z-axis FIFO enabled value
  611. * @see getZGyroFIFOEnabled()
  612. * @see MPU6050_RA_FIFO_EN
  613. */
  614. void MPU6050::setZGyroFIFOEnabled(bool enabled) {
  615.     I2Cdev::writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_ZG_FIFO_EN_BIT, enabled);
  616. }
  617. /** Get accelerometer FIFO enabled value.
  618. * When set to 1, this bit enables ACCEL_XOUT_H, ACCEL_XOUT_L, ACCEL_YOUT_H,
  619. * ACCEL_YOUT_L, ACCEL_ZOUT_H, and ACCEL_ZOUT_L (Registers 59 to 64) to be
  620. * written into the FIFO buffer.
  621. * @return Current accelerometer FIFO enabled value
  622. * @see MPU6050_RA_FIFO_EN
  623. */
  624. bool MPU6050::getAccelFIFOEnabled() {
  625.     I2Cdev::readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_ACCEL_FIFO_EN_BIT, buffer);
  626.     return buffer[0];
  627. }
  628. /** Set accelerometer FIFO enabled value.
  629. * @param enabled New accelerometer FIFO enabled value
  630. * @see getAccelFIFOEnabled()
  631. * @see MPU6050_RA_FIFO_EN
  632. */
  633. void MPU6050::setAccelFIFOEnabled(bool enabled) {
  634.     I2Cdev::writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_ACCEL_FIFO_EN_BIT, enabled);
  635. }
  636. /** Get Slave 2 FIFO enabled value.
  637. * When set to 1, this bit enables EXT_SENS_DATA registers (Registers 73 to 96)
  638. * associated with Slave 2 to be written into the FIFO buffer.
  639. * @return Current Slave 2 FIFO enabled value
  640. * @see MPU6050_RA_FIFO_EN
  641. */
  642. bool MPU6050::getSlave2FIFOEnabled() {
  643.     I2Cdev::readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV2_FIFO_EN_BIT, buffer);
  644.     return buffer[0];
  645. }
  646. /** Set Slave 2 FIFO enabled value.
  647. * @param enabled New Slave 2 FIFO enabled value
  648. * @see getSlave2FIFOEnabled()
  649. * @see MPU6050_RA_FIFO_EN
  650. */
  651. void MPU6050::setSlave2FIFOEnabled(bool enabled) {
  652.     I2Cdev::writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV2_FIFO_EN_BIT, enabled);
  653. }
  654. /** Get Slave 1 FIFO enabled value.
  655. * When set to 1, this bit enables EXT_SENS_DATA registers (Registers 73 to 96)
  656. * associated with Slave 1 to be written into the FIFO buffer.
  657. * @return Current Slave 1 FIFO enabled value
  658. * @see MPU6050_RA_FIFO_EN
  659. */
  660. bool MPU6050::getSlave1FIFOEnabled() {
  661.     I2Cdev::readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV1_FIFO_EN_BIT, buffer);
  662.     return buffer[0];
  663. }
  664. /** Set Slave 1 FIFO enabled value.
  665. * @param enabled New Slave 1 FIFO enabled value
  666. * @see getSlave1FIFOEnabled()
  667. * @see MPU6050_RA_FIFO_EN
  668. */
  669. void MPU6050::setSlave1FIFOEnabled(bool enabled) {
  670.     I2Cdev::writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV1_FIFO_EN_BIT, enabled);
  671. }
  672. /** Get Slave 0 FIFO enabled value.
  673. * When set to 1, this bit enables EXT_SENS_DATA registers (Registers 73 to 96)
  674. * associated with Slave 0 to be written into the FIFO buffer.
  675. * @return Current Slave 0 FIFO enabled value
  676. * @see MPU6050_RA_FIFO_EN
  677. */
  678. bool MPU6050::getSlave0FIFOEnabled() {
  679.     I2Cdev::readBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV0_FIFO_EN_BIT, buffer);
  680.     return buffer[0];
  681. }
  682. /** Set Slave 0 FIFO enabled value.
  683. * @param enabled New Slave 0 FIFO enabled value
  684. * @see getSlave0FIFOEnabled()
  685. * @see MPU6050_RA_FIFO_EN
  686. */
  687. void MPU6050::setSlave0FIFOEnabled(bool enabled) {
  688.     I2Cdev::writeBit(devAddr, MPU6050_RA_FIFO_EN, MPU6050_SLV0_FIFO_EN_BIT, enabled);
  689. }

  690. // I2C_MST_CTRL register

  691. /** Get multi-master enabled value.
  692. * Multi-master capability allows multiple I2C masters to operate on the same
  693. * bus. In circuits where multi-master capability is required, set MULT_MST_EN
  694. * to 1. This will increase current drawn by approximately 30uA.
  695. *
  696. * In circuits where multi-master capability is required, the state of the I2C
  697. * bus must always be monitored by each separate I2C Master. Before an I2C
  698. * Master can assume arbitration of the bus, it must first confirm that no other
  699. * I2C Master has arbitration of the bus. When MULT_MST_EN is set to 1, the
  700. * MPU-60X0's bus arbitration detection logic is turned on, enabling it to
  701. * detect when the bus is available.
  702. *
  703. * @return Current multi-master enabled value
  704. * @see MPU6050_RA_I2C_MST_CTRL
  705. */
  706. bool MPU6050::getMultiMasterEnabled() {
  707.     I2Cdev::readBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_MULT_MST_EN_BIT, buffer);
  708.     return buffer[0];
  709. }
  710. /** Set multi-master enabled value.
  711. * @param enabled New multi-master enabled value
  712. * @see getMultiMasterEnabled()
  713. * @see MPU6050_RA_I2C_MST_CTRL
  714. */
  715. void MPU6050::setMultiMasterEnabled(bool enabled) {
  716.     I2Cdev::writeBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_MULT_MST_EN_BIT, enabled);
  717. }
  718. /** Get wait-for-external-sensor-data enabled value.
  719. * When the WAIT_FOR_ES bit is set to 1, the Data Ready interrupt will be
  720. * delayed until External Sensor data from the Slave Devices are loaded into the
  721. * EXT_SENS_DATA registers. This is used to ensure that both the internal sensor
  722. * data (i.e. from gyro and accel) and external sensor data have been loaded to
  723. * their respective data registers (i.e. the data is synced) when the Data Ready
  724. * interrupt is triggered.
  725. *
  726. * @return Current wait-for-external-sensor-data enabled value
  727. * @see MPU6050_RA_I2C_MST_CTRL
  728. */
  729. bool MPU6050::getWaitForExternalSensorEnabled() {
  730.     I2Cdev::readBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_WAIT_FOR_ES_BIT, buffer);
  731.     return buffer[0];
  732. }
  733. /** Set wait-for-external-sensor-data enabled value.
  734. * @param enabled New wait-for-external-sensor-data enabled value
  735. * @see getWaitForExternalSensorEnabled()
  736. * @see MPU6050_RA_I2C_MST_CTRL
  737. */
  738. void MPU6050::setWaitForExternalSensorEnabled(bool enabled) {
  739.     I2Cdev::writeBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_WAIT_FOR_ES_BIT, enabled);
  740. }
  741. /** Get Slave 3 FIFO enabled value.
  742. * When set to 1, this bit enables EXT_SENS_DATA registers (Registers 73 to 96)
  743. * associated with Slave 3 to be written into the FIFO buffer.
  744. * @return Current Slave 3 FIFO enabled value
  745. * @see MPU6050_RA_MST_CTRL
  746. */
  747. bool MPU6050::getSlave3FIFOEnabled() {
  748.     I2Cdev::readBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_SLV_3_FIFO_EN_BIT, buffer);
  749.     return buffer[0];
  750. }
  751. /** Set Slave 3 FIFO enabled value.
  752. * @param enabled New Slave 3 FIFO enabled value
  753. * @see getSlave3FIFOEnabled()
  754. * @see MPU6050_RA_MST_CTRL
  755. */
  756. void MPU6050::setSlave3FIFOEnabled(bool enabled) {
  757.     I2Cdev::writeBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_SLV_3_FIFO_EN_BIT, enabled);
  758. }
  759. /** Get slave read/write transition enabled value.
  760. * The I2C_MST_P_NSR bit configures the I2C Master's transition from one slave
  761. * read to the next slave read. If the bit equals 0, there will be a restart
  762. * between reads. If the bit equals 1, there will be a stop followed by a start
  763. * of the following read. When a write transaction follows a read transaction,
  764. * the stop followed by a start of the successive write will be always used.
  765. *
  766. * @return Current slave read/write transition enabled value
  767. * @see MPU6050_RA_I2C_MST_CTRL
  768. */
  769. bool MPU6050::getSlaveReadWriteTransitionEnabled() {
  770.     I2Cdev::readBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_I2C_MST_P_NSR_BIT, buffer);
  771.     return buffer[0];
  772. }
  773. /** Set slave read/write transition enabled value.
  774. * @param enabled New slave read/write transition enabled value
  775. * @see getSlaveReadWriteTransitionEnabled()
  776. * @see MPU6050_RA_I2C_MST_CTRL
  777. */
  778. void MPU6050::setSlaveReadWriteTransitionEnabled(bool enabled) {
  779.     I2Cdev::writeBit(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_I2C_MST_P_NSR_BIT, enabled);
  780. }
  781. /** Get I2C master clock speed.
  782. * I2C_MST_CLK is a 4 bit unsigned value which configures a divider on the
  783. * MPU-60X0 internal 8MHz clock. It sets the I2C master clock speed according to
  784. * the following table:
  785. *
  786. * <pre>
  787. * I2C_MST_CLK | I2C Master Clock Speed | 8MHz Clock Divider
  788. * ------------+------------------------+-------------------
  789. * 0           | 348kHz                 | 23
  790. * 1           | 333kHz                 | 24
  791. * 2           | 320kHz                 | 25
  792. * 3           | 308kHz                 | 26
  793. * 4           | 296kHz                 | 27
  794. * 5           | 286kHz                 | 28
  795. * 6           | 276kHz                 | 29
  796. * 7           | 267kHz                 | 30
  797. * 8           | 258kHz                 | 31
  798. * 9           | 500kHz                 | 16
  799. * 10          | 471kHz                 | 17
  800. * 11          | 444kHz                 | 18
  801. * 12          | 421kHz                 | 19
  802. * 13          | 400kHz                 | 20
  803. * 14          | 381kHz                 | 21
  804. * 15          | 364kHz                 | 22
  805. * </pre>
  806. *
  807. * @return Current I2C master clock speed
  808. * @see MPU6050_RA_I2C_MST_CTRL
  809. */
  810. uint8_t MPU6050::getMasterClockSpeed() {
  811.     I2Cdev::readBits(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_I2C_MST_CLK_BIT, MPU6050_I2C_MST_CLK_LENGTH, buffer);
  812.     return buffer[0];
  813. }
  814. /** Set I2C master clock speed.
  815. * @reparam speed Current I2C master clock speed
  816. * @see MPU6050_RA_I2C_MST_CTRL
  817. */
  818. void MPU6050::setMasterClockSpeed(uint8_t speed) {
  819.     I2Cdev::writeBits(devAddr, MPU6050_RA_I2C_MST_CTRL, MPU6050_I2C_MST_CLK_BIT, MPU6050_I2C_MST_CLK_LENGTH, speed);
  820. }

  821. // I2C_SLV* registers (Slave 0-3)

  822. /** Get the I2C address of the specified slave (0-3).
  823. * Note that Bit 7 (MSB) controls read/write mode. If Bit 7 is set, it's a read
  824. * operation, and if it is cleared, then it's a write operation. The remaining
  825. * bits (6-0) are the 7-bit device address of the slave device.
  826. *
  827. * In read mode, the result of the read is placed in the lowest available
  828. * EXT_SENS_DATA register. For further information regarding the allocation of
  829. * read results, please refer to the EXT_SENS_DATA register description
  830. * (Registers 73 ?96).
  831. *
  832. * The MPU-6050 supports a total of five slaves, but Slave 4 has unique
  833. * characteristics, and so it has its own functions (getSlave4* and setSlave4*).
  834. *
  835. * I2C data transactions are performed at the Sample Rate, as defined in
  836. * Register 25. The user is responsible for ensuring that I2C data transactions
  837. * to and from each enabled Slave can be completed within a single period of the
  838. * Sample Rate.
  839. *
  840. * The I2C slave access rate can be reduced relative to the Sample Rate. This
  841. * reduced access rate is determined by I2C_MST_DLY (Register 52). Whether a
  842. * slave's access rate is reduced relative to the Sample Rate is determined by
  843. * I2C_MST_DELAY_CTRL (Register 103).
  844. *
  845. * The processing order for the slaves is fixed. The sequence followed for
  846. * processing the slaves is Slave 0, Slave 1, Slave 2, Slave 3 and Slave 4. If a
  847. * particular Slave is disabled it will be skipped.
  848. *
  849. * Each slave can either be accessed at the sample rate or at a reduced sample
  850. * rate. In a case where some slaves are accessed at the Sample Rate and some
  851. * slaves are accessed at the reduced rate, the sequence of accessing the slaves
  852. * (Slave 0 to Slave 4) is still followed. However, the reduced rate slaves will
  853. * be skipped if their access rate dictates that they should not be accessed
  854. * during that particular cycle. For further information regarding the reduced
  855. * access rate, please refer to Register 52. Whether a slave is accessed at the
  856. * Sample Rate or at the reduced rate is determined by the Delay Enable bits in
  857. * Register 103.
  858. *
  859. * @param num Slave number (0-3)
  860. * @return Current address for specified slave
  861. * @see MPU6050_RA_I2C_SLV0_ADDR
  862. */
  863. uint8_t MPU6050::getSlaveAddress(uint8_t num) {
  864.     if (num > 3) return 0;
  865.     I2Cdev::readByte(devAddr, MPU6050_RA_I2C_SLV0_ADDR + num*3, buffer);
  866.     return buffer[0];
  867. }
  868. /** Set the I2C address of the specified slave (0-3).
  869. * @param num Slave number (0-3)
  870. * @param address New address for specified slave
  871. * @see getSlaveAddress()
  872. * @see MPU6050_RA_I2C_SLV0_ADDR
  873. */
  874. void MPU6050::setSlaveAddress(uint8_t num, uint8_t address) {
  875.     if (num > 3) return;
  876.     I2Cdev::writeByte(devAddr, MPU6050_RA_I2C_SLV0_ADDR + num*3, address);
  877. }
  878. /** Get the active internal register for the specified slave (0-3).
  879. * Read/write operations for this slave will be done to whatever internal
  880. * register address is stored in this MPU register.
  881. *
  882. * The MPU-6050 supports a total of five slaves, but Slave 4 has unique
  883. * characteristics, and so it has its own functions.
  884. *
  885. * @param num Slave number (0-3)
  886. * @return Current active register for specified slave
  887. * @see MPU6050_RA_I2C_SLV0_REG
  888. */
  889. uint8_t MPU6050::getSlaveRegister(uint8_t num) {
  890.     if (num > 3) return 0;
  891.     I2Cdev::readByte(devAddr, MPU6050_RA_I2C_SLV0_REG + num*3, buffer);
  892.     return buffer[0];
  893. }
  894. /** Set the active internal register for the specified slave (0-3).
  895. * @param num Slave number (0-3)
  896. * @param reg New active register for specified slave
  897. * @see getSlaveRegister()
  898. * @see MPU6050_RA_I2C_SLV0_REG
  899. */
  900. void MPU6050::setSlaveRegister(uint8_t num, uint8_t reg) {
  901.     if (num > 3) return;
  902.     I2Cdev::writeByte(devAddr, MPU6050_RA_I2C_SLV0_REG + num*3, reg);
  903. }
  904. /** Get the enabled value for the specified slave (0-3).
  905. * When set to 1, this bit enables Slave 0 for data transfer operations. When
  906. * cleared to 0, this bit disables Slave 0 from data transfer operations.
  907. * @param num Slave number (0-3)
  908. * @return Current enabled value for specified slave
  909. * @see MPU6050_RA_I2C_SLV0_CTRL
  910. */
  911. bool MPU6050::getSlaveEnabled(uint8_t num) {
  912.     if (num > 3) return 0;
  913.     I2Cdev::readBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_EN_BIT, buffer);
  914.     return buffer[0];
  915. }
  916. /** Set the enabled value for the specified slave (0-3).
  917. * @param num Slave number (0-3)
  918. * @param enabled New enabled value for specified slave
  919. * @see getSlaveEnabled()
  920. * @see MPU6050_RA_I2C_SLV0_CTRL
  921. */
  922. void MPU6050::setSlaveEnabled(uint8_t num, bool enabled) {
  923.     if (num > 3) return;
  924.     I2Cdev::writeBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_EN_BIT, enabled);
  925. }
  926. /** Get word pair byte-swapping enabled for the specified slave (0-3).
  927. * When set to 1, this bit enables byte swapping. When byte swapping is enabled,
  928. * the high and low bytes of a word pair are swapped. Please refer to
  929. * I2C_SLV0_GRP for the pairing convention of the word pairs. When cleared to 0,
  930. * bytes transferred to and from Slave 0 will be written to EXT_SENS_DATA
  931. * registers in the order they were transferred.
  932. *
  933. * @param num Slave number (0-3)
  934. * @return Current word pair byte-swapping enabled value for specified slave
  935. * @see MPU6050_RA_I2C_SLV0_CTRL
  936. */
  937. bool MPU6050::getSlaveWordByteSwap(uint8_t num) {
  938.     if (num > 3) return 0;
  939.     I2Cdev::readBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_BYTE_SW_BIT, buffer);
  940.     return buffer[0];
  941. }
  942. /** Set word pair byte-swapping enabled for the specified slave (0-3).
  943. * @param num Slave number (0-3)
  944. * @param enabled New word pair byte-swapping enabled value for specified slave
  945. * @see getSlaveWordByteSwap()
  946. * @see MPU6050_RA_I2C_SLV0_CTRL
  947. */
  948. void MPU6050::setSlaveWordByteSwap(uint8_t num, bool enabled) {
  949.     if (num > 3) return;
  950.     I2Cdev::writeBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_BYTE_SW_BIT, enabled);
  951. }
  952. /** Get write mode for the specified slave (0-3).
  953. * When set to 1, the transaction will read or write data only. When cleared to
  954. * 0, the transaction will write a register address prior to reading or writing
  955. * data. This should equal 0 when specifying the register address within the
  956. * Slave device to/from which the ensuing data transaction will take place.
  957. *
  958. * @param num Slave number (0-3)
  959. * @return Current write mode for specified slave (0 = register address + data, 1 = data only)
  960. * @see MPU6050_RA_I2C_SLV0_CTRL
  961. */
  962. bool MPU6050::getSlaveWriteMode(uint8_t num) {
  963.     if (num > 3) return 0;
  964.     I2Cdev::readBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_REG_DIS_BIT, buffer);
  965.     return buffer[0];
  966. }
  967. /** Set write mode for the specified slave (0-3).
  968. * @param num Slave number (0-3)
  969. * @param mode New write mode for specified slave (0 = register address + data, 1 = data only)
  970. * @see getSlaveWriteMode()
  971. * @see MPU6050_RA_I2C_SLV0_CTRL
  972. */
  973. void MPU6050::setSlaveWriteMode(uint8_t num, bool mode) {
  974.     if (num > 3) return;
  975.     I2Cdev::writeBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_REG_DIS_BIT, mode);
  976. }
  977. /** Get word pair grouping order offset for the specified slave (0-3).
  978. * This sets specifies the grouping order of word pairs received from registers.
  979. * When cleared to 0, bytes from register addresses 0 and 1, 2 and 3, etc (even,
  980. * then odd register addresses) are paired to form a word. When set to 1, bytes
  981. * from register addresses are paired 1 and 2, 3 and 4, etc. (odd, then even
  982. * register addresses) are paired to form a word.
  983. *
  984. * @param num Slave number (0-3)
  985. * @return Current word pair grouping order offset for specified slave
  986. * @see MPU6050_RA_I2C_SLV0_CTRL
  987. */
  988. bool MPU6050::getSlaveWordGroupOffset(uint8_t num) {
  989.     if (num > 3) return 0;
  990.     I2Cdev::readBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_GRP_BIT, buffer);
  991.     return buffer[0];
  992. }
  993. /** Set word pair grouping order offset for the specified slave (0-3).
  994. * @param num Slave number (0-3)
  995. * @param enabled New word pair grouping order offset for specified slave
  996. * @see getSlaveWordGroupOffset()
  997. * @see MPU6050_RA_I2C_SLV0_CTRL
  998. */
  999. void MPU6050::setSlaveWordGroupOffset(uint8_t num, bool enabled) {
  1000.     if (num > 3) return;
  1001.     I2Cdev::writeBit(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_GRP_BIT, enabled);
  1002. }
  1003. /** Get number of bytes to read for the specified slave (0-3).
  1004. * Specifies the number of bytes transferred to and from Slave 0. Clearing this
  1005. * bit to 0 is equivalent to disabling the register by writing 0 to I2C_SLV0_EN.
  1006. * @param num Slave number (0-3)
  1007. * @return Number of bytes to read for specified slave
  1008. * @see MPU6050_RA_I2C_SLV0_CTRL
  1009. */
  1010. uint8_t MPU6050::getSlaveDataLength(uint8_t num) {
  1011.     if (num > 3) return 0;
  1012.     I2Cdev::readBits(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_LEN_BIT, MPU6050_I2C_SLV_LEN_LENGTH, buffer);
  1013.     return buffer[0];
  1014. }
  1015. /** Set number of bytes to read for the specified slave (0-3).
  1016. * @param num Slave number (0-3)
  1017. * @param length Number of bytes to read for specified slave
  1018. * @see getSlaveDataLength()
  1019. * @see MPU6050_RA_I2C_SLV0_CTRL
  1020. */
  1021. void MPU6050::setSlaveDataLength(uint8_t num, uint8_t length) {
  1022.     if (num > 3) return;
  1023.     I2Cdev::writeBits(devAddr, MPU6050_RA_I2C_SLV0_CTRL + num*3, MPU6050_I2C_SLV_LEN_BIT, MPU6050_I2C_SLV_LEN_LENGTH, length);
  1024. }

  1025. // I2C_SLV* registers (Slave 4)

  1026. /** Get the I2C address of Slave 4.
  1027. * Note that Bit 7 (MSB) controls read/write mode. If Bit 7 is set, it's a read
  1028. * operation, and if it is cleared, then it's a write operation. The remaining
  1029. * bits (6-0) are the 7-bit device address of the slave device.
  1030. *
  1031. * @return Current address for Slave 4
  1032. * @see getSlaveAddress()
  1033. * @see MPU6050_RA_I2C_SLV4_ADDR
  1034. */
  1035. uint8_t MPU6050::getSlave4Address() {
  1036.     I2Cdev::readByte(devAddr, MPU6050_RA_I2C_SLV4_ADDR, buffer);
  1037.     return buffer[0];
  1038. }
  1039. /** Set the I2C address of Slave 4.
  1040. * @param address New address for Slave 4
  1041. * @see getSlave4Address()
  1042. * @see MPU6050_RA_I2C_SLV4_ADDR
  1043. */
  1044. void MPU6050::setSlave4Address(uint8_t address) {
  1045.     I2Cdev::writeByte(devAddr, MPU6050_RA_I2C_SLV4_ADDR, address);
  1046. }
  1047. /** Get the active internal register for the Slave 4.
  1048. * Read/write operations for this slave will be done to whatever internal
  1049. * register address is stored in this MPU register.
  1050. *
  1051. * @return Current active register for Slave 4
  1052. * @see MPU6050_RA_I2C_SLV4_REG
  1053. */
  1054. uint8_t MPU6050::getSlave4Register() {
  1055.     I2Cdev::readByte(devAddr, MPU6050_RA_I2C_SLV4_REG, buffer);
  1056.     return buffer[0];
  1057. }
  1058. /** Set the active internal register for Slave 4.
  1059. * @param reg New active register for Slave 4
  1060. * @see getSlave4Register()
  1061. * @see MPU6050_RA_I2C_SLV4_REG
  1062. */
  1063. void MPU6050::setSlave4Register(uint8_t reg) {
  1064.     I2Cdev::writeByte(devAddr, MPU6050_RA_I2C_SLV4_REG, reg);
  1065. }
  1066. /** Set new byte to write to Slave 4.
  1067. * This register stores the data to be written into the Slave 4. If I2C_SLV4_RW
  1068. * is set 1 (set to read), this register has no effect.
  1069. * @param data New byte to write to Slave 4
  1070. * @see MPU6050_RA_I2C_SLV4_DO
  1071. */
  1072. void MPU6050::setSlave4OutputByte(uint8_t data) {
  1073.     I2Cdev::writeByte(devAddr, MPU6050_RA_I2C_SLV4_DO, data);
  1074. }
  1075. /** Get the enabled value for the Slave 4.
  1076. * When set to 1, this bit enables Slave 4 for data transfer operations. When
  1077. * cleared to 0, this bit disables Slave 4 from data transfer operations.
  1078. * @return Current enabled value for Slave 4
  1079. * @see MPU6050_RA_I2C_SLV4_CTRL
  1080. */
  1081. bool MPU6050::getSlave4Enabled() {
  1082.     I2Cdev::readBit(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_EN_BIT, buffer);
  1083.     return buffer[0];
  1084. }
  1085. /** Set the enabled value for Slave 4.
  1086. * @param enabled New enabled value for Slave 4
  1087. * @see getSlave4Enabled()
  1088. * @see MPU6050_RA_I2C_SLV4_CTRL
  1089. */
  1090. void MPU6050::setSlave4Enabled(bool enabled) {
  1091.     I2Cdev::writeBit(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_EN_BIT, enabled);
  1092. }
  1093. /** Get the enabled value for Slave 4 transaction interrupts.
  1094. * When set to 1, this bit enables the generation of an interrupt signal upon
  1095. * completion of a Slave 4 transaction. When cleared to 0, this bit disables the
  1096. * generation of an interrupt signal upon completion of a Slave 4 transaction.
  1097. * The interrupt status can be observed in Register 54.
  1098. *
  1099. * @return Current enabled value for Slave 4 transaction interrupts.
  1100. * @see MPU6050_RA_I2C_SLV4_CTRL
  1101. */
  1102. bool MPU6050::getSlave4InterruptEnabled() {
  1103.     I2Cdev::readBit(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_INT_EN_BIT, buffer);
  1104.     return buffer[0];
  1105. }
  1106. /** Set the enabled value for Slave 4 transaction interrupts.
  1107. * @param enabled New enabled value for Slave 4 transaction interrupts.
  1108. * @see getSlave4InterruptEnabled()
  1109. * @see MPU6050_RA_I2C_SLV4_CTRL
  1110. */
  1111. void MPU6050::setSlave4InterruptEnabled(bool enabled) {
  1112.     I2Cdev::writeBit(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_INT_EN_BIT, enabled);
  1113. }
  1114. /** Get write mode for Slave 4.
  1115. * When set to 1, the transaction will read or write data only. When cleared to
  1116. * 0, the transaction will write a register address prior to reading or writing
  1117. * data. This should equal 0 when specifying the register address within the
  1118. * Slave device to/from which the ensuing data transaction will take place.
  1119. *
  1120. * @return Current write mode for Slave 4 (0 = register address + data, 1 = data only)
  1121. * @see MPU6050_RA_I2C_SLV4_CTRL
  1122. */
  1123. bool MPU6050::getSlave4WriteMode() {
  1124.     I2Cdev::readBit(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_REG_DIS_BIT, buffer);
  1125.     return buffer[0];
  1126. }
  1127. /** Set write mode for the Slave 4.
  1128. * @param mode New write mode for Slave 4 (0 = register address + data, 1 = data only)
  1129. * @see getSlave4WriteMode()
  1130. * @see MPU6050_RA_I2C_SLV4_CTRL
  1131. */
  1132. void MPU6050::setSlave4WriteMode(bool mode) {
  1133.     I2Cdev::writeBit(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_REG_DIS_BIT, mode);
  1134. }
  1135. /** Get Slave 4 master delay value.
  1136. * This configures the reduced access rate of I2C slaves relative to the Sample
  1137. * Rate. When a slave's access rate is decreased relative to the Sample Rate,
  1138. * the slave is accessed every:
  1139. *
  1140. *     1 / (1 + I2C_MST_DLY) samples
  1141. *
  1142. * This base Sample Rate in turn is determined by SMPLRT_DIV (register 25) and
  1143. * DLPF_CFG (register 26). Whether a slave's access rate is reduced relative to
  1144. * the Sample Rate is determined by I2C_MST_DELAY_CTRL (register 103). For
  1145. * further information regarding the Sample Rate, please refer to register 25.
  1146. *
  1147. * @return Current Slave 4 master delay value
  1148. * @see MPU6050_RA_I2C_SLV4_CTRL
  1149. */
  1150. uint8_t MPU6050::getSlave4MasterDelay() {
  1151.     I2Cdev::readBits(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_MST_DLY_BIT, MPU6050_I2C_SLV4_MST_DLY_LENGTH, buffer);
  1152.     return buffer[0];
  1153. }
  1154. /** Set Slave 4 master delay value.
  1155. * @param delay New Slave 4 master delay value
  1156. * @see getSlave4MasterDelay()
  1157. * @see MPU6050_RA_I2C_SLV4_CTRL
  1158. */
  1159. void MPU6050::setSlave4MasterDelay(uint8_t delay) {
  1160.     I2Cdev::writeBits(devAddr, MPU6050_RA_I2C_SLV4_CTRL, MPU6050_I2C_SLV4_MST_DLY_BIT, MPU6050_I2C_SLV4_MST_DLY_LENGTH, delay);
  1161. }
  1162. /** Get last available byte read from Slave 4.
  1163. * This register stores the data read from Slave 4. This field is populated
  1164. * after a read transaction.
  1165. * @return Last available byte read from to Slave 4
  1166. * @see MPU6050_RA_I2C_SLV4_DI
  1167. */
  1168. uint8_t MPU6050::getSlate4InputByte() {
  1169.     I2Cdev::readByte(devAddr, MPU6050_RA_I2C_SLV4_DI, buffer);
  1170.     return buffer[0];
  1171. }

  1172. // I2C_MST_STATUS register

  1173. /** Get FSYNC interrupt status.
  1174. * This bit reflects the status of the FSYNC interrupt from an external device
  1175. * into the MPU-60X0. This is used as a way to pass an external interrupt
  1176. * through the MPU-60X0 to the host application processor. When set to 1, this
  1177. * bit will cause an interrupt if FSYNC_INT_EN is asserted in INT_PIN_CFG
  1178. * (Register 55).
  1179. * @return FSYNC interrupt status
  1180. * @see MPU6050_RA_I2C_MST_STATUS
  1181. */
  1182. bool MPU6050::getPassthroughStatus() {
  1183.     I2Cdev::readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_PASS_THROUGH_BIT, buffer);
  1184.     return buffer[0];
  1185. }
  1186. /** Get Slave 4 transaction done status.
  1187. * Automatically sets to 1 when a Slave 4 transaction has completed. This
  1188. * triggers an interrupt if the I2C_MST_INT_EN bit in the INT_ENABLE register
  1189. * (Register 56) is asserted and if the SLV_4_DONE_INT bit is asserted in the
  1190. * I2C_SLV4_CTRL register (Register 52).
  1191. * @return Slave 4 transaction done status
  1192. * @see MPU6050_RA_I2C_MST_STATUS
  1193. */
  1194. bool MPU6050::getSlave4IsDone() {
  1195.     I2Cdev::readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV4_DONE_BIT, buffer);
  1196.     return buffer[0];
  1197. }
  1198. /** Get master arbitration lost status.
  1199. * This bit automatically sets to 1 when the I2C Master has lost arbitration of
  1200. * the auxiliary I2C bus (an error condition). This triggers an interrupt if the
  1201. * I2C_MST_INT_EN bit in the INT_ENABLE register (Register 56) is asserted.
  1202. * @return Master arbitration lost status
  1203. * @see MPU6050_RA_I2C_MST_STATUS
  1204. */
  1205. bool MPU6050::getLostArbitration() {
  1206.     I2Cdev::readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_LOST_ARB_BIT, buffer);
  1207.     return buffer[0];
  1208. }
  1209. /** Get Slave 4 NACK status.
  1210. * This bit automatically sets to 1 when the I2C Master receives a NACK in a
  1211. * transaction with Slave 4. This triggers an interrupt if the I2C_MST_INT_EN
  1212. * bit in the INT_ENABLE register (Register 56) is asserted.
  1213. * @return Slave 4 NACK interrupt status
  1214. * @see MPU6050_RA_I2C_MST_STATUS
  1215. */
  1216. bool MPU6050::getSlave4Nack() {
  1217.     I2Cdev::readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV4_NACK_BIT, buffer);
  1218.     return buffer[0];
  1219. }
  1220. /** Get Slave 3 NACK status.
  1221. * This bit automatically sets to 1 when the I2C Master receives a NACK in a
  1222. * transaction with Slave 3. This triggers an interrupt if the I2C_MST_INT_EN
  1223. * bit in the INT_ENABLE register (Register 56) is asserted.
  1224. * @return Slave 3 NACK interrupt status
  1225. * @see MPU6050_RA_I2C_MST_STATUS
  1226. */
  1227. bool MPU6050::getSlave3Nack() {
  1228.     I2Cdev::readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV3_NACK_BIT, buffer);
  1229.     return buffer[0];
  1230. }
  1231. /** Get Slave 2 NACK status.
  1232. * This bit automatically sets to 1 when the I2C Master receives a NACK in a
  1233. * transaction with Slave 2. This triggers an interrupt if the I2C_MST_INT_EN
  1234. * bit in the INT_ENABLE register (Register 56) is asserted.
  1235. * @return Slave 2 NACK interrupt status
  1236. * @see MPU6050_RA_I2C_MST_STATUS
  1237. */
  1238. bool MPU6050::getSlave2Nack() {
  1239.     I2Cdev::readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV2_NACK_BIT, buffer);
  1240.     return buffer[0];
  1241. }
  1242. /** Get Slave 1 NACK status.
  1243. * This bit automatically sets to 1 when the I2C Master receives a NACK in a
  1244. * transaction with Slave 1. This triggers an interrupt if the I2C_MST_INT_EN
  1245. * bit in the INT_ENABLE register (Register 56) is asserted.
  1246. * @return Slave 1 NACK interrupt status
  1247. * @see MPU6050_RA_I2C_MST_STATUS
  1248. */
  1249. bool MPU6050::getSlave1Nack() {
  1250.     I2Cdev::readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV1_NACK_BIT, buffer);
  1251.     return buffer[0];
  1252. }
  1253. /** Get Slave 0 NACK status.
  1254. * This bit automatically sets to 1 when the I2C Master receives a NACK in a
  1255. * transaction with Slave 0. This triggers an interrupt if the I2C_MST_INT_EN
  1256. * bit in the INT_ENABLE register (Register 56) is asserted.
  1257. * @return Slave 0 NACK interrupt status
  1258. * @see MPU6050_RA_I2C_MST_STATUS
  1259. */
  1260. bool MPU6050::getSlave0Nack() {
  1261.     I2Cdev::readBit(devAddr, MPU6050_RA_I2C_MST_STATUS, MPU6050_MST_I2C_SLV0_NACK_BIT, buffer);
  1262.     return buffer[0];
  1263. }

  1264. // INT_PIN_CFG register

  1265. /** Get interrupt logic level mode.
  1266. * Will be set 0 for active-high, 1 for active-low.
  1267. * @return Current interrupt mode (0=active-high, 1=active-low)
  1268. * @see MPU6050_RA_INT_PIN_CFG
  1269. * @see MPU6050_INTCFG_INT_LEVEL_BIT
  1270. */
  1271. bool MPU6050::getInterruptMode() {
  1272.     I2Cdev::readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_LEVEL_BIT, buffer);
  1273.     return buffer[0];
  1274. }
  1275. /** Set interrupt logic level mode.
  1276. * @param mode New interrupt mode (0=active-high, 1=active-low)
  1277. * @see getInterruptMode()
  1278. * @see MPU6050_RA_INT_PIN_CFG
  1279. * @see MPU6050_INTCFG_INT_LEVEL_BIT
  1280. */
  1281. void MPU6050::setInterruptMode(bool mode) {
  1282.    I2Cdev::writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_LEVEL_BIT, mode);
  1283. }
  1284. /** Get interrupt drive mode.
  1285. * Will be set 0 for push-pull, 1 for open-drain.
  1286. * @return Current interrupt drive mode (0=push-pull, 1=open-drain)
  1287. * @see MPU6050_RA_INT_PIN_CFG
  1288. * @see MPU6050_INTCFG_INT_OPEN_BIT
  1289. */
  1290. bool MPU6050::getInterruptDrive() {
  1291.     I2Cdev::readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_OPEN_BIT, buffer);
  1292.     return buffer[0];
  1293. }
  1294. /** Set interrupt drive mode.
  1295. * @param drive New interrupt drive mode (0=push-pull, 1=open-drain)
  1296. * @see getInterruptDrive()
  1297. * @see MPU6050_RA_INT_PIN_CFG
  1298. * @see MPU6050_INTCFG_INT_OPEN_BIT
  1299. */
  1300. void MPU6050::setInterruptDrive(bool drive) {
  1301.     I2Cdev::writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_OPEN_BIT, drive);
  1302. }
  1303. /** Get interrupt latch mode.
  1304. * Will be set 0 for 50us-pulse, 1 for latch-until-int-cleared.
  1305. * @return Current latch mode (0=50us-pulse, 1=latch-until-int-cleared)
  1306. * @see MPU6050_RA_INT_PIN_CFG
  1307. * @see MPU6050_INTCFG_LATCH_INT_EN_BIT
  1308. */
  1309. bool MPU6050::getInterruptLatch() {
  1310.     I2Cdev::readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_LATCH_INT_EN_BIT, buffer);
  1311.     return buffer[0];
  1312. }
  1313. /** Set interrupt latch mode.
  1314. * @param latch New latch mode (0=50us-pulse, 1=latch-until-int-cleared)
  1315. * @see getInterruptLatch()
  1316. * @see MPU6050_RA_INT_PIN_CFG
  1317. * @see MPU6050_INTCFG_LATCH_INT_EN_BIT
  1318. */
  1319. void MPU6050::setInterruptLatch(bool latch) {
  1320.     I2Cdev::writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_LATCH_INT_EN_BIT, latch);
  1321. }
  1322. /** Get interrupt latch clear mode.
  1323. * Will be set 0 for status-read-only, 1 for any-register-read.
  1324. * @return Current latch clear mode (0=status-read-only, 1=any-register-read)
  1325. * @see MPU6050_RA_INT_PIN_CFG
  1326. * @see MPU6050_INTCFG_INT_RD_CLEAR_BIT
  1327. */
  1328. bool MPU6050::getInterruptLatchClear() {
  1329.     I2Cdev::readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_RD_CLEAR_BIT, buffer);
  1330.     return buffer[0];
  1331. }
  1332. /** Set interrupt latch clear mode.
  1333. * @param clear New latch clear mode (0=status-read-only, 1=any-register-read)
  1334. * @see getInterruptLatchClear()
  1335. * @see MPU6050_RA_INT_PIN_CFG
  1336. * @see MPU6050_INTCFG_INT_RD_CLEAR_BIT
  1337. */
  1338. void MPU6050::setInterruptLatchClear(bool clear) {
  1339.     I2Cdev::writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_INT_RD_CLEAR_BIT, clear);
  1340. }
  1341. /** Get FSYNC interrupt logic level mode.
  1342. * @return Current FSYNC interrupt mode (0=active-high, 1=active-low)
  1343. * @see getFSyncInterruptMode()
  1344. * @see MPU6050_RA_INT_PIN_CFG
  1345. * @see MPU6050_INTCFG_FSYNC_INT_LEVEL_BIT
  1346. */
  1347. bool MPU6050::getFSyncInterruptLevel() {
  1348.     I2Cdev::readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_FSYNC_INT_LEVEL_BIT, buffer);
  1349.     return buffer[0];
  1350. }
  1351. /** Set FSYNC interrupt logic level mode.
  1352. * @param mode New FSYNC interrupt mode (0=active-high, 1=active-low)
  1353. * @see getFSyncInterruptMode()
  1354. * @see MPU6050_RA_INT_PIN_CFG
  1355. * @see MPU6050_INTCFG_FSYNC_INT_LEVEL_BIT
  1356. */
  1357. void MPU6050::setFSyncInterruptLevel(bool level) {
  1358.     I2Cdev::writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_FSYNC_INT_LEVEL_BIT, level);
  1359. }
  1360. /** Get FSYNC pin interrupt enabled setting.
  1361. * Will be set 0 for disabled, 1 for enabled.
  1362. * @return Current interrupt enabled setting
  1363. * @see MPU6050_RA_INT_PIN_CFG
  1364. * @see MPU6050_INTCFG_FSYNC_INT_EN_BIT
  1365. */
  1366. bool MPU6050::getFSyncInterruptEnabled() {
  1367.     I2Cdev::readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_FSYNC_INT_EN_BIT, buffer);
  1368.     return buffer[0];
  1369. }
  1370. /** Set FSYNC pin interrupt enabled setting.
  1371. * @param enabled New FSYNC pin interrupt enabled setting
  1372. * @see getFSyncInterruptEnabled()
  1373. * @see MPU6050_RA_INT_PIN_CFG
  1374. * @see MPU6050_INTCFG_FSYNC_INT_EN_BIT
  1375. */
  1376. void MPU6050::setFSyncInterruptEnabled(bool enabled) {
  1377.     I2Cdev::writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_FSYNC_INT_EN_BIT, enabled);
  1378. }
  1379. /** Get I2C bypass enabled status.
  1380. * When this bit is equal to 1 and I2C_MST_EN (Register 106 bit[5]) is equal to
  1381. * 0, the host application processor will be able to directly access the
  1382. * auxiliary I2C bus of the MPU-60X0. When this bit is equal to 0, the host
  1383. * application processor will not be able to directly access the auxiliary I2C
  1384. * bus of the MPU-60X0 regardless of the state of I2C_MST_EN (Register 106
  1385. * bit[5]).
  1386. * @return Current I2C bypass enabled status
  1387. * @see MPU6050_RA_INT_PIN_CFG
  1388. * @see MPU6050_INTCFG_I2C_BYPASS_EN_BIT
  1389. */
  1390. bool MPU6050::getI2CBypassEnabled() {
  1391.     I2Cdev::readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_I2C_BYPASS_EN_BIT, buffer);
  1392.     return buffer[0];
  1393. }
  1394. /** Set I2C bypass enabled status.
  1395. * When this bit is equal to 1 and I2C_MST_EN (Register 106 bit[5]) is equal to
  1396. * 0, the host application processor will be able to directly access the
  1397. * auxiliary I2C bus of the MPU-60X0. When this bit is equal to 0, the host
  1398. * application processor will not be able to directly access the auxiliary I2C
  1399. * bus of the MPU-60X0 regardless of the state of I2C_MST_EN (Register 106
  1400. * bit[5]).
  1401. * @param enabled New I2C bypass enabled status
  1402. * @see MPU6050_RA_INT_PIN_CFG
  1403. * @see MPU6050_INTCFG_I2C_BYPASS_EN_BIT
  1404. */
  1405. void MPU6050::setI2CBypassEnabled(bool enabled) {
  1406.     I2Cdev::writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_I2C_BYPASS_EN_BIT, enabled);
  1407. }
  1408. /** Get reference clock output enabled status.
  1409. * When this bit is equal to 1, a reference clock output is provided at the
  1410. * CLKOUT pin. When this bit is equal to 0, the clock output is disabled. For
  1411. * further information regarding CLKOUT, please refer to the MPU-60X0 Product
  1412. * Specification document.
  1413. * @return Current reference clock output enabled status
  1414. * @see MPU6050_RA_INT_PIN_CFG
  1415. * @see MPU6050_INTCFG_CLKOUT_EN_BIT
  1416. */
  1417. bool MPU6050::getClockOutputEnabled() {
  1418.     I2Cdev::readBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_CLKOUT_EN_BIT, buffer);
  1419.     return buffer[0];
  1420. }
  1421. /** Set reference clock output enabled status.
  1422. * When this bit is equal to 1, a reference clock output is provided at the
  1423. * CLKOUT pin. When this bit is equal to 0, the clock output is disabled. For
  1424. * further information regarding CLKOUT, please refer to the MPU-60X0 Product
  1425. * Specification document.
  1426. * @param enabled New reference clock output enabled status
  1427. * @see MPU6050_RA_INT_PIN_CFG
  1428. * @see MPU6050_INTCFG_CLKOUT_EN_BIT
  1429. */
  1430. void MPU6050::setClockOutputEnabled(bool enabled) {
  1431.     I2Cdev::writeBit(devAddr, MPU6050_RA_INT_PIN_CFG, MPU6050_INTCFG_CLKOUT_EN_BIT, enabled);
  1432. }

  1433. // INT_ENABLE register

  1434. /** Get Free Fall interrupt enabled status.
  1435. * Will be set 0 for disabled, 1 for enabled.
  1436. * @return Current interrupt enabled status
  1437. * @see MPU6050_RA_INT_ENABLE
  1438. * @see MPU6050_INTERRUPT_FF_BIT
  1439. **/
  1440. bool MPU6050::getIntFreefallEnabled() {
  1441.     I2Cdev::readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_FF_BIT, buffer);
  1442.     return buffer[0];
  1443. }
  1444. /** Set Free Fall interrupt enabled status.
  1445. * @param enabled New interrupt enabled status
  1446. * @see getIntFreefallEnabled()
  1447. * @see MPU6050_RA_INT_ENABLE
  1448. * @see MPU6050_INTERRUPT_FF_BIT
  1449. **/
  1450. void MPU6050::setIntFreefallEnabled(bool enabled) {
  1451.     I2Cdev::writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_FF_BIT, enabled);
  1452. }
  1453. /** Get Motion Detection interrupt enabled status.
  1454. * Will be set 0 for disabled, 1 for enabled.
  1455. * @return Current interrupt enabled status
  1456. * @see MPU6050_RA_INT_ENABLE
  1457. * @see MPU6050_INTERRUPT_MOT_BIT
  1458. **/
  1459. bool MPU6050::getIntMotionEnabled() {
  1460.     I2Cdev::readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_MOT_BIT, buffer);
  1461.     return buffer[0];
  1462. }
  1463. /** Set Motion Detection interrupt enabled status.
  1464. * @param enabled New interrupt enabled status
  1465. * @see getIntMotionEnabled()
  1466. * @see MPU6050_RA_INT_ENABLE
  1467. * @see MPU6050_INTERRUPT_MOT_BIT
  1468. **/
  1469. void MPU6050::setIntMotionEnabled(bool enabled) {
  1470.     I2Cdev::writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_MOT_BIT, enabled);
  1471. }
  1472. /** Get Zero Motion Detection interrupt enabled status.
  1473. * Will be set 0 for disabled, 1 for enabled.
  1474. * @return Current interrupt enabled status
  1475. * @see MPU6050_RA_INT_ENABLE
  1476. * @see MPU6050_INTERRUPT_ZMOT_BIT
  1477. **/
  1478. bool MPU6050::getIntZeroMotionEnabled() {
  1479.     I2Cdev::readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_ZMOT_BIT, buffer);
  1480.     return buffer[0];
  1481. }
  1482. /** Set Zero Motion Detection interrupt enabled status.
  1483. * @param enabled New interrupt enabled status
  1484. * @see getIntZeroMotionEnabled()
  1485. * @see MPU6050_RA_INT_ENABLE
  1486. * @see MPU6050_INTERRUPT_ZMOT_BIT
  1487. **/
  1488. void MPU6050::setIntZeroMotionEnabled(bool enabled) {
  1489.     I2Cdev::writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_ZMOT_BIT, enabled);
  1490. }
  1491. /** Get FIFO Buffer Overflow interrupt enabled status.
  1492. * Will be set 0 for disabled, 1 for enabled.
  1493. * @return Current interrupt enabled status
  1494. * @see MPU6050_RA_INT_ENABLE
  1495. * @see MPU6050_INTERRUPT_FIFO_OFLOW_BIT
  1496. **/
  1497. bool MPU6050::getIntFIFOBufferOverflowEnabled() {
  1498.     I2Cdev::readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_FIFO_OFLOW_BIT, buffer);
  1499.     return buffer[0];
  1500. }
  1501. /** Set FIFO Buffer Overflow interrupt enabled status.
  1502. * @param enabled New interrupt enabled status
  1503. * @see getIntFIFOBufferOverflowEnabled()
  1504. * @see MPU6050_RA_INT_ENABLE
  1505. * @see MPU6050_INTERRUPT_FIFO_OFLOW_BIT
  1506. **/
  1507. void MPU6050::setIntFIFOBufferOverflowEnabled(bool enabled) {
  1508.     I2Cdev::writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_FIFO_OFLOW_BIT, enabled);
  1509. }
  1510. /** Get I2C Master interrupt enabled status.
  1511. * This enables any of the I2C Master interrupt sources to generate an
  1512. * interrupt. Will be set 0 for disabled, 1 for enabled.
  1513. * @return Current interrupt enabled status
  1514. * @see MPU6050_RA_INT_ENABLE
  1515. * @see MPU6050_INTERRUPT_I2C_MST_INT_BIT
  1516. **/
  1517. bool MPU6050::getIntI2CMasterEnabled() {
  1518.     I2Cdev::readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_I2C_MST_INT_BIT, buffer);
  1519.     return buffer[0];
  1520. }
  1521. /** Set I2C Master interrupt enabled status.
  1522. * @param enabled New interrupt enabled status
  1523. * @see getIntI2CMasterEnabled()
  1524. * @see MPU6050_RA_INT_ENABLE
  1525. * @see MPU6050_INTERRUPT_I2C_MST_INT_BIT
  1526. **/
  1527. void MPU6050::setIntI2CMasterEnabled(bool enabled) {
  1528.     I2Cdev::writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_I2C_MST_INT_BIT, enabled);
  1529. }
  1530. /** Get Data Ready interrupt enabled setting.
  1531. * This event occurs each time a write operation to all of the sensor registers
  1532. * has been completed. Will be set 0 for disabled, 1 for enabled.
  1533. * @return Current interrupt enabled status
  1534. * @see MPU6050_RA_INT_ENABLE
  1535. * @see MPU6050_INTERRUPT_DATA_RDY_BIT
  1536. */
  1537. bool MPU6050::getIntDataReadyEnabled() {
  1538.     I2Cdev::readBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_DATA_RDY_BIT, buffer);
  1539.     return buffer[0];
  1540. }
  1541. /** Set Data Ready interrupt enabled status.
  1542. * @param enabled New interrupt enabled status
  1543. * @see getIntDataReadyEnabled()
  1544. * @see MPU6050_RA_INT_CFG
  1545. * @see MPU6050_INTERRUPT_DATA_RDY_BIT
  1546. */
  1547. void MPU6050::setIntDataReadyEnabled(bool enabled) {
  1548.     I2Cdev::writeBit(devAddr, MPU6050_RA_INT_ENABLE, MPU6050_INTERRUPT_DATA_RDY_BIT, enabled);
  1549. }

  1550. // INT_STATUS register

  1551. /** Get Free Fall interrupt status.
  1552. * This bit automatically sets to 1 when a Free Fall interrupt has been
  1553. * generated. The bit clears to 0 after the register has been read.
  1554. * @return Current interrupt status
  1555. * @see MPU6050_RA_INT_STATUS
  1556. * @see MPU6050_INTERRUPT_FF_BIT
  1557. */
  1558. bool MPU6050::getIntFreefallStatus() {
  1559.     I2Cdev::readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_FF_BIT, buffer);
  1560.     return buffer[0];
  1561. }
  1562. /** Get Motion Detection interrupt status.
  1563. * This bit automatically sets to 1 when a Motion Detection interrupt has been
  1564. * generated. The bit clears to 0 after the register has been read.
  1565. * @return Current interrupt status
  1566. * @see MPU6050_RA_INT_STATUS
  1567. * @see MPU6050_INTERRUPT_MOT_BIT
  1568. */
  1569. bool MPU6050::getIntMotionStatus() {
  1570.     I2Cdev::readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_MOT_BIT, buffer);
  1571.     return buffer[0];
  1572. }
  1573. /** Get Zero Motion Detection interrupt status.
  1574. * This bit automatically sets to 1 when a Zero Motion Detection interrupt has
  1575. * been generated. The bit clears to 0 after the register has been read.
  1576. * @return Current interrupt status
  1577. * @see MPU6050_RA_INT_STATUS
  1578. * @see MPU6050_INTERRUPT_ZMOT_BIT
  1579. */
  1580. bool MPU6050::getIntZeroMotionStatus() {
  1581.     I2Cdev::readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_ZMOT_BIT, buffer);
  1582.     return buffer[0];
  1583. }
  1584. /** Get FIFO Buffer Overflow interrupt status.
  1585. * This bit automatically sets to 1 when a Free Fall interrupt has been
  1586. * generated. The bit clears to 0 after the register has been read.
  1587. * @return Current interrupt status
  1588. * @see MPU6050_RA_INT_STATUS
  1589. * @see MPU6050_INTERRUPT_FIFO_OFLOW_BIT
  1590. */
  1591. bool MPU6050::getIntFIFOBufferOverflowStatus() {
  1592.     I2Cdev::readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_FIFO_OFLOW_BIT, buffer);
  1593.     return buffer[0];
  1594. }
  1595. /** Get I2C Master interrupt status.
  1596. * This bit automatically sets to 1 when an I2C Master interrupt has been
  1597. * generated. For a list of I2C Master interrupts, please refer to Register 54.
  1598. * The bit clears to 0 after the register has been read.
  1599. * @return Current interrupt status
  1600. * @see MPU6050_RA_INT_STATUS
  1601. * @see MPU6050_INTERRUPT_I2C_MST_INT_BIT
  1602. */
  1603. bool MPU6050::getIntI2CMasterStatus() {
  1604.     I2Cdev::readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_I2C_MST_INT_BIT, buffer);
  1605.     return buffer[0];
  1606. }
  1607. /** Get Data Ready interrupt status.
  1608. * This bit automatically sets to 1 when a Data Ready interrupt has been
  1609. * generated. The bit clears to 0 after the register has been read.
  1610. * @return Current interrupt status
  1611. * @see MPU6050_RA_INT_STATUS
  1612. * @see MPU6050_INTERRUPT_DATA_RDY_BIT
  1613. */
  1614. bool MPU6050::getIntDataReadyStatus() {
  1615.     I2Cdev::readBit(devAddr, MPU6050_RA_INT_STATUS, MPU6050_INTERRUPT_DATA_RDY_BIT, buffer);
  1616.     return buffer[0];
  1617. }

  1618. // ACCEL_*OUT_* registers

  1619. /** Get raw 9-axis motion sensor readings (accel/gyro/compass).
  1620. * FUNCTION NOT FULLY IMPLEMENTED YET.
  1621. * @param ax 16-bit signed integer container for accelerometer X-axis value
  1622. * @param ay 16-bit signed integer container for accelerometer Y-axis value
  1623. * @param az 16-bit signed integer container for accelerometer Z-axis value
  1624. * @param gx 16-bit signed integer container for gyroscope X-axis value
  1625. * @param gy 16-bit signed integer container for gyroscope Y-axis value
  1626. * @param gz 16-bit signed integer container for gyroscope Z-axis value
  1627. * @param mx 16-bit signed integer container for magnetometer X-axis value
  1628. * @param my 16-bit signed integer container for magnetometer Y-axis value
  1629. * @param mz 16-bit signed integer container for magnetometer Z-axis value
  1630. * @see getMotion6()
  1631. * @see getAcceleration()
  1632. * @see getRotation()
  1633. * @see MPU6050_RA_ACCEL_XOUT_H
  1634. */
  1635. void MPU6050::getMotion9(int16_t* ax, int16_t* ay, int16_t* az, int16_t* gx, int16_t* gy, int16_t* gz, int16_t* mx, int16_t* my, int16_t* mz) {
  1636.     getMotion6(ax, ay, az, gx, gy, gz);
  1637.     // TODO: magnetometer integration
  1638. }
  1639. /** Get raw 6-axis motion sensor readings (accel/gyro).
  1640. * Retrieves all currently available motion sensor values.
  1641. * @param ax 16-bit signed integer container for accelerometer X-axis value
  1642. * @param ay 16-bit signed integer container for accelerometer Y-axis value
  1643. * @param az 16-bit signed integer container for accelerometer Z-axis value
  1644. * @param gx 16-bit signed integer container for gyroscope X-axis value
  1645. * @param gy 16-bit signed integer container for gyroscope Y-axis value
  1646. * @param gz 16-bit signed integer container for gyroscope Z-axis value
  1647. * @see getAcceleration()
  1648. * @see getRotation()
  1649. * @see MPU6050_RA_ACCEL_XOUT_H
  1650. */
  1651. void MPU6050::getMotion6(int16_t* ax, int16_t* ay, int16_t* az, int16_t* gx, int16_t* gy, int16_t* gz) {
  1652.     I2Cdev::readBytes(devAddr, MPU6050_RA_ACCEL_XOUT_H, 14, buffer);
  1653.     *ax = (((int16_t)buffer[0]) << 8) | buffer[1];
  1654.     *ay = (((int16_t)buffer[2]) << 8) | buffer[3];
  1655.     *az = (((int16_t)buffer[4]) << 8) | buffer[5];
  1656.     *gx = (((int16_t)buffer[8]) << 8) | buffer[9];
  1657.     *gy = (((int16_t)buffer[10]) << 8) | buffer[11];
  1658.     *gz = (((int16_t)buffer[12]) << 8) | buffer[13];
  1659. }
  1660. /** Get 3-axis accelerometer readings.
  1661. * These registers store the most recent accelerometer measurements.
  1662. * Accelerometer measurements are written to these registers at the Sample Rate
  1663. * as defined in Register 25.
  1664. *
  1665. * The accelerometer measurement registers, along with the temperature
  1666. * measurement registers, gyroscope measurement registers, and external sensor
  1667. * data registers, are composed of two sets of registers: an internal register
  1668. * set and a user-facing read register set.
  1669. *
  1670. * The data within the accelerometer sensors' internal register set is always
  1671. * updated at the Sample Rate. Meanwhile, the user-facing read register set
  1672. * duplicates the internal register set's data values whenever the serial
  1673. * interface is idle. This guarantees that a burst read of sensor registers will
  1674. * read measurements from the same sampling instant. Note that if burst reads
  1675. * are not used, the user is responsible for ensuring a set of single byte reads
  1676. * correspond to a single sampling instant by checking the Data Ready interrupt.
  1677. *
  1678. * Each 16-bit accelerometer measurement has a full scale defined in ACCEL_FS
  1679. * (Register 28). For each full scale setting, the accelerometers' sensitivity
  1680. * per LSB in ACCEL_xOUT is shown in the table below:
  1681. *
  1682. * <pre>
  1683. * AFS_SEL | Full Scale Range | LSB Sensitivity
  1684. * --------+------------------+----------------
  1685. * 0       | +/- 2g           | 8192 LSB/mg
  1686. * 1       | +/- 4g           | 4096 LSB/mg
  1687. * 2       | +/- 8g           | 2048 LSB/mg
  1688. * 3       | +/- 16g          | 1024 LSB/mg
  1689. * </pre>
  1690. *
  1691. * @param x 16-bit signed integer container for X-axis acceleration
  1692. * @param y 16-bit signed integer container for Y-axis acceleration
  1693. * @param z 16-bit signed integer container for Z-axis acceleration
  1694. * @see MPU6050_RA_GYRO_XOUT_H
  1695. */
  1696. void MPU6050::getAcceleration(int16_t* x, int16_t* y, int16_t* z) {
  1697.     I2Cdev::readBytes(devAddr, MPU6050_RA_ACCEL_XOUT_H, 6, buffer);
  1698.     *x = (((int16_t)buffer[0]) << 8) | buffer[1];
  1699.     *y = (((int16_t)buffer[2]) << 8) | buffer[3];
  1700.     *z = (((int16_t)buffer[4]) << 8) | buffer[5];
  1701. }
  1702. /** Get X-axis accelerometer reading.
  1703. * @return X-axis acceleration measurement in 16-bit 2's complement format
  1704. * @see getMotion6()
  1705. * @see MPU6050_RA_ACCEL_XOUT_H
  1706. */
  1707. int16_t MPU6050::getAccelerationX() {
  1708.     I2Cdev::readBytes(devAddr, MPU6050_RA_ACCEL_XOUT_H, 2, buffer);
  1709.     return (((int16_t)buffer[0]) << 8) | buffer[1];
  1710. }
  1711. /** Get Y-axis accelerometer reading.
  1712. * @return Y-axis acceleration measurement in 16-bit 2's complement format
  1713. * @see getMotion6()
  1714. * @see MPU6050_RA_ACCEL_YOUT_H
  1715. */
  1716. int16_t MPU6050::getAccelerationY() {
  1717.     I2Cdev::readBytes(devAddr, MPU6050_RA_ACCEL_YOUT_H, 2, buffer);
  1718.     return (((int16_t)buffer[0]) << 8) | buffer[1];
  1719. }
  1720. /** Get Z-axis accelerometer reading.
  1721. * @return Z-axis acceleration measurement in 16-bit 2's complement format
  1722. * @see getMotion6()
  1723. * @see MPU6050_RA_ACCEL_ZOUT_H
  1724. */
  1725. int16_t MPU6050::getAccelerationZ() {
  1726.     I2Cdev::readBytes(devAddr, MPU6050_RA_ACCEL_ZOUT_H, 2, buffer);
  1727.     return (((int16_t)buffer[0]) << 8) | buffer[1];
  1728. }

  1729. // TEMP_OUT_* registers

  1730. /** Get current internal temperature.
  1731. * @return Temperature reading in 16-bit 2's complement format
  1732. * @see MPU6050_RA_TEMP_OUT_H
  1733. */
  1734. int16_t MPU6050::getTemperature() {
  1735.     I2Cdev::readBytes(devAddr, MPU6050_RA_TEMP_OUT_H, 2, buffer);
  1736.     return (((int16_t)buffer[0]) << 8) | buffer[1];
  1737. }

  1738. // GYRO_*OUT_* registers

  1739. /** Get 3-axis gyroscope readings.
  1740. * These gyroscope measurement registers, along with the accelerometer
  1741. * measurement registers, temperature measurement registers, and external sensor
  1742. * data registers, are composed of two sets of registers: an internal register
  1743. * set and a user-facing read register set.
  1744. * The data within the gyroscope sensors' internal register set is always
  1745. * updated at the Sample Rate. Meanwhile, the user-facing read register set
  1746. * duplicates the internal register set's data values whenever the serial
  1747. * interface is idle. This guarantees that a burst read of sensor registers will
  1748. * read measurements from the same sampling instant. Note that if burst reads
  1749. * are not used, the user is responsible for ensuring a set of single byte reads
  1750. * correspond to a single sampling instant by checking the Data Ready interrupt.
  1751. *
  1752. * Each 16-bit gyroscope measurement has a full scale defined in FS_SEL
  1753. * (Register 27). For each full scale setting, the gyroscopes' sensitivity per
  1754. * LSB in GYRO_xOUT is shown in the table below:
  1755. *
  1756. * <pre>
  1757. * FS_SEL | Full Scale Range   | LSB Sensitivity
  1758. * -------+--------------------+----------------
  1759. * 0      | +/- 250 degrees/s  | 131 LSB/deg/s
  1760. * 1      | +/- 500 degrees/s  | 65.5 LSB/deg/s
  1761. * 2      | +/- 1000 degrees/s | 32.8 LSB/deg/s
  1762. * 3      | +/- 2000 degrees/s | 16.4 LSB/deg/s
  1763. * </pre>
  1764. *
  1765. * @param x 16-bit signed integer container for X-axis rotation
  1766. * @param y 16-bit signed integer container for Y-axis rotation
  1767. * @param z 16-bit signed integer container for Z-axis rotation
  1768. * @see getMotion6()
  1769. * @see MPU6050_RA_GYRO_XOUT_H
  1770. */
  1771. void MPU6050::getRotation(int16_t* x, int16_t* y, int16_t* z) {
  1772.     I2Cdev::readBytes(devAddr, MPU6050_RA_GYRO_XOUT_H, 6, buffer);
  1773.     *x = (((int16_t)buffer[0]) << 8) | buffer[1];
  1774.     *y = (((int16_t)buffer[2]) << 8) | buffer[3];
  1775.     *z = (((int16_t)buffer[4]) << 8) | buffer[5];
  1776. }
  1777. /** Get X-axis gyroscope reading.
  1778. * @return X-axis rotation measurement in 16-bit 2's complement format
  1779. * @see getMotion6()
  1780. * @see MPU6050_RA_GYRO_XOUT_H
  1781. */
  1782. int16_t MPU6050::getRotationX() {
  1783.     I2Cdev::readBytes(devAddr, MPU6050_RA_GYRO_XOUT_H, 2, buffer);
  1784.     return (((int16_t)buffer[0]) << 8) | buffer[1];
  1785. }
  1786. /** Get Y-axis gyroscope reading.
  1787. * @return Y-axis rotation measurement in 16-bit 2's complement format
  1788. * @see getMotion6()
  1789. * @see MPU6050_RA_GYRO_YOUT_H
  1790. */
  1791. int16_t MPU6050::getRotationY() {
  1792.     I2Cdev::readBytes(devAddr, MPU6050_RA_GYRO_YOUT_H, 2, buffer);
  1793.     return (((int16_t)buffer[0]) << 8) | buffer[1];
  1794. }
  1795. /** Get Z-axis gyroscope reading.
  1796. * @return Z-axis rotation measurement in 16-bit 2's complement format
  1797. * @see getMotion6()
  1798. * @see MPU6050_RA_GYRO_ZOUT_H
  1799. */
  1800. int16_t MPU6050::getRotationZ() {
  1801.     I2Cdev::readBytes(devAddr, MPU6050_RA_GYRO_ZOUT_H, 2, buffer);
  1802.     return (((int16_t)buffer[0]) << 8) | buffer[1];
  1803. }

  1804. // EXT_SENS_DATA_* registers

  1805. /** Read single byte from external sensor data register.
  1806. * These registers store data read from external sensors by the Slave 0, 1, 2,
  1807. * and 3 on the auxiliary I2C interface. Data read by Slave 4 is stored in
  1808. * I2C_SLV4_DI (Register 53).
  1809. *
  1810. * External sensor data is written to these registers at the Sample Rate as
  1811. * defined in Register 25. This access rate can be reduced by using the Slave
  1812. * Delay Enable registers (Register 103).
  1813. *
  1814. * External sensor data registers, along with the gyroscope measurement
  1815. * registers, accelerometer measurement registers, and temperature measurement
  1816. * registers, are composed of two sets of registers: an internal register set
  1817. * and a user-facing read register set.
  1818. *
  1819. * The data within the external sensors' internal register set is always updated
  1820. * at the Sample Rate (or the reduced access rate) whenever the serial interface
  1821. * is idle. This guarantees that a burst read of sensor registers will read
  1822. * measurements from the same sampling instant. Note that if burst reads are not
  1823. * used, the user is responsible for ensuring a set of single byte reads
  1824. * correspond to a single sampling instant by checking the Data Ready interrupt.
  1825. *
  1826. * Data is placed in these external sensor data registers according to
  1827. * I2C_SLV0_CTRL, I2C_SLV1_CTRL, I2C_SLV2_CTRL, and I2C_SLV3_CTRL (Registers 39,
  1828. * 42, 45, and 48). When more than zero bytes are read (I2C_SLVx_LEN > 0) from
  1829. * an enabled slave (I2C_SLVx_EN = 1), the slave is read at the Sample Rate (as
  1830. * defined in Register 25) or delayed rate (if specified in Register 52 and
  1831. * 103). During each Sample cycle, slave reads are performed in order of Slave
  1832. * number. If all slaves are enabled with more than zero bytes to be read, the
  1833. * order will be Slave 0, followed by Slave 1, Slave 2, and Slave 3.
  1834. *
  1835. * Each enabled slave will have EXT_SENS_DATA registers associated with it by
  1836. * number of bytes read (I2C_SLVx_LEN) in order of slave number, starting from
  1837. * EXT_SENS_DATA_00. Note that this means enabling or disabling a slave may
  1838. * change the higher numbered slaves' associated registers. Furthermore, if
  1839. * fewer total bytes are being read from the external sensors as a result of
  1840. * such a change, then the data remaining in the registers which no longer have
  1841. * an associated slave device (i.e. high numbered registers) will remain in
  1842. * these previously allocated registers unless reset.
  1843. *
  1844. * If the sum of the read lengths of all SLVx transactions exceed the number of
  1845. * available EXT_SENS_DATA registers, the excess bytes will be dropped. There
  1846. * are 24 EXT_SENS_DATA registers and hence the total read lengths between all
  1847. * the slaves cannot be greater than 24 or some bytes will be lost.
  1848. *
  1849. * Note: Slave 4's behavior is distinct from that of Slaves 0-3. For further
  1850. * information regarding the characteristics of Slave 4, please refer to
  1851. * Registers 49 to 53.
  1852. *
  1853. * EXAMPLE:
  1854. * Suppose that Slave 0 is enabled with 4 bytes to be read (I2C_SLV0_EN = 1 and
  1855. * I2C_SLV0_LEN = 4) while Slave 1 is enabled with 2 bytes to be read so that
  1856. * I2C_SLV1_EN = 1 and I2C_SLV1_LEN = 2. In such a situation, EXT_SENS_DATA _00
  1857. * through _03 will be associated with Slave 0, while EXT_SENS_DATA _04 and 05
  1858. * will be associated with Slave 1. If Slave 2 is enabled as well, registers
  1859. * starting from EXT_SENS_DATA_06 will be allocated to Slave 2.
  1860. *
  1861. * If Slave 2 is disabled while Slave 3 is enabled in this same situation, then
  1862. * registers starting from EXT_SENS_DATA_06 will be allocated to Slave 3
  1863. * instead.
  1864. *
  1865. * REGISTER ALLOCATION FOR DYNAMIC DISABLE VS. NORMAL DISABLE:
  1866. * If a slave is disabled at any time, the space initially allocated to the
  1867. * slave in the EXT_SENS_DATA register, will remain associated with that slave.
  1868. * This is to avoid dynamic adjustment of the register allocation.
  1869. *
  1870. * The allocation of the EXT_SENS_DATA registers is recomputed only when (1) all
  1871. * slaves are disabled, or (2) the I2C_MST_RST bit is set (Register 106).
  1872. *
  1873. * This above is also true if one of the slaves gets NACKed and stops
  1874. * functioning.
  1875. *
  1876. * @param position Starting position (0-23)
  1877. * @return Byte read from register
  1878. */
  1879. uint8_t MPU6050::getExternalSensorByte(int position) {
  1880.     I2Cdev::readByte(devAddr, MPU6050_RA_EXT_SENS_DATA_00 + position, buffer);
  1881.     return buffer[0];
  1882. }
  1883. /** Read word (2 bytes) from external sensor data registers.
  1884. * @param position Starting position (0-21)
  1885. * @return Word read from register
  1886. * @see getExternalSensorByte()
  1887. */
  1888. uint16_t MPU6050::getExternalSensorWord(int position) {
  1889.     I2Cdev::readBytes(devAddr, MPU6050_RA_EXT_SENS_DATA_00 + position, 2, buffer);
  1890.     return (((uint16_t)buffer[0]) << 8) | buffer[1];
  1891. }
  1892. /** Read double word (4 bytes) from external sensor data registers.
  1893. * @param position Starting position (0-20)
  1894. * @return Double word read from registers
  1895. * @see getExternalSensorByte()
  1896. */
  1897. uint32_t MPU6050::getExternalSensorDWord(int position) {
  1898.     I2Cdev::readBytes(devAddr, MPU6050_RA_EXT_SENS_DATA_00 + position, 4, buffer);
  1899.     return (((uint32_t)buffer[0]) << 24) | (((uint32_t)buffer[1]) << 16) | (((uint16_t)buffer[2]) << 8) | buffer[3];
  1900. }

  1901. // MOT_DETECT_STATUS register

  1902. /** Get X-axis negative motion detection interrupt status.
  1903. * @return Motion detection status
  1904. * @see MPU6050_RA_MOT_DETECT_STATUS
  1905. * @see MPU6050_MOTION_MOT_XNEG_BIT
  1906. */
  1907. bool MPU6050::getXNegMotionDetected() {
  1908.     I2Cdev::readBit(devAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_XNEG_BIT, buffer);
  1909.     return buffer[0];
  1910. }
  1911. /** Get X-axis positive motion detection interrupt status.
  1912. * @return Motion detection status
  1913. * @see MPU6050_RA_MOT_DETECT_STATUS
  1914. * @see MPU6050_MOTION_MOT_XPOS_BIT
  1915. */
  1916. bool MPU6050::getXPosMotionDetected() {
  1917.     I2Cdev::readBit(devAddr, MPU6050_RA_MOT_DETECT_STATUS, MPU6050_MOTION_MOT_XPOS_BIT, buffer);
  1918.     return buffer[0];
  1919. }
  1920. /** Get Y-axis negative motion detection interrupt status.
  1921. * @return Motion detection status
  1922. * @see MPU6050_RA_MOT_DETECT_STATUS
  1923. * @see MPU6050_MOTION_MOT_YNEG_BIT
  1924. */

  1925. ……………………

  1926. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
CZ521mpu-6050资料.rar (2.86 MB, 下载次数: 18)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:304003 发表于 2018-4-9 08:51 | 只看该作者
有电路图的没有啊?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表