MTIM-timer accuracy and stability in MC9S08QG8
Greetings All,
I have a question regarding stability of wave frequency, which produced by MTIM timer module in MC9S08QG8 device. The problem appears when timer runs in high speed - the frequency of produced wave is not stable (more then 2%)!
I evaluate this chip using internal click source at maximal buss speed after trimming correction.
Platform: Freescele EVB - DEMO09S08QG8.
I set timer by following values:
MTIMCLK_PS = 2; // 2*38=>should give 25us period
MTIMCLK_CLKS = 0; // 0-Bus clock; 1-fixed-freq. clock
MTIMMOD = 38; // Modulo value for 25us period
MTIMSC &=~ 0x90; // clear TOF and exit stop TSTP
MTIMSC |= 0x60; // reset and start MTIM, enable interrupts (TOIE)
COP is fed regularly.
The only allowed interrupt is conducted with MTIM with toggling some hardware pin with connection to scope allowing accurate frequency measure.
The interrupt looks like:
interrupt 12 void MTIM_ISR(void)
{
volatile U_CHAR x;
LED2 = ~LED2; // toggle pin - set to high – connected to the scope
x = MTIMSC_TOF;
MTIMSC_TOF = 0; // clear TOF flag
MTIMSC_TRST = 1; // reset counter
LED2 = ~LED2; // toggle pin - set to low – connected to the scope
}
As I mentioned, the output signal is not stable. The stability is above 2%. Accuracy depends on quantity of tasks inside main loop (again: there are no additional interrupts in my code).
I have tested it also on demo application demo9S08QG8_test.c, which comes with EVB. The same result - when I speed up the timer frequency, the signal becomes unstable.
Had some body experience the same problem or can advice.
Thanks in advance.
Message Edited by YeOldeBDM on 2007-04-0511:28 PM
In addition to what Mac stated, I noticed that you are reseting the counter in the ISR. Not only is this unnecessary, but it will introduce counter errors.
If you allow the timer to restart on it's own, no counts will be lost. By clearing it within the ISR, you will loose counts, and the number of counts lost will vary with the interupt latency.
Also, since the LED is toggled in the ISR, what you see on the LED will include the interupt latency.
Message Edited by bigmac on 2007-04-0612:36 AM
Thanks to everybody, who participated in my question discussion.
To bigmac: the ISR execution time is less then 25 us. It is about 8us, so the are no overlapping. I have calculated cycles amount and checked it with the scope.
To rocco: you are right - I do not have to clear counter manually inside the interrupt. It was my mistake in attempt to stabilize timing. Fixing it doesn't resolve my problem, but improves absolute time counting.
The interrupt latency should add some constant delay, but should not change the frequency of the interrupt appearance.
To peg: as you have mentioned, the problem is in short time jittering. Actually, LED flashing has just evaluation reason - it is easy to measure by scope. The real purpose of this ISR is sampling some serial input line from wireless device, so I have to use interrupt.
Any other idea?
Thanks again, guys.
Hi Mac,
I still have the 2% error. Removing counter reset inside ISR just changed a bit the frequency, but it remains unstable.
I think, that this jitter occurs because command accomplishment cycles in main loop, as you mentioned. Followed calculation can approve it:
25us * 2% = 0.5us;
0.5us it is a time of 4 cycles @ 16MHz clock.
I guess, it could not be improved and I have to build my algorithm to tolerate this jittering.
Thank you again.
Regards,
Alexander Udler
