AnsweredAssumed Answered

STM32F4 TIM1 PWM missing ch1..3

Question asked by John F. on Jun 28, 2012
Latest reply on Jun 29, 2012 by John F.
STM32F4 TIMER 1 PWM1. Please can someone tell me how to enable channels 1, 2 and 3. Channel 4 is fine generating a PWM at 0..100% at 20Hz. I know channels 1..3 hardware is different - just can't figure out what to do to get the outputs enabled.

Thanks.

#include "stm32f4xx.h"  //CMSIS Cortex-M4 Device Peripheral Access Layer Header File.
#include "main.h"
 
 
uint16_t CCR1_Val = 30; //LED PWM range 0..100
uint16_t CCR2_Val = 40;
uint16_t CCR3_Val = 50;
uint16_t CCR4_Val = 60;
 
 
int main (void)
{
    /*
    At this stage the microcontroller clock setting is already configured by the
    SystemInit() function called from startup file (startup_stm32f4xx.s).   To reconfigure
    the default setting of SystemInit() function, refer to  system_stm32f4xx.c file.
    */
     
    //enable peripheral clocks before attempting to initialise them
    //for PWM based on Timer1 using GPIO on port E
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
 
     
    //initialise the NVIC before configuring or enabling peripherals
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); //4 bits for pre-emption priority 0 bits for subpriority
 
     
    //initialise the GPIO and alternate functions to set up hardware
    GPIO_Configuration_PWM();
     
     
    //finally, initialise peripherals as required
    Timer1_Configuration();
 
    while(1); // Spin endlessly
}
 
void GPIO_Configuration_PWM(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
 
    /* GPIOE Configuration: TIM1 CH1 (PE9), TIM1 CH2 (PE11), TIM1 CH3 (PE13) and TIM1 CH4 (PE14) */
    GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_9 | GPIO_Pin_11 | GPIO_Pin_13 | GPIO_Pin_14);
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
    GPIO_Init(GPIOE, &GPIO_InitStructure);
 
    // Connect TIM1 pins to AF
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource9,  GPIO_AF_TIM1);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_TIM1);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource13, GPIO_AF_TIM1);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource14, GPIO_AF_TIM1);
}
 
void Timer1_Configuration(void)
{
    //timer 1 is clocked by APB2 at 84MHz, prescale by 42000 to get 2kHz clock
    //use a period of 100 cycles to get 0..100% granularity and 20Hz frequency
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_OCInitTypeDef  TIM_OCInitStructure;
 
     
    TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
    TIM_TimeBaseStructure.TIM_Period = (100-1);
    TIM_TimeBaseStructure.TIM_Prescaler = (42000-1);
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
     
    /* PWM1 Mode configuration: Channel1 */
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;  
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
 
    TIM_OC1Init(TIM1, &TIM_OCInitStructure);
    TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
 
    /* PWM1 Mode configuration: Channel2 */
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
    TIM_OC2Init(TIM1, &TIM_OCInitStructure);
 
    TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
 
    /* PWM1 Mode configuration: Channel3 */
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
    TIM_OC3Init(TIM1, &TIM_OCInitStructure);
 
    TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
 
    /* PWM1 Mode configuration: Channel4 */
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
    TIM_OC4Init(TIM1, &TIM_OCInitStructure);
 
    TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
 
    TIM_ARRPreloadConfig(TIM1, ENABLE);
 
    //required for timers 1 or 8
    TIM_CtrlPWMOutputs(TIM1, ENABLE);
     
    /* TIM1 enable counter */
    TIM_Cmd(TIM1, ENABLE);
}

Outcomes