View previous topic :: View next topic |
Author |
Message |
40inD
Joined: 30 Jul 2007 Posts: 112 Location: Moscow, Russia
|
How to configure Timer to measure time between interrupts |
Posted: Tue Apr 22, 2014 2:04 am |
|
|
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
|
Re: How to configure Timer to measure time between interrupt |
Posted: Tue Apr 22, 2014 4:25 am |
|
|
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
|
|
Posted: Tue Apr 22, 2014 4:32 am |
|
|
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
|
|
Posted: Tue Apr 22, 2014 5:02 am |
|
|
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
|
|
Posted: Tue Apr 22, 2014 7:52 am |
|
|
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
|
|
Posted: Thu Apr 24, 2014 1:25 am |
|
|
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
|
|
Posted: Thu Apr 24, 2014 1:31 am |
|
|
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 |
|
|
|