Page 1 of 1

Measure Time

Posted: Tue Apr 17, 2012 6:21 pm
by wayne millard
Hi team,

Is there any example of how to measure a time between two rising edges on RB0 interrupt.
From 0 - 20ms with a accuracy of 1us or better. i can us flowcode 5 or Arm 4.

Thanks,
Wayne :D

Re: Measure Time

Posted: Wed Apr 18, 2012 9:31 am
by Benj
Hi Wayne,

What I would do is on the first INT interrupt I would reset my count variable and set a timer interrupt going. On the second INT interrupt I would disable the timer interrupt.

With a normal PIC running at 19.6608MHz you can get a timer interrupt rate of 19200Hz which equates to 52us.

With a USB enabled PIC running at 48MHz you can get a timer interrupt rate of 16875Hz which equates to 21us.

With every timer interrupt you simply add 1 to your counter and then this is a multiplier for the delay shown above.

You could then use a C icon read the contents of the actual timer register and this would give you a result in 1/256th of the time resolution shown above. Add the two values together and you should have an accurate time reading.

Re: Measure Time

Posted: Sat Apr 21, 2012 4:56 am
by Mikat
Benj wrote:Hi Wayne,

What I would do is on the first INT interrupt I would reset my count variable and set a timer interrupt going. On the second INT interrupt I would disable the timer interrupt.

With a normal PIC running at 19.6608MHz you can get a timer interrupt rate of 19200Hz which equates to 52us.
Ben why you don't use timer1 or timer3 as a counter? That what I would do. I the rising edge int, first read the timer, and then reset it, that way I don't need to have very high frequency timer in macro running...
Like I use in my boat CAN bus rpm measuring:
First set the timer 1 at right count speed.
Then at the rb0 int read the tmr1hi and tmr1lo at the array
Write the tmr1hi and tmr1lo at 0
Then calc the moving average at the array values
Convert those values at 16bit
Divide proper value at the calculated 16bit timer value
And VOILA I have the exact rpm value..
And the timer1 rollover int can be used at discard too long pulses..
The benefit at that type counter is that it is quite lot "lighter" to MCU to run, the interrupt frequecy is only the frequency at measured pulses, and then all I need to do is the read and reset the timer values, all other calculations can be done just when needed at main loop...

Mika

Re: Measure Time

Posted: Wed Jun 27, 2012 2:26 pm
by Gary Freegard
Hi Mika

You may find this link useful, hopefully
http://www.matrixmultimedia.com/mmforum ... =29&t=9088

Jan was having an issue with his code, I came up with two alternative methods for him and then eventually decided to solve the problem :D

Good luck

Gary