cancel
Showing results for 
Search instead for 
Did you mean: 

[stm32f407][TIM5] Overflow interrupt-unexpected low frequency

licencje1
Associate II
Posted on October 15, 2014 at 21:49

Hi,

I've got some problem with my code that should run timer5 to overflow and generate interrupt it should appear each 500ms but appears each approx. 3s.

code:

void TimerInit(void){

 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);

    TIM_TimeBaseInitTypeDef timerInitStructure; 

//generated timer interrupt freq. 168MHz/(16800*5000) = 2Hz

    timerInitStructure.TIM_Prescaler = 16799;

    timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;

    timerInitStructure.TIM_Period = 4999;

    timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;

    timerInitStructure.TIM_RepetitionCounter = 0;

    TIM_TimeBaseInit(TIM5, &timerInitStructure);

    TIM_Cmd(TIM5, ENABLE);

NVIC_InitTypeDef nvicStructure;

    nvicStructure.NVIC_IRQChannel = TIM5_IRQn;

    nvicStructure.NVIC_IRQChannelPreemptionPriority = 0;

    nvicStructure.NVIC_IRQChannelSubPriority = 1;

    nvicStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&nvicStructure);

}

void SystemClock_Config(void)

{

//HSE: 8MHz pll set to achieve core and tim5 freq. 168MHz 

RCC_HSEConfig(RCC_HSE_ON);

RCC_PLLConfig(RCC_PLLSource_HSE , 8, 336, 2, 7);

RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK );

RCC_PLLCmd(ENABLE);

RCC_HCLKConfig(RCC_SYSCLK_Div1);

RCC_PCLK1Config(RCC_HCLK_Div4);

RCC_PCLK2Config(RCC_HCLK_Div2);

}

extern void TIM5_IRQHandler(void)

{

if(TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET)

{

TIM_ClearITPendingBit(TIM5, TIM_IT_Update);

GPIOD->ODR ^= 1<<15; //led here

}

}

Could someone help me to deal with that problem?

#interrupt #stm32f407 #timer
3 REPLIES 3
Posted on October 15, 2014 at 21:55

Probably because your chip isn't running at the speed you think it is.

Export the internal clock via MCO pin (PA8)

Your code to switch the clocks pays no attention to the time it takes for them to come ready or lock. Suggest you review code in system_stm32f4xx.c for a better example of how to bring them up.

The APB1 TIMCLK is 84 MHz, probably 8 MHz in your example
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
licencje1
Associate II
Posted on October 15, 2014 at 22:36

@''Export the internal clock via MCO pin (PA8)'' -> I haven't got oscilloscope to measure it :(

@''

The APB1 TIMCLK is 84 MHz, probably 8 MHz in your example'' -> I do not understand could you paraphrase it?

Posted on October 15, 2014 at 22:49

Then just assume it's wrong, and fix the HSE and PLL code.

Your code presumes the TIM5CLK is 168 MHz, it won't be, based on APB1 = SYSCLK/4 it will be 84 MHz. As you're probably running from the 16 MHz HSI, my guess is that TIM5CLK is in fact 8 MHz. You could recode based on that, and shoot the frequency by eye.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..