cancel
Showing results for 
Search instead for 
Did you mean: 

PWM Timer generation not working with low duty cycle

vtran1
Associate II

0693W000003RBXMQA4.jpgAt 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.

5 REPLIES 5
TDK
Guru

> The higher the duty cycle, the smaller the stop period.

Sounds more likely to be a bug in the code than a hardware issue.

If you feel a post has answered your question, please click "Accept as Solution".
vtran1
Associate II

My code is pretty simple. I have added the code in my question. Can you take a look at it?

Uwe Bonnes
Principal III

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.

I'd say, this is oscilloscope sampling/aliasing issue.

JW

TDK
Guru

> 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?

If you feel a post has answered your question, please click "Accept as Solution".