2017-01-22 01:58 PM
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-stm32f42017-01-22 02:39 PM
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
2017-01-22 06:33 PM
The method you've used to post the code makes it next to unreadable
/* TIM1 Main Output Enable */
TIM_CtrlPWMOutputs(TIM1, ENABLE);2017-01-23 01:45 AM
Hello
Drulko.Malko
,A advise you to refer to the 'TIM_PWMOutput' example provided when downloading the
package. The example is found under the path below: STM32Cube_FW_F4_V1.0\Projects\STM32446E_EVAL\Examples\TIMTheexample 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
2017-01-23 05:29 AM
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!
2017-01-23 05:30 AM
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.2017-01-23 05:31 AM
Thank you!