cancel
Showing results for 
Search instead for 
Did you mean: 

How to Update Timer period ???

lizerd
Associate III
Posted on July 23, 2011 at 21:37

I have tried to update Timer 2 with new period value without any good resualt.

I use two channels on Timer 2 as PWM outputs,

The PWM works fine, But when i tries to update the period value in timer 2 the PWM signals disappears.

sometimes i manage to update the timer with a period value that is larger then the one before. but never a smaller value.

please i really would like some help with this one. i have tried this for to long now.

My update code so far.

    /* Time Base configuration */

    TIM_TimeBaseStructure.TIM_Period = 1000;

    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

    TIM_ClearFlag(TIM2, TIM_FLAG_CC3);

    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

    TIM_OCInitStructure.TIM_Pulse =  500;

    TIM_OC3Init(TIM2, &TIM_OCInitStructure);

And the Timer initilize code

    // Timer clock

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_AFIO, ENABLE);

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

    /* Time Base configuration */

    TIM_TimeBaseStructure.TIM_Period = 2000;

    TIM_TimeBaseStructure.TIM_Prescaler = 0;

    TIM_TimeBaseStructure.TIM_ClockDivision = 0;

    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

   

    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

 

    /* PWM1 Mode configuration: Channel1 */

    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

    TIM_OCInitStructure.TIM_Pulse = 1000;

    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

    TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;

    TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;

 

    // Motor A channel

    TIM_OC2Init(TIM2, &TIM_OCInitStructure);

    TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);

    // Motor B channel

    TIM_OC3Init(TIM2, &TIM_OCInitStructure);

    TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);

    TIM_ARRPreloadConfig(TIM2, ENABLE);

 

    /* TIM1 enable counter */

    TIM_Cmd(TIM2, ENABLE);

    /* TIM1 Main Output Enable */

    TIM_CtrlPWMOutputs(TIM2, ENABLE);

1 REPLY 1
lizerd
Associate III
Posted on July 24, 2011 at 16:48

I found the solution, it sure is not the right one

because i need to reset the timer before and after (TIM_SetCounter(TIM2, 0);) to make it work, and it do not work every time.

Must be a safer and better way to do it ??

void MotorPWM_HighFreq(void)

{

    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

    TIM_OCInitTypeDef  TIM_OCInitStructure;

    GPIO_InitTypeDef GPIO_InitStructure;

    TIM_SetCounter(TIM2, 0);

    /* Time Base configuration */

    TIM_TimeBaseStructure.TIM_Period = 999;

    TIM_TimeBaseStructure.TIM_Prescaler = 0;

    TIM_TimeBaseStructure.TIM_ClockDivision = 0;

   

    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

    TIM_SetCounter(TIM2, 0);

}