mpu6050.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. #include "mpu6050.h"
  2. #include "twi_master.h"
  3. #include "nrf_delay.h"
  4. //#include "twi_master.h"
  5. //#include "mltypes.h"
  6. //#include "inv_mpu_dmp_motion_driver.h"
  7. //#include "log.h"
  8. //#include "invensense.h"
  9. //#include "invensense_adv.h"
  10. //#include "eMPL_outputs.h"
  11. //#include "mpu.h"
  12. //#include "inv_mpu.h"
  13. //#include "log.h"
  14. //#include "data_builder.h"
  15. //unsigned char *mpl_key = (unsigned char*)"eMPL 5.1";
  16. //volatile uint32_t hal_timestamp = 0;
  17. //#define ACCEL_ON (0x01)
  18. //#define GYRO_ON (0x02)
  19. //#define COMPASS_ON (0x04)
  20. //#define MOTION (0)
  21. //#define NO_MOTION (1)
  22. ///* Starting sampling rate. */
  23. //#define DEFAULT_MPU_HZ (100)
  24. //#define FLASH_SIZE (512)
  25. //#define FLASH_MEM_START ((void*)0x1800)
  26. //#define PEDO_READ_MS (1000)
  27. //#define TEMP_READ_MS (500)
  28. //#define COMPASS_READ_MS (100)
  29. ////struct rx_s {
  30. //// unsigned char header[3];
  31. //// unsigned char cmd;
  32. ////};
  33. ////struct hal_s {
  34. //// unsigned char lp_accel_mode;
  35. //// unsigned char sensors;
  36. //// unsigned char dmp_on;
  37. //// unsigned char wait_for_tap;
  38. //// volatile unsigned char new_gyro;
  39. //// unsigned char motion_int_mode;
  40. //// unsigned long no_dmp_hz;
  41. //// unsigned long next_pedo_ms;
  42. //// unsigned long next_temp_ms;
  43. //// unsigned long next_compass_ms;
  44. //// unsigned int report;
  45. //// unsigned short dmp_features;
  46. //// struct rx_s rx;
  47. ////};
  48. ////static struct hal_s hal = {0};
  49. ///* Platform-specific information. Kinda like a boardfile. */
  50. //struct platform_data_s {
  51. // signed char orientation[9];
  52. //};
  53. //static struct platform_data_s gyro_pdata = {
  54. // .orientation = { 1, 0, 0,
  55. // 0, 1, 0,
  56. // 0, 0, 1}
  57. //};
  58. int mpu6050_register_write_len(uint8_t addr,uint8_t register_address, uint8_t len,uint8_t *buf)
  59. {
  60. uint8_t w2_data[50],i;
  61. // printf("\r\n");
  62. // printf("write addr=0x%2X,",addr);
  63. // printf("register_address=0x%2X,",register_address);
  64. w2_data[0] = register_address;
  65. for(i=0;i<len;i++){
  66. w2_data[i+1] = *(buf+i);
  67. // printf("buf[%d]=%2X,",i,buf[i]);
  68. }
  69. // printf("\r\n");
  70. if(twi_master_transfer(addr, w2_data, len+1, TWI_ISSUE_STOP) == true) return 0;
  71. else return -1;
  72. }
  73. int mpu6050_register_read_len(uint8_t addr,uint8_t register_address, uint8_t number_of_bytes,uint8_t * destination )
  74. {
  75. bool transfer_succeeded;
  76. transfer_succeeded = twi_master_transfer(addr, &register_address, 1, TWI_DONT_ISSUE_STOP);
  77. transfer_succeeded &= twi_master_transfer(addr|TWI_READ_BIT, destination, number_of_bytes, TWI_ISSUE_STOP);
  78. if(transfer_succeeded == true)return 0;
  79. else return -1;
  80. }
  81. //volatile uint32_t g_ul_ms_ticks=0;
  82. //int get_tick_count(unsigned long *count)
  83. //{
  84. // count[0] = g_ul_ms_ticks;
  85. // return 0;
  86. //}
  87. //void mdelay(unsigned long nTime)
  88. //{
  89. // nrf_delay_ms(nTime);
  90. //}
  91. //int _MLPrintLog (int priority, const char* tag, const char* fmt, ...)
  92. //{
  93. // return 0;
  94. //}
  95. //static void tap_cb(unsigned char direction, unsigned char count)
  96. //{
  97. //// uint8_t buf[2];
  98. //// uint16_t L=0;
  99. // printf("tap_cb:direction=%d,count=%d\n",direction,count);
  100. // return;
  101. //}
  102. //static void android_orient_cb(unsigned char orientation)
  103. //{
  104. // printf("android_orient_cb:orientation=%d\n",orientation);
  105. // return;
  106. //}
  107. //int mpu6050_Init(void)
  108. //{
  109. // inv_error_t result;
  110. // unsigned char accel_fsr;
  111. // unsigned short gyro_rate, gyro_fsr;
  112. //// unsigned long timestamp;
  113. // static struct int_param_s int_param;
  114. //
  115. //
  116. // if(mpu_init(&int_param)){ printf("mpu_init fail! \r\n");
  117. // return -1;
  118. // }
  119. // if(inv_init_mpl()){ printf("inv_init_mpl fail! \r\n");
  120. // return -1;
  121. // }
  122. //
  123. // /* Compute 6-axis and 9-axis quaternions. */
  124. // inv_enable_quaternion();
  125. // inv_enable_9x_sensor_fusion();
  126. //
  127. // inv_enable_fast_nomot();
  128. // inv_enable_gyro_tc();
  129. //
  130. //#ifdef COMPASS_ENABLED
  131. // /* Compass calibration algorithms. */
  132. // inv_enable_vector_compass_cal();
  133. // inv_enable_magnetic_disturbance();
  134. //#endif
  135. //
  136. // inv_enable_eMPL_outputs();
  137. //
  138. // result = inv_start_mpl();
  139. // if (result == INV_ERROR_NOT_AUTHORIZED) {
  140. // printf("Not authorized.\n");
  141. // }
  142. // if (result) {
  143. // printf("Could not start the MPL.\n");
  144. // }
  145. //
  146. //
  147. // /* Get/set hardware configuration. Start gyro. */
  148. // /* Wake up all sensors. */
  149. //#ifdef COMPASS_ENABLED
  150. // mpu_set_sensors(INV_XYZ_GYRO | INV_XYZ_ACCEL | INV_XYZ_COMPASS);
  151. //#else
  152. // mpu_set_sensors(INV_XYZ_GYRO | INV_XYZ_ACCEL);
  153. //#endif
  154. // /* Push both gyro and accel data into the FIFO. */
  155. // mpu_configure_fifo(INV_XYZ_GYRO | INV_XYZ_ACCEL);
  156. // mpu_set_sample_rate(DEFAULT_MPU_HZ);
  157. //
  158. //#ifdef COMPASS_ENABLED
  159. // /* The compass sampling rate can be less than the gyro/accel sampling rate.
  160. // * Use this function for proper power management.
  161. // */
  162. // mpu_set_compass_sample_rate(1000 / COMPASS_READ_MS);
  163. //#endif
  164. // /* Read back configuration in case it was set improperly. */
  165. // mpu_get_sample_rate(&gyro_rate);
  166. // mpu_get_gyro_fsr(&gyro_fsr);
  167. // mpu_get_accel_fsr(&accel_fsr);
  168. //#ifdef COMPASS_ENABLED
  169. // mpu_get_compass_fsr(&compass_fsr);
  170. //#endif
  171. // /* Sync driver configuration with MPL. */
  172. // /* Sample rate expected in microseconds. */
  173. // inv_set_gyro_sample_rate(1000000L / gyro_rate);
  174. // inv_set_accel_sample_rate(1000000L / gyro_rate);
  175. //
  176. //#ifdef COMPASS_ENABLED
  177. // /* The compass rate is independent of the gyro and accel rates. As long as
  178. // * inv_set_compass_sample_rate is called with the correct value, the 9-axis
  179. // * fusion algorithm's compass correction gain will work properly.
  180. // */
  181. // inv_set_compass_sample_rate(COMPASS_READ_MS * 1000L);
  182. //#endif
  183. // /* Set chip-to-body orientation matrix.
  184. // * Set hardware units to dps/g's/degrees scaling factor.
  185. // */
  186. // inv_set_gyro_orientation_and_scale(
  187. // inv_orientation_matrix_to_scalar(gyro_pdata.orientation),
  188. // (long)gyro_fsr<<15);
  189. // inv_set_accel_orientation_and_scale(
  190. // inv_orientation_matrix_to_scalar(gyro_pdata.orientation),
  191. // (long)accel_fsr<<15);
  192. //#ifdef COMPASS_ENABLED
  193. // inv_set_compass_orientation_and_scale(
  194. // inv_orientation_matrix_to_scalar(compass_pdata.orientation),
  195. // (long)compass_fsr<<15);
  196. //#endif
  197. // /* Initialize HAL state variables. */
  198. ////#ifdef COMPASS_ENABLED
  199. //// hal.sensors = ACCEL_ON | GYRO_ON | COMPASS_ON;
  200. ////#else
  201. //// hal.sensors = ACCEL_ON | GYRO_ON;
  202. ////#endif
  203. //// hal.dmp_on = 0;
  204. //// hal.report = 0;
  205. //// hal.rx.cmd = 0;
  206. //// hal.next_pedo_ms = 0;
  207. //// hal.next_compass_ms = 0;
  208. //// hal.next_temp_ms = 0;
  209. //
  210. // /* Compass reads are handled by scheduler. */
  211. //// get_tick_count(&timestamp);
  212. // /* To initialize the DMP:
  213. // * 1. Call dmp_load_motion_driver_firmware(). This pushes the DMP image in
  214. // * inv_mpu_dmp_motion_driver.h into the MPU memory.
  215. // * 2. Push the gyro and accel orientation matrix to the DMP.
  216. // * 3. Register gesture callbacks. Don't worry, these callbacks won't be
  217. // * executed unless the corresponding feature is enabled.
  218. // * 4. Call dmp_enable_feature(mask) to enable different features.
  219. // * 5. Call dmp_set_fifo_rate(freq) to select a DMP output rate.
  220. // * 6. Call any feature-specific control functions.
  221. // *
  222. // * To enable the DMP, just call mpu_set_dmp_state(1). This function can
  223. // * be called repeatedly to enable and disable the DMP at runtime.
  224. // *
  225. // * The following is a short summary of the features supported in the DMP
  226. // * image provided in inv_mpu_dmp_motion_driver.c:
  227. // * DMP_FEATURE_LP_QUAT: Generate a gyro-only quaternion on the DMP at
  228. // * 200Hz. Integrating the gyro data at higher rates reduces numerical
  229. // * errors (compared to integration on the MCU at a lower sampling rate).
  230. // * DMP_FEATURE_6X_LP_QUAT: Generate a gyro/accel quaternion on the DMP at
  231. // * 200Hz. Cannot be used in combination with DMP_FEATURE_LP_QUAT.
  232. // * DMP_FEATURE_TAP: Detect taps along the X, Y, and Z axes.
  233. // * DMP_FEATURE_ANDROID_ORIENT: Google's screen rotation algorithm. Triggers
  234. // * an event at the four orientations where the screen should rotate.
  235. // * DMP_FEATURE_GYRO_CAL: Calibrates the gyro data after eight seconds of
  236. // * no motion.
  237. // * DMP_FEATURE_SEND_RAW_ACCEL: Add raw accelerometer data to the FIFO.
  238. // * DMP_FEATURE_SEND_RAW_GYRO: Add raw gyro data to the FIFO.
  239. // * DMP_FEATURE_SEND_CAL_GYRO: Add calibrated gyro data to the FIFO. Cannot
  240. // * be used in combination with DMP_FEATURE_SEND_RAW_GYRO.
  241. // */
  242. // dmp_load_motion_driver_firmware();
  243. // dmp_set_orientation(
  244. // inv_orientation_matrix_to_scalar(gyro_pdata.orientation));
  245. // dmp_register_tap_cb(tap_cb);
  246. // dmp_register_android_orient_cb(android_orient_cb);
  247. // /*
  248. // * Known Bug -
  249. // * DMP when enabled will sample sensor data at 200Hz and output to FIFO at the rate
  250. // * specified in the dmp_set_fifo_rate API. The DMP will then sent an interrupt once
  251. // * a sample has been put into the FIFO. Therefore if the dmp_set_fifo_rate is at 25Hz
  252. // * there will be a 25Hz interrupt from the MPU device.
  253. // *
  254. // * There is a known issue in which if you do not enable DMP_FEATURE_TAP
  255. // * then the interrupts will be at 200Hz even if fifo rate
  256. // * is set at a different rate. To avoid this issue include the DMP_FEATURE_TAP
  257. // *
  258. // * DMP sensor fusion works only with gyro at +-2000dps and accel +-2G
  259. // */
  260. //// hal.dmp_features = DMP_FEATURE_6X_LP_QUAT | DMP_FEATURE_TAP |
  261. //// DMP_FEATURE_ANDROID_ORIENT | DMP_FEATURE_SEND_RAW_ACCEL | DMP_FEATURE_SEND_CAL_GYRO |
  262. //// DMP_FEATURE_GYRO_CAL;
  263. //// dmp_enable_feature(hal.dmp_features);
  264. // dmp_enable_feature(DMP_FEATURE_6X_LP_QUAT | DMP_FEATURE_TAP |
  265. // DMP_FEATURE_ANDROID_ORIENT | DMP_FEATURE_SEND_RAW_ACCEL | DMP_FEATURE_SEND_CAL_GYRO |
  266. // DMP_FEATURE_GYRO_CAL);
  267. // dmp_set_fifo_rate(DEFAULT_MPU_HZ);
  268. // mpu_set_dmp_state(1);
  269. // return 0;
  270. //}
  271. //int mpu6050_get_dmp_data(short *gyro, short *accel, float *quat)
  272. //{
  273. // short gy[3], acc[3];
  274. // long q[4];
  275. //
  276. // unsigned long timestamp;
  277. //
  278. // short sensors;
  279. // unsigned char more;
  280. // float q0=1.0f,q1=0.0f,q2=0.0f,q3=0.0f;
  281. //// float pitch,roll,yaw;
  282. //
  283. // dmp_read_fifo(gy, acc, q, &timestamp, &sensors, &more);
  284. // if((sensors & INV_WXYZ_QUAT)){
  285. // /* DMP所得的四元数 */
  286. // q0=q[0] / 1073741824.0f;
  287. // q1=q[1] / 1073741824.0f;
  288. // q2=q[2] / 1073741824.0f;
  289. // q3=q[3] / 1073741824.0f;
  290. // quat[0] = q0;
  291. // quat[1] = q1;
  292. // quat[2] = q2;
  293. // quat[3] = q3;
  294. // /* 由四元数所得的欧拉角,单位度 */
  295. //// pitch = asin(-2 * q1 * q3 + 2 * q0* q2)* 57.3; // pitch
  296. //// roll = atan2(2 * q2 * q3 + 2 * q0 * q1, -2 * q1 * q1 - 2 * q2* q2 + 1)* 57.3; // roll
  297. //// yaw = atan2(2*(q1*q2 + q0*q3),q0*q0+q1*q1-q2*q2-q3*q3) * 57.3; //yaw
  298. //// printf("pitch=%f,roll=%f,yaw=%f\n",pitch,roll,yaw);
  299. // }
  300. // if((sensors & INV_XYZ_GYRO)){
  301. // gyro[0] = gy[0];
  302. // gyro[1] = gy[1];
  303. // gyro[2] = gy[2];
  304. // }
  305. // if((sensors & INV_XYZ_ACCEL)){
  306. // accel[0] = acc[0];
  307. // accel[1] = acc[1];
  308. // accel[2] = acc[2];
  309. // }
  310. //
  311. // if( (sensors & INV_WXYZ_QUAT) && (sensors & INV_XYZ_GYRO) && (sensors & INV_XYZ_ACCEL)) return 0;
  312. // return -1;
  313. //}
  314. //int mpu6050_get_linear_data(float *gyro, float *accel, float *quat)
  315. //{
  316. // short gy[3], acc[3];
  317. // long q[4];
  318. //
  319. // float acc_f[4];
  320. // unsigned long timestamp;
  321. // long acc_long[3];
  322. //
  323. // int8_t accuracy = 0;
  324. //
  325. // short sensors;
  326. // unsigned char more;
  327. // float q0=1.0f,q1=0.0f,q2=0.0f,q3=0.0f;
  328. //// float pitch,roll,yaw;
  329. //
  330. // dmp_read_fifo(gy, acc, q, &timestamp, &sensors, &more);
  331. // if((sensors & INV_WXYZ_QUAT)){
  332. // /* DMP所得的四元数 */
  333. // q0=q[0] / 1073741824.0f;
  334. // q1=q[1] / 1073741824.0f;
  335. // q2=q[2] / 1073741824.0f;
  336. // q3=q[3] / 1073741824.0f;
  337. // quat[0] = q0;
  338. // quat[1] = q1;
  339. // quat[2] = q2;
  340. // quat[3] = q3;
  341. // inv_build_quat(q, 0, timestamp);
  342. // /* 由四元数所得的欧拉角,单位度 */
  343. //// pitch = asin(-2 * q1 * q3 + 2 * q0* q2)* 57.3; // pitch
  344. //// roll = atan2(2 * q2 * q3 + 2 * q0 * q1, -2 * q1 * q1 - 2 * q2* q2 + 1)* 57.3; // roll
  345. //// yaw = atan2(2*(q1*q2 + q0*q3),q0*q0+q1*q1-q2*q2-q3*q3) * 57.3; //yaw
  346. //// printf("pitch=%f,roll=%f,yaw=%f\n",pitch,roll,yaw);
  347. // }
  348. // if((sensors & INV_XYZ_GYRO)){
  349. // gyro[0] = (float)(gy[0]/16.4f);
  350. // gyro[1] = (float)(gy[1]/16.4f);
  351. // gyro[2] = (float)(gy[2]/16.4f);
  352. // inv_build_gyro(gy, timestamp);
  353. // }
  354. // if((sensors & INV_XYZ_ACCEL)){
  355. //// accel[0] = acc[0];
  356. //// accel[1] = acc[1];
  357. //// accel[2] = acc[2];
  358. // acc_long[0] = (long)acc[0];
  359. // acc_long[1] = (long)acc[1];
  360. // acc_long[2] = (long)acc[2];
  361. // inv_build_accel(acc_long, 0, timestamp);
  362. // inv_execute_on_data();
  363. // inv_get_sensor_type_linear_acceleration(acc_f, &accuracy, (inv_time_t*)&timestamp);
  364. //// printf("%f,%f,%f \n",acc_f[0],acc_f[1],acc_f[2]);
  365. // accel[0] = acc_f[0];
  366. // accel[1] = acc_f[1];
  367. // accel[2] = acc_f[2];
  368. //
  369. //
  370. // return 0;
  371. // }
  372. //
  373. //// if( sensors & (INV_XYZ_GYRO | INV_XYZ_ACCEL | INV_WXYZ_QUAT)) return 0;
  374. // return -1;
  375. //}
  376. //int mpu6050_get_linear_data_int(short *gyro, short *accel, long *quat)
  377. //{
  378. // short gy[3], acc[3];
  379. // long q[4];
  380. //
  381. // float acc_f[4];
  382. // unsigned long timestamp;
  383. // long acc_long[3];
  384. //
  385. // int8_t accuracy = 0;
  386. //
  387. // short sensors;
  388. // unsigned char more;
  389. //// float q0=1.0f,q1=0.0f,q2=0.0f,q3=0.0f;
  390. //// float pitch,roll,yaw;
  391. //
  392. // dmp_read_fifo(gy, acc, q, &timestamp, &sensors, &more);
  393. // if((sensors & INV_WXYZ_QUAT)){
  394. // /* DMP所得的四元数 */
  395. //// q0=q[0] / 1073741824.0f;
  396. //// q1=q[1] / 1073741824.0f;
  397. //// q2=q[2] / 1073741824.0f;
  398. //// q3=q[3] / 1073741824.0f;
  399. // quat[0] = q[0];
  400. // quat[1] = q[1];
  401. // quat[2] = q[2];
  402. // quat[3] = q[3];
  403. // inv_build_quat(q, 0, timestamp);
  404. // /* 由四元数所得的欧拉角,单位度 */
  405. //// pitch = asin(-2 * q1 * q3 + 2 * q0* q2)* 57.3; // pitch
  406. //// roll = atan2(2 * q2 * q3 + 2 * q0 * q1, -2 * q1 * q1 - 2 * q2* q2 + 1)* 57.3; // roll
  407. //// yaw = atan2(2*(q1*q2 + q0*q3),q0*q0+q1*q1-q2*q2-q3*q3) * 57.3; //yaw
  408. //// printf("pitch=%f,roll=%f,yaw=%f\n",pitch,roll,yaw);
  409. // }
  410. // if((sensors & INV_XYZ_GYRO)){
  411. // gyro[0] = gy[0];
  412. // gyro[1] = gy[1];
  413. // gyro[2] = gy[2];
  414. // inv_build_gyro(gy, timestamp);
  415. // }
  416. // if((sensors & INV_XYZ_ACCEL)){
  417. //// accel[0] = acc[0];
  418. //// accel[1] = acc[1];
  419. //// accel[2] = acc[2];
  420. // acc_long[0] = (long)acc[0];
  421. // acc_long[1] = (long)acc[1];
  422. // acc_long[2] = (long)acc[2];
  423. // inv_build_accel(acc_long, 0, timestamp);
  424. // inv_execute_on_data();
  425. // inv_get_sensor_type_linear_acceleration(acc_f, &accuracy, (inv_time_t*)&timestamp);
  426. // accel[0] = acc_f[0]*100;
  427. // accel[1] = acc_f[1]*100;
  428. // accel[2] = acc_f[2]*100;
  429. // return 0;
  430. // }
  431. //
  432. //// if( sensors & (INV_XYZ_GYRO | INV_XYZ_ACCEL | INV_WXYZ_QUAT)) return 0;
  433. // return -1;
  434. //}
  435. /****************************************** 寄存器版 ****************************************************/
  436. //int mpu6050_register_write_len(uint8_t addr,uint8_t register_address, uint8_t len,uint8_t *buf)
  437. //int mpu6050_register_read_len(uint8_t addr,uint8_t register_address, uint8_t number_of_bytes,uint8_t * destination )
  438. /**
  439. * @brief 写数据到MPU6050寄存器
  440. * @param
  441. * @retval
  442. */
  443. int MPU6050_WriteReg(uint8_t reg_add,uint8_t reg_dat)
  444. {
  445. if (mpu6050_register_write_len(MPU6050_ADDR, reg_add, 1, &reg_dat))
  446. return -1;
  447. return 0;
  448. }
  449. /**
  450. * @brief 从MPU6050寄存器读取数据
  451. * @param
  452. * @retval
  453. */
  454. int MPU6050_ReadData(uint8_t reg_add,unsigned char*Read,uint8_t num)
  455. {
  456. if (mpu6050_register_read_len(MPU6050_ADDR,reg_add,num,Read))
  457. return -1;
  458. return 0;
  459. }
  460. /**
  461. * @brief 初始化MPU6050芯片
  462. * @param
  463. * @retval
  464. */
  465. void MPU6050_Init_reg(void)
  466. {
  467. MPU6050_WriteReg(MPU6050_RA_SIGNAL_PATH_RESET, 0x07); //Resets gyro, accelerometer and temperature sensor signal paths
  468. nrf_delay_ms(100);
  469. MPU6050_WriteReg(MPU6050_RA_PWR_MGMT_1, 0x00); //解除休眠状态
  470. nrf_delay_ms(100);
  471. MPU6050_WriteReg(MPU6050_RA_SMPLRT_DIV , 0x07); //陀螺仪采样率,1KHz
  472. MPU6050_WriteReg(MPU6050_RA_CONFIG , 0x06); //低通滤波器的设置,截止频率是1K,带宽是5K
  473. MPU6050_WriteReg(MPU6050_RA_ACCEL_CONFIG , 0x18); //4g
  474. MPU6050_WriteReg(MPU6050_RA_GYRO_CONFIG, 0x18); //2000deg/s
  475. MPU6050_WriteReg(MPU6050_RA_USER_CTRL, 0x00);
  476. MPU6050_WriteReg(MPU6050_RA_INT_PIN_CFG, 0x32);
  477. MPU6050_WriteReg(MPU6050_RA_INT_ENABLE, 0x01);
  478. nrf_delay_ms(100);
  479. // MPU6050_WriteReg(MPU6050_RA_SIGNAL_PATH_RESET, 0x07); //Resets gyro, accelerometer and temperature sensor signal paths
  480. // MPU6050_WriteReg(MPU6050_RA_PWR_MGMT_1, 0x00); //??????
  481. // MPU6050_WriteReg(MPU6050_RA_SMPLRT_DIV , 0x07); //??????,1KHz
  482. // MPU6050_WriteReg(MPU6050_RA_CONFIG , 0x06); //????????,?????1K,???5K
  483. //
  484. // MPU6050_WriteReg(MPU6050_RA_ACCEL_CONFIG , 0x08); //???????????2G??,???
  485. // MPU6050_WriteReg(MPU6050_RA_GYRO_CONFIG, 0x08); //??????????,???:0x18(???,2000deg/s) 0x00 2502000deg/s
  486. //
  487. // MPU6050_WriteReg(MPU6050_RA_USER_CTRL, 0x00);
  488. // MPU6050_WriteReg(MPU6050_RA_INT_PIN_CFG, 0x32);
  489. // MPU6050_WriteReg(MPU6050_RA_INT_ENABLE, 0x01);
  490. // MPU6050_WriteReg(MPU6050_RA_SIGNAL_PATH_RESET, 0x04U | 0x02U | 0x01U);
  491. // MPU6050_WriteReg(MPU6050_RA_SMPLRT_DIV, 0x00);
  492. // MPU6050_WriteReg(MPU6050_RA_CONFIG, 0x00);
  493. // MPU6050_WriteReg(MPU6050_RA_PWR_MGMT_1, 0x03);
  494. // MPU6050_WriteReg(MPU6050_RA_GYRO_CONFIG, 0x10);
  495. // MPU6050_WriteReg(MPU6050_RA_USER_CTRL, 0x00);
  496. // MPU6050_WriteReg(MPU6050_RA_INT_PIN_CFG, 0x32);
  497. // MPU6050_WriteReg(MPU6050_RA_INT_ENABLE, 0x01);
  498. // MPU6050_WriteReg(MPU6050_RA_ACCEL_CONFIG, 0x00);
  499. }
  500. /**
  501. * @brief 读取MPU6050的ID
  502. * @param
  503. * @retval
  504. */
  505. uint8_t MPU6050ReadID(void)
  506. {
  507. unsigned char Re = 0;
  508. MPU6050_ReadData(MPU6050_RA_WHO_AM_I,&Re,1); //读器件地址
  509. if(Re != 0x68)
  510. {
  511. printf("MPU6050 dectected error!\r\n检测不到MPU6050模块,请检查模块与开发板的接线");
  512. return 0;
  513. }
  514. else
  515. {
  516. printf("MPU6050 ID = %d\r\n",Re);
  517. return 1;
  518. }
  519. }
  520. /**
  521. * @brief 读取MPU6050的加速度数据
  522. * @param
  523. * @retval
  524. */
  525. int MPU6050ReadAcc(short *accData)
  526. {
  527. uint8_t buf[6];
  528. if(MPU6050_ReadData(MPU6050_ACC_OUT, buf, 6))
  529. return -1;
  530. accData[0] = (buf[0] << 8) | buf[1];
  531. accData[1] = (buf[2] << 8) | buf[3];
  532. accData[2] = (buf[4] << 8) | buf[5];
  533. return 0;
  534. }
  535. /**
  536. * @brief 读取MPU6050的角加速度数据
  537. * @param
  538. * @retval
  539. */
  540. int MPU6050ReadGyro(short *gyroData)
  541. {
  542. uint8_t buf[6];
  543. if(MPU6050_ReadData(MPU6050_GYRO_OUT,buf,6))
  544. return -1;
  545. gyroData[0] = (buf[0] << 8) | buf[1];
  546. gyroData[1] = (buf[2] << 8) | buf[3];
  547. gyroData[2] = (buf[4] << 8) | buf[5];
  548. return 0;
  549. }
  550. int mpu6050_get_reg_data(short *gyro, short *accel)
  551. {
  552. int ret = 0;
  553. if(MPU6050ReadGyro(gyro)) ret = -1;
  554. if(MPU6050ReadAcc(accel)) ret = -1;
  555. return ret;
  556. }
  557. /**
  558. * @brief 读取MPU6050的原始温度数据
  559. * @param
  560. * @retval
  561. */
  562. int MPU6050ReadTemp(short *tempData)
  563. {
  564. uint8_t buf[2];
  565. if(MPU6050_ReadData(MPU6050_RA_TEMP_OUT_H,buf,2))
  566. return -1;
  567. *tempData = (buf[0] << 8) | buf[1];
  568. return 0;
  569. }
  570. /**
  571. * @brief 读取MPU6050的温度数据,转化成摄氏度
  572. * @param
  573. * @retval
  574. */
  575. int MPU6050_ReturnTemp(float *Temperature)
  576. {
  577. short temp3;
  578. uint8_t buf[2];
  579. if(MPU6050_ReadData(MPU6050_RA_TEMP_OUT_H,buf,2))
  580. return -1;
  581. temp3= (buf[0] << 8) | buf[1];
  582. *Temperature=((double) temp3/340.0)+36.53;
  583. return 0;
  584. }