cancel
Showing results for 
Search instead for 
Did you mean: 

Adding PWM function in existing Ping Pong example

cpc
Associate II

Hi,

I have generated the code for PWM generation using STM32 CubeMX and it is working fine on B-l072Z-lrwan1.

But when I was trying to integrate same code with the existing Ping Pong example, I was not getting any PWM signal on respective pin. I have also configure the pin according to timer and channel with the help of datasheet and also check the alternate function for the pin.

I have tried different Timers but I am not getting any PWM signal at output.

Can anyone tell me, why this is happening?

13 REPLIES 13

Some of the code is not getting executed?

You could perhaps use the debugger and inspect the GPIO, TIM and RCC peripheral registers.

Check you have pulled all the init and msp code over.​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Rest of all code is executing well except PWM.

I am generating PWM on PB5 so Green LED Should be dim, but it remains off all the time.

Read out and check/post content of relevant GPIO and timer registers.

JW

cpc
Associate II

Registers configuration is right because the same configuration individually works fine but not with Ping Pong code.

I can't understand what is the problem ?

Below is the way I configured the registers and pin for PWM.

void PWM_LED_Init(Led_TypeDef Led)
{
 
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);
 
  GPIO_InitTypeDef  GPIO_InitStruct;
 
  /* Enable the GPIO_LED Clock */
  LEDx_GPIO_CLK_ENABLE( Led );
 
  /* Configure the GPIO_LED pin */
  GPIO_InitStruct.Pin = GPIO_PIN_5;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  GPIO_InitStruct.Alternate = GPIO_AF4_TIM3;
 
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
}
 
void MX_TIM2_Init(int8_t value)
{
	  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
	  TIM_MasterConfigTypeDef sMasterConfig = {0};
 
	  htim2.Instance = TIM3;
	  htim2.Init.Prescaler = 4;
	  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
	  htim2.Init.Period = 100;
	  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 = value;
	  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
	  sConfigOC.OCFastMode = TIM_OCFAST_ENABLE;
	  if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
	  {
	    Error_Handler();
	  }
}

I don't use Cube.

Can you please read out and check/post the content of the relevant GPIO and TIM registers?

JW

TIM2 probably won't come out of a pin configured for TIM3​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

I am using TIM3 only.

TIM3_CH2 on PB5 is AF1

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

This is given in datasheet.

0690X00000ARCrmQAH.jpg