cancel
Showing results for 
Search instead for 
Did you mean: 

Timer 2 isr not firing unless in debug mode

danielblesener9
Associate II
Posted on August 14, 2013 at 14:25

Strange problem...Timer 2 works great when debugging. When I turn my supply off and reboot, everything is hunky dory except timer2. Any clues?

    TIM_DeInit(TIM2);

    //ENABLE CLOCKS

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);

    //TIM2 INIT

    TIM_TimeBaseInitTypeDef TIM2_struct;

    TIM2_struct.TIM_ClockDivision = 0;

    TIM2_struct.TIM_CounterMode = TIM_CounterMode_Up;

    TIM2_struct.TIM_Period = 4000;                      //Timer2 is clcoked at 48MHz with prescaler of 0; SOLVE(1/48000000 * X = 1/8000, X) gives interrupt time of 100uS when being used. Perfect for our SPI

    TIM2_struct.TIM_RepetitionCounter = 0;              //This is only valid for timer 1 and 8

    TIM_TimeBaseInit(TIM2, &TIM2_struct);

    TIM_OCInitTypeDef  TIM_OCInitStructure;

    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;

    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

    TIM_OCInitStructure.TIM_Pulse = 0;

    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;

    TIM_OC1Init(TIM2, &TIM_OCInitStructure);

    TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);

    TIM_ARRPreloadConfig(TIM2, ENABLE);

    /* TIM2 enable counter */

    TIM_Cmd(TIM2, ENABLE);

   

        //INTERRUPT CONFIG

    NVIC_InitTypeDef TIM2_isr_struct;

    TIM2_isr_struct.NVIC_IRQChannel = TIM2_IRQn;

    TIM2_isr_struct.NVIC_IRQChannelCmd = ENABLE;

    TIM2_isr_struct.NVIC_IRQChannelPreemptionPriority = 0;

    TIM2_isr_struct.NVIC_IRQChannelSubPriority = 1;

    NVIC_Init(&TIM2_isr_struct);

   

        /* TIM IT enable */

    TIM_ITConfig(TIM2, TIM_IT_CC1  , ENABLE);

5 REPLIES 5
Posted on August 14, 2013 at 14:45

What is hunky dory?

How do you know that the ISR is not firing?

Post a minimal but complete compilable example exhibiting the problem.

JW

danielblesener9
Associate II
Posted on August 14, 2013 at 15:31

Hunky dory is just saying all is fine.

If I place the following code in the isr, it makes a square wave when debugging. Then stops when I reboot. This tells me that the isr is not firing.

                    if(toggle)

                        GPIO_SetBits(GPIOF, GPIO_Pin_0);

                    else

                        GPIO_ResetBits(GPIOF, GPIO_Pin_0);

                    toggle = !toggle;

Posted on August 14, 2013 at 16:12

The request for minimal but functional code has several purposes, one of them is to give the OP an impulse to rethink his problem. Other is to increase chances that somebody else is willing to have a look at it.

You neglect to initialize your TIM_TimeBaseInitStruct, namely the prescaler field.

JW

Posted on August 14, 2013 at 16:28

If this is C++ (mid function variables), then make sure it's not mangling the IRQHandler name.

Also not sure the reason for CC1 and Pulse=0, might make more sense to use Update?

Finally, needs 4000-1 for the period, and as Jan mentions the Prescaler is probably some random stack junk at this point.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
danielblesener9
Associate II
Posted on August 14, 2013 at 20:39

Thanks for the replies. It was the prescaler. Stupid mistake. Overlooked that one for many hours.