Page 1 of 1

Fast Timer interrupts

Posted: Mon Sep 12, 2011 8:17 pm
by Jan Lichtenbelt
Please find some constrains in using fast timer interrupts.

I wanted to measure the on-time of a pulse (about 1 msec). And of coarse I wanted as accurate as possible. The timer interrupt TRM2 gives the possibilities to interrupt frequencies up to 307.2 kHz (with 19660800 Hz oscillator). Thus about 300 interrupt-counts are expected to measure the pulse. This is over the 255 and therefore an integer counter is needed.

The interrupt macro is just: counter=counter+1.

So far the theory. Now the practice.

I started, fortunately, with a lower interrupt frequency of 76.8 KHz. And indeed the counter showed about 76 counts for the 1 msec pulse. But, as said, I wanted the best accuracy. I increased the frequency up to 153.6 kHz and found to my astonishment about the same counts 70-80 instead of 150. Also increasing the interrupt frequency to best 'accuracy' of 307.2 kHz showed the same number of counts, about 70-80 instead of 300.

What is wrong? To answer this question I looked at the assembler and found the interrupt macro to take about 10 programming steps, without the time needed to start the interrupt. The oscillator is about 20 MHz or 0.05 μsec. One programming step takes 4x0.05=0.2 μsec. Thus the 10 programming steps take at least 2 μsec. This will be longer due to time needed to save and replace stack parameters.

Interrupt frequency of 76.8 KHz is equal to 1/76.8 or about 13 μsec, 153.6 KHz equals about 6 μsec and 307.2 kHz about 3 μsec.

This shows the constrains of fast timer interrupts. It can be that there is not enough time to perform the interrupts within the time needed.
In my example interrupt higher than 76.8 KHz can not be used.

Be warned!

Jan Lichtenbelt

Re: Fast Timer interrupts

Posted: Mon Sep 12, 2011 8:41 pm
by Spanish_dude
All interrupts are disabled when an interrupt is called and then enabled once you return from it.
So even though the interrupt functions needs more time to execute than the time the interrupt is triggered, there should be no problem.

I don't know if this applies to all microcontroller. I'm saying this because the 18F2455 does that.

BR,

Nicolas

Re: Fast Timer interrupts

Posted: Mon Sep 12, 2011 10:14 pm
by Jan Lichtenbelt
If that is true, the time interrupt frequency will be lost, as my example shows.