cancel
Showing results for 
Search instead for 
Did you mean: 

Configure an PWM on a timer output.

CDan.1
Associate II

Hi guys,

I am having a problem with the configurations for a PWM on an timer channel. I am working on a STM32L552ZE with LL library. I am tring to configure a PWM on pin PB9 (TIM17 CH1). I have first configuret as a PWM with interrupts (TIM17 update interrupt and in interrupt toggle pin) it works ok. But when I trie to configure it with OC I get nothing just the pin in 1.3V. To the pin I have connected only the base of an NPN transistor. Below you will find the code used to configure the PWM:

void Config_125kHzPWM(void)

{

   LL_TIM_InitTypeDef TIM_InitStruct={0};

   LL_TIM_OC_InitTypeDef TIM_OC_InitStruct={0};

   LL_GPIO_InitTypeDef   GPIO_InitStruct={0};

   /*Enable registers CLK*/

   RCCCLK_ENABLE_DISABLE((uint32_t *)&RCC->AHB2ENR,RCC_AHB2ENR_GPIOBEN,ENABLE);

   GPIO_InitStruct.Pin             = LL_GPIO_PIN_9;

   GPIO_InitStruct.Mode            = LL_GPIO_MODE_ALTERNATE;

   GPIO_InitStruct.Speed           = LL_GPIO_SPEED_HIGH;

   GPIO_InitStruct.Pull            = LL_GPIO_OUTPUT_PUSHPULL;

   GPIO_InitStruct.OutputType      = LL_GPIO_OUTPUT_OPENDRAIN;

   GPIO_InitStruct.Alternate       = LL_GPIO_AF_14;

   LL_GPIO_Init(GPIOB, &GPIO_InitStruct);

   RCCCLK_ENABLE_DISABLE((uint32_t *)&RCC->APB2ENR,RCC_APB2ENR_TIM17EN ,ENABLE);

   LL_TIM_SetClockSource(TIM17, LL_TIM_CLOCKSOURCE_INTERNAL);

   /* Set the default configuration */

      TIM_InitStruct.Prescaler        = (uint16_t)0x0000;

      TIM_InitStruct.CounterMode      = LL_TIM_COUNTERMODE_UP;

      TIM_InitStruct.Autoreload       = 0x00DCU;

      TIM_InitStruct.ClockDivision    = LL_TIM_CLOCKDIVISION_DIV4;

      TIM_InitStruct.RepetitionCounter = 0U;

      LL_TIM_Init(TIM17, &TIM_InitStruct);

      LL_TIM_EnableCounter(TIM17);

      //LL_TIM_EnableIT_UPDATE(TIM17);

      //LL_TIM_ClearFlag_UPDATE(TIM17);

      //NVIC_SetPriority(TIM17_IRQn, 2);

      //NVIC_EnableIRQ(TIM17_IRQn);

   /* Set the default configuration */

   TIM_OC_InitStruct.OCMode      = LL_TIM_OCMODE_PWM1;

   TIM_OC_InitStruct.OCState     = LL_TIM_OCSTATE_ENABLE;

   TIM_OC_InitStruct.OCNState    = LL_TIM_OCSTATE_ENABLE;

   TIM_OC_InitStruct.CompareValue = 0x006EU;

   TIM_OC_InitStruct.OCPolarity  = LL_TIM_OCPOLARITY_HIGH;

   TIM_OC_InitStruct.OCNPolarity = LL_TIM_OCPOLARITY_HIGH;

   TIM_OC_InitStruct.OCIdleState = LL_TIM_OCIDLESTATE_HIGH;

   TIM_OC_InitStruct.OCNIdleState = LL_TIM_OCIDLESTATE_HIGH;

   LL_TIM_OC_Init(TIM17, LL_TIM_CHANNEL_CH1, &TIM_OC_InitStruct);

   LL_TIM_OC_EnablePreload(TIM17, LL_TIM_CHANNEL_CH1);

}

And in main i just execut this function.

The MCU APB clock is 110MHz.

Can someone point me to my mistake?

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions

Hi CDan.1,

Sorry for the late answer.

TIM17 needs to have BDTR.MOE set.

JW

View solution in original post

6 REPLIES 6

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

JW

Here are the hex value for the registers imediatly after executing the config function:

   GPIOB

   0x00000000->AFR[0]                                                                 

   0x000000E0->AFR[1]                                                  

   0x00040100->PUPDR                                                                 

   0x00000000->OTYPER                                                                 

   0xFFFBFEBF->MODER 

   Timer17

   0x00000201->CR1                                                                 

   0x00000300->CR2                                                                 

   0x00000000->SMCR                                                                 

   0x00000000->DIER                                                                 

   0x00000003->SR                                                                 

   0x00000000->EGR                                                                

   0x00000068->CCMR1                                                                

   0x00000000->CCMR2                                                                

   0x00000005->CCER                                                               

   0x00000030->CNT                                                                

   0x00000000->PSC                                                                

   0x000000DC->ARR                                                                

   0x00000000->RCR                                                               

   0x0000006E->CCR1                                                               

   0x00000000->BDTR                                                               

   0x00000000->OR1                                                               

   0x00000000->CCMR3                                                              

   0x00000001->OR2                                                               

   0x00000000->OR3

Mohamed Aymen HZAMI
ST Employee

Hello @CDan.1​ ,

Can you please send me your code configuration ?

Thanks and Best Regards,

Mohamed Aymen.

Sry but I do not understand the question, what code do you need?

The configuration code for used to config the PWM was posted in the original post. The function is void Config_125kHzPWM(void) and it is posted.

Mohamed Aymen HZAMI
ST Employee

Hello,

You can try an example from the L5 CubeFW.

Best Regards,

Mohamed Aymen.

Hi CDan.1,

Sorry for the late answer.

TIM17 needs to have BDTR.MOE set.

JW