AnsweredAssumed Answered

TIM1 3 phase PWM setup

Question asked by usafape on Nov 12, 2013
Latest reply on Nov 13, 2013 by Clive One
HELP!!!!

I've been banging my head against my keyboard and I can't find what's wrong with this code:

/*********************************************************
 ****         INITIALIZE TIMER AND PWM OUTPUTS       *****
 *********************************************************/
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_OCInitTypeDef  TIM_OCInitStructure;
    TIM_BDTRInitTypeDef TIM_BDTRInitStruccture;
 
    /* GPIOC and GPIOB clock enable */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
 
    /* TIM1 clock enable */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
 
    /* Connect TIM1 pins to AF1 */
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource8,  GPIO_AF_TIM1);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9,  GPIO_AF_TIM1);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_TIM1);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_TIM1);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_TIM1);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_TIM1);
 
    /* GPIOA Configuration:
     * Phase A high: TIM1_CH1(PA8)
     * Phase B high: TIM1_CH2(PA9)
     * Phase C high: TIM1_CH3(PA10) */
    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_NOPULL ;
    GPIO_InitStructure.GPIO_Pin = PIN_PHASE_A_H | PIN_PHASE_B_H | PIN_PHASE_C_H;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
 
    /* GPIOB Configuration:
     * Phase A low: TIM1_CH1N(PB13)
     * Phase B low: TIM1_CH2N(PB14)
     * Phase C low: TIM1_CH3N(PB15) */
    GPIO_InitStructure.GPIO_Pin = PIN_PHASE_A_L | PIN_PHASE_B_L | PIN_PHASE_C_L;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
 
    /* TIM1 enable counter */
    TIM_Cmd(TIM1, ENABLE);
 
    /* PWM Mode configuration */
    TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set;
    TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = PWM_Duty;
 
    /* PWM1 Mode configuration: Channel1 */
    TIM_OC1Init(TIM1, &TIM_OCInitStructure);
    TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
 
    /* PWM1 Mode configuration: Channel2 */
    TIM_OC2Init(TIM1, &TIM_OCInitStructure);
    TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
 
    /* PWM1 Mode configuration: Channel3 */
    TIM_OC3Init(TIM1, &TIM_OCInitStructure);
    TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
 
    TIM_BDTRInitStruccture.TIM_LOCKLevel = 0;
    TIM_BDTRInitStruccture.TIM_DeadTime = 10;
    TIM_BDTRInitStruccture.TIM_AutomaticOutput = 0;
    TIM_BDTRInitStruccture.TIM_Break = 0;
    TIM_BDTRInitStruccture.TIM_BreakPolarity = 0;
    TIM_BDTRInitStruccture.TIM_OSSIState = 0;
    TIM_BDTRInitStruccture.TIM_OSSRState = 0;
    TIM_BDTRConfig(TIM1, &TIM_BDTRInitStruccture);
 
    /* Compute the prescaler value */
    PrescalerValue = (uint16_t) ((SystemCoreClock) / TIMER_COUNTER_CLOCK) - 1;
 
    /* Time base configuration */
    TIM_TimeBaseStructure.TIM_Period = (uint16_t) (TIMER_COUNTER_CLOCK / PWM_FREQ);
    TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
 
    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
 
    TIM_CtrlPWMOutputs(TIM1, ENABLE);

I get PWM signals on channels 2 and 3, but channel 1 does not work. The positive output is always 0 and the negative output is always 1. I checked the compare value and it's correct. Is there any other place I need to look at???? I am using the STL32F4 discovery board.

Thanks,

Outcomes