训练营PLSR题目
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

534 行
14 KiB

  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 "dma.h"
  22. #include "tim.h"
  23. #include "usart.h"
  24. #include "gpio.h"
  25. /* Private includes ----------------------------------------------------------*/
  26. /* USER CODE BEGIN Includes */
  27. #include "PLSR.h"
  28. #include "includes.h"
  29. #include "modbus.h"
  30. /* USER CODE END Includes */
  31. /* Private typedef -----------------------------------------------------------*/
  32. /* USER CODE BEGIN PTD */
  33. uint8_t ArrFlag = 0; /* 加速度Flag */
  34. float Acc; /* 加速度 */
  35. uint32_t AllPulseCNT = 0;
  36. uint8_t EndFlag = 0;
  37. uint8_t FullFlag = 0;
  38. /* USER CODE END PTD */
  39. /* Private define ------------------------------------------------------------*/
  40. /* USER CODE BEGIN PD */
  41. /* USER CODE END PD */
  42. /* Private macro -------------------------------------------------------------*/
  43. /* USER CODE BEGIN PM */
  44. /* USER CODE END PM */
  45. /* Private variables ---------------------------------------------------------*/
  46. /* USER CODE BEGIN PV */
  47. /* USER CODE END PV */
  48. /* Private function prototypes -----------------------------------------------*/
  49. void SystemClock_Config(void);
  50. /* USER CODE BEGIN PFP */
  51. void PVD_Init(void);
  52. /* USER CODE END PFP */
  53. /* Private user code ---------------------------------------------------------*/
  54. /* USER CODE BEGIN 0 */
  55. /* 任务优先级 */
  56. #define TASK_Y1Direction 4
  57. #define TASK_PulseStart 4
  58. #define TASK_DirOutput 4
  59. /* 任务堆栈大小 */
  60. #define Task_Y1Direction_SIZE 256
  61. #define Task_PulseStart_SIZE 256
  62. #define Task_DirOutput_SIZE 256
  63. /* 任务控制块 */
  64. OS_TCB Y1Direction_Tsk;
  65. OS_TCB PulseStart_Tsk;
  66. OS_TCB DirOutput_Tsk;
  67. /* 任务栈 */
  68. CPU_STK Task_Y1Direction_STK[Task_Y1Direction_SIZE];
  69. CPU_STK Task_PulseStart_STK[Task_PulseStart_SIZE];
  70. CPU_STK Task_DirOutput_STK[Task_DirOutput_SIZE];
  71. /* 任务函数 */
  72. void Y1Direction(void *p_arg);
  73. void PulseStartTsk(void *p_arg);
  74. void DirOutput(void *p_arg);
  75. /* USER CODE END 0 */
  76. /**
  77. * @brief The application entry point.
  78. * @retval int
  79. */
  80. int main(void)
  81. {
  82. /* USER CODE BEGIN 1 */
  83. OS_ERR err;
  84. /* USER CODE END 1 */
  85. /* MCU Configuration--------------------------------------------------------*/
  86. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  87. HAL_Init();
  88. /* USER CODE BEGIN Init */
  89. PVD_Init();
  90. /* USER CODE END Init */
  91. /* Configure the system clock */
  92. SystemClock_Config();
  93. /* USER CODE BEGIN SysInit */
  94. HAL_PWR_EnableBkUpAccess();/* 使能备份域访问 */
  95. __HAL_RCC_BKPSRAM_CLK_ENABLE();/* 使能备份SRAM时钟 */
  96. HAL_PWREx_EnableBkUpReg();/* 使能备份SRAM */
  97. /* USER CODE END SysInit */
  98. /* Initialize all configured peripherals */
  99. MX_GPIO_Init();
  100. MX_DMA_Init();
  101. MX_USART1_UART_Init();
  102. MX_TIM10_Init();
  103. MX_TIM2_Init();
  104. /* USER CODE BEGIN 2 */
  105. PulseBaseInit(0, 0, 200);//设置为第0段脉冲开始,相对模式, 最大脉冲为0
  106. AccCount = 50;
  107. PulseInit(0, 1, 5, 0); //第几段脉冲,频率,数量,下一段脉冲
  108. AddPulse(1, 100, -200, 0);
  109. AddPulse(2, 1, 55, 0);
  110. BSP_Init();
  111. OSInit(&err);
  112. /* 任务1 */
  113. OSTaskCreate((OS_TCB * )&Y1Direction_Tsk, /* 任务控制块 */
  114. (CPU_CHAR* )"Y1Direction", /* 任务名字 */
  115. (OS_TASK_PTR)Y1Direction, /* 任务函数 */
  116. (void * )0, /* 传递给任务函数的参数 */
  117. (OS_PRIO )TASK_Y1Direction, /* 任务优先级 */
  118. (CPU_STK * )&Task_Y1Direction_STK[0], /* 任务堆栈基地址 */
  119. (CPU_STK_SIZE)Task_Y1Direction_SIZE/10, /* 任务堆栈深度限位 */
  120. (CPU_STK_SIZE)Task_Y1Direction_SIZE, /* 任务堆栈大小 */
  121. (OS_MSG_QTY)0, /* 任务内部消息队列能够接收的最大消息数目,为0时禁止接收消息 */
  122. (OS_TICK )0, /* 当使能时间片轮转时的时间片长度,为0时为默认长度 */
  123. (void * )0, /* 用户补充的存储区 */
  124. (OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR, /* 任务选项 */
  125. (OS_ERR * )&err); /* 存放该函数错误时的返回值 */
  126. /* 任务2 */
  127. OSTaskCreate((OS_TCB * )&PulseStart_Tsk,
  128. (CPU_CHAR* )"PulseStartTsk",
  129. (OS_TASK_PTR)PulseStartTsk,
  130. (void * )0,
  131. (OS_PRIO )TASK_PulseStart,
  132. (CPU_STK * )&Task_PulseStart_STK[0],
  133. (CPU_STK_SIZE)Task_PulseStart_SIZE/10,
  134. (CPU_STK_SIZE)Task_PulseStart_SIZE,
  135. (OS_MSG_QTY)0,
  136. (OS_TICK )0,
  137. (void * )0,
  138. (OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR,
  139. (OS_ERR * )&err);
  140. /* 任务3 */
  141. OSTaskCreate((OS_TCB * )&DirOutput_Tsk,
  142. (CPU_CHAR* )"DirOutput_Tsk",
  143. (OS_TASK_PTR)DirOutput,
  144. (void * )0,
  145. (OS_PRIO )TASK_DirOutput,
  146. (CPU_STK * )&Task_DirOutput_STK[0],
  147. (CPU_STK_SIZE)Task_DirOutput_SIZE/10,
  148. (CPU_STK_SIZE)Task_DirOutput_SIZE,
  149. (OS_MSG_QTY)0,
  150. (OS_TICK )0,
  151. (void * )0,
  152. (OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR,
  153. (OS_ERR * )&err);
  154. OSStart(&err);
  155. /* USER CODE END 2 */
  156. /* Infinite loop */
  157. /* USER CODE BEGIN WHILE */
  158. while (1)
  159. {
  160. /* USER CODE END WHILE */
  161. /* USER CODE BEGIN 3 */
  162. }
  163. /* USER CODE END 3 */
  164. }
  165. /**
  166. * @brief System Clock Configuration
  167. * @retval None
  168. */
  169. void SystemClock_Config(void)
  170. {
  171. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  172. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  173. /** Configure the main internal regulator output voltage
  174. */
  175. __HAL_RCC_PWR_CLK_ENABLE();
  176. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  177. /** Initializes the RCC Oscillators according to the specified parameters
  178. * in the RCC_OscInitTypeDef structure.
  179. */
  180. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  181. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  182. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  183. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  184. RCC_OscInitStruct.PLL.PLLM = 6;
  185. RCC_OscInitStruct.PLL.PLLN = 72;
  186. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  187. RCC_OscInitStruct.PLL.PLLQ = 4;
  188. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  189. {
  190. Error_Handler();
  191. }
  192. /** Initializes the CPU, AHB and APB buses clocks
  193. */
  194. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  195. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  196. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  197. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  198. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  199. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  200. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  201. {
  202. Error_Handler();
  203. }
  204. }
  205. /* USER CODE BEGIN 4 */
  206. /**
  207. * @brief 任务1,获取总脉冲数, 脉冲加减速
  208. * @return 无
  209. */
  210. void Y1Direction(void *p_arg)
  211. {
  212. p_arg = p_arg;
  213. OS_ERR err;
  214. while(1)
  215. {
  216. /* 脉冲加减速 */
  217. if (ArrFlag == 1)
  218. {
  219. if(TIM2->CNT < AccCount)
  220. {
  221. SetFrequency(0, PulseOutput[PrePulse].Frequency + TIM2->CNT * Acc);
  222. }
  223. else
  224. {
  225. SetFrequency(0, PulseOutput[NowPulse].Frequency);
  226. ArrFlag = 0;
  227. }
  228. }
  229. /* 获取总的脉冲数 */
  230. if(EndFlag || FullFlag)
  231. {
  232. if(EndFlag)
  233. {
  234. AllPulseCNT = GetBase(NowPulse) + PulseOutput[NowPulse].PulseCount;
  235. }
  236. if(FullFlag)
  237. {
  238. AllPulseCNT = MAX_Pulse;
  239. }
  240. }
  241. else
  242. {
  243. AllPulseCNT = TIM2->CNT + GetBase(NowPulse);
  244. }
  245. OSTimeDly(100, OS_OPT_TIME_DLY, &err); // 延时 100 个节拍
  246. }
  247. }
  248. /**
  249. * @brief 任务2,脉冲开始
  250. * @return 无
  251. */
  252. void PulseStartTsk(void *p_arg)
  253. {
  254. p_arg = p_arg;
  255. OS_ERR err;
  256. uint8_t EN = 1;
  257. while(1)
  258. {
  259. if(Register_L[3000] == 0x01 && EN == 1)
  260. {
  261. PulseStart();
  262. EN = 0;
  263. }
  264. else if(Register_L[3000] == 0x02 && EN == 0)
  265. {
  266. EN = 1;
  267. }
  268. OSTimeDly(100, OS_OPT_TIME_DLY, &err); // 延时 100 个节拍
  269. }
  270. }
  271. /**
  272. * @brief 任务3,脉冲方向输出
  273. * @return 无
  274. */
  275. void DirOutput(void *p_arg)
  276. {
  277. p_arg = p_arg;
  278. OS_ERR err;
  279. while(1)
  280. {
  281. if (Options.DirPost == 0)
  282. {
  283. if (Options.Dir == 1)
  284. {
  285. HAL_GPIO_WritePin(GPIOH, Y12_Pin, GPIO_PIN_SET);
  286. }
  287. else
  288. {
  289. HAL_GPIO_WritePin(GPIOH, Y12_Pin, GPIO_PIN_RESET);
  290. }
  291. }
  292. if (Options.DirPost == 1)
  293. {
  294. if (Options.Dir == 1)
  295. {
  296. HAL_GPIO_WritePin(GPIOH, Y13_Pin, GPIO_PIN_SET);
  297. }
  298. else
  299. {
  300. HAL_GPIO_WritePin(GPIOH, Y13_Pin, GPIO_PIN_RESET);
  301. }
  302. }
  303. if (Options.DirPost == 2)
  304. {
  305. if (Options.Dir == 1)
  306. {
  307. HAL_GPIO_WritePin(GPIOH, Y14_Pin, GPIO_PIN_SET);
  308. }
  309. else
  310. {
  311. HAL_GPIO_WritePin(GPIOH, Y14_Pin, GPIO_PIN_RESET);
  312. }
  313. }
  314. if (Options.DirPost == 3)
  315. {
  316. if (Options.Dir == 1)
  317. {
  318. HAL_GPIO_WritePin(GPIOH, Y15_Pin, GPIO_PIN_SET);
  319. }
  320. else
  321. {
  322. HAL_GPIO_WritePin(GPIOH, Y15_Pin, GPIO_PIN_RESET);
  323. }
  324. }
  325. OSTimeDly(100, OS_OPT_TIME_DLY, &err); // 延时 100 个节拍
  326. }
  327. }
  328. #if 0
  329. /**
  330. * @brief 定时器中断回调(PWM计数)
  331. * @return 无
  332. */
  333. void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
  334. {
  335. if (htim->Instance == TIM10)
  336. {
  337. PulseCount++;
  338. if (1 == Base.PulseMod) /* 如果是绝对模式 */
  339. {
  340. if (TIM2->CNT == Base.MAX_Pulse) /* 达到了最大脉冲数 */
  341. {
  342. HAL_TIM_PWM_Stop_IT(&htim10,TIM_CHANNEL_1); /* 停止PWM输出 */
  343. }
  344. else if(PulseCount == PulseOutput[NowPulse].PulseCount) /* 如果当前段的计数等于要求的计数 */
  345. {
  346. PulseCount = 0;
  347. HAL_TIM_PWM_Stop_IT(&htim10,TIM_CHANNEL_1); /* 停止PWM输出 */
  348. if(Base.PulseNum - NowPulse >= 1)
  349. {
  350. Base.PrePulse = NowPulse; /* 保存之前的脉冲段 */
  351. if(PulseOutput[NowPulse].NextPulse == 0)
  352. {
  353. NowPulse = NowPulse+ 1;
  354. }
  355. else
  356. {
  357. NowPulse = PulseOutput[NowPulse].NextPulse; /* 进入下一段脉冲 */
  358. }
  359. Acc = GetAcc(PulseOutput[Base.PrePulse].Frequency,
  360. PulseOutput[NowPulse].Frequency, Base.AccCount); /* 计算Acc */
  361. ArrFlag = 1;
  362. PulseStart(); //开始产生脉冲
  363. }
  364. }
  365. }
  366. else if (0 == Base.PulseMod) /* 如果是相对模式 */
  367. {
  368. if (PulseCount == PulseOutput[NowPulse].PulseCount) /* 如果当前段的计数等于要求的计数 */
  369. {
  370. PulseCount = 0;
  371. HAL_TIM_PWM_Stop_IT(&htim10,TIM_CHANNEL_1); /* 停止PWM输出 */
  372. if(Base.PulseNum - NowPulse > 1)
  373. {
  374. Base.PrePulse = NowPulse; /* 保存之前的脉冲段 */
  375. if(PulseOutput[NowPulse].NextPulse == 0)
  376. {
  377. NowPulse = NowPulse+ 1;
  378. }
  379. else
  380. {
  381. NowPulse = PulseOutput[NowPulse].NextPulse; /* 进入下一段脉冲 */
  382. }
  383. Acc = GetAcc(PulseOutput[Base.PrePulse].Frequency,
  384. PulseOutput[NowPulse].Frequency, Base.AccCount); /* 计算Acc */
  385. ArrFlag = 1;
  386. PulseStart(); //开始产生脉冲
  387. }
  388. else
  389. {
  390. PulseCount = 0;
  391. HAL_TIM_PWM_Stop_IT(&htim10,TIM_CHANNEL_1); /* 停止PWM输出 */
  392. }
  393. }
  394. }
  395. if (ArrFlag == 1)
  396. {
  397. if(PulseCount < Base.AccCount)
  398. {
  399. SetFrequency(0, PulseOutput[Base.PrePulse].Frequency + PulseCount * Acc);
  400. }
  401. else
  402. {
  403. SetFrequency(0, PulseOutput[NowPulse].Frequency);
  404. ArrFlag = 0;
  405. }
  406. }
  407. }
  408. }
  409. #endif
  410. /**
  411. * @brief 掉电中断
  412. * @return 无
  413. */
  414. void HAL_PWR_PVDCallback(void)
  415. {
  416. OSIntEnter(); /* 进入中断 */
  417. // 检查电压是否低于阈值
  418. if (__HAL_PWR_GET_FLAG(PWR_FLAG_PVDO))
  419. {
  420. /* 掉电处理内容 */
  421. PLSRSramSave();
  422. }
  423. OSIntExit(); /* 退出中断 */
  424. }
  425. /**
  426. * @brief PVD配置
  427. * @return 无
  428. */
  429. void PVD_Init(void)
  430. {
  431. PWR_PVDTypeDef PvdStruct;
  432. HAL_PWR_EnablePVD(); /* 使能PVD */
  433. PvdStruct.PVDLevel = PWR_PVDLEVEL_3; /* PVD阈值3.1V */
  434. PvdStruct.Mode = PWR_PVD_MODE_IT_RISING; /* 检测掉电 */
  435. HAL_PWR_ConfigPVD(&PvdStruct);
  436. HAL_NVIC_SetPriority(PVD_IRQn, 0, 0); /* 配置PVD中断优先级 */
  437. HAL_NVIC_EnableIRQ(PVD_IRQn); /* 使能PVD中断 */
  438. }
  439. /* USER CODE END 4 */
  440. /**
  441. * @brief This function is executed in case of error occurrence.
  442. * @retval None
  443. */
  444. void Error_Handler(void)
  445. {
  446. /* USER CODE BEGIN Error_Handler_Debug */
  447. /* User can add his own implementation to report the HAL error return state */
  448. __disable_irq();
  449. while (1)
  450. {
  451. }
  452. /* USER CODE END Error_Handler_Debug */
  453. }
  454. #ifdef USE_FULL_ASSERT
  455. /**
  456. * @brief Reports the name of the source file and the source line number
  457. * where the assert_param error has occurred.
  458. * @param file: pointer to the source file name
  459. * @param line: assert_param error line source number
  460. * @retval None
  461. */
  462. void assert_failed(uint8_t *file, uint32_t line)
  463. {
  464. /* USER CODE BEGIN 6 */
  465. /* User can add his own implementation to report the file name and line number,
  466. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  467. /* USER CODE END 6 */
  468. }
  469. #endif /* USE_FULL_ASSERT */