| @@ -1468,8 +1468,7 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) | |||
| { | |||
| // TIM2中断:负责段切换逻辑 | |||
| if(htim->Instance == TIM2) | |||
| { | |||
| { | |||
| // 立即停止PWM输出,防止多发脉冲 | |||
| PLSR_PWM_Stop(); | |||
| // 精确累加当前段已发送的脉冲数 | |||
| @@ -1477,8 +1476,12 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) | |||
| AllPluse += current_section_pulses; | |||
| g_plsr_route.pulse_count = AllPluse; | |||
| PLSR_UpdateGlobalPulseCount(AllPluse); | |||
| // 当前段脉冲发送完毕,进入等待状态 | |||
| g_plsr_route.run_state = PLSR_STATE_WAIT; | |||
| /*如果当前段不是最后一段且,等待条件为脉冲发送完成*/ | |||
| if(g_plsr_route.current_section_num < g_plsr_route.section_num | |||
| && g_plsr_route.section[g_plsr_route.current_section_num - 1].wait_condition.wait_type == PLSR_WAIT_PLUSEEND) | |||
| { | |||
| // 当前段脉冲发送完毕,进入等待状态 | |||
| g_plsr_route.run_state = PLSR_STATE_WAIT; | |||
| // 检查等待条件是否满足 | |||
| if (PLSR_Section_CheckWaitCondition(&g_plsr_route)) | |||
| @@ -1486,14 +1489,6 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) | |||
| // 等待条件满足,切换到下一段 | |||
| PLSR_SectionConfig_t* current_section = &g_plsr_route.section[g_plsr_route.current_section_num - 1]; | |||
| if (current_section->next_section == 0 && g_plsr_route.current_section_num >= g_plsr_route.section_num) | |||
| { | |||
| // 当前段是最后一段或跳转段为0,路径结束 | |||
| PLSR_Route_Stop(&g_plsr_route); | |||
| return; | |||
| } | |||
| else if (current_section->next_section <= g_plsr_route.section_num) | |||
| { | |||
| // 更新prevPulseCount为当前段的累计脉冲数 | |||
| if (g_plsr_route.mode == PLSR_MODE_RELATIVE) | |||
| { | |||
| @@ -1560,14 +1555,131 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) | |||
| PLSR_Route_Stop(&g_plsr_route); | |||
| return; | |||
| } | |||
| } | |||
| } | |||
| else | |||
| { | |||
| if(g_plsr_route.run_state == PLSR_STATE_ACCEL) | |||
| { | |||
| // 加速阶段完成,清零加速脉冲计数 | |||
| g_plsr_route.accel_pulse_count = 0; | |||
| // 判断下一阶段:优先匀速,如果匀速脉冲数为0则直接进入减速 | |||
| if(g_plsr_route.const_pulse_count > 0) | |||
| { | |||
| // 进入匀速阶段 | |||
| g_plsr_route.run_state = PLSR_STATE_CONST; | |||
| __HAL_TIM_SetAutoreload(&htim2, g_plsr_route.const_pulse_count); | |||
| __HAL_TIM_SET_COUNTER(&htim2, 0); | |||
| PLSR_PWM_Start(); | |||
| } | |||
| else if(g_plsr_route.decel_pulse_count > 0) | |||
| { | |||
| // 无匀速阶段,直接进入减速 | |||
| g_plsr_route.run_state = PLSR_STATE_DECEL; | |||
| __HAL_TIM_SetAutoreload(&htim2, g_plsr_route.decel_pulse_count); | |||
| __HAL_TIM_SET_COUNTER(&htim2, 0); | |||
| PLSR_PWM_Start(); | |||
| } | |||
| else | |||
| { | |||
| // 没有下一段,停止路径 | |||
| // 加速完成且无后续阶段,路径结束 | |||
| PLSR_Route_Stop(&g_plsr_route); | |||
| return; | |||
| } | |||
| } | |||
| else if(g_plsr_route.run_state == PLSR_STATE_CONST) | |||
| { | |||
| // 匀速阶段完成,清零匀速脉冲计数 | |||
| g_plsr_route.const_pulse_count = 0; | |||
| // 进入减速阶段 | |||
| if(g_plsr_route.decel_pulse_count > 0) | |||
| { | |||
| g_plsr_route.run_state = PLSR_STATE_DECEL; | |||
| __HAL_TIM_SetAutoreload(&htim2, g_plsr_route.decel_pulse_count); | |||
| __HAL_TIM_SET_COUNTER(&htim2, 0); | |||
| PLSR_PWM_Start(); | |||
| } | |||
| else | |||
| { | |||
| // 匀速完成且无减速阶段,路径结束 | |||
| PLSR_Route_Stop(&g_plsr_route); | |||
| return; | |||
| } | |||
| } | |||
| else if(g_plsr_route.run_state == PLSR_STATE_DECEL) | |||
| { | |||
| if(g_plsr_route.current_section_num == g_plsr_route.section_num) | |||
| { | |||
| // 减速阶段完成,清零减速脉冲计数 | |||
| g_plsr_route.decel_pulse_count = 0; | |||
| // 减速完成,路径结束 | |||
| PLSR_Route_Stop(&g_plsr_route); | |||
| return; | |||
| } | |||
| else | |||
| { | |||
| // 减速阶段完成,清零减速脉冲计数 | |||
| g_plsr_route.decel_pulse_count = 0; | |||
| // 进入等待状态,等待下一段开始 | |||
| g_plsr_route.run_state = PLSR_STATE_WAIT; | |||
| // 检查等待条件是否满足 | |||
| if (PLSR_Section_CheckWaitCondition(&g_plsr_route)) | |||
| { | |||
| // 等待条件满足,切换到下一段 | |||
| PLSR_SectionConfig_t* current_section = &g_plsr_route.section[g_plsr_route.current_section_num - 1]; | |||
| if (current_section->next_section == 0 && g_plsr_route.current_section_num >= g_plsr_route.section_num) | |||
| { | |||
| // 当前段是最后一段同时跳转段为0,路径结束 | |||
| PLSR_Route_Stop(&g_plsr_route); | |||
| return; | |||
| } | |||
| // 检查是否有下一段可执行 | |||
| if (g_plsr_route.current_section_num < g_plsr_route.section_num) | |||
| { | |||
| // 更新prevPulseCount为当前段的累计脉冲数 | |||
| if (g_plsr_route.mode == PLSR_MODE_RELATIVE) | |||
| { | |||
| g_plsr_route.prevPulseCount = g_plsr_route.pulse_count; | |||
| } | |||
| // 切换到下一段 | |||
| PLSR_SectionConfig_t* next_section = &g_plsr_route.section[g_plsr_route.current_section_num]; | |||
| g_plsr_route.current_section_num++; | |||
| g_plsr_route.current_freq = next_section->target_freq; // 更新当前频率 | |||
| // 启动新段 | |||
| PLSR_Section_StartNewSection(&g_plsr_route); | |||
| // 计算新段需要发送的脉冲数 | |||
| uint32_t next_pulse_target = next_section->target_pulse; | |||
| if (next_pulse_target > 0 && next_pulse_target <= 0xFFFFFFFF) | |||
| { | |||
| __HAL_TIM_SetAutoreload(&htim2, next_pulse_target); | |||
| __HAL_TIM_SET_COUNTER(&htim2, 0); | |||
| PLSR_PWM_Start(); | |||
| } | |||
| else | |||
| { | |||
| // 脉冲数异常,停止路径 | |||
| PLSR_Route_Stop(&g_plsr_route); | |||
| return; | |||
| } | |||
| } | |||
| else | |||
| { | |||
| // 没有下一段,路径结束 | |||
| PLSR_Route_Stop(&g_plsr_route); | |||
| return; | |||
| } | |||
| } | |||
| } | |||
| } | |||
| } | |||
| } | |||
| // TIM6中断:负责加减速过程的频率更新、等待时间计时和实时脉冲计数更新 | |||
| if(htim->Instance == TIM6) | |||