Fast Timer interrupts

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

Post Reply
User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Fast Timer interrupts

Post 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

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times

Re: Fast Timer interrupts

Post 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

User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Re: Fast Timer interrupts

Post by Jan Lichtenbelt »

If that is true, the time interrupt frequency will be lost, as my example shows.

Post Reply