cancel
Showing results for 
Search instead for 
Did you mean: 

Is there any difference in configuring TIM1 for PWM than TIM4?

denix8
Associate II
Posted on January 22, 2017 at 22:58

Hi!

I am using STM32F407 board.

I have configured timer 4 for PWM with following code and connected LED to the configured pin. It is working as expected. I used following code:

TIM_TimeBaseInitTypeDef TIM_BaseStruct; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); // Enable bus clock TIM_BaseStruct.TIM_Prescaler = 0; // Set prescaler (frequency divider) TIM_BaseStruct.TIM_CounterMode = TIM_CounterMode_CenterAligned1; // Set counting mode (up, down, center-aligned) to center aligned for better motor control TIM_BaseStruct.TIM_Period = 4000; // Set timer period (timer reset/change direction value) TIM_BaseStruct.TIM_ClockDivision = TIM_CKD_DIV1; TIM_BaseStruct.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM4, &TIM_BaseStruct); // Initialize timer with chosen settings TIM_Cmd(TIM4, ENABLE); // Start timer GPIO_InitTypeDef GPIO_InitStruct; /* Clock for GPIOD */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); /* Alternating functions for pins */ GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4); /* Set pins */ GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz; GPIO_Init(GPIOD, &GPIO_InitStruct); TIM_OCInitTypeDef TIM_OCStruct; TIM_OCStruct.TIM_OCMode = TIM_OCMode_PWM2; TIM_OCStruct.TIM_OutputState = TIM_OutputState_Enable; TIM_OCStruct.TIM_OCIdleState = TIM_OCIdleState_Reset; TIM_OCStruct.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OCStruct.TIM_Pulse = 1999; TIM_OC1Init(TIM4, &TIM_OCStruct); TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);

but when I configured TIM 1 and connected LED to its pin, nothing happened (I tested pin separately by toggling it as output to make sure I connected LED to the right pin and it lit up). I used practically the same code, only with minor differences that TIM1 demands:

TIM_TimeBaseInitTypeDef TIM_BaseStruct; RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); // Enable bus clock TIM_BaseStruct.TIM_Prescaler = 1; // Set prescaler (frequency divider) TIM_BaseStruct.TIM_CounterMode = TIM_CounterMode_CenterAligned1; // Set counting mode (up, down, center-aligned) to center aligned for better motor control TIM_BaseStruct.TIM_Period = 4000; // Set timer period (timer reset/change direction value) TIM_BaseStruct.TIM_ClockDivision = TIM_CKD_DIV1; TIM_BaseStruct.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM1, &TIM_BaseStruct); // Initialize timer with chosen settings TIM_Cmd(TIM1, ENABLE); // Start timer GPIO_InitTypeDef GPIO_InitStruct; /* Clock for GPIOE */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); /* Alternating functions for pins */ GPIO_PinAFConfig(GPIOE, GPIO_PinSource9, GPIO_AF_TIM1); /* Set pins */ GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz; GPIO_Init(GPIOE, &GPIO_InitStruct); TIM_OCInitTypeDef TIM_OCStruct; TIM_OCStruct.TIM_OCMode = TIM_OCMode_PWM2; TIM_OCStruct.TIM_OutputState = TIM_OutputState_Enable; TIM_OCStruct.TIM_OCIdleState = TIM_OCIdleState_Reset; TIM_OCStruct.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OCStruct.TIM_Pulse = 1999; TIM_OC1Init(TIM1, &TIM_OCStruct); TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);

Is there something different I have to do about advanced timers to make them work with pwm? I also achieved success with TIM3 and TIM12 (which are general purpose timers), but not with TIM8 (which is also an advanced timer).

Thanks for any help! #pwm-timer-output #stm32-stm32f4
6 REPLIES 6
Posted on January 22, 2017 at 23:39

Is there something different I have to do about advanced timers to make them work with pwm?

Yes, you have to set TIMx_BDTR.MOE.

I don't know the 'library' incantation to achieve this, but it's an often recurring question so you should be able to find it in the forum.

JW

Posted on January 23, 2017 at 03:33

The method you've used to post the code makes it next to unreadable

  /* TIM1 Main Output Enable */

  TIM_CtrlPWMOutputs(TIM1, ENABLE);
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Khouloud GARSI
Lead II
Posted on January 23, 2017 at 10:45

Hello

Drulko.Malko

,

A advise you to refer to the 'TIM_PWMOutput' example provided when downloading the

http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32cube-embedded-software/stm32cubef4.html

package.

The example is found under the path below:

STM32Cube_FW_F4_V1.0\Projects\STM32446E_EVAL\Examples\TIM

Theexample shows how to configure the TIM8 peripheral in PWMmode. Itruns on STM32F446xx devices but you can take it as a reference to configure your timer conveniently.

Khouloud

Posted on January 23, 2017 at 13:29

I am sorry when I copied it from the source it was in good fortmatting then suddenly here it was in one line when I clicked 'post' :\ The following (additional) code did the trick (although I don't know what CCPreloadControl actually does):

TIM_BDTRInitTypeDef TIM_BDTRInitStruct;

TIM_BDTRStructInit(&TIM_BDTRInitStruct);

TIM_BDTRConfig(TIM1, &TIM_BDTRInitStruct);

TIM_CCPreloadControl(TIM1, ENABLE);

TIM_CtrlPWMOutputs(TIM1, ENABLE);

Thank you for help!

Posted on January 23, 2017 at 13:30

Thank you, based on your comment I was able to find this 

http://www.cs.indiana.edu/~bhimebau/f3lib/html/group__TIM__Group4.html

  which helped me make working code.
Posted on January 23, 2017 at 13:31

Thank you!