November 23, 2024, 02:00:17 AM

STM32-E407 Timer 1 PWM question

Started by dvd_video, July 05, 2013, 08:21:39 PM

Previous topic - Next topic

dvd_video

Hi all i am trying to run Timer 1 PWM on pin PE13 ( CH_3 ) and give it period in uSeconds.
Does my Init is as follows, and how should i enter the duty cycle ( again in uSeconds ) 10x.

If i call

InitTimer1PWM(40000);
Timer1PWMSetDuty(30000);

When i call Timer1PWMSetDuty(10000); Pwm drops 3 times ( so far so good )

I am getting PWM of 50 % and period of 400us with Timer1PWMSetDuty(30000); no matter what i put in "InitTimer1PWM".

Any comment will be nice 10x!


My code is as follows ...


void InitTimer1PWM(u32 usPeriod)
{

    GPIO_InitTypeDef        GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_OCInitTypeDef       TIM_OCInitStructure;

// Timer1 1 CH: PE9   Con 12
//   2 CH: PE11  Con 14
//   3 CH: PE13  Con 16
//   4 CH: PE14  Con 17

    // Reset Timer 1
    TIM_DeInit(TIM1);

    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_50MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP ;

    TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
    TIM_TimeBaseStructure.TIM_Period        = (usPeriod-1);
    TIM_TimeBaseStructure.TIM_Prescaler     = (168-1);
    TIM_TimeBaseStructure.TIM_CounterMode   = TIM_CounterMode_Up;
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

    /* PWM1 Mode configuration: Channel3 */
    TIM_OCStructInit(&TIM_OCInitStructure);
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OutputState  = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
    TIM_OCInitStructure.TIM_Pulse        = usPeriod;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OCInitStructure.TIM_OCNPolarity  = TIM_OCPolarity_High;
    TIM_OCInitStructure.TIM_OCIdleState  = TIM_OCIdleState_Reset;
    TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;

// Clock enable
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);

// GPIO Pins function as TIM1 pins
    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);
    GPIO_Init(GPIOE, &GPIO_InitStructure);

    // Timer Stuff
    TIM_OC3Init(TIM1, &TIM_OCInitStructure);
    TIM_OC3PreloadConfig(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);
}

void Timer1PWMSetDuty(u32 usPeriod)
{
   TIM1->CCR3 = usPeriod;
}


I am an automation engineer graduated from TU-Sofia, Bulgaria. My passion is Programming, hardware martial arts and cycling.