AnsweredAssumed Answered

STM32F429 PWM with Timer 2

Question asked by Daattavya Aggarwal on Aug 2, 2017
Latest reply on Aug 3, 2017 by Daattavya Aggarwal

I am using STM32F429 discovery board.

I am trying to generate pwm output on pin 11 of GPIO G. I have set and enabled the corresponding registers and bits but there is no output on pin 11. If I write a simple delay code then pin 11 does go high and the code works fine. I am coding using direct registers. Any suggestions?

 

#include <stm32f4xx.h>
void clk_sys()
{
RCC ->CFGR |= RCC_CFGR_MCO1_0;
RCC ->CFGR |= RCC_CFGR_MCO1_1;
RCC ->CR |= RCC_CR_PLLON;
RCC ->APB1ENR |= RCC_APB1ENR_TIM2EN;
}
void enable_gpio()//setting pin 11 of port g for AF1 which corresponds to timer 2.
{
RCC ->AHB1ENR |= RCC_AHB1ENR_GPIOGEN; //Enables port G input output pins by setting the corresponding bit in advanced high performance bus 1 register.
GPIOG ->MODER |= GPIO_MODER_MODER11_1; //Configures pin 11 for alternate function mode by setting the corresponding bit in the mode register
GPIOG ->OTYPER &= ~(GPIO_OTYPER_OT_11); //Makes the output type as push-pull by resetting the corresponding bit in output type register.
GPIOG ->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR11_1;//Makes the output high speed.
GPIOG ->PUPDR &= ~(GPIO_PUPDR_PUPDR11);//No pull up or pull down.
GPIOG ->AFR[1] |= GPIO_AF1_TIM2;
}
void generate_pwm()
{

TIM2 ->CCMR1 |= TIM_CCMR1_OC1M_2;
TIM2 ->CCMR1 |= TIM_CCMR1_OC1M_1;
TIM2 ->CCMR1 &= ~(TIM_CCMR1_OC1M_0);
TIM2 ->CCMR1 |= TIM_CCMR1_OC1PE;
TIM2 ->CR1 |= TIM_CR1_ARPE;
TIM2 ->EGR |= TIM_EGR_UG;
TIM2 ->CCER |= TIM_CCER_CC1P;
TIM2 ->CCER |= TIM_CCER_CC1E;
TIM2 ->ARR = 0xFFF;
TIM2 ->CCR1 = 0xFF;
TIM2 ->CR1 |= TIM_CR1_CEN;
HAL_TIM_PWM_Start;
while(TIM2 ->CNT < TIM2 ->CCR1)
{
GPIOG ->BSRRL |= GPIO_BSRR_BS_11;
}
}
int main(void)
{
clk_sys();
enable_gpio();
while(1)
{
generate_pwm();
// GPIOG ->BSRRL |= GPIO_BSRR_BS_11;
}
}

Outcomes