mpu6050.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2021 IOsetting <iosetting(at)outlook.com>
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "mpu6050.h"
  15. uint16_t swap(uint16_t num)
  16. {
  17. return (num >> 8) | (num << 8);
  18. }
  19. void MPU6050_Write(uint8_t addr, uint8_t dat)
  20. {
  21. I2C_Write(MPU6050_ADDR, addr, &dat, 1);
  22. }
  23. uint8_t MPU6050_Read(uint8_t addr)
  24. {
  25. uint8_t ret;
  26. I2C_Read(MPU6050_ADDR, addr, &ret, 1);
  27. return ret;
  28. }
  29. uint16_t MPU6050_ReadInt(uint8_t addr)
  30. {
  31. uint16_t ret;
  32. I2C_Read(MPU6050_ADDR, addr, (uint8_t *)&ret, 2);
  33. return swap(ret); // swap high/low bits for correct order
  34. }
  35. void MPU6050_ReadAll(uint16_t *buf)
  36. {
  37. uint8_t i;
  38. I2C_Read(MPU6050_ADDR, MPU6050_REG_ACCEL_XOUT_H, (uint8_t *)buf, 14);
  39. for (i = 0; i < 7; i++)
  40. {
  41. *(buf + i) = swap(*(buf + i));
  42. }
  43. }
  44. void MPU6050_Init(void) {
  45. MPU6050_DisableLowPowerMode();
  46. MPU6050_SetSampleRateDiv(0x07);
  47. MPU6050_SetDLPF(MPU6050_DLPF_Delay2ms);
  48. MPU6050_SetGyroFullScaleRange(MPU6050_Gyro_FullScaleRange_500dps);
  49. MPU6050_SetAccFullScaleRange(MPU6050_Acc_FullScaleRange_4g);
  50. }
  51. void MPU6050_Reset(void) {
  52. MPU6050_Write(MPU6050_REG_PWR_MGMT_1, 0x80);
  53. }
  54. void MPU6050_EnterSleepMode(void) {
  55. MPU6050_Write(MPU6050_REG_PWR_MGMT_1, 0x40);
  56. }
  57. void MPU6050_DisableTemperature(HAL_State_t state) {
  58. uint8_t reg = MPU6050_Read(MPU6050_REG_PWR_MGMT_1);
  59. MPU6050_Write(MPU6050_REG_PWR_MGMT_1, reg & ~0x08 | (state << 3));
  60. }
  61. void MPU6050_EnableLowPowerMode(MPU6050_Wakeup_Freq_t freq) {
  62. MPU6050_Write(MPU6050_REG_PWR_MGMT_1, 0x28); // 0010,1000 sleep:0, cycle:1, dis_temp:1
  63. MPU6050_Write(MPU6050_REG_PWR_MGMT_2, freq << 6 | 0x03); // STBY_XG, STBY_YG, STBY_ZG -> 1
  64. }
  65. void MPU6050_DisableLowPowerMode(void) {
  66. MPU6050_Write(MPU6050_REG_PWR_MGMT_1, 0x00);
  67. MPU6050_Write(MPU6050_REG_PWR_MGMT_2, 0x00);
  68. }
  69. /**
  70. * Sample Rate = Gyroscope Output Rate / (1 + SMPLRT_DIV)
  71. * where Gyroscope Output Rate = 8kHz when the DLPF is disabled (DLPF_CFG = 0 or 7), and 1kHz
  72. * when the DLPF is enabled
  73. */
  74. void MPU6050_SetSampleRateDiv(uint8_t div)
  75. {
  76. MPU6050_Write(MPU6050_REG_SMPLRT_DIV, div);
  77. }
  78. void MPU6050_SetDLPF(MPU6050_DLPF_t filter)
  79. {
  80. MPU6050_Write(MPU6050_REG_CONFIG, filter);
  81. }
  82. void MPU6050_SetGyroFullScaleRange(MPU6050_Gyro_FullScaleRange_t range)
  83. {
  84. MPU6050_Write(MPU6050_REG_GYRO_CONFIG, range << 3);
  85. }
  86. void MPU6050_SetAccFullScaleRange(MPU6050_Acc_FullScaleRange_t range)
  87. {
  88. MPU6050_Write(MPU6050_REG_ACCEL_CONFIG, range << 3);
  89. }