mpu9250.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include <stdbool.h>
  2. #include <stdint.h>
  3. #include "nrf_delay.h"
  4. #include "twi_master.h"
  5. #include "mpu9250.h"
  6. MPU_t mMPU9250;
  7. //**************************************
  8. //写入一个字节数据
  9. //**************************************
  10. bool MPU9250_Write_Byte(uint8_t Device_Address,uint8_t REG_Address,uint8_t REG_data)
  11. {
  12. uint8_t buf[2];
  13. buf[0] = REG_Address;
  14. buf[1] = REG_data;
  15. return twi_master_transfer(Device_Address, buf, 2, TWI_ISSUE_STOP);
  16. }
  17. //**************************************
  18. //读取N个字节数据
  19. //**************************************
  20. bool MPU9250_Read_nBytes(uint8_t Device_Address,uint8_t REG_Address,uint8_t *readDataBuf,uint8_t readDataLen)
  21. {
  22. bool ret;
  23. ret = twi_master_transfer(Device_Address, &REG_Address, 1, TWI_DONT_ISSUE_STOP);
  24. ret &= twi_master_transfer(Device_Address|TWI_READ_BIT, readDataBuf, readDataLen, TWI_ISSUE_STOP);
  25. return ret;
  26. }
  27. bool MPU9250_register_write_len(uint8_t Device_Address,uint8_t register_address, uint8_t len,uint8_t *buf)
  28. {
  29. uint8_t w2_data[50],i;
  30. w2_data[0] = register_address;
  31. for(i=0;i<len;i++)w2_data[i+1] = *(buf+i);
  32. if(twi_master_transfer(Device_Address, w2_data, len+1, TWI_ISSUE_STOP) == true)return 0;
  33. else return true;
  34. }
  35. bool MPU9250_register_read_len(uint8_t Device_Address,uint8_t register_address, uint8_t number_of_bytes,uint8_t * destination )
  36. {
  37. bool transfer_succeeded;
  38. transfer_succeeded = twi_master_transfer(Device_Address, &register_address, 1, TWI_DONT_ISSUE_STOP);
  39. transfer_succeeded &= twi_master_transfer(Device_Address|TWI_READ_BIT, destination, number_of_bytes, TWI_ISSUE_STOP);
  40. if(transfer_succeeded == true)return 0;
  41. else return true;
  42. }
  43. uint8_t MPU9250_init(void)
  44. {
  45. if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_PWR_MGMT_1,0x00)) return 1; //唤醒mpu9250
  46. if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_SMPLRT_DIV,0x07)) return 2;
  47. if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_CONFIG,0x01)) return 2; //低通滤波5hz
  48. if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_GYRO_CONFIG,0x18)) return 3; //不自检,2000deg/s
  49. if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_ACCEL_CONFIG,0x10)) return 4; //(0x00 +-2g;) ( 0x08 +-4g;) (0x10 +-8g;) (0x18 +-16g)
  50. if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_ACCEL_CONFIG_2,0x04)) return 4; //
  51. if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_INT_PIN_CFG,0x02)) return 5;
  52. if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_USER_CTRL,0x00)) return 6; //使能I2C
  53. if(!MPU9250_Write_Byte(AK8963_I2C_ADDR,AK8963_CNTL1,0x0f)) return 7;
  54. nrf_delay_ms(50);
  55. MPU9250_Write_Byte(AK8963_I2C_ADDR,AK8963_CNTL1,0x11);
  56. return 0;
  57. }
  58. void MPU9250_ReadData(void)
  59. {
  60. uint8_t buf[14],bufM[6];
  61. MPU9250_Read_nBytes(MPU9250_I2C_ADDR,MPU9250_ACCEL_XOUT_H, buf, 14);//读取加速度,温度,陀螺仪传感器
  62. MPU9250_Read_nBytes(AK8963_I2C_ADDR,AK8963_HXL, bufM, 6);//读取地磁传感器
  63. MPU9250_Write_Byte(AK8963_I2C_ADDR,AK8963_CNTL1,0x11);//每读一次数据,ak8963会自动进入powerdown模式,这里需要重新设定为单测量模式
  64. mMPU9250.acc_x = ((int16_t)buf[0]<<8)|((int16_t)buf[1]);
  65. mMPU9250.acc_y = ((int16_t)buf[2]<<8)|((int16_t)buf[3]);
  66. mMPU9250.acc_z = ((int16_t)buf[4]<<8)|((int16_t)buf[5]);
  67. mMPU9250.temp = ((int16_t)buf[6]<<8)|((int16_t)buf[7]);
  68. mMPU9250.gyro_x = ((int16_t)buf[8]<<8)|((int16_t)buf[9]);
  69. mMPU9250.gyro_y = ((int16_t)buf[10]<<8)|((int16_t)buf[11]);
  70. mMPU9250.gyro_z = ((int16_t)buf[12]<<8)|((int16_t)buf[13]);
  71. mMPU9250.mag_x = ((int16_t)bufM[1]<<8)|((int16_t)bufM[0]);
  72. mMPU9250.mag_y = ((int16_t)bufM[3]<<8)|((int16_t)bufM[2]);
  73. mMPU9250.mag_z = ((int16_t)bufM[5]<<8)|((int16_t)bufM[4]);
  74. }