AnsweredAssumed Answered

weird problem with clocks and timers

Question asked by yun.yan on May 27, 2014
Latest reply on May 28, 2014 by waclawek.jan
Hi all,

at the moment I am trying to configure the update interrupt of timer 2 like this. The update interrupt should occur every 4ms. With the following code everything is fine, the pin is toggling every 4 ms.

TIM_OCInitTypeDef  TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
   
// TIM clock enable
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
   
// Time base configuration
TIM_TimeBaseStructure.TIM_Prescaler = 9; // (9+1) * 1/84MHz = 119ns
TIM_TimeBaseStructure.TIM_Period = 33600; // 119ns * 33600 = 4ms
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
   
// Enable the TIM Interrupt
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
   
// enable it and timer
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM2, ENABLE);
  
...
  
void TIM2_IRQHandler(void){
    GPIOD->ODR ^= GPIO_Pin_15; //toggle
  
    // tim update
    if ((TIM2->SR & TIM_IT_Update) != (uint16_t)RESET){
        TIM2->SR = (uint16_t)~TIM_IT_Update;
    }
}

but if I try to do the same with timer 1, the frequency is not as expected but veeeeery slow (600ms)

TIM_OCInitTypeDef  TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
   
// TIM clock enable
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM1, ENABLE);
   
// Time base configuration
TIM_TimeBaseStructure.TIM_Prescaler = 19; // (19+1) * 1/168MHz = 119ns
TIM_TimeBaseStructure.TIM_Period = 33600; // 119ns * 33600 = 4ms
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
   
// Enable the TIM Interrupt
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TIM10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
   
// enable it and timer
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM1, ENABLE);
  
...
  
void TIM1_UP_TIM10_IRQHandler(void){
    GPIOD->ODR ^= GPIO_Pin_15; //toggle
  
    // tim update
    if ((TIM1->SR & TIM_IT_Update) != (uint16_t)RESET){
        TIM1->SR = (uint16_t)~TIM_IT_Update;
    }
}

Does somebody has an explanation for this behavior?


Outcomes