cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 Timer2 off - Pin status problem

totally
Associate II
Posted on September 12, 2013 at 23:49

Hello,

I have problems to set up the right pin state while TIM2 is configured in PWM mode and turned off. The corresponding CC output pin stays HIGH while timer is off but I strongly need it to be LOW. I thought TIM_OCIdleState would solve this problem but it doesn't.

Heres the initialization code:

        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

        GPIO_InitTypeDef GPIO_InitStruct;

        GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;

        GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;

        GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

        GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;

        GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_DOWN;

        GPIO_Init(GPIOA, &GPIO_InitStruct);

        GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_TIM2);

        TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;

        TIM_TimeBaseStruct.TIM_Prescaler = 0;

        TIM_TimeBaseStruct.TIM_CounterMode = TIM_CounterMode_Up;

        TIM_TimeBaseStruct.TIM_Period = 103;

        TIM_TimeBaseStruct.TIM_ClockDivision = TIM_CKD_DIV1;

        TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStruct);

        TIM_OCInitTypeDef TIM_OCInitStruct;

        TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;

        TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;

        TIM_OCInitStruct.TIM_Pulse = 80;

        TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;

        TIM_OCInitStruct.TIM_OCIdleState = TIM_OCIdleState_Reset;

        TIM_OCInitStruct.TIM_OutputNState = TIM_OutputState_Disable;

        TIM_OC3Init(TIM2, &TIM_OCInitStruct);

    /* --- Interrupt TIM1 CC Event --- */

    NVIC_InitTypeDef NVIC_InitStructure;

    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 5;

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 5;

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStructure);

    TIM_SetCompare3(TIM2,80);

    TIM_ITConfig(TIM2, TIM_IT_CC3, ENABLE);

    TIM_Cmd(TIM2, DISABLE);

Is there a way to get the output pin low while the timer is not running?

(btw., PWM works fine)

4 REPLIES 4
Posted on September 13, 2013 at 00:32

Set the pulse width to zero (TIM2->CCR3 = 0) first?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
totally
Associate II
Posted on September 13, 2013 at 08:46

Well, this works somehow, but it's not really a solution to my problem beacause as soon as I write to the CC register the output goes in high state, but I need the very fist PWM cycle to be exact! It has now a greater dutycycle than the others.

I think I could somehow workaround this problem but I just dont understand why this pin is high

Posted on September 13, 2013 at 13:35

I just dont understand why this pin is high

Me neither, you should be able to control period/duty/phase without disabling the timer.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
totally
Associate II
Posted on September 16, 2013 at 00:06

Okay, got it, adding

TIM_OCxPreloadConfig(TIMx, TIM_OCPreload_Enable);

does the trick ;)