# Inaccurate timer interrupt STM32

I wrote a simple program to toggle an LED on and off using the TIM1 interrupt on an STM32F0 discovery board.

CLK Freq = 48MHz
Prescalar = 48000

Using the following formula to calculate the timer frequency:

Timer frequency = (CLK FREQ/ (Prescalar -1) * ARR)

In my case it is equal to 1 second. If I run this code without an interrupt, it toggles the LED on and off at 1-second intervals, which is what I want.

As soon as I use the ISR, my LED TOGGLE is really fast (approximately 500ms), if I make my Timer_Value = 60000

void TIM1_BRK_UP_TRG_COM_IRQHandler(void)
{
Timer_Value++;

if(Timer_Value == 60000)
{
if(Timer_Change_Status == Timer_Deactive)
{
SendBitToPortAndPin(LEDIND_Port, LEDIND_Pin,1);
Timer_Change_Status = Timer_Active;
}
else {
SendBitToPortAndPin(LEDIND_Port, LEDIND_Pin,0);
Timer_Change_Status = Timer_Deactive;
}
Timer_Value = 0;
}
TIM1->EGR |= 0x01;
}


In my main loop, I call the following function

void COnfig_Start_Timer(void)
{
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;

NVIC_EnableIRQ(TIM1_BRK_UP_TRG_COM_IRQn);   ///Enable interrupt
NVIC_SetPriority(TIM1_BRK_UP_TRG_COM_IRQn,1);///Set interrupt priority
TIM1->DIER |= TIM_DIER_UIE;                 ///Activate Timer Interrupt
TIM1->CR1 |= TIM_CR1_CEN;                   ///start Timer

while(1)
{

}
}


I would like to know what is causing my ISR to fire so frequently, when it should only fire once every second. I have referred to my reference manual with no luck.

• Except for your problem, probably you have to use 47999 and 999 for PSC and ARR, according to the formula: tim uptade frequency = TIM_CLK/(TIM_PSC+1)/(TIM_ARR + 1), but this does not explain the 0.5 instead of 1.0 seconds firing. – Michel Keijzers Jan 9 '19 at 10:21