cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 PWM starting with different Polarity and shift time state

tropinka06
Associate
Posted on May 16, 2015 at 11:29

Hi.

I work on STM32F4 MCU. For current project I work with 3 PWM are configured on TIM1 and Channels 1, 2, 3, GPIOs PE9, PE11, PE13.

Every channel configured with 60 degrees shift. When I do ''Enable'' for all channels

TIM_CCxCmd

(TIM1, TIM_Channel_1, TIM_CCx_Enable);

TIM_CCxCmd

(TIM1, TIM_Channel_2, TIM_CCx_Enable);

TIM_CCxCmd

(TIM1, TIM_Channel_3, TIM_CCx_Enable);

 they are work with 60 degres shift as defined, but every start pulse can be started from different state - high or low in the 0-120 degrees time.

How can I define PWM output that Channel1 will start every time with ''Enable'' channel and from low to high state (0 degrees delay), not shifted between 0-120 degrees.

Thanks.

This is my part code.

void

GPIO_Configuration

(void)

{

    GPIO_InitTypeDef GPIO_InitStructure;

    

    // GPIOE clock enable

   

RCC_AHB1PeriphClockCmd

(RCC_AHB1Periph_GPIOE, ENABLE);

    //------------------------------------------------------------------------    

    // PWM GPIOs

    // Connect TIM1 pins

   

GPIO_PinAFConfig

(GPIOE, GPIO_PinSource9,  GPIO_AF_TIM1); // PE9  TIM1_CH1  

   

GPIO_PinAFConfig

(GPIOE, GPIO_PinSource11, GPIO_AF_TIM1); // PE11 TIM1_CH2 

   

GPIO_PinAFConfig

(GPIOE, GPIO_PinSource13, GPIO_AF_TIM1); // PE13 TIM1_CH3 

    

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_11 | GPIO_Pin_13;

    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_DOWN; 

   

GPIO_Init

(GPIOE, &GPIO_InitStructure);

}

void

TIM1_Configuration

(uint32_t PWM_Frequency)

{

    RCC_ClocksTypeDef RCC_ClocksStructure;

    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

    TIM_OCInitTypeDef  TIM_OCInitStructure;

    int Prescaler, Period;

    uint32_t TimerFrequency = 0;

    

    // enable peripheral clock for TIM1

   

RCC_APB2PeriphClockCmd

(RCC_APB2Periph_TIM1, ENABLE);

    // Get clocks

RCC_GetClocksFreq

(&RCC_ClocksStructure);

    // if(TIMx == TIM1)

TimerFrequency = RCC_ClocksStructure.HCLK_Frequency;

    Prescaler = 1;

    Period = TimerFrequency / 2 / PWM_Frequency;   // KHz

    // The toggle halves the frequency

    

    /* Time base configuration */

    TIM_TimeBaseStructure.TIM_Period = Period - 1;

    TIM_TimeBaseStructure.TIM_Prescaler = Prescaler - 1;

    TIM_TimeBaseStructure.TIM_ClockDivision = 0;

    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

   

TIM_TimeBaseInit

(TIM1, &TIM_TimeBaseStructure);

    

    /* Output Compare Toggle Mode configuration: Channel 1, 2 and 3 */

    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;

    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;

    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

    TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCPolarity_High;

    

    /* Default unused fields TIM1 more sensitive */

    TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;

    TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;

   

    /* Don't want to set 0 or Period, but have 3 points at 120 degrees from each other */

    TIM_OCInitStructure.TIM_Pulse = ((Period * 1) / 3) - 1;

   

TIM_OC1Init

(TIM1, &TIM_OCInitStructure);

    

    TIM_OCInitStructure.TIM_Pulse = ((Period * 2) / 3) - 1;

   

TIM_OC2Init

(TIM1, &TIM_OCInitStructure);

    

    TIM_OCInitStructure.TIM_Pulse = ((Period * 3) / 3) - 1;

   

TIM_OC3Init

(TIM1, &TIM_OCInitStructure);  

 

    /* Enable pins, TIM1 more sensitive */

   

TIM_CtrlPWMOutputs

(TIM1, ENABLE);

     /* Configures the TIM1 Update Request Interrupt source (SETS the CR1->URS bit)*/   

     

TIM_UpdateRequestConfig

(TIM1,TIM_UpdateSource_Global);

    

    /* TIM1 enable counter */

   

TIM_Cmd

(TIM1, ENABLE);

    // Disable Channels

   

TIM_CCxCmd

(TIM1, TIM_Channel_1, TIM_CCx_Disable);

   

TIM_CCxCmd

(TIM1, TIM_Channel_2, TIM_CCx_Disable);

   

TIM_CCxCmd

(TIM1, TIM_Channel_3, TIM_CCx_Disable);

}
0 REPLIES 0