main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2024 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "adc.h"
  22. #include "i2c.h"
  23. #include "quadspi.h"
  24. #include "tim.h"
  25. #include "usart.h"
  26. #include "gpio.h"
  27. /* Private includes ----------------------------------------------------------*/
  28. /* USER CODE BEGIN Includes */
  29. #include "MPU6050.h"
  30. #include <stdio.h>
  31. /* USER CODE END Includes */
  32. /* Private typedef -----------------------------------------------------------*/
  33. /* USER CODE BEGIN PTD */
  34. /* USER CODE END PTD */
  35. /* Private define ------------------------------------------------------------*/
  36. /* USER CODE BEGIN PD */
  37. /* USER CODE END PD */
  38. /* Private macro -------------------------------------------------------------*/
  39. /* USER CODE BEGIN PM */
  40. /* USER CODE END PM */
  41. /* Private variables ---------------------------------------------------------*/
  42. /* USER CODE BEGIN PV */
  43. int a=0x68;
  44. int b=0xD1;
  45. int c=0xD0;
  46. int t = 1;
  47. //int a = 0x69;
  48. //int b = 0xD3;
  49. //int c = 0xD2;
  50. /* USER CODE END PV */
  51. /* Private function prototypes -----------------------------------------------*/
  52. void SystemClock_Config(void);
  53. /* USER CODE BEGIN PFP */
  54. /* USER CODE END PFP */
  55. /* Private user code ---------------------------------------------------------*/
  56. /* USER CODE BEGIN 0 */
  57. int fputc(int ch, FILE *f)
  58. {
  59. HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 0xffff); // �����ض���
  60. return ch;
  61. }
  62. int fgetc(FILE *f)
  63. {
  64. uint8_t ch = 0;
  65. HAL_UART_Receive(&huart2, &ch, 1, 0xffff); // �����ض���
  66. return ch;
  67. }
  68. /* USER CODE END 0 */
  69. /**
  70. * @brief The application entry point.
  71. * @retval int
  72. */
  73. int main(void)
  74. {
  75. /* USER CODE BEGIN 1 */
  76. /* USER CODE END 1 */
  77. /* MCU Configuration--------------------------------------------------------*/
  78. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  79. HAL_Init();
  80. /* USER CODE BEGIN Init */
  81. /* USER CODE END Init */
  82. /* Configure the system clock */
  83. SystemClock_Config();
  84. /* USER CODE BEGIN SysInit */
  85. /* USER CODE END SysInit */
  86. /* Initialize all configured peripherals */
  87. MX_GPIO_Init();
  88. MX_ADC1_Init();
  89. MX_I2C1_Init();
  90. MX_QUADSPI_Init();
  91. MX_TIM1_Init();
  92. MX_TIM3_Init();
  93. MX_TIM4_Init();
  94. MX_USART2_UART_Init();
  95. MX_I2C2_Init();
  96. MX_I2C3_Init();
  97. /* USER CODE BEGIN 2 */
  98. a=0x69;
  99. b=0xD3;
  100. c=0XD2;
  101. t=1;
  102. DMP_Init();
  103. a=0x68;
  104. b=0xD1;
  105. c=0XD0;
  106. t=1;
  107. DMP_Init();
  108. a=0x68;
  109. b=0xD1;
  110. c=0XD0;
  111. t=2;
  112. DMP_Init();
  113. a=0x69;
  114. b=0xD3;
  115. c=0XD2;
  116. t=2;
  117. DMP_Init();
  118. a=0x69;
  119. b=0xD3;
  120. c=0XD2;
  121. t=3;
  122. DMP_Init();
  123. a=0x68;
  124. b=0xD1;
  125. c=0XD0;
  126. t=3;
  127. DMP_Init();
  128. HAL_QSPI_Init(&hqspi);
  129. int g = 1;
  130. float Pitch1, Roll1, Yaw1;
  131. float Pitch2, Roll2, Yaw2;
  132. float Pitch3, Roll3, Yaw3;
  133. float Pitch4, Roll4, Yaw4;
  134. float Pitch5, Roll5, Yaw5;
  135. float Pitch6, Roll6, Yaw6;
  136. int gx1,gy1,gz1;
  137. short gx2,gy2,gz2;
  138. short gx3,gy3,gz3;
  139. short gx4,gy4,gz4;
  140. short gx5,gy5,gz5;
  141. short gx6,gy6,gz6;
  142. /* USER CODE END 2 */
  143. /* Infinite loop */
  144. /* USER CODE BEGIN WHILE */
  145. while (1)
  146. {
  147. //测试
  148. // a=0x68;
  149. // b=0xD1;
  150. // c=0XD0;
  151. // a=0x69;
  152. // b=0xD3;
  153. // c=0XD2;
  154. // t=3;
  155. //
  156. //
  157. // uint8_t ret = Read_DMP(&Pitch1, &Roll1, &Yaw1);
  158. // a=0x68;
  159. // b=0xD1;
  160. // c=0XD0;
  161. // t=1;
  162. //
  163. //
  164. // uint8_t ret1 = Read_DMP(&Pitch2, &Roll2, &Yaw2);
  165. // printf("%f,%f,%f,%f,%f,%f\r\n",Pitch1,Roll1,Yaw1,Pitch2,Roll2,Yaw2);
  166. //
  167. //
  168. //
  169. if(g==1){ //15
  170. a=0x69;
  171. b=0xD3;
  172. c=0XD2;
  173. t=1;
  174. // char buff[100]={0};
  175. uint8_t ret = Read_DMP(&Pitch1, &Roll1, &Yaw1);
  176. MPU_Get_Gyroscope(&gx1,&gy1,&gz1);
  177. printf("%d,%d,%d",gx1,gy1,gz1);
  178. printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\r\n",Pitch1, Roll1, Yaw1,Pitch2, Roll2, Yaw2,Pitch3, Roll3, Yaw3,Pitch4, Roll4, Yaw4,Pitch5, Roll5, Yaw5,Pitch6, Roll6, Yaw6);
  179. ////printf("%d,%f,%f,%f\r\n", g,Pitch1, Roll1, Yaw1);
  180. ////printf("com:imu:%d:ang:%f:%f:%f\r\n", g,Pitch1, Roll1, Yaw1);
  181. //// sprintf(buff,"16:%f,%f,%f\r\n", Pitch, Roll, Yaw);
  182. //// HAL_QSPI_Transmit(&hqspi, buff, 100);
  183. g=2;
  184. }
  185. else if(g==2){ //18
  186. a=0x68;
  187. b=0xD1;
  188. c=0XD0;
  189. t=1;
  190. // float Pitch2, Roll2, Yaw2;
  191. uint8_t ret = Read_DMP(&Pitch2, &Roll2, &Yaw2);
  192. //printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\r\n",Pitch1, Roll1, Yaw1,Pitch2, Roll2, Yaw2,Pitch3, Roll3, Yaw3,Pitch4, Roll4, Yaw4,Pitch5, Roll5, Yaw5,Pitch6, Roll6, Yaw6);
  193. // printf("com:imu:%d:ang:%f:%f:%f\r\n", g,Pitch2, Roll2, Yaw2);
  194. g=3;
  195. }
  196. else if(g==3){ //16
  197. a=0x68;
  198. b=0xD1;
  199. c=0XD0;
  200. t=2;
  201. // float Pitch, Roll, Yaw;
  202. uint8_t ret = Read_DMP(&Pitch3, &Roll3, &Yaw3);
  203. //printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\r\n",Pitch1, Roll1, Yaw1,Pitch2, Roll2, Yaw2,Pitch3, Roll3, Yaw3,Pitch4, Roll4, Yaw4,Pitch5, Roll5, Yaw5,Pitch6, Roll6, Yaw6);
  204. //printf("%f,%f,%f,%f,%f,%f,%f,%f,%f\r\n",Pitch1, Roll1, Yaw1,Pitch2, Roll2, Yaw2,Pitch3, Roll3, Yaw3);
  205. // printf("com:imu:%d:ang:%f:%f:%f\r\n", g,Pitch3, Roll3, Yaw3);
  206. g=4;
  207. }
  208. else if(g==4){ //13
  209. a = 0x69;
  210. b = 0xD3;
  211. c = 0XD2;
  212. t = 2;
  213. uint8_t ret = Read_DMP(&Pitch4, &Roll4, &Yaw4);
  214. //printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\r\n",Pitch1, Roll1, Yaw1,Pitch2, Roll2, Yaw2,Pitch3, Roll3, Yaw3,Pitch4, Roll4, Yaw4,Pitch5, Roll5, Yaw5,Pitch6, Roll6, Yaw6);
  215. //printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\r\n",Pitch1, Roll1, Yaw1,Pitch2, Roll2, Yaw2,Pitch3, Roll3, Yaw3,Pitch4, Roll4, Yaw4,Pitch5, Roll5, Yaw5,Pitch6, Roll6, Yaw6);
  216. //printf("%d,%f,%f,%f\r\n", g, Pitch4, Roll4, Yaw4);
  217. ////printf("com:imu:%d:ang:%f:%f:%f\r\n", g,Pitch4, Roll4, Yaw4);
  218. g=5;
  219. }
  220. else if(g==5){ //17
  221. a=0x68;
  222. b=0xD1;
  223. c=0XD0;
  224. t=3;
  225. ////float Pitch5, Roll5, Yaw5;
  226. uint8_t ret = Read_DMP(&Pitch5, &Roll5, &Yaw5);
  227. //printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\r\n",Pitch1, Roll1, Yaw1,Pitch2, Roll2, Yaw2,Pitch3, Roll3, Yaw3,Pitch4, Roll4, Yaw4,Pitch5, Roll5, Yaw5,Pitch6, Roll6, Yaw6);
  228. //// printf("%d,%f,%f,%f\r\n", g,Pitch5, Roll5, Yaw5);
  229. //printf("com:imu:%d:ang:%f:%f:%f\r\n", g,Pitch5, Roll5, Yaw5);
  230. g=6;
  231. }
  232. else if(g==6){ //14
  233. a=0x69;
  234. b=0xD3;
  235. c=0XD2;
  236. t=3;
  237. uint8_t ret = Read_DMP(&Pitch6, &Roll6, &Yaw6);
  238. //printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\r\n",Pitch1, Roll1, Yaw1,Pitch2, Roll2, Yaw2,Pitch3, Roll3, Yaw3,Pitch4, Roll4, Yaw4,Pitch5, Roll5, Yaw5,Pitch6, Roll6, Yaw6);
  239. //printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\r\n",Pitch1, Roll1, Yaw1,Pitch2, Roll2, Yaw2,Pitch3, Roll3, Yaw3,Pitch4, Roll4, Yaw4,Pitch5, Roll5, Yaw5,Pitch6, Roll6, Yaw6);
  240. //printf("com:imu:%d:ang:%f:%f:%f\r\n", g,Pitch, Roll, Yaw);
  241. g=1;
  242. }
  243. //
  244. //printf("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\r\n",Pitch1, Roll1, Yaw1,Pitch2, Roll2, Yaw2,Pitch3, Roll3, Yaw3,Pitch4, Roll4, Yaw4,Pitch5, Roll5, Yaw5,Pitch6, Roll6, Yaw6);
  245. // HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,0);
  246. // HAL_Delay(10);
  247. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, 1);
  248. // HAL_Delay(10);
  249. //
  250. /* USER CODE END WHILE */
  251. /* USER CODE BEGIN 3 */
  252. }
  253. /* USER CODE END 3 */
  254. }
  255. /**
  256. * @brief System Clock Configuration
  257. * @retval None
  258. */
  259. void SystemClock_Config(void)
  260. {
  261. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  262. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  263. /** Supply configuration update enable
  264. */
  265. HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
  266. /** Configure the main internal regulator output voltage
  267. */
  268. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  269. while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
  270. /** Initializes the RCC Oscillators according to the specified parameters
  271. * in the RCC_OscInitTypeDef structure.
  272. */
  273. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  274. RCC_OscInitStruct.HSIState = RCC_HSI_DIV1;
  275. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  276. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  277. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  278. RCC_OscInitStruct.PLL.PLLM = 4;
  279. RCC_OscInitStruct.PLL.PLLN = 50;
  280. RCC_OscInitStruct.PLL.PLLP = 2;
  281. RCC_OscInitStruct.PLL.PLLQ = 2;
  282. RCC_OscInitStruct.PLL.PLLR = 2;
  283. RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_3;
  284. RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
  285. RCC_OscInitStruct.PLL.PLLFRACN = 0;
  286. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  287. {
  288. Error_Handler();
  289. }
  290. /** Initializes the CPU, AHB and APB buses clocks
  291. */
  292. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  293. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
  294. |RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
  295. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  296. RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  297. RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
  298. RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
  299. RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
  300. RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
  301. RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
  302. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  303. {
  304. Error_Handler();
  305. }
  306. }
  307. /* USER CODE BEGIN 4 */
  308. /* USER CODE END 4 */
  309. /**
  310. * @brief This function is executed in case of error occurrence.
  311. * @retval None
  312. */
  313. void Error_Handler(void)
  314. {
  315. /* USER CODE BEGIN Error_Handler_Debug */
  316. /* User can add his own implementation to report the HAL error return state */
  317. __disable_irq();
  318. while (1)
  319. {
  320. }
  321. /* USER CODE END Error_Handler_Debug */
  322. }
  323. #ifdef USE_FULL_ASSERT
  324. /**
  325. * @brief Reports the name of the source file and the source line number
  326. * where the assert_param error has occurred.
  327. * @param file: pointer to the source file name
  328. * @param line: assert_param error line source number
  329. * @retval None
  330. */
  331. void assert_failed(uint8_t *file, uint32_t line)
  332. {
  333. /* USER CODE BEGIN 6 */
  334. /* User can add his own implementation to report the file name and line number,
  335. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  336. /* USER CODE END 6 */
  337. }
  338. #endif /* USE_FULL_ASSERT */