I've just started using STM8 microcontrollers, specifically the STM8S003F3P6 chip, in my own custom test board. The board works and I am able to blink an LED on PA3 without using an interrupt. However, when I try to make the blink take place in the Tim2_OVF interrupt, nothing happens. I think the interrupt is not taking place. What have I missed?? I have declared the function, "rim" has been added, checked the lst file that shows the int vector has been written. I am adding the hex file also for your reference:

lst file showing vector for IRQ13 has been added

hex file contents

My complete code is below. It blinks the led 5 times before the timer TIM2 is started. My SDCC version is 3.9.0 Your help is appreciated.

#include <stdint.h>
#include <stm8s.h>

#define F_CPU 2000000UL
#define LED_PIN     3

void delay_ms(uint16_t ms);
void mS125_isr() __interrupt(TIM2_OVF_ISR);

char tix, i,j,six, mins, b_chgLED;

void mS125_isr() __interrupt(TIM2_OVF_ISR) 


    if(tix==0 || tix==4)
        PA_ODR ^= (1 << LED_PIN);

    TIM2_SR1 &= ~(1 << TIM2_SR1_UIF);

//Main routine starts
void main() 
    PA_DDR |= (1 << LED_PIN); // configure PA3 as output
    PA_CR1 |= (1 << LED_PIN); // push-pull mode

    /* Prescaler = 128 */
    TIM2_PSCR = 0b00000111;

    /* Frequency = F_CLK / (2 * prescaler * (1 + ARR))
     *           = 2 MHz / (2 * 128 * (1 + 1952)) = 8 Hz */
    TIM2_ARRH = 0x07;
    TIM2_ARRL = 0xa1;

        /* toggle pin initially with a delay */
        PA_ODR ^= (1 << LED_PIN);


    TIM2_IER |= (1 << TIM2_IER_UIE); // Enable Update Interrupt
    TIM2_CR1 |= (1 << TIM2_CR1_CEN); // Enable TIM2

    while (1){};
void delay_ms(uint16_t ms) 
    long li;
    for (li = 0; li < ((F_CPU / 18000UL) * ms); li++)
  • \$\begingroup\$ Don't you have to enable the interrupt using NVIC as well? \$\endgroup\$
    – Abel Tom
    Commented Aug 3, 2019 at 6:03
  • \$\begingroup\$ The timer interrupt examples I see on the internet don't say anything about programming the ITC in the STM8 for the TIM2 interrupt? link link link \$\endgroup\$
    – dynamag
    Commented Aug 3, 2019 at 8:57
  • \$\begingroup\$ Did you try debugging the code? where is the PC? You need to declare the variables tix, i,j,six, mins, b_chgLED as volatile otherwise the compiler might optimize these variables and your Interrupt handler might not work with these variables as you want them to. \$\endgroup\$
    – Abel Tom
    Commented Aug 5, 2019 at 9:37
  • \$\begingroup\$ Thank you Abel, the issue has been solved. The issue was in the stm8s.h file that I had got from a blog. I've informed the owner of this typo/oversight (the error is only for TIM2 and the file is used for the STM8S003F3 chip, which is my chip, which is the reason I copied the header from there, lazy me :-/) \$\endgroup\$
    – dynamag
    Commented Aug 6, 2019 at 4:21

1 Answer 1


Okay, My problem has been solved. The definition of root address for the TIM2 in the stm8s.h file was not in hex(The author must have missed putting '0x' before 5300), so all the commands were not going to the TIM2. A big 'Thank You' goes to Richard Hodges of the SDCC user mail list for this. I've already informed the owner of the blog where I got the header file.



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.