2

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) 
{
    tix++;
    if(tix==8)
    {

        six++;
        tix=0;
    }
    if(six==60)
    {
        mins++;
        six=0;
    }

    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;

    for(i=0;i<10;i++)
    {
        /* toggle pin initially with a delay */
        PA_ODR ^= (1 << LED_PIN);
        delay_ms(125);
    }

    __asm__("rim\n");

    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++)
        __asm__("nop\n");
}
dynamag
  • 127
  • 1
  • 7
  • Don't you have to enable the interrupt using NVIC as well? – Abel Tom Aug 03 '19 at 06:03
  • 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](https://blog.junaid.site/2018-01-20-stm8s-timer2-with-overflow-interrupt/) [link](https://lujji.github.io/blog/bare-metal-programming-stm8/) [link](https://blog.mark-stevens.co.uk/2012/08/using-timers-on-the-stm8s/) – dynamag Aug 03 '19 at 08:57
  • 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. – Abel Tom Aug 05 '19 at 09:37
  • 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 :-/) – dynamag Aug 06 '19 at 04:21

1 Answers1

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.

Cheers!

dynamag
  • 127
  • 1
  • 7