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

Parents Reply Children
No data