STM8 timer 2 interrupt

Hello, I'm using STM8S103F6 development board. I need more than 1 timer for application so i would like use Timer4(used with built-in delay functions) & Timer2.

I found online code for Timer4 and successful in execution n board. However the same code modified t Timer2 NOT working. can you please help me with this.

Code:
/################## Timer2 ###############################/
#define SFR(mem_addr) (*(volatile uint8_t *)(0x5000+(mem_addr)))

#define TIM2_CR1 SFR(0x300)
#define TIM2_IER SFR(0x303)
#define TIM2_SR1 SFR(0x304)
#define TIM2_EGR SFR(0x306)
#define TIM2_PSCR SFR(0x30E)
#define TIM2_ARRH SFR(0x30F)
#define TIM2_ARRL SFR(0x310)
#define TIM2_CNTRH SFR(0x30C)
#define TIM2_CNTRL SFR(0x30D)

uint16_t ms_count=0; //Millisecond count
bool Sec_Flag=0;

INTERRUPT_HANDLER(TIM2_UPD_OVF_IRQHandler,13)
{
ms_count++;
if(ms_count>10000) //100uS*10000 = 1000mS = 1S
{
ms_count=0;
Sec_Flag^=1;
}
TIM2_SR1=0; //Clear interruption mark
}

void setup()
{
disableInterrupts();

pinMode(PB5,OUTPUT);

TIM2_PSCR = 4;
TIM2_ARRH = 0;
TIM2_ARRL = 100; //100us
TIM2_CNTRH= 0; //It is necessary to clear the counter
TIM2_CNTRL= 0; //It is necessary to clear the counter

TIM2_IER |= 1<<0; //Enable tim2 update interrupt
TIM2_SR1 |= 1<<0; //Clear tim2 update interrupt flag
TIM2_CR1 |= 1<<7; //Allow reassembly to enable timer
TIM2_CR1 |= 1<<0; //Enabling tim2 counter

enableInterrupts(); //asm("rim");
}

void loop()
{
if(Sec_Flag)
{
digitalWrite(PB5,HIGH);
}
else
{
digitalWrite(PB5,LOW);
}
}

/##########################################################/

/######################Timer 4 ##########################/

#define SFR(mem_addr) (*(volatile uint8_t *)(0x5000+(mem_addr)))

#define TIM4_CR1 SFR(0x340)
#define TIM4_IER SFR(0x343)
#define TIM4_SR SFR(0x344)
#define TIM4_PSCR SFR(0x347)
#define TIM4_ARR SFR(0x348)
#define TIM4_CNTR SFR(0x346)

uint16_t ms_count=0; //Millisecond count
bool Sec_Flag=0;

INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler,23)
{
ms_count++;
if(ms_count>10000) //100uS*10000 = 1000mS = 1S
{
ms_count=0;
Sec_Flag^=1;
}
TIM4_SR=0; //Clear interruption mark
}

void setup()
{
disableInterrupts();

pinMode(PB5,OUTPUT);

TIM4_PSCR = 4;
TIM4_ARR = 100; //100us
TIM4_CNTR = 0; //It is necessary to clear the counter

TIM4_IER |= 1<<0; //Enable tim update interrupt
TIM4_SR |= 1<<0; //Clear tim update interrupt flag
TIM4_CR1 |= 1<<7; //Allow reassembly to enable timer
TIM4_CR1 |= 1<<0; //Enabling tim counter

enableInterrupts(); //asm("rim");
}

void loop()
{
if(Sec_Flag)
{
digitalWrite(PB5,HIGH);
}
else
{
digitalWrite(PB5,LOW);
}
}
/##########################################################/

Please help me with Timer 2 Interrupt.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.