main.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. /***
  15. * Demo: MPU6050, 3-axis gyroscope + 3-axis accelerometer + Digital Motion Processor™ (DMP™)
  16. * Board: STC8H8K64U
  17. *
  18. * P32 -> SCL
  19. * P33 -> SDA
  20. * GND -> GND
  21. * 3.3V -> VCC
  22. */
  23. #include "fw_hal.h"
  24. #include "mpu6050.h"
  25. #include <stdio.h>
  26. void I2C_Init(void)
  27. {
  28. // Master mode
  29. I2C_SetWorkMode(I2C_WorkMode_Master);
  30. /**
  31. * I2C clock = FOSC / 2 / (__prescaler__ * 2 + 4)
  32. * MPU6050 works with i2c clock up to 400KHz
  33. *
  34. * 44.2368 / 2 / (26 * 2 + 4) = 0.39 MHz
  35. */
  36. I2C_SetClockPrescaler(0x1A);
  37. // Switch alternative port
  38. I2C_SetPort(I2C_AlterPort_P32_P33);
  39. // Start I2C
  40. I2C_SetEnabled(HAL_State_ON);
  41. }
  42. void GPIO_Init(void)
  43. {
  44. // SDA
  45. GPIO_P3_SetMode(GPIO_Pin_3, GPIO_Mode_InOut_QBD);
  46. // SCL
  47. GPIO_P3_SetMode(GPIO_Pin_2, GPIO_Mode_Output_PP);
  48. }
  49. int main(void)
  50. {
  51. uint8_t i;
  52. uint16_t buf[7];
  53. SYS_SetClock();
  54. GPIO_Init();
  55. UART1_Config8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200);
  56. I2C_Init();
  57. MPU6050_Init();
  58. while(1)
  59. {
  60. for (i = 0; i < 100; i++)
  61. {
  62. if (i == 0)
  63. {
  64. MPU6050_EnableLowPowerMode(MPU6050_Wakeup_Freq_1p25Hz);
  65. }
  66. else if (i == 50)
  67. {
  68. MPU6050_DisableLowPowerMode();
  69. }
  70. MPU6050_ReadAll(buf);
  71. printf("ax:%6d, ay:%6d, az:%6d, tp:%6d, gx:%6d, gy:%6d, gz:%6d\r\n",
  72. buf[0], buf[1], buf[2], (int16_t)buf[3] / 34 + 365, buf[4], buf[5], buf[6]);
  73. SYS_Delay(100);
  74. }
  75. }
  76. }