mpu6050.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "mpu6050.h"
  2. #include "twi_master.h"
  3. #include "nrf_delay.h"
  4. #include "twi_master.h"
  5. #include "rec.h"
  6. int mpu6050_register_write_len(uint8_t addr,uint8_t register_address, uint8_t len,uint8_t *buf)
  7. {
  8. uint8_t w2_data[50],i;
  9. // printf("\r\n");
  10. // printf("write addr=0x%2X,",addr);
  11. // printf("register_address=0x%2X,",register_address);
  12. w2_data[0] = register_address;
  13. for(i=0;i<len;i++){
  14. w2_data[i+1] = *(buf+i);
  15. // printf("buf[%d]=%2X,",i,buf[i]);
  16. }
  17. // printf("\r\n");
  18. if(twi_master_transfer(addr, w2_data, len+1, TWI_ISSUE_STOP) == true) return 0;
  19. else return -1;
  20. }
  21. int mpu6050_register_read_len(uint8_t addr,uint8_t register_address, uint8_t number_of_bytes,uint8_t * destination )
  22. {
  23. bool transfer_succeeded;
  24. transfer_succeeded = twi_master_transfer(addr, &register_address, 1, TWI_DONT_ISSUE_STOP);
  25. transfer_succeeded &= twi_master_transfer(addr|TWI_READ_BIT, destination, number_of_bytes, TWI_ISSUE_STOP);
  26. if(transfer_succeeded == true)return 0;
  27. else return -1;
  28. }
  29. void mdelay(unsigned long nTime)
  30. {
  31. nrf_delay_ms(nTime);
  32. }
  33. /****************************************** 寄存器版 ****************************************************/
  34. //int mpu6050_register_write_len(uint8_t addr,uint8_t register_address, uint8_t len,uint8_t *buf)
  35. //int mpu6050_register_read_len(uint8_t addr,uint8_t register_address, uint8_t number_of_bytes,uint8_t * destination )
  36. /**
  37. * @brief 写数据到MPU6050寄存器
  38. * @param
  39. * @retval
  40. */
  41. int MPU6050_WriteReg(uint8_t reg_add,uint8_t reg_dat)
  42. {
  43. if (mpu6050_register_write_len(MPU6050_ADDR, reg_add, 1, &reg_dat))
  44. return -1;
  45. return 0;
  46. }
  47. /**
  48. * @brief 从MPU6050寄存器读取数据
  49. * @param
  50. * @retval
  51. */
  52. int MPU6050_ReadData(uint8_t reg_add,unsigned char*Read,uint8_t num)
  53. {
  54. if (mpu6050_register_read_len(MPU6050_ADDR,reg_add,num,Read))
  55. return -1;
  56. return 0;
  57. }
  58. /**
  59. * @brief 初始化MPU6050芯片
  60. * @param
  61. * @retval
  62. */
  63. void MPU6050_Init_reg(void)
  64. {
  65. MPU6050_WriteReg(MPU6050_RA_SIGNAL_PATH_RESET, 0x07); //Resets gyro, accelerometer and temperature sensor signal paths
  66. MPU6050_WriteReg(MPU6050_RA_PWR_MGMT_1, 0x00); //解除休眠状态
  67. MPU6050_WriteReg(MPU6050_RA_SMPLRT_DIV , 0x07); //陀螺仪采样率,1KHz
  68. MPU6050_WriteReg(MPU6050_RA_CONFIG , 0x06); //低通滤波器的设置,截止频率是1K,带宽是5K
  69. MPU6050_WriteReg(MPU6050_RA_ACCEL_CONFIG , ACCEL_CONFIG); //配置加速度传感器工作在2G模式,不自检 正常我 用0x08
  70. MPU6050_WriteReg(MPU6050_RA_GYRO_CONFIG, GYRO_CONFIG); //陀螺仪自检及测量范围,典型值:0x18(不自检,2000deg/s) 0x00 2502000deg/s 正常我 用0x08
  71. MPU6050_WriteReg(MPU6050_RA_USER_CTRL, 0x00);
  72. MPU6050_WriteReg(MPU6050_RA_INT_PIN_CFG, 0x32);
  73. MPU6050_WriteReg(MPU6050_RA_INT_ENABLE, 0x01);
  74. // MPU6050_WriteReg(MPU6050_RA_SIGNAL_PATH_RESET, 0x04U | 0x02U | 0x01U);
  75. // MPU6050_WriteReg(MPU6050_RA_SMPLRT_DIV, 0x00);
  76. // MPU6050_WriteReg(MPU6050_RA_CONFIG, 0x00);
  77. // MPU6050_WriteReg(MPU6050_RA_PWR_MGMT_1, 0x03);
  78. // MPU6050_WriteReg(MPU6050_RA_GYRO_CONFIG, 0x10);
  79. // MPU6050_WriteReg(MPU6050_RA_USER_CTRL, 0x00);
  80. // MPU6050_WriteReg(MPU6050_RA_INT_PIN_CFG, 0x32);
  81. // MPU6050_WriteReg(MPU6050_RA_INT_ENABLE, 0x01);
  82. // MPU6050_WriteReg(MPU6050_RA_ACCEL_CONFIG, 0x00);
  83. }
  84. /**
  85. * @brief 读取MPU6050的ID
  86. * @param
  87. * @retval
  88. */
  89. uint8_t MPU6050ReadID(void)
  90. {
  91. unsigned char Re = 0;
  92. MPU6050_ReadData(MPU6050_RA_WHO_AM_I,&Re,1); //读器件地址
  93. if(Re != 0x68)
  94. {
  95. printf("MPU6050 dectected error!\r\n检测不到MPU6050模块,请检查模块与开发板的接线");
  96. return 0;
  97. }
  98. else
  99. {
  100. printf("MPU6050 ID = %d\r\n",Re);
  101. return 1;
  102. }
  103. }
  104. /**
  105. * @brief 读取MPU6050的加速度数据
  106. * @param
  107. * @retval
  108. */
  109. int MPU6050ReadAcc(short *accData)
  110. {
  111. uint8_t buf[6];
  112. if(MPU6050_ReadData(MPU6050_ACC_OUT, buf, 6))
  113. return -1;
  114. accData[0] = (buf[0] << 8) | buf[1];
  115. accData[1] = (buf[2] << 8) | buf[3];
  116. accData[2] = (buf[4] << 8) | buf[5];
  117. return 0;
  118. }
  119. /**
  120. * @brief 读取MPU6050的角加速度数据
  121. * @param
  122. * @retval
  123. */
  124. int MPU6050ReadGyro(short *gyroData)
  125. {
  126. uint8_t buf[6];
  127. if(MPU6050_ReadData(MPU6050_GYRO_OUT,buf,6))
  128. return -1;
  129. gyroData[0] = (buf[0] << 8) | buf[1];
  130. gyroData[1] = (buf[2] << 8) | buf[3];
  131. gyroData[2] = (buf[4] << 8) | buf[5];
  132. return 0;
  133. }
  134. int mpu6050_get_reg_data(short *gyro, short *accel)
  135. {
  136. int ret = 0;
  137. if(MPU6050ReadGyro(gyro)) ret = -1;
  138. if(MPU6050ReadAcc(accel)) ret = -1;
  139. return ret;
  140. }
  141. /**
  142. * @brief 读取MPU6050的原始温度数据
  143. * @param
  144. * @retval
  145. */
  146. int MPU6050ReadTemp(short *tempData)
  147. {
  148. uint8_t buf[2];
  149. if(MPU6050_ReadData(MPU6050_RA_TEMP_OUT_H,buf,2))
  150. return -1;
  151. *tempData = (buf[0] << 8) | buf[1];
  152. return 0;
  153. }
  154. /**
  155. * @brief 读取MPU6050的温度数据,转化成摄氏度
  156. * @param
  157. * @retval
  158. */
  159. int MPU6050_ReturnTemp(float *Temperature)
  160. {
  161. short temp3;
  162. uint8_t buf[2];
  163. if(MPU6050_ReadData(MPU6050_RA_TEMP_OUT_H,buf,2))
  164. return -1;
  165. temp3= (buf[0] << 8) | buf[1];
  166. *Temperature=((double) temp3/340.0)+36.53;
  167. return 0;
  168. }