Skip to main content
MSamy.1
Associate II
October 16, 2020
Question

TIM2 PWM is not working on P-Nucleo-wb55

  • October 16, 2020
  • 4 replies
  • 1269 views

I have set the TIM2 with the following code:

 __HAL_RCC_TIM2_CLK_ENABLE();
 
 htim2.Instance = TIM2;
 htim2.Init.Prescaler = 0;
 htim2.Init.Period = 80;
 htim2.Init.RepetitionCounter = 0;
 htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
 HAL_TIM_PWM_Init(&htim2);
 
 TIM_OC_InitTypeDef tim2OC1;
 TIM_OC_InitTypeDef tim2OC2;
 
 tim2OC1.OCMode = TIM_OCMODE_PWM1;
 tim2OC1.Pulse = 20;
 tim2OC1.OCPolarity = TIM_OCPOLARITY_HIGH;
 tim2OC1.OCFastMode = TIM_OCFAST_DISABLE;
 tim2OC1.OCNPolarity = TIM_OCNPOLARITY_HIGH;
 HAL_TIM_PWM_ConfigChannel(&htim2, &tim2OC1, TIM_CHANNEL_1);
 
 tim2OC2.OCMode = TIM_OCMODE_PWM1;
 tim2OC2.Pulse = 40;
 tim2OC2.OCPolarity = TIM_OCPOLARITY_HIGH;
 tim2OC2.OCFastMode = TIM_OCFAST_DISABLE;
 tim2OC2.OCNPolarity = TIM_OCNPOLARITY_HIGH;
 tim2OC2.OCIdleState = TIM_OCIDLESTATE_RESET;
 tim2OC2.OCNIdleState = TIM_OCNIDLESTATE_RESET;
 HAL_TIM_PWM_ConfigChannel(&htim2, &tim2OC2, TIM_CHANNEL_2);
 
 HAL_TIM_Base_Start(&htim2);
 HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
 HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
 
 __HAL_RCC_GPIOA_CLK_ENABLE();

The timer is working fine and in the debugging mode I find the value of counter increasing normally but when I connect the scope to the channel_1 pin PA_0 ==> A3 on the board no PWM out of the controller I checked with channel_2 also.

I also tried the code generated from Cube IDE mx with adding the TIM Start and PWM Start in the main but with no result.

So what's wrong ?

This topic has been closed for replies.

4 replies

TDK
Super User
October 16, 2020

You need to initialize the pin(s) with the appropriate (TIMx) alternate function mode.

"If you feel a post has answered your question, please click ""Accept as Solution""."
MSamy.1
MSamy.1Author
Associate II
October 18, 2020

I'm sorry I missed to attach the code for initializing PWM pins

__HAL_RCC_GPIOA_CLK_ENABLE();
 /**TIM2 GPIO Configuration
 PA0 ------> TIM2_CH1
 PA1 ------> TIM2_CH2
 PA2 ------> TIM2_CH3
 PA3 ------> TIM2_CH4
 */
 GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Pull = GPIO_PULLUP;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
 GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

And also the PWM not working is it possible that the pins are damaged ?

TDK
Super User
October 18, 2020

It seems like the code should work, although I'm not super familiar with the STM32WB series. It's possible the pins are damaged but way more likely there's an issue in the code.

Attach a debugger, step through the code, see if/where it's stuck, inspect the TIM registers to see if it's working, and the GPIO registers to see if they're configured correctly, etc.

"If you feel a post has answered your question, please click ""Accept as Solution""."
MSamy.1
MSamy.1Author
Associate II
October 19, 2020

I already did that and checked the timer registers the code doesn't stuck at any point and the CNT register is working fine and CCRx values are correct that's why I assumed that the pins may be damaged.