AnsweredAssumed Answered

STM32F29 PWM with Timer 2

Question asked by Daattavya Aggarwal on Aug 8, 2017
Latest reply on Aug 9, 2017 by Vangelis Fortounas

In my code for pwm on timer 2 in stm32F429, I have to use multiple channels (2 at a time). However only my channel 1 pin is giving pwm output. Please advise me what to do

void enable_gpio1()//setting pin 5 of port a for AF1 which corresponds to timer 2.
{
RCC ->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; //Enables port A input output pins by setting the corresponding bit in advanced high performance bus 1 register.
GPIOA ->MODER |= GPIO_MODER_MODER5_1; //Configures pin 5 for alternate function mode by setting the corresponding bit in the mode register
GPIOA ->OTYPER &= ~(GPIO_OTYPER_OT_5); //Makes the output type as push-pull by resetting the corresponding bit in output type register.
GPIOA ->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR5_1;//Makes the output high speed.
GPIOA ->PUPDR &= ~(GPIO_PUPDR_PUPDR5);//No pull up or pull down.
GPIOA ->AFR[0] = GPIO_AF1_TIM2<<20;//Sets the AF1 bit in the AFRL(AFR[0]) register which links AF1 to pin A5
}
void enable_gpio2()//setting pin 2 of port a for AF1 which corresponds to timer 2.
{
RCC ->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; //Enables port A input output pins by setting the corresponding bit in advanced high performance bus 1 register.
GPIOA ->MODER |= GPIO_MODER_MODER2_1; //Configures pin 5 for alternate function mode by setting the corresponding bit in the mode register
GPIOA ->OTYPER &= ~(GPIO_OTYPER_OT_2); //Makes the output type as push-pull by resetting the corresponding bit in output type register.
GPIOA ->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR2_1;//Makes the output high speed.
GPIOA ->PUPDR &= ~(GPIO_PUPDR_PUPDR2);//No pull up or pull down.
GPIOA ->AFR[0] = GPIO_AF1_TIM2<<20;//Sets the AF1 bit in the AFRL(AFR[0]) register which links AF1 to pin A5
}
void generate_pwm1(int n)
{
TIM2 ->CCMR1 |= TIM_CCMR1_OC1M_2;
TIM2 ->CCMR1 |= TIM_CCMR1_OC1M_1;
TIM2 ->CCMR1 &= ~(TIM_CCMR1_OC1M_0);//Selects pwm mode 1 by writing 110
TIM2 ->CCMR1 |= TIM_CCMR1_OC1PE;//
TIM2 ->CR1 |= TIM_CR1_ARPE;//Enables preload register
TIM2 ->EGR |= TIM_EGR_UG;//Enables update event whenever cnt equals ccr.
TIM2 ->CCER |= TIM_CCER_CC1P;//Sets the polarity of the pwm
TIM2 ->CCER |= TIM_CCER_CC1E;
TIM2 ->ARR = (int)n*40/250.0;//Sets the value for the auto reload register.
TIM2 ->CCR1 = 1;//Sets the value for the ccr register.
TIM2 ->CR1 |= TIM_CR1_CEN;//Starts the counter for timer 2.
}
void generate_pwm2(int n)
{
TIM2 ->CCMR2 |= TIM_CCMR2_OC3M_2;
TIM2 ->CCMR2 |= TIM_CCMR2_OC3M_1;
TIM2 ->CCMR2 &= ~(TIM_CCMR2_OC3M_0);//Selects pwm mode 1 by writing 110
TIM2 ->CCMR2 |= TIM_CCMR2_OC3PE;//
TIM2 ->CR1 |= TIM_CR1_ARPE;//Enables preload register
TIM2 ->EGR |= TIM_EGR_UG;//Enables update event whenever cnt equals ccr.
TIM2 ->CCER |= TIM_CCER_CC3P;//Sets the polarity of the pwm
TIM2 ->CCER |= TIM_CCER_CC3E;
TIM2 ->ARR = (int)n*40/250.0;//Sets the value for the auto reload register.
TIM2 ->CCR3 = 1;//Sets the value for the ccr register.
TIM2 ->CR1 |= TIM_CR1_CEN;//Starts the counter for timer 2.

 

My channel 1 pwm is working but channel 3 is not. I have also included the code I have used to enable the gpio pins.

Outcomes