2020-12-10 09:57 PM
Hi, I'm trying to Implement an interrupt every 60 seconds, however something is missing. The TIM2 itself is working but can't seem to generate any interrupt. Any idea on what am i missing? The following is the code I am using.
thanks
//Required Headers
#include "STM8S.h"
#define test_LED GPIOB, GPIO_PIN_5
void clock_setup(void);
void TIM2_setup(void);
void main(void)
{
GPIO_Init (test_LED, GPIO_MODE_OUT_PP_LOW_SLOW);
clock_setup();
TIM2_setup();
while(1);
}
void clock_setup(void)
{
CLK_DeInit();
CLK_HSECmd(DISABLE);
CLK_LSICmd(DISABLE);
CLK_HSICmd(ENABLE);
while(CLK_GetFlagStatus(CLK_FLAG_HSIRDY) == FALSE);
CLK_ClockSwitchCmd(ENABLE);
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV8);
CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);
CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI,
DISABLE, CLK_CURRENTCLOCKSTATE_ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, ENABLE);}
void TIM2_setup(void)
{
TIM2_DeInit();
TIM2_TimeBaseInit(TIM2_PRESCALER_32768, 3662);
TIM2_ITConfig( TIM2_IT_UPDATE, ENABLE );
TIM2_PrescalerConfig( TIM2_PRESCALER_32768, TIM2_PSCRELOADMODE_IMMEDIATE );
TIM2_ClearITPendingBit( TIM2_IT_UPDATE );
TIM2_Cmd( ENABLE );
}
In the stm8s_it.c
INTERRUPT_HANDLER(TIM2_UPD_OVF_BRK_IRQHandler, 13)
{
GPIO_Init (test_LED, GPIO_MODE_OUT_PP_LOW_SLOW);
/* Clear Interrupt Pending bit */
TIM2_ClearITPendingBit(TIM2_IT_UPDATE);
}
2021-08-24 01:49 PM
Hay
Could you solve this? I have same problem with almost similar code.
If I enable TIM2_Interrupt_Enable flag in debug peripheral watch, it works, but does not enable by software.
If you have solution, let me know.
2021-08-25 06:42 AM
Hi!
Does your compiler automatically enable interrupts in the while (1) loop? Is this main loop implemented with wfi instruction or endless jumps?
How do you know the interrupt is not executed? In the handler you setup the port with it's already settings..., do you check it with a debugger?
2021-08-25 07:10 AM
No. Here is my code for enabling instruction.
void timer2_config(void)
{
TIM2_ClearITPendingBit(TIM2_IT_UPDATE); // 10 mSec Timer
TIM2_TimeBaseInit( TIM2_PRESCALER_16, 1000); // 1mSec
TIM2_ITConfig(TIM2_IT_UPDATE, ENABLE);
TIM2_Cmd(ENABLE);
}
void HW_Init(void)
{
clk_config();
gpio_config();
timer1_config();
enableInterrupts();
}
I check with debugger in single step.
While debugging, all TIM2 register can be seen being loaded in Peripheral window including TIM2_Cmd(ENABLE), but it fails to Enable
TIM2_ITConfig(TIM2_IT_UPDATE, ENABLE)
Now running in free run with break point at TIM2_IT_UPDATE interrupt in Interrupt file, break point is never reached.
Now in Peripheral window, if I set TIM2_IT_UPDATE bit manually (by clicking mouse) and free run program, it breaks at TIM2_IT_UPDATE Interrupt in Interrupt file.
I know it seems very absurd, but that's what it happens.
I am using stm8s in many project, (50+) but never encountered this problem.
TIM1 works perfectly all right with break in it's interrupt.