本帖最后由 cry1109 于 2020-2-27 17:23 编辑
STM32F407的TIM1输出PWM,cubeMX配置。使用的是内部时钟,配置系统时钟为64MHz。TIM2每200us进入一次中断,每次中断中增加或减少PWM脉冲频率,达到匀加速、匀减速的效果。但是匀加速期间发出的PWM脉冲个数并不等与计算的个数,而匀减速期间所发的脉冲个数等于计算的个数。
比如说脉冲频率从50Hz增加到28346Hz,期间需要发出502脉冲,可实际至发了6个脉冲;脉冲频率从28346Hz降低到50Hz时,实际发出的脉冲个数504个。从大降到小没问题,从小增到大就不对了,求解。
速度调节函数:void Speed_Adjust(uint16_t delta_speed)
{
if(Motor.ActiveSpeed > (Motor.TargetSpeed+delta_speed))
{
Motor.ActiveSpeed -= delta_speed;
}
else if(Motor.ActiveSpeed < (Motor.TargetSpeed-delta_speed))
{
Motor.ActiveSpeed += delta_speed;
}
else
{
Motor.ActiveSpeed = Motor.TargetSpeed;
}
if(Motor.ActiveSpeed < 50)
{
Motor.ActiveSpeed = 50;
}
else if(Motor.ActiveSpeed > 1000000)
{
Motor.ActiveSpeed = 1000000;
}
__HAL_TIM_SET_AUTORELOAD(&htim1,MOTOR_TIME_FREQ/Motor.ActiveSpeed);
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, (MOTOR_TIME_FREQ/Motor.ActiveSpeed)/2);
}
期望的电机运动轨迹:
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
- void MX_TIM1_Init(void)
- {
- TIM_ClockConfigTypeDef sClockSourceConfig = {0};
- TIM_MasterConfigTypeDef sMasterConfig = {0};
- TIM_OC_InitTypeDef sConfigOC = {0};
- TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
- htim1.Instance = TIM1;
- htim1.Init.Prescaler = 31;
- htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
- htim1.Init.Period = 40000;
- htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
- htim1.Init.RepetitionCounter = 0;
- htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
- if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
- {
- Error_Handler();
- }
- sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
- if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)
- {
- Error_Handler();
- }
- sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
- sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
- if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
- {
- Error_Handler();
- }
- sConfigOC.OCMode = TIM_OCMODE_PWM1;
- sConfigOC.Pulse = 20000;
- sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
- sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
- sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
- sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
- sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
- if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
- {
- Error_Handler();
- }
- __HAL_TIM_DISABLE_OCxPRELOAD(&htim1, TIM_CHANNEL_1);
- sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
- sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
- sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
- sBreakDeadTimeConfig.DeadTime = 0;
- sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
- sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
- sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
- if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
- {
- Error_Handler();
- }
- HAL_TIM_MspPostInit(&htim1);
- }
复制代码一周热门 更多>