主从定时器参考我之前这篇文章:传送门
加减速控制
void SpeedAdjust(uint16_t count)
{
TIM_SetAutoreload(TIM1,CurveS_Para[count]);//设定自动重装值
TIM_SetCompare2(TIM1,CurveS_Para[count]>>1); //匹配值2等于重装值一半,是以占空比为50%
TIM_CtrlPWMOutputs(TIM1,ENABLE); //MOE 主输出使能
if(Motor.Status == SPEED_INCREASE) //加速
{
Motor.CountTemp++;
}
else if(Motor.Status == SPEED_DECREASE) //减速
{
Motor.CountTemp--;
}
//Motor.Count = Motor.CountTemp*(50/Motor.CountPropor); //加速次数
Motor.Count = Motor.CountTemp;
}
状态机放在TIM4中断中
void SpeedAdjustMachine(void)
{
switch(Motor.Status)
{
/*加速*/
case SPEED_INCREASE:
if(Motor.Count >= Motor.COUNT_MAX)
{
Motor.Status = SPEED_STABLE;
}
else
{
SpeedAdjust(Motor.Count);
}
break;
/*匀速*/
case SPEED_STABLE:
if(Motor.PWMcount >= (Motor.PWMneed-Motor.SpeedDecPWM))
{
Motor.Status = SPEED_DECREASE;
}
break;
/*减速*/
case SPEED_DECREASE:
if(Motor.Count >=1)
{
SpeedAdjust(Motor.Count);
}
if(Motor.PWMcount==Motor.PWMneed )
{
Motor.Status = Stop;
}
break;
/*停止*/
case Stop:
TIM_CtrlPWMOutputs(TIM1,DISABLE);
TIM_Cmd(TIM1,DISABLE);
TIM_Cmd(TIM4,DISABLE);
Motor.CountTemp = 0;
Motor.PWMcount = 0;
TIM_ITConfig( TIM4, TIM_IT_Update, DISABLE);
break;
/*无加减速*/
case UNIFM:
if(Motor.PWMcount==Motor.PWMneed )
{
Motor.Status = Stop;
}
break;
default :
break;
}
}