• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

STM8 timer not ticking correctly

Not open for further replies.
Hello and thanks always.

I am working maintaining a code for a STM8, that involves timers. The clock is set at 16MHz, ergo 0.0625uS. The settings of the timer are ARRH=0x03 and ARRL=0x20 therefore(0x0320=800) it resets at 800( 50uS). PSCR is set to 0 so the timer has the same freq as the micro

Anyway, I checked with an oscilloscope and it does not give good readings. The timer interrupt is called at
56us , 54uS, 54uS, 52uS, 52uS, 52us, 38us(!!!), 42us(?), 50us, 50us....
curiously summed up it gives 500uS so it does count as 10 times 50uS

The first 8 times at the timer interrupt some AD conversion is happening so I think that the AD finished interrupt is being called in between too.

I disable the AD conversions and the timer interrupt is called at exactly 50uS, so I am concluding that the AD conversion is what is affecting the timing of the timer. Any comments on this??

1) How would you use the AD conversion on a regular time basis?

but what is puzzling me is :

2) why does it "correct" itself by firing an interrupt at 38uS?? (and later 42uS)

I would appreciate any comment based on your embedded or STM8 experience, has something of this sort ever happened to you?

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
Assume the ADC interrupt is firing around the same time as the timer interrupt.. Now the ADC just happens first.. BUT!!!! the timer hits 50us 1us after... The ADC is serviced.. the timer interrupt waits and then will be serviced as soon as the ADC has been completed some time after REMEMBER the overflow has already flagged and the timer is counting again... Once the timer ISR is serviced.. there will be 50us - wait time so the next ISR for the timer will seem to come earlier... when in fact the overflow flag will be set at exactly the same time..

If you can prioritize... Make the timer a higher priority than the ADC..
I am not sure if AD conversion would affect timer so much. Atleast i am lucky that i did not face such issues. May be can you check your timer and AD initializations once again may be one is disabling the other.
Thanks for the answers. I further investigate and I found this:
As you can see the red Timer label is where the timer gets called. This timer starts a AD conversion and the black AD int is where the AD conversion finishes. It takes 20uS. In the first cases, the same AD interrupt service function calls an AD again which takes another 19uS and finishes in the green label. after that the AD interruption is disabled.
Now you see that 20uS+19uS= 39uS so there is plenty of time for the timer to tick at 50uS again but for some reason there is always a gap of 15uS until the next timer call. That makes it the 50uS timer to tick at approx 54uS

on the other hand when the AD service (in black) does not call another conversion there still is a 15uS gap which explains why the 50uS timer "self correct" and calls at 35~36uS.

I wonder why this is happening. Apparently there is no superposing of interruption calls
In whatever controllers i have worked there was never a situation where i have to call for AD conversion in timer routine. It is done automatically by the controller. You only need to read the ADC buffer data at periodical interrupts based on requirements.
Not open for further replies.

Latest threads

EE World Online Articles