Skip to main content
ST MICROCONTOLLER
Associate II
December 21, 2016
Question

Interrupt not working

  • December 21, 2016
  • 12 replies
  • 5923 views
Posted on December 21, 2016 at 13:02

I am trying to run a code in which timer causes an interrupt and everytime interrupt is generated, a variable h is incremented, but it remains zero in quickwatch window. My code is written below in stvd and cosmic compiler.

#include 'stm8s.h'

#include 'stm8s_tim1.h'

#include 'stm8s_itc.h'

#define TIM1_PSCRH (*(volatile uint8_t *)0x5260)

#define TIM1_CR1    (*(volatile uint8_t *)0x5250)

#define TIM1_ARRH    (*(volatile uint8_t *)0x5263)

#define TIM1_ARRL    (*(volatile uint8_t *)0x5264)

#define TIM1_PSCRL    (*(volatile uint8_t *)0x5261)

#define TIM1_IER (*(volatile uint8_t *)0x5254)

int h;

void main(void)

{

            //TIM1_SetCounter(5);enableInterrupts();GPIO_Config();

            //TIM1_DeInit();

TIM1_ARRH=0X55;

TIM1_ARRL=0X26;

TIM1_PSCRH = 0x3e;

TIM1_PSCRL = 0x80;

TIM1_CR1=0X81;

TIM1_IER=0X01;

enableInterrupts();

      while(1)

      {

        

       }

         }

INTERRUPT_HANDLER (TIM1_UPD_OVF_IRQHANDLER,23)

//@interrupt @far void TIM1_OVF_TRG_BRK_IRQ_Handler(void)

{

    h++;

}

    

#ifdef USE_FULL_ASSERT

/**

  * @brief  Reports the name of the source file and the source line number

  *   where the assert_param error has occurred.

  * @param file: pointer to the source file name

  * @param line: assert_param error line source number

  * @retval None

  */

void assert_failed(uint8_t* file, uint32_t line)

{

  /* User can add his own implementation to report the file name and line number,

     ex: printf('Wrong parameters value: file %s on line %d\r\n', file, line) */

  /* Infinite loop */

  while (1)

  {

  }

}

#endif

What is wrong with the code? Kindly help me.

Thank you.

    This topic has been closed for replies.

    12 replies

    Schmidt.Andreas
    Explorer
    December 21, 2016
    Posted on December 21, 2016 at 15:48

    Hi,

    try to use : volatile int h;

    Regards

    Andreas

    ST MICROCONTOLLER
    Associate II
    December 22, 2016
    Posted on December 22, 2016 at 07:10

    Thanks, but declaring variable h as volatile didn't make any change.

    Seb
    ST Employee
    December 21, 2016
    Posted on December 21, 2016 at 16:28

    Are you using the debugger? 

    Is the code enabling timer internal clock so you can actually write the timer registers?

    In an old code, I've got this:

      CLK_PeripheralClockConfig(CLK_Peripheral_TIM1, ENABLE);

    Put as first step... (STM8L151)

    ==============================

    Digging at the std lib:

    void CLK_PeripheralClockConfig(CLK_Peripheral_TypeDef CLK_Peripheral, FunctionalState NewState)

    {

    /* Check the parameters */

    assert_param(IS_CLK_PERIPHERAL(CLK_Peripheral));

    assert_param(IS_FUNCTIONAL_STATE(NewState));

    if (((uint8_t)CLK_Peripheral & (uint8_t)0x10) == 0x00)

    {

    if (NewState != DISABLE)

    {

    /* Enable the peripheral Clock */

    CLK->PCKENR1 |= (uint8_t)((uint8_t)1 << ((uint8_t)CLK_Peripheral & (uint8_t)0x0F));

    }

    else

    {

    /* Disable the peripheral Clock */

    CLK->PCKENR1 &= (uint8_t)(~(uint8_t)(((uint8_t)1 << ((uint8_t)CLK_Peripheral & (uint8_t)0x0F))));

    }

    }

    else

    {

    if (NewState != DISABLE)

    {

    /* Enable the peripheral Clock */

    CLK->PCKENR2 |= (uint8_t)((uint8_t)1 << ((uint8_t)CLK_Peripheral & (uint8_t)0x0F));

    }

    else

    {

    /* Disable the peripheral Clock */

    CLK->PCKENR2 &= (uint8_t)(~(uint8_t)(((uint8_t)1 << ((uint8_t)CLK_Peripheral & (uint8_t)0x0F))));

    }

    }

    }

    and yes, you need to declare the variable as volatile to be sure.

    ST MICROCONTOLLER
    Associate II
    December 22, 2016
    Posted on December 22, 2016 at 07:13

    Thanks, I am using stm8s003k3 board with debugger, but the value of h is seen as 0 in simulator also.

    simon ferrer
    Associate
    December 21, 2016
    Posted on December 21, 2016 at 23:08

    Have you enabled the interrupt at NVIC Level?

    ST MICROCONTOLLER
    Associate II
    December 22, 2016
    Posted on December 22, 2016 at 07:15

    Thanks, do I have to edit any of the header files to enable interrupt at NVIC level?

    ST MICROCONTOLLER
    Associate II
    December 22, 2016
    Posted on December 22, 2016 at 14:23

    I have included stm8s_itc.h. I have seen somewhere stm8s_it.h file being included, which I think, is supported for raisonance as well as cosmic. When I include that file, another error takes place  'symbol f_NonHandeledInterrupt multiply defined'

    Thank you.

    Schmidt.Andreas
    Explorer
    January 5, 2017
    Posted on January 05, 2017 at 08:55

    Have you enabled the interrupts?

    enableInterrupts();  // usally before the main while loop...

    therefore you have to include: stm8l15x.h

    ramon13666
    Associate II
    January 9, 2017
    Posted on January 09, 2017 at 08:55

    TIM1_CR1=0X81;
    TIM1_IER=0X01;�?�?

    Change to

    TIM1_IER=0X01;
    TIM1_CR1=0X81;�?�?