Skip to main content
cpc
Associate II
September 13, 2019
Question

Adding PWM function in existing Ping Pong example

  • September 13, 2019
  • 6 replies
  • 2526 views

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?

This topic has been closed for replies.

6 replies

Tesla DeLorean
Guru
September 13, 2019

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 VenmoUp vote any posts that you find helpful, it shows what's working..
cpc
cpcAuthor
Associate II
September 13, 2019

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.

waclawek.jan
Super User
September 13, 2019

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

JW

cpc
cpcAuthor
Associate II
September 13, 2019

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();
	 }
}

Tesla DeLorean
Guru
September 13, 2019

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

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
cpc
cpcAuthor
Associate II
September 13, 2019

I am using TIM3 only.

waclawek.jan
Super User
September 13, 2019

I don't use Cube.

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

JW

waclawek.jan
Super User
September 15, 2019

Okay, so read out and check/post also this register:0690X00000ARDbkQAH.jpg

JW

cpc
cpcAuthor
Associate II
September 16, 2019

Thanks for your help.

cpc
cpcAuthor
Associate II
September 23, 2019

Now I am getting the PWM. But the problem is SPI clock is affecting the PWM.

PWM is resetting according to SPI and when I disabled the SPI I am getting exact PWM as required.

According to me SPI clock is triggering the PWM.

What can be done to avoid this ?