Skip to main content
enrico239955_st
Associate III
May 27, 2014
Question

weird problem with clocks and timers

  • May 27, 2014
  • 11 replies
  • 1790 views
Posted on May 27, 2014 at 12:04

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?
    This topic has been closed for replies.

    11 replies

    waclawek.jan
    Super User
    May 28, 2014
    Posted on May 28, 2014 at 15:48

    > The library is not bad, but undocumented.

    It may be not bad, but in my opinion it is simply superfluous.

    And it *is* sort of documented, but that documentation has its shortcomings - stressing the need to fully (or appropriately) init the init_structs is one of those.

    > And if I am not using the library, I think I would have more problems because I do not know which parameters/registers I have to set for a particular function.

    That should be clear from reading the chips' basic documentation (mainly manual, but also datasheet,appnotes,examples - all of which have issues too, but that's another story) - and using the ''library'' won't eliminate the need for that anyway.

    JW