cancel
Showing results for 
Search instead for 
Did you mean: 

Setup PWM on TIM12 for trailing edge or leading edge

julred
Associate II

 

Dear all,

I have a custom pcb with a STM32H753 on it.
There I'm already using the TIM12 function for generating a PWM output signal

 

This is my init function - generated from CubeMX:

 

void MX_TIM12_Init(void)
{

  /* USER CODE BEGIN TIM12_Init 0 */

  /* USER CODE END TIM12_Init 0 */

  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};
  TIM_OC_InitTypeDef sConfigOC = {0};

  /* USER CODE BEGIN TIM12_Init 1 */

  /* USER CODE END TIM12_Init 1 */
  htim12.Instance = TIM12;
  htim12.Init.Prescaler = 225;
  htim12.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim12.Init.Period = 1000;
  htim12.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim12.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim12) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim12, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_TIM_PWM_Init(&htim12) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim12, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sConfigOC.OCMode = TIM_OCMODE_PWM1;
  sConfigOC.Pulse = 500;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  if (HAL_TIM_PWM_ConfigChannel(&htim12, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM12_Init 2 */

  /* USER CODE END TIM12_Init 2 */
  HAL_TIM_MspPostInit(&htim12);

}

 

So the normal operation of PWM with a trailing edge is working (see attached image for seeing the waveform).

img_temp_6429c917c5df00-74083290-87095399.png

Now I want to adjust parameters to get - based on same frequency and duty cycle - the leading edge pwm wave form.

What needs to be adjusted on the sConfigOC, or what else needs to be adjusted / manipulated if I switch from trailing edge to leading edge?

 

4 REPLIES 4
MasterT
Lead
sConfigOC.OCMode = TIM_OCMODE_PWM2;

Anything else to be considered?

E.g. _PWM[ch].TimHandler->Init.CounterMode = TIM_COUNTERMODE_DOWN;

TDK
Guru

I think you need to change the pulse value and the polarity.

  sConfigOC.Pulse = 500;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;

Your current waveform is 50% duty cycle, so changing the pulse doesn't do much. 

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

Hi @julred 

 

This post has been escalated to the ST Online Support Team for additional assistance.  We'll contact you directly.

 

Regards,

Billy