main.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2025 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 "cmsis_os.h"
  22. #include "adc.h"
  23. #include "dma.h"
  24. #include "i2c.h"
  25. #include "tim.h"
  26. #include "usart.h"
  27. #include "gpio.h"
  28. /* Private includes ----------------------------------------------------------*/
  29. /* USER CODE BEGIN Includes */
  30. extern uint8_t dataReceive485[BUFFER_SIZE485]; // 485
  31. extern uint16_t uartIRQ_rx_len ; // 485串口接收数据长度
  32. /* USER CODE END Includes */
  33. /* Private typedef -----------------------------------------------------------*/
  34. /* USER CODE BEGIN PTD */
  35. /* USER CODE END PTD */
  36. /* Private define ------------------------------------------------------------*/
  37. /* USER CODE BEGIN PD */
  38. /* USER CODE END PD */
  39. /* Private macro -------------------------------------------------------------*/
  40. /* USER CODE BEGIN PM */
  41. /* USER CODE END PM */
  42. /* Private variables ---------------------------------------------------------*/
  43. /* USER CODE BEGIN PV */
  44. /* USER CODE END PV */
  45. /* Private function prototypes -----------------------------------------------*/
  46. void SystemClock_Config(void);
  47. void MX_FREERTOS_Init(void);
  48. /* USER CODE BEGIN PFP */
  49. /* USER CODE END PFP */
  50. /* Private user code ---------------------------------------------------------*/
  51. /* USER CODE BEGIN 0 */
  52. void uart_enable(void)
  53. {
  54. memset(dataReceive485, 0, sizeof(dataReceive485));
  55. __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);//使能IDLE中断
  56. HAL_UART_Receive_DMA(&huart1, dataReceive485, BUFFER_SIZE485);
  57. }
  58. void Restart_UART1_DMA(void)
  59. {
  60. HAL_UART_DMAStop(&huart1); // 停止 DMA
  61. __HAL_UART_DISABLE_IT(&huart1, UART_IT_IDLE); // 禁止 IDLE 中断
  62. uartIRQ_rx_len = 0;
  63. // 清空接收缓冲区
  64. memset(dataReceive485, 0, sizeof(dataReceive485));
  65. // 重新启动 DMA 接收
  66. HAL_UART_Receive_DMA(&huart1, dataReceive485, BUFFER_SIZE485);
  67. // 重新开启 IDLE 中断
  68. __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
  69. }
  70. /* USER CODE END 0 */
  71. /**
  72. * @brief The application entry point.
  73. * @retval int
  74. */
  75. int main(void)
  76. {
  77. /* USER CODE BEGIN 1 */
  78. // SCB ->VTOR = FLASH_BASE | 0x6800;
  79. // __set_PRIMASK(0);
  80. /* USER CODE END 1 */
  81. /* MCU Configuration--------------------------------------------------------*/
  82. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  83. HAL_Init();
  84. /* USER CODE BEGIN Init */
  85. // SCB->VTOR = 0x08006800UL; /* 重定向中断向量表到 APP 起始地址 */
  86. /* USER CODE END Init */
  87. /* Configure the system clock */
  88. SystemClock_Config();
  89. /* USER CODE BEGIN SysInit */
  90. // SCB ->VTOR = FLASH_BASE | 0x6800;
  91. // __set_PRIMASK(0);
  92. /* USER CODE END SysInit */
  93. /* Initialize all configured peripherals */
  94. MX_GPIO_Init();
  95. MX_DMA_Init();
  96. MX_ADC1_Init();
  97. MX_TIM2_Init();
  98. MX_USART1_UART_Init();
  99. MX_USART2_UART_Init();
  100. MX_I2C2_Init();
  101. MX_TIM1_Init();
  102. MX_TIM15_Init();
  103. MX_TIM7_Init();
  104. /* USER CODE BEGIN 2 */
  105. /* USER CODE END 2 */
  106. /* Init scheduler */
  107. osKernelInitialize(); /* Call init function for freertos objects (in freertos.c) */
  108. MX_FREERTOS_Init();
  109. /* Start scheduler */
  110. osKernelStart();
  111. /* We should never get here as control is now taken by the scheduler */
  112. /* Infinite loop */
  113. /* USER CODE BEGIN WHILE */
  114. while (1)
  115. {
  116. /* USER CODE END WHILE */
  117. /* USER CODE BEGIN 3 */
  118. }
  119. /* USER CODE END 3 */
  120. }
  121. /**
  122. * @brief System Clock Configuration
  123. * @retval None
  124. */
  125. void SystemClock_Config(void)
  126. {
  127. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  128. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  129. /** Configure the main internal regulator output voltage
  130. */
  131. if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  132. {
  133. Error_Handler();
  134. }
  135. /** Initializes the RCC Oscillators according to the specified parameters
  136. * in the RCC_OscInitTypeDef structure.
  137. */
  138. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  139. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  140. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  141. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  142. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  143. RCC_OscInitStruct.PLL.PLLM = 1;
  144. RCC_OscInitStruct.PLL.PLLN = 10;
  145. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
  146. RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  147. RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  148. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  149. {
  150. Error_Handler();
  151. }
  152. /** Initializes the CPU, AHB and APB buses clocks
  153. */
  154. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  155. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  156. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  157. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  158. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  159. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  160. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  161. {
  162. Error_Handler();
  163. }
  164. }
  165. /* USER CODE BEGIN 4 */
  166. /*
  167. // 定义变量
  168. uint32_t capture_rising = 0; // 上升沿捕获值
  169. uint32_t capture_falling = 0; // 下降沿捕获值
  170. uint32_t pwm_period = 0; // PWM 周期
  171. uint32_t pwm_pulse_width = 0; // PWM 高电平时间
  172. float duty_cycle = 0.0f; // 占空比
  173. float angle = 0.0f; // 计算出的角度值
  174. // 获取捕获极性的宏
  175. #define GET_CAPTURE_POLARITY(htim, channel) \
  176. ((htim)->Instance->CCER & (TIM_CCER_CC1P << ((channel) - 1)))
  177. void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
  178. {
  179. if (htim->Instance == TIM1)
  180. {
  181. if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
  182. {
  183. // 判断是上升沿还是下降沿触发
  184. if (GET_CAPTURE_POLARITY(htim, TIM_CHANNEL_1) == 0) // 上升沿触发
  185. {
  186. // 上升沿触发
  187. capture_rising = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
  188. __HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_1, TIM_INPUTCHANNELPOLARITY_FALLING); // 设置为下降沿触发
  189. }
  190. else // 下降沿触发
  191. {
  192. // 下降沿触发
  193. capture_falling = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
  194. __HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_1, TIM_INPUTCHANNELPOLARITY_RISING); // 设置为上升沿触发
  195. // 计算 PWM 周期和占空比
  196. if (capture_falling > capture_rising)
  197. {
  198. pwm_pulse_width = capture_falling - capture_rising;
  199. }
  200. else
  201. {
  202. pwm_pulse_width = (0xFFFF - capture_rising) + capture_falling; // 处理计数器溢出
  203. }
  204. // 计算 PWM 周期
  205. pwm_period = htim->Instance->ARR + 1; // 自动重装载值 + 1
  206. // 计算占空比
  207. duty_cycle = (float)pwm_pulse_width / (float)pwm_period * 100.0f;
  208. // 计算角度值(假设 PWM 占空比 0% 对应 0 度,100% 对应 360 度)
  209. angle = duty_cycle * 3.6f; // 1% 占空比对应 3.6 度
  210. // 打印结果
  211. printf("PWM Period: %lu, Pulse Width: %lu, Duty Cycle: %.2f%%, Angle: %.2f degrees\n",
  212. pwm_period, pwm_pulse_width, duty_cycle, angle);
  213. }
  214. }
  215. }
  216. }
  217. */
  218. /* USER CODE END 4 */
  219. /**
  220. * @brief Period elapsed callback in non blocking mode
  221. * @note This function is called when TIM6 interrupt took place, inside
  222. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  223. * a global variable "uwTick" used as application time base.
  224. * @param htim : TIM handle
  225. * @retval None
  226. */
  227. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  228. {
  229. /* USER CODE BEGIN Callback 0 */
  230. /* USER CODE END Callback 0 */
  231. if (htim->Instance == TIM6) {
  232. HAL_IncTick();
  233. }
  234. /* USER CODE BEGIN Callback 1 */
  235. /* USER CODE END Callback 1 */
  236. }
  237. /**
  238. * @brief This function is executed in case of error occurrence.
  239. * @retval None
  240. */
  241. void Error_Handler(void)
  242. {
  243. /* USER CODE BEGIN Error_Handler_Debug */
  244. /* User can add his own implementation to report the HAL error return state */
  245. __disable_irq();
  246. while (1)
  247. {
  248. }
  249. /* USER CODE END Error_Handler_Debug */
  250. }
  251. #ifdef USE_FULL_ASSERT
  252. /**
  253. * @brief Reports the name of the source file and the source line number
  254. * where the assert_param error has occurred.
  255. * @param file: pointer to the source file name
  256. * @param line: assert_param error line source number
  257. * @retval None
  258. */
  259. void assert_failed(uint8_t *file, uint32_t line)
  260. {
  261. /* USER CODE BEGIN 6 */
  262. /* User can add his own implementation to report the file name and line number,
  263. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  264. /* USER CODE END 6 */
  265. }
  266. #endif /* USE_FULL_ASSERT */