CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

How to configure Timer to measure time between interrupts

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
40inD



Joined: 30 Jul 2007
Posts: 112
Location: Moscow, Russia

View user's profile Send private message

How to configure Timer to measure time between interrupts
PostPosted: Tue Apr 22, 2014 2:04 am     Reply with quote

How to configure Timer to measure time between ext ints in msec?
measuring range - 800 - 1200 ms.
PIC18F258 @8MHz
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

Re: How to configure Timer to measure time between interrupt
PostPosted: Tue Apr 22, 2014 4:25 am     Reply with quote

40inD wrote:
How to configure Timer to measure time between ext ints in msec?


If you want good resolution and low jitter, this is a job for a CCP, not a timer on its own. Set up the timer to clock at the desired timing resolution, e.g. 0.01ms, then use the CPP to capture when triggered by your external event. Firmware has to deal with reading the capture values and with timer roll-overs in ISRs.

If the timing resolution is large and some jitter, of the order of us, is acceptable, then you can do the capture in firmware, i.e. use the signal as an external interrupt and capture the count of a freerunning timer in the ISR, taking care to deal with the non-atomic nature of a 16 bit counter.

Both solutions suffer from the limited count length of timers: its difficult to have both good resolution AND long time before roll-over of the timer. So,with either solution, you're probably going to have to count roll-overs between timing events.
Ttelmah



Joined: 11 Mar 2010
Posts: 19480

View user's profile Send private message

PostPosted: Tue Apr 22, 2014 4:32 am     Reply with quote

You don't.....

What you do is count faster, and then divide the result.

The most flexible prescaler, is on timer0. 8 binary divisions of 2MHz available. So 1MHz down to 7812.5Hz in multiples of 2. Use /16, which then gives counting at 125000Hz (the lowest frequency that is still an integer multiple of your target), and divide by 125.
40inD



Joined: 30 Jul 2007
Posts: 112
Location: Moscow, Russia

View user's profile Send private message

PostPosted: Tue Apr 22, 2014 5:02 am     Reply with quote

The hardware is not editable. Event pulse comes to the INT_EXT pin.
So, i think the algorythm must be like this:


Code:
#INT_EXT
   void get_period()
   {
   period=get_timer0();
   set_timer0(0);
   }
...
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_64); //2.1s overflow @8MHz
...

for time about 1000 ms between interrupts the "period" will be about 32000. Im right?
Then its need to convert the period value into msec.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Apr 22, 2014 7:52 am     Reply with quote

with the code you just posted - i think you need to also do a dummy read of the port_b register to re enable ints correctly

and
might want to consider making VAR "period"
be a circular buffer array - depending on how often the EXT interrupt might be taken.

also consider that if you have other INTS enabled, the number of cycles between the EXT_INT pin assertion - and your EXT ISR entry may vary, adding to error.
40inD



Joined: 30 Jul 2007
Posts: 112
Location: Moscow, Russia

View user's profile Send private message

PostPosted: Thu Apr 24, 2014 1:25 am     Reply with quote

asmboy wrote:
with the code you just posted - i think you need to also do a dummy read of the port_b register to re enable ints correctly



Is this really need for INT_EXT?
I thought it was only for INT_RB.
Ttelmah



Joined: 11 Mar 2010
Posts: 19480

View user's profile Send private message

PostPosted: Thu Apr 24, 2014 1:31 am     Reply with quote

40inD wrote:
asmboy wrote:
with the code you just posted - i think you need to also do a dummy read of the port_b register to re enable ints correctly



Is this really need for INT_EXT?
I thought it was only for INT_RB.


Correct.

He had a momentary lapse (as I often do), and thought you were using the interrupt on change.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group