Skip to main content
marko2
Associate II
July 4, 2014
Question

STM32F407 PWM issues

  • July 4, 2014
  • 2 replies
  • 790 views
Posted on July 04, 2014 at 17:29

Hi all,

I'm trying to use a PWM on a Olimex STM32-H407 board to drive the green LED (only user LED on the board), but without any success. I managed to use timers for generating output signal to turn on/off the LED, but inside the infinite loop in main program. Now I want to generate this output signal in ''background'', i.e. as a PWM. Here is an example of a code that I've used:


int
main(
void
)

{


GPIO_InitTypeDef GPIO_InitStructure;

TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;

TIM_OCInitTypeDef TIM_OCInitStructure;


/* Enable GPIOC */

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);


/* Enable TIM4 */

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);


/* Configure GPIOC to access LED */

GPIO_StructInit(&GPIO_InitStructure); 
// initalization


GPIO_PinAFConfig(GPIOC, GPIO_PinSource13, GPIO_AF_TIM4);


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; 
// LED @ Olimex-STM32-H407

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

GPIO_Init(GPIOC, &GPIO_InitStructure);


TIM_TimeBaseStructInit(&TIM_TimeBaseInitStructure); 
// initialization


TIM_TimeBaseInitStructure.TIM_Prescaler = 40000; 
// 40 MHz -> 1 kHz (1 ms)

TIM_TimeBaseInitStructure.TIM_Period = 1000; 
// 1000 ms = 1 s

TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;

TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitStructure);


TIM_OCStructInit(&TIM_OCInitStructure); 
// initialization


TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

TIM_OCInitStructure.TIM_Pulse = 500;

TIM_OC1Init(TIM4, &TIM_OCInitStructure);


/* Enable TIM4 */

TIM_Cmd(TIM4, ENABLE);


while
(1);


}

As a result of this program, LED is always on (this is by default, since LED is on for reset state), but it should be blinking with 50% duty-cycle, and with 1 sec time period. I also tried to ''send'' a PWM signal on GPIO output and to monitor that port/pin with oscilloscope - in this case the output pin used is always at low level, i.e. in reset state. Can you please tell me what am I doing wrong? Thanks! Best, Marko.
    This topic has been closed for replies.

    2 replies

    Tesla DeLorean
    Guru
    July 4, 2014
    Posted on July 04, 2014 at 20:12

    Can you please tell me what am I doing wrong?

    PC13 has no relationship with TIM4_CH1, you can't invent connectivity, review the data manual for your part.
    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    marko2
    marko2Author
    Associate II
    July 7, 2014
    Posted on July 07, 2014 at 10:28

    Now that I connected TIM4 CH1 to GPIOD Pin12, PWM works just fine.

    Thanks!

    Marko.