2020-09-09 06:52 AM
At low duty cycle (like below 5%), the timer pwm stops periodically (see the attached picture). The higher the duty cycle, the smaller the stop period. Does anyone know why this happens?
For context, I am using the STM32F072 Discovery board. I use CubeIDE to generate the code with the timer setting as below:
/* TIM3 init function */
void MX_TIM3_Init(void)
{
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
htim3.Instance = TIM3;
htim3.Init.Prescaler = 7;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 9999;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 9999;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
HAL_TIM_MspPostInit(&htim3);
}
Then in the while loop in main, I call
while(1)
{
htim3.Instance->CCR1 = 799;
HAL_Delay(200);
}
Thank you.
2020-09-09 06:53 AM
> The higher the duty cycle, the smaller the stop period.
Sounds more likely to be a bug in the code than a hardware issue.
2020-09-09 07:41 AM
My code is pretty simple. I have added the code in my question. Can you take a look at it?
2020-09-09 08:14 AM
HRTIMER with PLL has restrictions on low/high duty cycles. Reread the datasheet and teh hrtimer section in the reference manual. If I remember right, you can not get shorter than the bus clock of the hrtimer.
2020-09-09 09:56 AM
I'd say, this is oscilloscope sampling/aliasing issue.
JW
2020-09-09 12:13 PM
> My code is pretty simple. I have added the code in my question. Can you take a look at it?
The code that you posted seems fine to me. Presumably there's more code that you didn't show, since you don't start the timer anywhere in there.
I would be surprised if aliasing shows up like that on a scope, but it could explain the result. What is your PWM frequency and what is your scope sampling rate?