cancel
Showing results for 
Search instead for 
Did you mean: 

STM8S105C6 - TIM2, TIM4 NOT working, not incrementing, TIM1 - ok

MCuli.1
Associate

I'm trying to "activate" TIM2 or TIM4 on a STM8S105C6 (discovery board) part without success for more than 4 days now... I'm getting sooo frustrated.

I tried all examples I could find and not a single one works.

Ended up with a basic, dummy test to show the issue. Just enable ti timer and "watch" the counter register - it is NOT incrementing, consequently nothing works.

It's the same with TIM2.

But TIM1 works normally.

TIM2 & TIM4 cout registers just won't update/change.

Any idea what could be possibly wrong?

I tried with 5 different boards (always the same model, STM8S Discovery with S105C6 onboard)

void main(void) {
    int cnt;
 
    TIM4_IER  = 0; // 0x01;
    TIM4_EGR  = 0x01;
    TIM4_PSCR = 0x01;
    TIM4_ARR  = 0x10;
    TIM4_CNTR = 0xff;
    TIM4_CR1  = 0x01;
 
     while (1) {
          for (cnt=0;cnt<1000;cnt++) ; 
          printf("tim4: %u", TIM4_CNTR);     // always prints tim4: 0
          for (cnt=0;cnt<333;cnt++) ; 
          printf("tim4: %u", TIM4_CNTR);     // always print tim4: 0
     
     }
}

4 REPLIES 4

PCKENR1 settings required?

Would probably set CNTR at or below ARR value.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
AA1
Senior III

Try this code in while(1):

for(cnt=0;cnt<1000;cnt++);
 
if(TIM4_SR)
	printf("It's working\n");
else
	printf("Doesn't work\n");

AA1
Senior III

And remove this line:

TIM4_EGR  = 0x01;

MCuli.1
Associate

Hi!

Thanks for the answer(s)!

I finally managed to make it work!

The problem was actually in the headers/include files.... But i "discovered" this only after carefully inspecting the generated ASM and the datasheet for the specific MCU...

After I red somewhere that "...the registers on S105 are slightly different..." I went the "this is not possible" route and checked all the defines, register specifications in the specific datasheet... and, of course, the S105 has different register addresses for some timer registers! The names etc are all identical... just the addresses are different. As I mainly used the reference manual where "timX is the same same everywhere..." did not think that registers would/could be different in this way... (maybe the base address or so... but not the "structure").

I'm using SDCC on Linux... and started with one "light" include/library which... does not take this differences into account.

So... I was constantly using the "right" register names (ex TIM2_SR1) but the actual register address was off by 2 bytes - so I manipulated the wrong registers...

After switching to the official STD peripheral library (just the register definitions, no drivers etc) the damn thing finally started to move in the right direction...

So... after almost a week I now have a "interrupt blinking led" on TIM2 or TIM4...

Bah... no comment!

THANKS for your assistance!