Advertisement
DanielEsp

MPU 6050 nivel 8x8

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