Page 1 of 1

Interrupt

Posted: Wed Feb 23, 2011 9:26 pm
by wayne millard
Hi Team,

How do i get a accurate 10us interrupt need to generate times to the nearest 10us.

Thanks
Wayne Millard :roll:

Re: Interrupt

Posted: Thu Feb 24, 2011 2:05 am
by medelec35
Hi Wayne,
Simulator shown interrupt is generated every 10.0 seconds.
timer0 10us.png
(4.33 KiB) Downloaded 2719 times
It is important to place delay before
tmr0l += 158;
Not after.

have you any means of testing for 10us interrupt?
E.g an oscilloscope?

Re: Interrupt

Posted: Thu Feb 24, 2011 9:05 am
by wayne millard
Hi medelec35,

Don't understand were you say put a delay before the tmr0l +=158.
And why is it not 156 0n a 40Mhz clock. on the last post you said on a 156 it generated 10.2uS which is a 2% error in the timing. so i calculate interrupt counts for required time then subtract 2% of the counts i still dont get the accuracy over all the time spane.

Thanks,
Wayne M

Re: Interrupt

Posted: Thu Feb 24, 2011 9:46 am
by medelec35
wayne millard wrote:Hi medelec35,

Don't understand were you say put a delay before the tmr0l +=158.
loop count is a very short delay.
As soon as interrupt is accessed, there need to be a very small delay before timer0 is set to 158. Then when timer0 rolls over to 0 the interrupt is accessed.
Without the 5 loops, timer 0 interrupt is accessed every 9.90 mS. The loop delays the accessing to 10mS.
wayne millard wrote:And why is it not 156 0n a 40Mhz clock. on the last post you said on a 156 it generated 10.2uS
That's why changed from 156 to 158. the higher the number the faster timer0 rolls over, so the faster interrupt is triggered.
Not sure why there are timing errors in your program, without scopeing and going over it with a fine tooth comb.
Are you 100% sure the frequency of osc after x4 is is exatly 40MHz?
If that is not 100% accurate then there will be timing errors.

Re: Interrupt

Posted: Thu Feb 24, 2011 1:41 pm
by wayne millard
Hi medelec35,

i have a electronic 10Mhz clock and then using the x4 PLL to give 40Mhz.

i will give it a go.

Thanks for the help

Wayne

Re: Interrupt

Posted: Thu Feb 24, 2011 3:56 pm
by Sean
Timing errors will be caused by the finite time it takes to perform the 'tmr0l +=156' operation.

The processor uses a read-modify-write operation to update the tmr0l value. Any clock counts received between the instant the timer is read, and the instant the modified value is written back, will effectively be cancelled, extending the overflow time period.

With the timer prescaler set to 1:1, each program instruction probably equates to 1 or 2 counts. It should be possible to analyze the asm code that performs this operation and calculate an exact value for the tmrol offset.

The best timers for accurate, repeatable timing operations (using a PIC16F877A as an example) are tmr2 (using pr2 to set the overflow value in hardware) or tmr1 with one of the ccp modules set to 'reset on compare'. Both can be configured to generate an interrupt when an overflow happens.

Re: Interrupt

Posted: Thu Feb 24, 2011 4:15 pm
by wayne millard
Hi Sean,

Thanks for a reply have you got any examples to show how to do this.

Wayne

Re: Interrupt

Posted: Thu Feb 24, 2011 4:54 pm
by Sean
The program in the Multiple Servo Control article uses tmr1 in conjunction with one of the CCP registers to reset and interrupt every 2.5ms. This could be modified for 10us. Unfortunately some of the values need to be calculated manually.

Of more interest might be the TMR2 interrupt, which is supported in Flowcode V4 (so most of the configuration values will be put in the program for you).

The 'Rollover Value' property is loaded directly into the PR2 register (after being decremented by 1). The code then appears in the C file (default value 256)

pr2=256-1;

You could use a C code block to re-write the value of pr2 after the interrupt has been configured:

pr2 = 99; //zero produces a 1 cycle delay

This will give a regular 10us interrupt (Prescaler = 1:1, Postscaler = 1:1, Fosc = 40MHz [Fcyc = 10MHz]).

Re: Interrupt

Posted: Thu Feb 24, 2011 9:57 pm
by wayne millard
Thanks Sean i will give it a go.