AnsweredAssumed Answered

STM32F407 PWM issues

Question asked by gulin.marko on Jul 4, 2014
Latest reply on Jul 7, 2014 by gulin.marko
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:

01.int main(void)
02.{
03. 
04.    GPIO_InitTypeDef GPIO_InitStructure;
05.    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
06.    TIM_OCInitTypeDef TIM_OCInitStructure;
07. 
08.    /* Enable GPIOC */
09.    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
10. 
11.    /* Enable TIM4 */
12.    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
13. 
14.    /* Configure GPIOC to access LED */
15.    GPIO_StructInit(&GPIO_InitStructure); // initalization
16. 
17.    GPIO_PinAFConfig(GPIOC, GPIO_PinSource13, GPIO_AF_TIM4);
18. 
19.    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; // LED @ Olimex-STM32-H407
20.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
21.    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
22.    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
23.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
24.    GPIO_Init(GPIOC, &GPIO_InitStructure);
25. 
26.    TIM_TimeBaseStructInit(&TIM_TimeBaseInitStructure); // initialization
27. 
28.    TIM_TimeBaseInitStructure.TIM_Prescaler = 40000; // 40 MHz -> 1 kHz (1 ms)
29.    TIM_TimeBaseInitStructure.TIM_Period = 1000; // 1000 ms = 1 s
30.    TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;
31.    TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
32.    TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitStructure);
33. 
34.    TIM_OCStructInit(&TIM_OCInitStructure); // initialization
35. 
36.    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
37.    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
38.    TIM_OCInitStructure.TIM_Pulse = 500;
39.    TIM_OC1Init(TIM4, &TIM_OCInitStructure);
40. 
41.    /* Enable TIM4 */
42.    TIM_Cmd(TIM4, ENABLE);
43. 
44.    while(1);
45. 
46.}


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.

Outcomes