Interrupt

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
wayne millard
Posts: 234
Joined: Thu May 31, 2007 2:18 pm
Has thanked: 7 times
Been thanked: 12 times

Interrupt

Post 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:

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: Interrupt

Post 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?
Martin

wayne millard
Posts: 234
Joined: Thu May 31, 2007 2:18 pm
Has thanked: 7 times
Been thanked: 12 times

Re: Interrupt

Post 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

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: Interrupt

Post 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.
Martin

wayne millard
Posts: 234
Joined: Thu May 31, 2007 2:18 pm
Has thanked: 7 times
Been thanked: 12 times

Re: Interrupt

Post 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

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times

Re: Interrupt

Post 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.

wayne millard
Posts: 234
Joined: Thu May 31, 2007 2:18 pm
Has thanked: 7 times
Been thanked: 12 times

Re: Interrupt

Post by wayne millard »

Hi Sean,

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

Wayne

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times

Re: Interrupt

Post 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]).

wayne millard
Posts: 234
Joined: Thu May 31, 2007 2:18 pm
Has thanked: 7 times
Been thanked: 12 times

Re: Interrupt

Post by wayne millard »

Thanks Sean i will give it a go.

Post Reply