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

696 行
18 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. #include "bitset.h"
  31. /* USER CODE END Includes */
  32. /* Private typedef -----------------------------------------------------------*/
  33. /* USER CODE BEGIN PTD */
  34. uint8_t ArrFlag = 0; /* 加速度Flag */
  35. float Acc; /* 加速度 */
  36. double AccUp;
  37. double AccDown;
  38. int32_t AllPulseCNT = 0;
  39. int32_t BaseCNT = 0;
  40. int32_t CNT_Only[11] = {0};
  41. uint32_t N_Acc = 0;
  42. uint8_t EXT_Flag = 0;
  43. uint8_t PulseStartFlag = 0;
  44. uint8_t ArrTimes = 0;
  45. uint8_t FixArrFlag = 0;
  46. uint8_t PulseRuning = 0;
  47. extern uint8_t Register_H[16384]; ///<寄存器的高字节
  48. extern uint8_t Register_L[16384]; ///<寄存器的低字节
  49. /* USER CODE END PTD */
  50. /* Private define ------------------------------------------------------------*/
  51. /* USER CODE BEGIN PD */
  52. /* USER CODE END PD */
  53. /* Private macro -------------------------------------------------------------*/
  54. /* USER CODE BEGIN PM */
  55. /* USER CODE END PM */
  56. /* Private variables ---------------------------------------------------------*/
  57. /* USER CODE BEGIN PV */
  58. /* USER CODE END PV */
  59. /* Private function prototypes -----------------------------------------------*/
  60. void SystemClock_Config(void);
  61. /* USER CODE BEGIN PFP */
  62. /* USER CODE END PFP */
  63. /* Private user code ---------------------------------------------------------*/
  64. /* USER CODE BEGIN 0 */
  65. /* 任务优先级 */
  66. #define TASK_Y1Direction 6
  67. #define TASK_PulseStart 5
  68. #define TASK_DirOutput 6
  69. #define TASK_PulseGetCount 4
  70. #define TASK_EXTSet 6
  71. /* 任务堆栈大小 */
  72. #define Task_Y1Direction_SIZE 256
  73. #define Task_PulseStart_SIZE 512
  74. #define Task_DirOutput_SIZE 256
  75. #define Task_PulseGetCount_SIZE 256
  76. #define Task_EXTSet_SIZE 256
  77. /* 任务控制块 */
  78. OS_TCB Y1Direction_Tsk;
  79. OS_TCB PulseStart_Tsk;
  80. OS_TCB DirOutput_Tsk;
  81. OS_TCB PulseGetCount_Tsk;
  82. OS_TCB EXTSet_Tsk;
  83. /* 任务栈 */
  84. CPU_STK Task_Y1Direction_STK[Task_Y1Direction_SIZE];
  85. CPU_STK Task_PulseStart_STK[Task_PulseStart_SIZE];
  86. CPU_STK Task_DirOutput_STK[Task_DirOutput_SIZE];
  87. CPU_STK Task_PulseGetCount_STK[Task_PulseGetCount_SIZE];
  88. CPU_STK Task_EXTSet_STK[Task_EXTSet_SIZE];
  89. /* 任务函数 */
  90. void Y1Direction(void *p_arg);
  91. void PulseStartTsk(void *p_arg);
  92. void DirOutput(void *p_arg);
  93. void PulseGetCount(void *p_arg);
  94. void EXTSet(void *p_arg);
  95. /* USER CODE END 0 */
  96. /**
  97. * @brief The application entry point.
  98. * @retval int
  99. */
  100. int main(void)
  101. {
  102. /* USER CODE BEGIN 1 */
  103. OS_ERR err;
  104. /* USER CODE END 1 */
  105. /* MCU Configuration--------------------------------------------------------*/
  106. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  107. HAL_Init();
  108. /* USER CODE BEGIN Init */
  109. /* USER CODE END Init */
  110. /* Configure the system clock */
  111. SystemClock_Config();
  112. /* USER CODE BEGIN SysInit */
  113. HAL_PWR_EnableBkUpAccess();/* 使能备份域访问 */
  114. __HAL_RCC_BKPSRAM_CLK_ENABLE();/* 使能备份SRAM时钟 */
  115. HAL_PWREx_EnableBkUpReg();/* 使能备份SRAM */
  116. /* USER CODE END SysInit */
  117. /* Initialize all configured peripherals */
  118. MX_GPIO_Init();
  119. MX_DMA_Init();
  120. MX_USART1_UART_Init();
  121. MX_TIM10_Init();
  122. MX_TIM2_Init();
  123. MX_TIM11_Init();
  124. MX_TIM13_Init();
  125. MX_TIM14_Init();
  126. MX_TIM3_Init();
  127. /* USER CODE BEGIN 2 */
  128. BSP_Init();
  129. OSInit(&err);
  130. CPU_SR_ALLOC();
  131. CPU_CRITICAL_ENTER();///进入临界区
  132. /* 任务1 */
  133. OSTaskCreate((OS_TCB * )&Y1Direction_Tsk, /* 任务控制块 */
  134. (CPU_CHAR* )"Y1Direction", /* 任务名字 */
  135. (OS_TASK_PTR)Y1Direction, /* 任务函数 */
  136. (void * )0, /* 传递给任务函数的参数 */
  137. (OS_PRIO )TASK_Y1Direction, /* 任务优先级 */
  138. (CPU_STK * )&Task_Y1Direction_STK[0], /* 任务堆栈基地址 */
  139. (CPU_STK_SIZE)Task_Y1Direction_SIZE/10, /* 任务堆栈深度限位 */
  140. (CPU_STK_SIZE)Task_Y1Direction_SIZE, /* 任务堆栈大小 */
  141. (OS_MSG_QTY)0,/* 任务内部消息队列能够接收的最大消息数目,为0时禁止接收消息 */
  142. (OS_TICK )0,/* 当使能时间片轮转时的时间片长度,为0时为默认长度 */
  143. (void * )0,/* 用户补充的存储区 */
  144. (OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR,/* 任务选项 */
  145. (OS_ERR * )&err);/* 存放该函数错误时的返回值 */
  146. /* 任务2 */
  147. OSTaskCreate((OS_TCB * )&PulseStart_Tsk,
  148. (CPU_CHAR* )"PulseStartTsk",
  149. (OS_TASK_PTR)PulseStartTsk,
  150. (void * )0,
  151. (OS_PRIO )TASK_PulseStart,
  152. (CPU_STK * )&Task_PulseStart_STK[0],
  153. (CPU_STK_SIZE)Task_PulseStart_SIZE/10,
  154. (CPU_STK_SIZE)Task_PulseStart_SIZE,
  155. (OS_MSG_QTY)0,
  156. (OS_TICK )0,
  157. (void * )0,
  158. (OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR,
  159. (OS_ERR * )&err);
  160. /* 任务3 */
  161. OSTaskCreate((OS_TCB * )&DirOutput_Tsk,
  162. (CPU_CHAR* )"DirOutput_Tsk",
  163. (OS_TASK_PTR)DirOutput,
  164. (void * )0,
  165. (OS_PRIO )TASK_DirOutput,
  166. (CPU_STK * )&Task_DirOutput_STK[0],
  167. (CPU_STK_SIZE)Task_DirOutput_SIZE/10,
  168. (CPU_STK_SIZE)Task_DirOutput_SIZE,
  169. (OS_MSG_QTY)0,
  170. (OS_TICK )0,
  171. (void * )0,
  172. (OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR,
  173. (OS_ERR * )&err);
  174. /* 任务4 */
  175. OSTaskCreate((OS_TCB * )&PulseGetCount_Tsk,
  176. (CPU_CHAR* )"PulseGetCount_Tsk",
  177. (OS_TASK_PTR)PulseGetCount,
  178. (void * )0,
  179. (OS_PRIO )TASK_PulseGetCount,
  180. (CPU_STK * )&Task_PulseGetCount_STK[0],
  181. (CPU_STK_SIZE)Task_PulseGetCount_SIZE/10,
  182. (CPU_STK_SIZE)Task_PulseGetCount_SIZE,
  183. (OS_MSG_QTY)0,
  184. (OS_TICK )0,
  185. (void * )0,
  186. (OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR,
  187. (OS_ERR * )&err);
  188. /* 任务5 */
  189. OSTaskCreate((OS_TCB * )&EXTSet_Tsk,
  190. (CPU_CHAR* )"EXTSet_Tsk",
  191. (OS_TASK_PTR)EXTSet,
  192. (void * )0,
  193. (OS_PRIO )TASK_EXTSet,
  194. (CPU_STK * )&Task_EXTSet_STK[0],
  195. (CPU_STK_SIZE)Task_EXTSet_SIZE/10,
  196. (CPU_STK_SIZE)Task_EXTSet_SIZE,
  197. (OS_MSG_QTY)0,
  198. (OS_TICK )0,
  199. (void * )0,
  200. (OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR,
  201. (OS_ERR * )&err);
  202. /* 掉电数据读取 */
  203. ModbusLoadSRAM();
  204. PLSRPluseLoad(); //读取脉冲设置
  205. PLSROptionLoad(); //读取脉冲基础设置
  206. AccUp = (double)(Options.InitSpeed / Options.AccUpTime);
  207. AccDown = - (double)(Options.InitSpeed / Options.AccDownTime);
  208. NowFrequency = PulseOutput[Options.StartPulse].Frequency;
  209. NowPulse = Options.StartPulse;
  210. N_Acc = PulseOutput[Options.StartPulse].Frequency / 2000 * Options.AccDownTime;
  211. CPU_CRITICAL_EXIT() ; ///退出临界区
  212. OSStart(&err);
  213. /* USER CODE END 2 */
  214. /* Infinite loop */
  215. /* USER CODE BEGIN WHILE */
  216. while (1)
  217. {
  218. /* USER CODE END WHILE */
  219. /* USER CODE BEGIN 3 */
  220. }
  221. /* USER CODE END 3 */
  222. }
  223. /**
  224. * @brief System Clock Configuration
  225. * @retval None
  226. */
  227. void SystemClock_Config(void)
  228. {
  229. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  230. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  231. /** Configure the main internal regulator output voltage
  232. */
  233. __HAL_RCC_PWR_CLK_ENABLE();
  234. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  235. /** Initializes the RCC Oscillators according to the specified parameters
  236. * in the RCC_OscInitTypeDef structure.
  237. */
  238. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  239. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  240. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  241. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  242. RCC_OscInitStruct.PLL.PLLM = 6;
  243. RCC_OscInitStruct.PLL.PLLN = 72;
  244. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  245. RCC_OscInitStruct.PLL.PLLQ = 4;
  246. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  247. {
  248. Error_Handler();
  249. }
  250. /** Initializes the CPU, AHB and APB buses clocks
  251. */
  252. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  253. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  254. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  255. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  256. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  257. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  258. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  259. {
  260. Error_Handler();
  261. }
  262. }
  263. /* USER CODE BEGIN 4 */
  264. /**
  265. * @brief 任务1, 脉冲加减速
  266. * @return 无
  267. */
  268. void Y1Direction(void *p_arg)
  269. {
  270. p_arg = p_arg;
  271. OS_ERR err;
  272. while(1)
  273. {
  274. if (NowFrequency != PulseOutput[NowPulse].Frequency && PulseRuning == 1)/* 如果现在寄存器内的频率与实际输出的频率不一致 */
  275. {
  276. FixArrFlag = 1; //使能频率修改脉冲加减速
  277. HAL_TIM_Base_Start_IT(&htim3); /* 启动定时器3和中断 */
  278. }
  279. /* 计算加速度(斜率) */
  280. AccUp = (double)(Options.InitSpeed / Options.AccUpTime);
  281. AccDown = - (double)(Options.InitSpeed / Options.AccDownTime);
  282. /* 计算当前段脉冲减速的脉冲数 */
  283. N_Acc = PulseOutput[NowPulse].Frequency / 2000 * Options.AccDownTime;
  284. /* 挂起指定的时钟节拍 */
  285. OSTimeDly(100, OS_OPT_TIME_DLY, &err);
  286. }
  287. }
  288. /**
  289. * @brief 任务2,脉冲开始,数据处理
  290. * @return 无
  291. */
  292. void PulseStartTsk(void *p_arg)
  293. {
  294. p_arg = p_arg;
  295. OS_ERR err;
  296. uint8_t EN = 1;
  297. while(1)
  298. {
  299. PLSRPluseLoad(); //读取脉冲设置
  300. PLSROptionLoad(); //读取脉冲基础设置
  301. if (Register_L[0x3000] == 0x01 && EN == 1)
  302. {
  303. EN = 0;
  304. PulseStartFlag = 1; /* 脉冲第一段开始标志位 */
  305. PulseRuning = 1; /* 脉冲进行中标志位 */
  306. NowFrequency = PulseOutput[NowPulse].Frequency; /* 保存当前脉冲的频率 */
  307. HAL_TIM_Base_Start_IT(&htim3); /* 启动定时器3和中断 */
  308. }
  309. else if (Register_L[0x3000] == 0x02 && EN == 0)
  310. {
  311. EN = 1;
  312. }
  313. ModbusSaveSRAM();
  314. OSTimeDly(10, OS_OPT_TIME_DLY, &err); // 延时 10 个节拍
  315. }
  316. }
  317. /**
  318. * @brief 任务3,脉冲方向输出
  319. * @return 无
  320. */
  321. void DirOutput(void *p_arg)
  322. {
  323. p_arg = p_arg;
  324. OS_ERR err;
  325. while(1)
  326. {
  327. /* 根据当前的方向和输出的方向的端口进行输出的配置 */
  328. if (Options.DirPost == 0)
  329. {
  330. if (Options.Dir == 1)
  331. {
  332. HAL_GPIO_WritePin(GPIOH, Y12_Pin, GPIO_PIN_SET);
  333. }
  334. else
  335. {
  336. HAL_GPIO_WritePin(GPIOH, Y12_Pin, GPIO_PIN_RESET);
  337. }
  338. }
  339. if (Options.DirPost == 1)
  340. {
  341. if (Options.Dir == 1)
  342. {
  343. HAL_GPIO_WritePin(GPIOH, Y13_Pin, GPIO_PIN_SET);
  344. }
  345. else
  346. {
  347. HAL_GPIO_WritePin(GPIOH, Y13_Pin, GPIO_PIN_RESET);
  348. }
  349. }
  350. if (Options.DirPost == 2)
  351. {
  352. if (Options.Dir == 1)
  353. {
  354. HAL_GPIO_WritePin(GPIOH, Y14_Pin, GPIO_PIN_SET);
  355. }
  356. else
  357. {
  358. HAL_GPIO_WritePin(GPIOH, Y14_Pin, GPIO_PIN_RESET);
  359. }
  360. }
  361. if (Options.DirPost == 3)
  362. {
  363. if (Options.Dir == 1)
  364. {
  365. HAL_GPIO_WritePin(GPIOH, Y15_Pin, GPIO_PIN_SET);
  366. }
  367. else
  368. {
  369. HAL_GPIO_WritePin(GPIOH, Y15_Pin, GPIO_PIN_RESET);
  370. }
  371. }
  372. /* 预留时间给其他任务 */
  373. OSTimeDly(100, OS_OPT_TIME_DLY, &err); // 延时 100 个节拍
  374. }
  375. }
  376. /**
  377. * @brief 任务4,脉冲计数
  378. * @return 无
  379. */
  380. void PulseGetCount(void *p_arg)
  381. {
  382. p_arg = p_arg;
  383. OS_ERR err;
  384. while(1)
  385. {
  386. /* 根据当前脉冲的方向设置输出的方向 */
  387. if(PulseOutput[NowPulse].PulseCount < 0)
  388. {
  389. Options.Dir = 1;
  390. }
  391. else
  392. {
  393. Options.Dir = 0;
  394. }
  395. if (TIM2->CNT != 0) CNT_Only[NowPulse] = TIM2->CNT;
  396. AllPulseCNT = CNT_Only[0] + CNT_Only[1] + CNT_Only[2] + CNT_Only[3] +
  397. CNT_Only[4] + CNT_Only[5] + CNT_Only[6] + CNT_Only[7] +
  398. CNT_Only[8] + CNT_Only[9];
  399. CountSave();
  400. OSTimeDly(10, OS_OPT_TIME_DLY, &err);
  401. }
  402. }
  403. /**
  404. * @brief 任务5,EXT
  405. * @return 无
  406. */
  407. void EXTSet(void *p_arg)
  408. {
  409. p_arg = p_arg;
  410. OS_ERR err;
  411. uint8_t X4_Sta, X5_Sta;
  412. while(1)
  413. {
  414. if(HAL_GPIO_ReadPin(GPIOB, X4_Pin) == GPIO_PIN_SET)
  415. {
  416. OSTimeDly(10, OS_OPT_TIME_DLY, &err);
  417. if(HAL_GPIO_ReadPin(GPIOB, X4_Pin) == GPIO_PIN_SET)
  418. {
  419. X4_Sta = 1;
  420. while(HAL_GPIO_ReadPin(GPIOB, X4_Pin) == GPIO_PIN_SET);
  421. }
  422. }
  423. if(HAL_GPIO_ReadPin(GPIOG, X5_Pin) == GPIO_PIN_SET)
  424. {
  425. OSTimeDly(10, OS_OPT_TIME_DLY, &err);
  426. if(HAL_GPIO_ReadPin(GPIOG, X5_Pin) == GPIO_PIN_SET)
  427. {
  428. X5_Sta = 1;
  429. while(HAL_GPIO_ReadPin(GPIOG, X5_Pin) == GPIO_PIN_SET);
  430. }
  431. }
  432. /* X4引脚的EXT信号 */
  433. if(X4_Sta == 1 && Options.EXT == 0 && PulseOutput[NowPulse].EXT == 1)
  434. {
  435. if (TIM2->CNT != 0) CNT_Only[NowPulse] = TIM2->CNT; /* 保存当前的脉冲计数 */
  436. TIM2->CNT = TIM2->ARR - 1;
  437. EXT_Flag = 1;
  438. X4_Sta = 0;
  439. }
  440. else
  441. {
  442. X4_Sta = 0;
  443. }
  444. /* X5引脚的EXT信号 */
  445. if(X5_Sta == 1 && Options.EXT == 1 && PulseOutput[NowPulse].EXT == 1)
  446. {
  447. if (TIM2->CNT != 0) CNT_Only[NowPulse] = TIM2->CNT; /* 保存当前的脉冲计数 */
  448. TIM2->CNT = TIM2->ARR - 1;
  449. EXT_Flag = 1;
  450. X5_Sta = 0;
  451. }
  452. else
  453. {
  454. X5_Sta = 0;
  455. }
  456. if(EXT_Flag == 1)
  457. {
  458. EXT_Flag = 0;
  459. HAL_TIM_GenerateEvent(&htim2, TIM_EVENTSOURCE_UPDATE);
  460. }
  461. OSTimeDly(100, OS_OPT_TIME_DLY, &err);
  462. }
  463. }
  464. /**
  465. * @brief 定时器中断回调函数,用来实现脉冲加减速(定时时间1ms)
  466. * @return 无
  467. */
  468. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  469. {
  470. OSIntEnter(); /* 进入中断 */
  471. if (htim == (&htim3))
  472. {
  473. /* 段切换的加减速 */
  474. if (ArrFlag == 1 && NowPulse != 10)
  475. {
  476. /* 脉冲加速 */
  477. if (PulseOutput[NowPulse].Frequency > PulseOutput[PrePulse].Frequency) /* 如果当前的脉冲频率大于之前的脉冲频率 */
  478. {
  479. if (ArrTimes <= (int32_t)(PulseOutput[NowPulse].Frequency - PulseOutput[PrePulse].Frequency) / AccUp)
  480. {
  481. SetFrequency(Options.SentPost, PulseOutput[PrePulse].Frequency + ArrTimes * AccUp);
  482. if(ArrTimes == 0) PulseStart(); /* 开始产生脉冲 */
  483. ArrTimes++;
  484. }
  485. else
  486. {
  487. SetFrequency(Options.SentPost, PulseOutput[NowPulse].Frequency);
  488. ArrTimes = 0;
  489. ArrFlag = 0;
  490. HAL_TIM_Base_Stop_IT(&htim3); // 停止定时器并禁用中断
  491. }
  492. }
  493. /* 脉冲减速 */
  494. else if (PulseOutput[NowPulse].Frequency <= PulseOutput[PrePulse].Frequency) /* 如果当前的脉冲频率小于之前的脉冲频率 */
  495. {
  496. if (ArrTimes <= (int32_t)(PulseOutput[NowPulse].Frequency - PulseOutput[PrePulse].Frequency) / AccDown)
  497. {
  498. SetFrequency(Options.SentPost, PulseOutput[PrePulse].Frequency + ArrTimes * AccDown);
  499. if (ArrTimes == 0) PulseStart(); /* 开始产生脉冲 */
  500. ArrTimes++;
  501. }
  502. else
  503. {
  504. SetFrequency(Options.SentPost, PulseOutput[NowPulse].Frequency);
  505. ArrTimes = 0;
  506. ArrFlag = 0;
  507. HAL_TIM_Base_Stop_IT(&htim3); // 停止定时器并禁用中断
  508. }
  509. }
  510. }
  511. /* 实时修改频率的加减速 */
  512. if (FixArrFlag == 1 && NowPulse != 10)
  513. {
  514. /* 脉冲加速 */
  515. if (PulseOutput[NowPulse].Frequency > NowFrequency)
  516. {
  517. if (ArrTimes <= Options.AccUpTime)
  518. {
  519. SetFrequency(Options.SentPost, NowFrequency + ArrTimes * AccUp);
  520. if(ArrTimes == 0) PulseStart(); /* 开始产生脉冲 */
  521. ArrTimes++;
  522. }
  523. else
  524. {
  525. SetFrequency(Options.SentPost, PulseOutput[NowPulse].Frequency);
  526. ArrTimes = 0;
  527. FixArrFlag = 0;
  528. HAL_TIM_Base_Stop_IT(&htim3); // 停止定时器并禁用中断
  529. }
  530. }
  531. /* 脉冲减速 */
  532. else if (PulseOutput[NowPulse].Frequency <= NowFrequency)
  533. {
  534. if (ArrTimes <= Options.AccDownTime)
  535. {
  536. SetFrequency(Options.SentPost, NowFrequency + ArrTimes * AccDown);
  537. if (ArrTimes == 0) PulseStart(); /* 开始产生脉冲 */
  538. ArrTimes++;
  539. }
  540. else
  541. {
  542. SetFrequency(Options.SentPost, PulseOutput[NowPulse].Frequency);
  543. ArrTimes = 0;
  544. FixArrFlag = 0;
  545. HAL_TIM_Base_Stop_IT(&htim3); // 停止定时器并禁用中断
  546. }
  547. }
  548. }
  549. /* 第一段脉冲的加速 */
  550. if (NowPulse == Options.StartPulse && PulseStartFlag == 1)
  551. {
  552. if (ArrTimes < PulseOutput[NowPulse].Frequency / AccUp)
  553. {
  554. ArrTimes++;
  555. SetFrequency(Options.SentPost, ArrTimes * AccUp);
  556. if (ArrTimes == 1) PulseStart(); /* 开始产生脉冲 */
  557. }
  558. else
  559. {
  560. SetFrequency(Options.SentPost, PulseOutput[NowPulse].Frequency);
  561. HAL_TIM_Base_Stop_IT(&htim3); // 停止定时器并禁用中断
  562. PulseStartFlag = 0;
  563. ArrTimes = 0;
  564. }
  565. }
  566. /* 最后一段脉冲的减速 */
  567. if (PulseRuning == 0)
  568. {
  569. if (ArrTimes <= PulseOutput[PrePulse].Frequency / (- AccDown))
  570. {
  571. SetFrequency(Options.SentPost, PulseOutput[PrePulse].Frequency + ArrTimes * AccDown);
  572. if (ArrTimes == 0) PulseStart(); /* 开始产生脉冲 */
  573. ArrTimes++;
  574. }
  575. else
  576. {
  577. ArrTimes = 0;
  578. FixArrFlag = 0;
  579. HAL_TIM_PWM_Stop_IT(&htim10,TIM_CHANNEL_1); /* 停止PWM输出 */
  580. HAL_TIM_Base_Stop_IT(&htim3); // 停止定时器并禁用中断
  581. }
  582. }
  583. }
  584. OSIntExit(); /* 退出中断 */
  585. }
  586. /* USER CODE END 4 */
  587. /**
  588. * @brief This function is executed in case of error occurrence.
  589. * @retval None
  590. */
  591. void Error_Handler(void)
  592. {
  593. /* USER CODE BEGIN Error_Handler_Debug */
  594. /* User can add his own implementation to report the HAL error return state */
  595. __disable_irq();
  596. while (1)
  597. {
  598. }
  599. /* USER CODE END Error_Handler_Debug */
  600. }
  601. #ifdef USE_FULL_ASSERT
  602. /**
  603. * @brief Reports the name of the source file and the source line number
  604. * where the assert_param error has occurred.
  605. * @param file: pointer to the source file name
  606. * @param line: assert_param error line source number
  607. * @retval None
  608. */
  609. void assert_failed(uint8_t *file, uint32_t line)
  610. {
  611. /* USER CODE BEGIN 6 */
  612. /* User can add his own implementation to report the file name and line number,
  613. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  614. /* USER CODE END 6 */
  615. }
  616. #endif /* USE_FULL_ASSERT */