This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Timer interrupt occasionally fires too early...

Hello All
New to this forum and had a couple questions/observations regarding the standard peripheral driver (v3.10). Specifically, the stm32f10x_tim.c/h driver.

I am developing on a STM32F103ZE using Keil Microvision 4.6.

I am observing odd behavior out of my TIM3 peripheral where the interrupt fires pre-maturely before it is supposed to.

My init is as follows...

  /* Enable the TIM3 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQChannel;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_Init(&NVIC_InitStructure);

  /* TIM3 Peripheral Configuration */
  TIM_TimeBaseStructure.TIM_Period = 2666;//26.66 mSec timer
  TIM_TimeBaseStructure.TIM_Prescaler = 719;   // TIM3 counter clock = TIM3CLK / (Prescaler+1)
  TIM_TimeBaseStructure.TIM_ClockDivision = 1;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);//enable isr
  TIM_SetCounter(TIM3,0x0000);//clear counter reg
  TIM_Cmd(TIM3, ENABLE);//enable the counter

My ISR is as follows...

volatile bool b30Milliseconds;//flag for ADC conversions
volatile bool b240Milliseconds;//flag for PID control
volatile bool bInitComplete;//flag for allowing relays etc to function
/**
  * @brief  This function handles TIM3 Handler.
  * @param  None
  * @retval None
  */
void TIM3_IRQHandler(void)
{
  static int iTicks240Milliseconds = 0;
  static int iTicks10Seconds = 0;

  TIM_ClearITPendingBit(TIM3, TIM_IT_Update);

  b30Milliseconds = TRUE;
  if(iTicks240Milliseconds++ >= 6){
    b240Milliseconds = TRUE;
    iTicks240Milliseconds = 0;
  }
  if(iTicks10Seconds++ > 333){
    bInitComplete = TRUE;//allow relays, analog out...etc to function
  }

}


What I observe is that the first timer interrupt occurs immediately after I call TIM_ITConfig. Wondering how this happens when I haven't enabled the counter yet?

Next, the 2nd timer interrupt happens about 24.9mSec after the first (should be 26.66)?

After that the system seems to run at the programmed 26.66 mSec rate but will randomly fire at the 24.9mSec rate (happens every few minutes to every few hours...)

Any ideas what might be happening...I am guessing I may not be getting the init right??
Regards
Rich

  • Just a question:

      if(iTicks10Seconds++ > 333){
        bInitComplete = TRUE;//allow relays, analog out...etc to function
      }
    


    Shouldn't you have a clear of iTicks10Seconds? Without clear, it will set the flag att 334, 335, 336, 337, ... which might possibly result in lots of interesting things depending on what your main loop does if bInitComplete is set.

    As it is now, you seem to set that flag every 26.66 ms.

  • As an update to my previous post...
    I found that there is a newer version of the driver 3.5.0. Updating this file had no effect...fyi


  • Shouldn't you have a clear of iTicks10Seconds? Without clear, it will set the flag att 334, 335, 336, 337, ... which might possibly result in lots of interesting things depending on what your main loop does if bInitComplete is set.

    This is a "one-time" flag that gets set once the sensor system has stabilized. As long as nothing sets the flag to FALSE...

    Now the b30Milliseconds (really 26.66 ms) flag is important and is processed in the "background" loop when set. If anything, I'd expect the processing done when this flag gets set might be delayed...not processed earlier...??

    As noted earlier, as soon as I enable the interrupt I immediately get interrupts regardless of the state of the CEN bit in TIMx->CR1...not sure how this is happening