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
Auto Reload Register = 1000
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;
}
// reload counter
TIM1->EGR |= 0x01;
}
In my main loop, I call the following function
void COnfig_Start_Timer(void)
{
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;
TIM1->PSC = 48000; ///Load Prescalar
TIM1->ARR = 1000; ///Load Auto Reload value
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.