Guest User

Darn

a guest
Feb 28th, 2026
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.13 KB | Source Code | 0 0
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * Copyright (c) 2026 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.  
  22. /* Private includes ----------------------------------------------------------*/
  23. /* USER CODE BEGIN Includes */
  24. #include <math.h>
  25. #include <stdio.h>
  26. /* USER CODE END Includes */
  27.  
  28. /* Private typedef -----------------------------------------------------------*/
  29. /* USER CODE BEGIN PTD */
  30.  
  31. /* USER CODE END PTD */
  32.  
  33. /* Private define ------------------------------------------------------------*/
  34. /* USER CODE BEGIN PD */
  35.  
  36. /* USER CODE END PD */
  37.  
  38. /* Private macro -------------------------------------------------------------*/
  39. /* USER CODE BEGIN PM */
  40.  
  41. /* USER CODE END PM */
  42.  
  43. /* Private variables ---------------------------------------------------------*/
  44. I2C_HandleTypeDef hi2c1;
  45.  
  46. UART_HandleTypeDef huart1;
  47.  
  48. PCD_HandleTypeDef hpcd_USB_OTG_FS;
  49.  
  50. /* USER CODE BEGIN PV */
  51. #define MPU6050_ADDR 0x68 << 1
  52. #define PWR_MGMT_1   0x6B
  53. #define ACCEL_XOUT_H 0x3B
  54. #define RAD_TO_DEG 57.2957795131f
  55.  
  56. int16_t Accel_X_RAW, Accel_Y_RAW, Accel_Z_RAW;
  57. int16_t Gyro_X_RAW, Gyro_Y_RAW, Gyro_Z_RAW;
  58. float Ax, Ay, Az, Gx, Gy, Gz;
  59. float roll = 0, pitch = 0, yaw = 0;
  60. uint32_t timer;
  61. /* USER CODE END PV */
  62.  
  63. /* Private function prototypes -----------------------------------------------*/
  64. void SystemClock_Config(void);
  65. static void MX_GPIO_Init(void);
  66. static void MX_I2C1_Init(void);
  67. static void MX_USART1_UART_Init(void);
  68. static void MX_USB_OTG_FS_PCD_Init(void);
  69. /* USER CODE BEGIN PFP */
  70.  
  71. /* USER CODE END PFP */
  72.  
  73. /* Private user code ---------------------------------------------------------*/
  74. /* USER CODE BEGIN 0 */
  75.  
  76. /* USER CODE END 0 */
  77.  
  78. /**
  79.   * @brief  The application entry point.
  80.   * @retval int
  81.   */
  82. int main(void)
  83. {
  84.  
  85.   /* USER CODE BEGIN 1 */
  86.  
  87.   /* USER CODE END 1 */
  88.  
  89.   /* MCU Configuration--------------------------------------------------------*/
  90.  
  91.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  92.   HAL_Init();
  93.  
  94.   /* USER CODE BEGIN Init */
  95.  
  96.   /* USER CODE END Init */
  97.  
  98.   /* Configure the system clock */
  99.   SystemClock_Config();
  100.  
  101.   /* USER CODE BEGIN SysInit */
  102.  
  103.   /* USER CODE END SysInit */
  104.  
  105.   /* Initialize all configured peripherals */
  106.   MX_GPIO_Init();
  107.   MX_I2C1_Init();
  108.   MX_USART1_UART_Init();
  109.   MX_USB_OTG_FS_PCD_Init();
  110.   /* USER CODE BEGIN 2 */
  111.   uint8_t check, data;
  112.  
  113.   // 1. Check if device is present
  114.   HAL_I2C_Mem_Read(&hi2c1, MPU6050_ADDR, 0x75, 1, &check, 1, 100);
  115.  
  116.   if (check == 0x68) { // 0x68 is the default WHO_AM_I value
  117.       // 2. Wake the sensor up
  118.       data = 0;
  119.       HAL_I2C_Mem_Write(&hi2c1, MPU6050_ADDR, PWR_MGMT_1, 1, &data, 1, 100);
  120.       printf("MPU6050 Initialized Successfully!\n");
  121.   } else {
  122.       printf("MPU6050 NOT FOUND! Check connections.\n");
  123.   }
  124.   /* USER CODE END 2 */
  125.  
  126.   /* Infinite loop */
  127.   /* USER CODE BEGIN WHILE */
  128.   while (1)
  129.   {
  130.     /* USER CODE END WHILE */
  131.       HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_13);
  132.       HAL_Delay(500);
  133.     /* USER CODE BEGIN 3 */
  134.     }
  135.   /* USER CODE END 3 */
  136. }
  137.  
  138. /**
  139.   * @brief System Clock Configuration
  140.   * @retval None
  141.   */
  142. void SystemClock_Config(void)
  143. {
  144.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  145.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  146.  
  147.   /** Configure the main internal regulator output voltage
  148.   */
  149.   __HAL_RCC_PWR_CLK_ENABLE();
  150.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  151.  
  152.   /** Initializes the RCC Oscillators according to the specified parameters
  153.   * in the RCC_OscInitTypeDef structure.
  154.   */
  155.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
  156.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  157.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  158.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  159.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  160.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  161.   RCC_OscInitStruct.PLL.PLLM = 12;
  162.   RCC_OscInitStruct.PLL.PLLN = 72;
  163.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  164.   RCC_OscInitStruct.PLL.PLLQ = 3;
  165.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  166.   {
  167.     Error_Handler();
  168.   }
  169.  
  170.   /** Initializes the CPU, AHB and APB buses clocks
  171.   */
  172.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  173.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  174.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  175.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  176.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  177.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  178.  
  179.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  180.   {
  181.     Error_Handler();
  182.   }
  183. }
  184.  
  185. /**
  186.   * @brief I2C1 Initialization Function
  187.   * @param None
  188.   * @retval None
  189.   */
  190. static void MX_I2C1_Init(void)
  191. {
  192.  
  193.   /* USER CODE BEGIN I2C1_Init 0 */
  194.  
  195.   /* USER CODE END I2C1_Init 0 */
  196.  
  197.   /* USER CODE BEGIN I2C1_Init 1 */
  198.  
  199.   /* USER CODE END I2C1_Init 1 */
  200.   hi2c1.Instance = I2C1;
  201.   hi2c1.Init.ClockSpeed = 100000;
  202.   hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  203.   hi2c1.Init.OwnAddress1 = 0;
  204.   hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  205.   hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  206.   hi2c1.Init.OwnAddress2 = 0;
  207.   hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  208.   hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  209.   if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  210.   {
  211.     Error_Handler();
  212.   }
  213.   /* USER CODE BEGIN I2C1_Init 2 */
  214.  
  215.   /* USER CODE END I2C1_Init 2 */
  216.  
  217. }
  218.  
  219. /**
  220.   * @brief USART1 Initialization Function
  221.   * @param None
  222.   * @retval None
  223.   */
  224. static void MX_USART1_UART_Init(void)
  225. {
  226.  
  227.   /* USER CODE BEGIN USART1_Init 0 */
  228.  
  229.   /* USER CODE END USART1_Init 0 */
  230.  
  231.   /* USER CODE BEGIN USART1_Init 1 */
  232.  
  233.   /* USER CODE END USART1_Init 1 */
  234.   huart1.Instance = USART1;
  235.   huart1.Init.BaudRate = 115200;
  236.   huart1.Init.WordLength = UART_WORDLENGTH_8B;
  237.   huart1.Init.StopBits = UART_STOPBITS_1;
  238.   huart1.Init.Parity = UART_PARITY_NONE;
  239.   huart1.Init.Mode = UART_MODE_TX_RX;
  240.   huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  241.   huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  242.   if (HAL_UART_Init(&huart1) != HAL_OK)
  243.   {
  244.     Error_Handler();
  245.   }
  246.   /* USER CODE BEGIN USART1_Init 2 */
  247.  
  248.   /* USER CODE END USART1_Init 2 */
  249.  
  250. }
  251.  
  252. /**
  253.   * @brief USB_OTG_FS Initialization Function
  254.   * @param None
  255.   * @retval None
  256.   */
  257. static void MX_USB_OTG_FS_PCD_Init(void)
  258. {
  259.  
  260.   /* USER CODE BEGIN USB_OTG_FS_Init 0 */
  261.  
  262.   /* USER CODE END USB_OTG_FS_Init 0 */
  263.  
  264.   /* USER CODE BEGIN USB_OTG_FS_Init 1 */
  265.  
  266.   /* USER CODE END USB_OTG_FS_Init 1 */
  267.   hpcd_USB_OTG_FS.Instance = USB_OTG_FS;
  268.   hpcd_USB_OTG_FS.Init.dev_endpoints = 4;
  269.   hpcd_USB_OTG_FS.Init.speed = PCD_SPEED_FULL;
  270.   hpcd_USB_OTG_FS.Init.dma_enable = DISABLE;
  271.   hpcd_USB_OTG_FS.Init.phy_itface = PCD_PHY_EMBEDDED;
  272.   hpcd_USB_OTG_FS.Init.Sof_enable = DISABLE;
  273.   hpcd_USB_OTG_FS.Init.low_power_enable = DISABLE;
  274.   hpcd_USB_OTG_FS.Init.lpm_enable = DISABLE;
  275.   hpcd_USB_OTG_FS.Init.vbus_sensing_enable = DISABLE;
  276.   hpcd_USB_OTG_FS.Init.use_dedicated_ep1 = DISABLE;
  277.   if (HAL_PCD_Init(&hpcd_USB_OTG_FS) != HAL_OK)
  278.   {
  279.     Error_Handler();
  280.   }
  281.   /* USER CODE BEGIN USB_OTG_FS_Init 2 */
  282.  
  283.   /* USER CODE END USB_OTG_FS_Init 2 */
  284.  
  285. }
  286.  
  287. /**
  288.   * @brief GPIO Initialization Function
  289.   * @param None
  290.   * @retval None
  291.   */
  292. static void MX_GPIO_Init(void)
  293. {
  294.   GPIO_InitTypeDef GPIO_InitStruct = {0};
  295.   /* USER CODE BEGIN MX_GPIO_Init_1 */
  296.  
  297.   /* USER CODE END MX_GPIO_Init_1 */
  298.  
  299.   /* GPIO Ports Clock Enable */
  300.   __HAL_RCC_GPIOC_CLK_ENABLE();
  301.   __HAL_RCC_GPIOH_CLK_ENABLE();
  302.   __HAL_RCC_GPIOB_CLK_ENABLE();
  303.   __HAL_RCC_GPIOA_CLK_ENABLE();
  304.  
  305.   /*Configure GPIO pin Output Level */
  306.   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, GPIO_PIN_RESET);
  307.  
  308.   /*Configure GPIO pins : PB12 PB14 PB15 */
  309.   GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_14|GPIO_PIN_15;
  310.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  311.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  312.   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  313.  
  314.   /*Configure GPIO pin : PB13 */
  315.   GPIO_InitStruct.Pin = GPIO_PIN_13;
  316.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  317.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  318.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  319.   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  320.  
  321.   /*Configure GPIO pin : I2C1_INT_Pin */
  322.   GPIO_InitStruct.Pin = I2C1_INT_Pin;
  323.   GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  324.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  325.   HAL_GPIO_Init(I2C1_INT_GPIO_Port, &GPIO_InitStruct);
  326.  
  327.   /* EXTI interrupt init*/
  328.   HAL_NVIC_SetPriority(EXTI9_5_IRQn, 0, 0);
  329.   HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
  330.  
  331.   /* USER CODE BEGIN MX_GPIO_Init_2 */
  332.  
  333.   /* USER CODE END MX_GPIO_Init_2 */
  334. }
  335.  
  336. /* USER CODE BEGIN 4 */
  337.  
  338. /* USER CODE END 4 */
  339.  
  340. /**
  341.   * @brief  This function is executed in case of error occurrence.
  342.   * @retval None
  343.   */
  344. void Error_Handler(void)
  345. {
  346.   /* USER CODE BEGIN Error_Handler_Debug */
  347.   /* User can add his own implementation to report the HAL error return state */
  348.   __disable_irq();
  349.   while (1)
  350.   {
  351.   }
  352.   /* USER CODE END Error_Handler_Debug */
  353. }
  354. #ifdef USE_FULL_ASSERT
  355. /**
  356.   * @brief  Reports the name of the source file and the source line number
  357.   *         where the assert_param error has occurred.
  358.   * @param  file: pointer to the source file name
  359.   * @param  line: assert_param error line source number
  360.   * @retval None
  361.   */
  362. void assert_failed(uint8_t *file, uint32_t line)
  363. {
  364.   /* USER CODE BEGIN 6 */
  365.   /* User can add his own implementation to report the file name and line number,
  366.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  367.   /* USER CODE END 6 */
  368. }
  369. #endif /* USE_FULL_ASSERT */
  370.  
Advertisement
Add Comment
Please, Sign In to add comment