Skip to main content
Visitor II
July 21, 2025
Question

STM8L152C4 TIMER2 and 4 - Interrupt not working (Solved)

  • July 21, 2025
  • 1 reply
  • 1566 views
Hi 
I am writing this post to help the community. 
I have struggled a while before to get all this "Interrupt" receipe working with "ST Visual Develop"
and COSMIC compiler. Development and tests with only one timer enabled at the time.
Note this code is always looping in "delay_ms" function when not interrupted,
make your own modification.
See also C code file attached
Good luck ....
SuperGDx
 
1- Timers configuration
// Function to initialize TIM4 for 1ms interrupt
void TIM4_Config(void) {
//Preload Buffered, Int generated UV OV and Couter enable
TIM4->CR1   = 0x00; // Not enabled at start
TIM4->CR2   = 0x00;
TIM4->SMCR   = 0x00;
TIM4->DER  = 0x00;
TIM4->IER   = 0x01;
TIM4->SR1   = 0x00;
TIM4->EGR   = 0x01;
TIM4->CNTR   = 0x00;
TIM4->PSCR  = 0x00;
TIM4->ARR   = 0xFF;
}
 
void TIM2_Config(void) {
//Preload Buffered, Int generated UV OV and Couter enable
TIM2->CR1   = 0x00; // Not enabled at start
TIM2->CR2   = 0x00;
TIM2->SMCR   = 0x00;
TIM2->ETR   = 0x00;
TIM2->DER  = 0x00;
TIM2->IER   = 0x01;
TIM2->SR1   = 0x00;
TIM2->SR2   = 0x00;
TIM2->EGR   = 0x01;
TIM2->CCMR1   = 0x00;
TIM2->CCMR2   = 0x00;
TIM2->CCER1   = 0x00;
TIM2->CNTRH   = 0x00;
TIM2->CNTRL   = 0x00;
TIM2->PSCR   = 0x00;
TIM2->ARRH   = 0xFF;
TIM2->ARRL   = 0xFF;
TIM2->CCR1H   = 0x00;
TIM2->CCR1L   = 0x00;
TIM2->BKR   = 0x00;
TIM2->OISR   = 0x00;
}
 
2- Main clock setting, enabling timers to count and then enabling global interrupt.
CLK_DeInit();
CLK_LSICmd(DISABLE);
while(CLK_GetFlagStatus(CLK_FLAG_HSIRDY) == FALSE);
// Timers 2.3 and 4
CLK->PCKENR1 |= 0x5;
TIM4_Config();
TIM2_Config();
 
// Initialize system clock
CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_2);
CLK->SWR |= 0x1;
 
// Initialize GPIO for LED
GPIO_Init_LED();
 
rst_status = RST->SR;
RST->SR = 0xFF;
 
// Enabling either TMR2 or 4 when CR1 is at 0x01
// Not enabled during initialization
TIM4->CR1   = 0x01;
TIM2->CR1   = 0x00;
 
/* Enable interrupt */
enableInterrupts();
  
  
  3- Definition in stm8l15x_it.c
  INTERRUPT_HANDLER(TIM1_UPD_OVF_TRG_COM_IRQHandler,23)
  {
      GPIO_ToggleBits(GPIOB, GPIO_Pin_0); // Example: Toggle an LED
      TIM4_ClearITPendingBit(TIM2_IT_Update);
  }
 
  */
  INTERRUPT_HANDLER(TIM4_UPD_OVF_TRG_IRQHandler,25)
  {
      GPIO_ToggleBits(GPIOB, GPIO_Pin_0); // Example: Toggle an LED
      TIM4_ClearITPendingBit(TIM2_IT_Update);
  }
  
  4- Definition in st8_interrupt_vector.c
  Specially timer 4 got it working when located at 25, not 23 has I found on the WEB.
  
struct interrupt_vector const _vectab[] = {
{0x82, (interrupt_handler_t)_stext}, /* reset */
{0x82, NonHandledInterrupt}, /* trap  */
{0x82, NonHandledInterrupt}, /* irq0  */
{0x82, NonHandledInterrupt}, /* irq1  */
{0x82, NonHandledInterrupt}, /* irq2  */
{0x82, NonHandledInterrupt}, /* irq3  */
{0x82, NonHandledInterrupt}, /* irq4  */
{0x82, NonHandledInterrupt}, /* irq5  */
{0x82, NonHandledInterrupt}, /* irq6  */
{0x82, NonHandledInterrupt}, /* irq7  */
{0x82, NonHandledInterrupt}, /* irq8  */
{0x82, NonHandledInterrupt}, /* irq9  */
{0x82, NonHandledInterrupt}, /* irq10 */
{0x82, NonHandledInterrupt}, /* irq11 */
{0x82, NonHandledInterrupt}, /* irq12 */
{0x82, NonHandledInterrupt}, /* irq13 */
{0x82, NonHandledInterrupt}, /* irq14 */
{0x82, NonHandledInterrupt}, /* irq15 */
{0x82, NonHandledInterrupt}, /* irq16 */
{0x82, NonHandledInterrupt}, /* irq17 */
{0x82, NonHandledInterrupt}, /* irq18 */
{0x82, (interrupt_handler_t)&TIM2_UPD_OVF_TRG_BRK_USART2_TX_IRQHandler}, 
{0x82, NonHandledInterrupt}, /* irq20 */
{0x82, NonHandledInterrupt}, /* irq21 */
{0x82, NonHandledInterrupt}, /* irq22 */
{0x82, NonHandledInterrupt}, /* irq23 */
{0x82, NonHandledInterrupt}, /* irq24 */
{0x82, (interrupt_handler_t)TIM4_UPD_OVF_TRG_IRQHandler}, 
{0x82, NonHandledInterrupt}, /* irq26 */
{0x82, NonHandledInterrupt}, /* irq27 */
{0x82, NonHandledInterrupt}, /* irq28 */
{0x82, NonHandledInterrupt}, /* irq29 */
};

 

    This topic has been closed for replies.

    1 reply

    SuperGDxAuthor
    Visitor II
    July 21, 2025

    Note: We should read:

    TIM2_ClearITPendingBit(TIM2_IT_Update)

    instead of

    TIM4_ClearITPendingBit(TIM2_IT_Update)