PDA

View Full Version : Measure time in mS between two pulses



Fredrick
- 6th March 2011, 17:29
I want to measure the time between two pulses in milliseconds, how can I do that in PBP?

https://lh3.googleusercontent.com/_eOXvPG6Er-A/TXPA4JibzuI/AAAAAAAAAMU/bCc1fIrbem8/pulse.jpg

aratti
- 6th March 2011, 17:35
Start timer1 on rising edge of first cycle and stop it on rising edge of second cycle.

Check the count accumulated and work out the time elapsed in function of fosc/4 and prescaler.

Cheers

Al.

Fredrick
- 6th March 2011, 17:54
Never used the PICīs timer before, Anyone have a short example of how to use timers?

HenrikOlsson
- 6th March 2011, 18:45
Many ways to do it. A simple, non elegant, way:

TimerTicks VAR WORD
TMR1H = 0
TMR1L = 0 'Reset TMR1

While PortB.0 = 0 : WEND 'Wait for rising edge
T1CON.0 = 1 'Start timer
WHILE PortB.0 = 1 : WEND 'Wait for falling edge
WHILE PortB.0 = 0 : WEND 'Wait for second rising edge
T1CON.0 = 0 'Stop timer
TimerTicks.HighByte = TMR1H
TimerTicks.LowByte = TMR1L
HSEROUT [#TimerTicks, 10,13]

Obviously this will "hang" the PIC while measuring and there may be some (in the us region) error due to polling the inputs but it should work. Another, more elegant way, would be to use the timer in conjuction with one of the CCP modules set up in capture mode. As it happens they have a mode where it captures the value of the timer, in the backgroud, on every rising edge.

/Henrik.

Fredrick
- 6th March 2011, 19:02
How does TMR1 know that it should count in ms?

HenrikOlsson
- 6th March 2011, 19:17
It doesn't. As Al wrote previously it measures in "ticks" where one "tick" is equal to 1/(Fosc/4). At 4MHz one tick is 1us, at 20Mhz one tick is 200ns and so on. This is with the timers prescaler set to 1:1.

You need to convert from "ticks" to ms when the measurement is done.

In what range are we talking here, 10ms, 100ms, 50000000ms? TMR1 is 16 bits wide so it can measure 65536 "ticks", if you need more than that you need to use the prescaler at the cost of a drop in resolution but that doesn't sound like a problem right now.

Fredrick
- 6th March 2011, 23:07
How do I change the timers prescaler?
The range is from 1ms up to 10 seconds or more.

HenrikOlsson
- 7th March 2011, 06:34
You look in the datasheet for the particular PIC you're using, look up the section on the particular timer you're about to use, say TMR1. It'll tell you where the prescaler selection bits are (usually in T1CON for TMR1) and how to set them to get various prescaler ratios.

IF you run the PIC at 4Mhz the timer ticks at 1Mhz, it's 16bits wide so the longest time it can measure is 65.5ms, each "tick" is 1us. On the 16F877, for example, the highest prescaler ratio is 1:8 so the longest time possible to measure is 524ms....

TMR2 has a prescaler possible of 1:16 which will give you just over one second. EDIT: Sorry TMR2 may only be 8bits but look it for the PIC in question.

Depending on what else you're going to do with the PIC there are various ways to go about this. You can run the PIC at a much lower frequency, like 100kHz or something like that. Or you can use an external oscillator to clock TMR1. If you design a 1kHz oscillator and set TMR1 up derive its clock extarnally (again, see T1CON register for TMR1 details) you'll be able to measte 65535ms or ~65seconds with a resolution of 1ms.

Make the oscillator 5kHz and you'll get 200us resolution and you'll still be able to measure up to ~13 seconds.

/Henrik.

mister_e
- 7th March 2011, 07:20
Rough explanation of something.

Use an interrupt to start/stop the timebase/counter. Use a fixed timebase and monitor the timer overflow. at each overflow, increment a variable.

Select a interuptable pin
Set it to rising edge
once the interrupt happen Start the Timebase/counter

At next interrupt. Stop timer and process the maths.

Overflow can be managed with an ISR.

When well done, you can have a really accurate reading in uSec precision or so. Sounds like a OlympicTimer