AnsweredAssumed Answered

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

Question asked by Drulko.Malko on Jan 22, 2017
Latest reply on Jan 23, 2017 by Drulko.Malko

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!

Outcomes