cancel
Showing results for 
Search instead for 
Did you mean: 

I cant seem to make a pwm work.

ulao
Associate III

I have a project where I want to add a simple PWM. I'm using the stm32h series. I have a scope on PA3, and I set up my timer2 for PWM on channel 4. I did this all in the IOC file as I want to understand how to create the PWM I need. I see it added my init code and I'm guessing I need to add a few lines to start it.

maybe a timer start

HAL_TIM_Base_Start_IT(&htim2);

and a pwm start

HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4);

the lines it added were

static void MX_TIM2_Init(void)
{
 
  /* USER CODE BEGIN TIM2_Init 0 */
 
  /* USER CODE END TIM2_Init 0 */
 
  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};
  TIM_OC_InitTypeDef sConfigOC = {0};
 
  /* USER CODE BEGIN TIM2_Init 1 */
 
  /* USER CODE END TIM2_Init 1 */
  htim2.Instance = TIM2;
  htim2.Init.Prescaler = 0;
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim2.Init.Period = 83999999;
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sConfigOC.OCMode = TIM_OCMODE_PWM1;
  sConfigOC.Pulse = 0;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM2_Init 2 */
 
  /* USER CODE END TIM2_Init 2 */
  HAL_TIM_MspPostInit(&htim2);
 
}

I assume it configured my pins for output somewhere? Is there something more I need to do here?

If I use my GPIO and in code bring the line up and down I see the result. So I'm pretty sure my scope is on the right pin.

1 ACCEPTED SOLUTION

Accepted Solutions

> sConfigOC.Pulse = 0;

This means PWM with 0 duty, i.e. output permanently at one level (low or high, depending on PWM type and polarity setting etc.)

View solution in original post

7 REPLIES 7
Javier1
Principal

Only

HAL_TIM_Base_Start_IT(&htim2);

HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4);

and then , to modify the duty cycle you could do TIM2->CCR4=whatever;

https://www.youtube.com/watch?v=K0_hfbT7vWA

we dont need to firmware by ourselves, lets talk

Read out and check/post TIM and relevant GPIO registers content.

JW

[EDIT}

> sConfigOC.Pulse = 0;

This means PWM with 0 duty, i.e. output permanently at one level (low or high, depending on PWM type and polarity setting etc.)

ulao
Associate III

Thx, but still not seeing a PWM. I'm not sure what this tells me but if I tap a 1k pull up on the line with out enablindg the timer the lines will go high. if I tap the same 1k pull up when the timer is enabled it does not. It is like the PWM is holding the line low.

> sConfigOC.Pulse = 0;

This means PWM with 0 duty, i.e. output permanently at one level (low or high, depending on PWM type and polarity setting etc.)

ulao
Associate III

I put sConfigOC.Pulse = 32

and I got 1 pulse ever 300ms

not sure I understand yet but its a good sign, thx.

As I've said, read out TIM registers and check their content against the description in TIM chapter in Reference Manual.

In debugger, you can stop the program, and change the content of TIM registers directly in the debugger (e.g. here the respective TIMx_CCRx register) while observing the effect of doing this on the pin.

JW

ulao
Associate III

yeha I see that is changing the timing thx!