训练营PLSR题目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

693 line
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 * Options.AccDownTime / 2000;
  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. OSTimeDly(100, OS_OPT_TIME_DLY, &err);
  284. }
  285. }
  286. /**
  287. * @brief 任务2,脉冲开始,数据处理
  288. * @return 无
  289. */
  290. void PulseStartTsk(void *p_arg)
  291. {
  292. p_arg = p_arg;
  293. OS_ERR err;
  294. uint8_t EN = 1;
  295. while(1)
  296. {
  297. PLSRPluseLoad(); //读取脉冲设置
  298. PLSROptionLoad(); //读取脉冲基础设置
  299. if (Register_L[0x3000] == 0x01 && EN == 1)
  300. {
  301. EN = 0;
  302. PulseStartFlag = 1; /* 脉冲第一段开始标志位 */
  303. PulseRuning = 1; /* 脉冲进行中标志位 */
  304. NowFrequency = PulseOutput[NowPulse].Frequency; /* 保存当前脉冲的频率 */
  305. HAL_TIM_Base_Start_IT(&htim3); /* 启动定时器3和中断 */
  306. }
  307. else if (Register_L[0x3000] == 0x02 && EN == 0)
  308. {
  309. EN = 1;
  310. }
  311. ModbusSaveSRAM();
  312. OSTimeDly(10, OS_OPT_TIME_DLY, &err); // 延时 10 个节拍
  313. }
  314. }
  315. /**
  316. * @brief 任务3,脉冲方向输出
  317. * @return 无
  318. */
  319. void DirOutput(void *p_arg)
  320. {
  321. p_arg = p_arg;
  322. OS_ERR err;
  323. while(1)
  324. {
  325. /* 根据当前的方向和输出的方向的端口进行输出的配置 */
  326. if (Options.DirPost == 0)
  327. {
  328. if (Options.Dir == 1)
  329. {
  330. HAL_GPIO_WritePin(GPIOH, Y12_Pin, GPIO_PIN_SET);
  331. }
  332. else
  333. {
  334. HAL_GPIO_WritePin(GPIOH, Y12_Pin, GPIO_PIN_RESET);
  335. }
  336. }
  337. if (Options.DirPost == 1)
  338. {
  339. if (Options.Dir == 1)
  340. {
  341. HAL_GPIO_WritePin(GPIOH, Y13_Pin, GPIO_PIN_SET);
  342. }
  343. else
  344. {
  345. HAL_GPIO_WritePin(GPIOH, Y13_Pin, GPIO_PIN_RESET);
  346. }
  347. }
  348. if (Options.DirPost == 2)
  349. {
  350. if (Options.Dir == 1)
  351. {
  352. HAL_GPIO_WritePin(GPIOH, Y14_Pin, GPIO_PIN_SET);
  353. }
  354. else
  355. {
  356. HAL_GPIO_WritePin(GPIOH, Y14_Pin, GPIO_PIN_RESET);
  357. }
  358. }
  359. if (Options.DirPost == 3)
  360. {
  361. if (Options.Dir == 1)
  362. {
  363. HAL_GPIO_WritePin(GPIOH, Y15_Pin, GPIO_PIN_SET);
  364. }
  365. else
  366. {
  367. HAL_GPIO_WritePin(GPIOH, Y15_Pin, GPIO_PIN_RESET);
  368. }
  369. }
  370. /* 预留时间给其他任务 */
  371. OSTimeDly(100, OS_OPT_TIME_DLY, &err); // 延时 100 个节拍
  372. }
  373. }
  374. /**
  375. * @brief 任务4,脉冲计数
  376. * @return 无
  377. */
  378. void PulseGetCount(void *p_arg)
  379. {
  380. p_arg = p_arg;
  381. OS_ERR err;
  382. while(1)
  383. {
  384. /* 根据当前脉冲的方向设置输出的方向 */
  385. if(PulseOutput[NowPulse].PulseCount < 0)
  386. {
  387. Options.Dir = 1;
  388. }
  389. else
  390. {
  391. Options.Dir = 0;
  392. }
  393. if (TIM2->CNT != 0) CNT_Only[NowPulse] = TIM2->CNT;
  394. AllPulseCNT = CNT_Only[0] + CNT_Only[1] + CNT_Only[2] + CNT_Only[3] +
  395. CNT_Only[4] + CNT_Only[5] + CNT_Only[6] + CNT_Only[7] +
  396. CNT_Only[8] + CNT_Only[9] + CNT_Only[10];
  397. CountSave();
  398. OSTimeDly(10, OS_OPT_TIME_DLY, &err);
  399. }
  400. }
  401. /**
  402. * @brief 任务5,EXT
  403. * @return 无
  404. */
  405. void EXTSet(void *p_arg)
  406. {
  407. p_arg = p_arg;
  408. OS_ERR err;
  409. uint8_t X4_Sta, X5_Sta;
  410. while(1)
  411. {
  412. if(HAL_GPIO_ReadPin(GPIOB, X4_Pin) == GPIO_PIN_SET)
  413. {
  414. OSTimeDly(10, OS_OPT_TIME_DLY, &err);
  415. if(HAL_GPIO_ReadPin(GPIOB, X4_Pin) == GPIO_PIN_SET)
  416. {
  417. X4_Sta = 1;
  418. while(HAL_GPIO_ReadPin(GPIOB, X4_Pin) == GPIO_PIN_SET);
  419. }
  420. }
  421. if(HAL_GPIO_ReadPin(GPIOG, X5_Pin) == GPIO_PIN_SET)
  422. {
  423. OSTimeDly(10, OS_OPT_TIME_DLY, &err);
  424. if(HAL_GPIO_ReadPin(GPIOG, X5_Pin) == GPIO_PIN_SET)
  425. {
  426. X5_Sta = 1;
  427. while(HAL_GPIO_ReadPin(GPIOG, X5_Pin) == GPIO_PIN_SET);
  428. }
  429. }
  430. /* X4引脚的EXT信号 */
  431. if(X4_Sta == 1 && Options.EXT == 0 && PulseOutput[NowPulse].EXT == 1)
  432. {
  433. if (TIM2->CNT != 0) CNT_Only[NowPulse] = TIM2->CNT; /* 保存当前的脉冲计数 */
  434. TIM2->CNT = TIM2->ARR - 1;
  435. EXT_Flag = 1;
  436. X4_Sta = 0;
  437. }
  438. else
  439. {
  440. X4_Sta = 0;
  441. }
  442. /* X5引脚的EXT信号 */
  443. if(X5_Sta == 1 && Options.EXT == 1 && PulseOutput[NowPulse].EXT == 1)
  444. {
  445. if (TIM2->CNT != 0) CNT_Only[NowPulse] = TIM2->CNT; /* 保存当前的脉冲计数 */
  446. TIM2->CNT = TIM2->ARR - 1;
  447. EXT_Flag = 1;
  448. X5_Sta = 0;
  449. }
  450. else
  451. {
  452. X5_Sta = 0;
  453. }
  454. if(EXT_Flag == 1)
  455. {
  456. EXT_Flag = 0;
  457. HAL_TIM_GenerateEvent(&htim2, TIM_EVENTSOURCE_UPDATE);
  458. }
  459. OSTimeDly(100, OS_OPT_TIME_DLY, &err);
  460. }
  461. }
  462. /**
  463. * @brief 定时器中断回调函数,用来实现脉冲加减速(定时时间1ms)
  464. * @return 无
  465. */
  466. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  467. {
  468. OSIntEnter(); /* 进入中断 */
  469. if (htim == (&htim3))
  470. {
  471. /* 段切换的加减速 */
  472. if (ArrFlag == 1 && NowPulse != 10)
  473. {
  474. /* 脉冲加速 */
  475. if (PulseOutput[NowPulse].Frequency > PulseOutput[PrePulse].Frequency) /* 如果当前的脉冲频率大于之前的脉冲频率 */
  476. {
  477. if (ArrTimes <= (int32_t)(PulseOutput[NowPulse].Frequency - PulseOutput[PrePulse].Frequency) / AccUp)
  478. {
  479. SetFrequency(Options.SentPost, PulseOutput[PrePulse].Frequency + ArrTimes * AccUp);
  480. ArrTimes++;
  481. }
  482. else
  483. {
  484. SetFrequency(Options.SentPost, PulseOutput[NowPulse].Frequency);
  485. ArrTimes = 0;
  486. ArrFlag = 0;
  487. HAL_TIM_Base_Stop_IT(&htim3); // 停止定时器并禁用中断
  488. }
  489. }
  490. /* 脉冲减速 */
  491. else if (PulseOutput[NowPulse].Frequency <= PulseOutput[PrePulse].Frequency) /* 如果当前的脉冲频率小于之前的脉冲频率 */
  492. {
  493. if (ArrTimes <= (int32_t)(PulseOutput[NowPulse].Frequency - PulseOutput[PrePulse].Frequency) / AccDown)
  494. {
  495. SetFrequency(Options.SentPost, PulseOutput[PrePulse].Frequency + ArrTimes * AccDown);
  496. // if (ArrTimes == 0) PulseStart(); /* 开始产生脉冲 */
  497. ArrTimes++;
  498. }
  499. else
  500. {
  501. SetFrequency(Options.SentPost, PulseOutput[NowPulse].Frequency);
  502. ArrTimes = 0;
  503. ArrFlag = 0;
  504. HAL_TIM_Base_Stop_IT(&htim3); // 停止定时器并禁用中断
  505. }
  506. }
  507. }
  508. /* 实时修改频率的加减速 */
  509. if (FixArrFlag == 1 && NowPulse != 10)
  510. {
  511. /* 脉冲加速 */
  512. if (PulseOutput[NowPulse].Frequency > NowFrequency)
  513. {
  514. if (ArrTimes <= Options.AccUpTime)
  515. {
  516. SetFrequency(Options.SentPost, NowFrequency + ArrTimes * AccUp);
  517. // if(ArrTimes == 0) PulseStart(); /* 开始产生脉冲 */
  518. ArrTimes++;
  519. }
  520. else
  521. {
  522. SetFrequency(Options.SentPost, PulseOutput[NowPulse].Frequency);
  523. ArrTimes = 0;
  524. FixArrFlag = 0;
  525. HAL_TIM_Base_Stop_IT(&htim3); // 停止定时器并禁用中断
  526. }
  527. }
  528. /* 脉冲减速 */
  529. else if (PulseOutput[NowPulse].Frequency <= NowFrequency)
  530. {
  531. if (ArrTimes <= Options.AccDownTime)
  532. {
  533. SetFrequency(Options.SentPost, NowFrequency + ArrTimes * AccDown);
  534. // if (ArrTimes == 0) PulseStart(); /* 开始产生脉冲 */
  535. ArrTimes++;
  536. }
  537. else
  538. {
  539. SetFrequency(Options.SentPost, PulseOutput[NowPulse].Frequency);
  540. ArrTimes = 0;
  541. FixArrFlag = 0;
  542. HAL_TIM_Base_Stop_IT(&htim3); // 停止定时器并禁用中断
  543. }
  544. }
  545. }
  546. /* 第一段脉冲的加速 */
  547. if (NowPulse == Options.StartPulse && PulseStartFlag == 1)
  548. {
  549. if (ArrTimes <= PulseOutput[Options.StartPulse].Frequency / AccUp)
  550. {
  551. ArrTimes++;
  552. SetFrequency(Options.SentPost, ArrTimes * AccUp);
  553. if (ArrTimes == 1) PulseStart(); /* 开始产生脉冲 */
  554. }
  555. else
  556. {
  557. SetFrequency(Options.SentPost, PulseOutput[NowPulse].Frequency);
  558. PulseStartFlag = 0;
  559. ArrTimes = 0;
  560. HAL_TIM_Base_Stop_IT(&htim3); // 停止定时器并禁用中断
  561. }
  562. }
  563. /* 最后一段脉冲的减速 */
  564. if (PulseRuning == 0 && NowPulse == 10)
  565. {
  566. if (ArrTimes < PulseOutput[PrePulse].Frequency / (- AccDown))
  567. {
  568. SetFrequency(Options.SentPost, PulseOutput[PrePulse].Frequency + ArrTimes * AccDown);
  569. // if (ArrTimes == 0) PulseStart(); /* 开始产生脉冲 */
  570. ArrTimes++;
  571. }
  572. else
  573. {
  574. ArrTimes = 0;
  575. FixArrFlag = 0;
  576. HAL_TIM_PWM_Stop_IT(&htim10,TIM_CHANNEL_1); /* 停止PWM输出 */
  577. HAL_TIM_Base_Stop_IT(&htim3); // 停止定时器并禁用中断
  578. }
  579. }
  580. }
  581. OSIntExit(); /* 退出中断 */
  582. }
  583. /* USER CODE END 4 */
  584. /**
  585. * @brief This function is executed in case of error occurrence.
  586. * @retval None
  587. */
  588. void Error_Handler(void)
  589. {
  590. /* USER CODE BEGIN Error_Handler_Debug */
  591. /* User can add his own implementation to report the HAL error return state */
  592. __disable_irq();
  593. while (1)
  594. {
  595. }
  596. /* USER CODE END Error_Handler_Debug */
  597. }
  598. #ifdef USE_FULL_ASSERT
  599. /**
  600. * @brief Reports the name of the source file and the source line number
  601. * where the assert_param error has occurred.
  602. * @param file: pointer to the source file name
  603. * @param line: assert_param error line source number
  604. * @retval None
  605. */
  606. void assert_failed(uint8_t *file, uint32_t line)
  607. {
  608. /* USER CODE BEGIN 6 */
  609. /* User can add his own implementation to report the file name and line number,
  610. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  611. /* USER CODE END 6 */
  612. }
  613. #endif /* USE_FULL_ASSERT */