AnsweredAssumed Answered

PWM Output on STM32F2XX (Solved)

Question asked by turbofishy on Feb 2, 2012
Latest reply on Jul 18, 2016 by Clive One

I am currently toying around with the KEIL MCBSTM32F200 Evaluation Board
(the STM32F207VG uC) and have run into some problems with the PWM output.

I want a simple PWM-signal on TIM1, Channel1 (PA8 for STM32F205&207) using
the standard peripheral library for STM32Fxx.

The code below is a mix from the different examples from the standard
peripheral library examples for PWM, and I'm sure something critical is

After initializing, all TIM1 registers looks "ok" (as far as I can tell, the
timer is a bit too advanced for me...) and the TIM1_CNT register is counting
up and resetting.

When connecting the oscilloscope to PA8, all I get is a steady 3.3V signal,
no PWM.

What am I missing here? It's quite frustrating since all other peripherals
I'm using are working fine. Thanks in advance.
(code also at Pastebin:

#include "stm32f2xx.h"
#include "stm32f2xx_gpio.h"
#include "stm32f2xx_rcc.h"
#include "stm32f2xx_tim.h"
#include "system_stm32f2xx.h"

void main(void) {
     TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
     TIM_OCInitTypeDef          TIM_OCInitStructure;
     GPIO_InitTypeDef          GPIO_InitStructure;
     uint16_t                    period;
     uint16_t                    pulse;

     /* Initialize clock - defined in system_stm32f2xx.h */

     /* Compute the value for the ARR register to have a period of 20 KHz */
     period = (SystemCoreClock / 20000 ) - 1;     

     /* Compute the CCR1 value to generate a PWN signal with 50% duty cycle */
     pulse = (uint16_t) (((uint32_t) 5 * (period - 1)) / 10);

     /* GPIOA clock enable */
     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

     /* Initialize PA8, Alternative Function, 100Mhz, Output, Push-pull */
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
     GPIO_Init(GPIOA, &GPIO_InitStructure);
     GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1);

     /* TIM1 clock enable */
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);

     /* Timer Base configuration */
     TIM_TimeBaseStructure.TIM_Prescaler = 0;
     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
     TIM_TimeBaseStructure.TIM_Period = period;
     TIM_TimeBaseStructure.TIM_ClockDivision = 0;
     TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
     TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

     /* Channel 1 output configuration */
     TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
     TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
     TIM_OCInitStructure.TIM_Pulse = pulse;
     TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
     TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
     TIM_OC1Init(TIM1, &TIM_OCInitStructure);

     /* TIM1 counter enable */
     TIM_Cmd(TIM1, ENABLE);

     /* TIM1 Main Output Enable */
     TIM_CtrlPWMOutputs(TIM1, ENABLE);

     /* forever... */
     while (1) {