Timing pulses

What is the best way to measure the time between 2 pulses, each a separate signal? I'd like to do this with a micro, but am concerned about faulty timing when the pulses are near each other. The pulse pair repetition rate is about 80Hz, with the time between pulses from near 0.0 ms to near 12.5ms.

Reply to
BobS
Loading thread data ...

On a sunny day (Tue, 11 Aug 2009 14:38:36 -0700 (PDT)) it happened BobS wrote in :

if you use one pulse to set a FF, and the other to reset it, then you can measure the output pulse length of that FF.

Reply to
Jan Panteltje

The best way depends on your requirements for accuracy and resolution and of the exact time of "near 0 ms" and "near 12.5 ms". If your microcontroller has an analog input, maybe some of the analog guys in this newsgroup can give you some hints how to build something. If you don't need very high resolution, you could use a timer: first pulse issues an interrupt, which starts the timer, second pulse reads the timer value. Accuracy depends on interrupt latency variation and resolution on timer clock. For higher resolution and accuracy requirements it is easy to implement it in a small CPLD.

--
Frank Buss, fb@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
Reply to
Frank Buss

"BobS" schreef in bericht news: snipped-for-privacy@18g2000yqa.googlegroups.com...

You can bet you will get faulty timing when the pulse are very near. A micro has a instruction cycle time of let's say 0.2us. Impossible to measure the time between the pulses edges if that time is only some ns. So with a simple micro you can measure times downto let's say 1us. Using some extra hardware (Jan Panteljes flipflop for instance) and the right micro you can go downto about 0.1us. Shorter times can be detected or missed. Accuracy depends on the accuracy of your clock.

petrus bitbyter

Reply to
petrus bitbyter

Idea #1:

Use a micro with a built in ADC. When the pulse arrives, it flips a flip flop. The output of the flip flop goes to an RC and to the micro. When the micro sees the flip-flop is flipped, it triggers the ADC. Using the time that the ADC got triggered and the voltage from the RC, the micro will get a very good estimate of when the pulse came.

If you do this independently for the two pulses and then subtract the times, you will get the time between pulses.

Idea #2:

Feed the pulses into two of the PCA inputs of a Silabs F120 chip. This can give you arrival times to the nearest 10 or so nS.

Reply to
MooseFET

I have a circuit that is a ramp reset by the first pulse, and does a sample and hold on the second pulse, then read by an ADC. This is fine, except it is an analog solution to digital signals. Seems like with a micro analog stuff should not be necessary. I suppose interrupts would work if you can code around errors from nearly coincidental pulses. To much work around code could burden and/or slow response. Any further ideas appreciated.

Reply to
BobS

What sort of resolution do you need?

Is it possible for pulse A to happen both before and after pulse B? If so, would you prefer to have "negative" times report zero, or report the actual negative times?

How wide are the pulses?

What uP are you using, and what timer facilities does it have?

John

Reply to
John Larkin

What degree of accuracy do you need?

With a 16-bit counter, 12.5ms limits you to around 5MHz, i.e. 200ns. That's assuming that the pulses occur in a fixed order; if you don't know which will occur first (i.e. the difference is -12.5ms to 12.5ms rather than 0ms to 12.5ms), then you're limited to 2.5MHz or 400ns.

How to implement it depends upon the micro. If you have a PIC with two CCP (Capture/Compare/PWM) modules (e.g. PIC18F2550), just put both modules into capture mode and connect the signals to the CCPx (CCP1/CCP2) pins. When the selected transition occurs on one of the CCPx pins, the timer value is latched into the corresponding CCPRx registers.

When both pulses have occurred (you can configure either or both CCP modules to generate an interrupt upon capture, or just poll the status bits), subtract CCPR1 from CCPR2. This will provide 200ns resolution without much difficulty.

If you're relying upon capturing the timer value in software, that limits the degree to which you can distinguish near-simultaneous transitions.

Reply to
Nobody

One pulse starts a timer and the second stops it; resolution depends on the timer count rate. Or, one pulse starts a linear ramp from zero volts and the other stops the ramp; the amplitude is measured - if full scale is 12.5V for

12.5mSec, then 12.5mV would (everything being ideal) represent 12.5uSec which bay be all you need.
Reply to
Robert Baer

Not impossible. You could, for example, measure the bulk of the delay with the internal timer and an interrupt routine. If it has to be super-precise, the "left-overs" at the beginning and the end could be handled by ramp circuits that are then read out with an internal ADC.

--
Regards, Joerg

http://www.analogconsultants.com/

"gmail" domain blocked because of excessive spam.
Use another domain or send PM.
Reply to
Joerg

My first approach would be brute force. Just free-run a counter at a frequency that's appropriate for the resolution, and on the first pulse, latch the counter's value, on the second pulse, latch the counter's value in another register; then just subtract.

Or use a freq. counter/timer, and just start it with one pulse and stop it with the other.

Have Fun! Rich

Reply to
Rich Grise

--- Depending on the width of the pulses, when they're very close to each other using them to set and reset an RS flip-flop to enable and disable a counter used as an accumulator won't work.

What I'd do would be to differentiate the leading edges of the input pulses digitally and use those pulses to set and reset the flip-flop, like this:

COUNT +-----+ HFCK>-----+----------------------------------|> | | | | AIN>------|-+-------------A _ |___ | | | +----+ NAND Y--+------------|CLR | | +--|D | +--B | | | | | _| | | +_---+ | | +----|> Q|--+ +--|S Q|----|E | | +----+ |_ _| +-----+ | +--|R Q| | | +----+ BIN>------|-+-------------A _ | | | +----+ NAND Y--+ | +--|D | +--B | | _| | +----|> Q|--+ +----+

That way, even if the pulses do overlap, their edges will generate pulses guaranteed to be no more than one high frequency clock wide.

There are some timing and race issues, but if you answer the questions which Larkin asked they can be worked out.

JF

Reply to
John Fields

A micro, two interrupts, a free-running timer, and a simple state machine in software.

Best? Maybe not... I have this hammer, so I see nails.

-- Les Cargill

Reply to
Les Cargill

I wouldn't use ADCs. Just use comparators or schmidt trigger gates (like the 74HC14) at the inputs. Does your controller have timers that have capture inputs? That way you can use a free running timer which captures the two events. Once the second event is captured the measurement is finished. Determining the delay is a matter of substracting the capture registers. The overhead is minimal. I used this method to decode/validate a manchester encoded signal.

On an LPC2000 or LPC1700 series ARM controller from NXP you can have the timers run at speeds up to 100MHz (10ns resolution!). Since the timers are 32 bit you can measure intervals up to 42.9 seconds.

--
Failure does not prove something is impossible, failure simply
indicates you are not using the right tools...
                     "If it doesn\'t fit, use a bigger hammer!"
--------------------------------------------------------------
Reply to
Nico Coesel

As far as I understood, the time between the start of two pulses has to be measured. Each on has own line and they are apparently unrelated. I see no way to measure ns or even tens of ns using a micro with a us clock. The standard procedure, starting a timer on one edge and stopping it on the other, using interrupts, will fail if the time between that edges becomes too short. Uncertainty is in the magnitude of at least on processorcycle.

You sure can do better using some additional hardware. Jan Pantelje mentioned a flipflop and you some ramp circuits. A fast (external) clock may help as well. Most important you can prevent unreliable results that you are not aware of.

I do not like to go much deeper in it as long as the OP does not make clear what his actual requirements are.

petrus bitbyter

Reply to
petrus bitbyter

mid.individual.net...

.
d
.

ith

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0... So with a simple

e
o

ck

you

ar

Its been an interesting discussion.

Additional information;

Both pulses are about 1 ms long, and can coincide with the time between A and B pulses edges right at if not zero, or the time is long enough B pulse is on top of the following A pulse. This indicates edge detection is required, and missing an edge could cause wildly inaccurate results. The data is in the time the B pulse follows the preceding A pulse. and is always a positive number.

8 bit accuracy is adequate. A micro has not been selected pending arriving at a scheme for this task, preferably the smallest micro possible for cost and space. The micro does have to manage a 25 Hz PWM out in addition to the input pulses.

The ramp circuit referred to above does differentiate the pulse edge and requires bilateral switches or possibly a couple Mosfets. An RC ramp is iffy, nonlinear but simple. I'd like to use a constant current source for the ramp but prefer minimum parts. A constant current diode might work if the current tolerance isn't an issue.

Any further suggestions appreciated.

Reply to
BobS

berichtnews: snipped-for-privacy@mid.individual.net...

So A goes high is the start? And B goes high is the end? Or is the interval to be measured between the trailing edges?

Simplest solution is a free running counter with two edge triggered interrupts where A zeros the counter and has highest priority and B reads it.

8 bit resolution of a 12.5ms period is 50us steps? That is huge - almost anything should be able to do that adequately. Most of the discussion so far has been about obtaining sub us timing precision with a micro. You might even manage 16bit accuracy with a free running 5MHz clocked timer register that you read whenever an edge triggered event occurs.

The cheapest PIC with a dedicated PWM output would be my choice.

Incidentally a very cheap and cheerful way to measure very short time intervals with an Intel based PC is the RDTSC instruction to access the

64bit timestamp counter registers which increment at the native clock rate. Intel warns about using them this way, but they work well enough to be useful for rough and ready testing. The jitter from running under 'Doze is a bit of a pain but there are ways to run native 32 bit code in DOS console mode without the extra baggage.

Regards, Martin Brown

Reply to
Martin Brown

The problem as described is basically an analog problem. Your ramp and ADC just chanages it from analog in time to analog in voltage.

--
These are my opinions, not necessarily my employer\'s.  I hate spam.
Reply to
Hal Murray

| | Its been an interesting discussion. | | Additional information; | | Both pulses are about 1 ms long, and can coincide with the time | between A and B pulses edges right at if not zero, or the time is long | enough B pulse is on top of the following A pulse. This indicates | edge detection is required, and missing an edge could cause wildly | inaccurate results. The data is in the time the B pulse follows the | preceding A pulse. and is always a positive number. | | 8 bit accuracy is adequate. A micro has not been selected pending | arriving at a scheme for this task, preferably the smallest micro | possible for cost and space. The micro does have to manage a 25 Hz | PWM out in addition to the input pulses. | | The ramp circuit referred to above does differentiate the pulse edge | and requires bilateral switches or possibly a couple Mosfets. An RC | ramp is iffy, nonlinear but simple. I'd like to use a constant | current source for the ramp but prefer minimum parts. A constant | current diode might work if the current tolerance isn't an issue. | | Any further suggestions appreciated. |

Measuring a 12.5ms interval with 8 bit accuracy means time steps of about

50us. A PIC like a 16F628 running on its internal 4MHz clock has an instruction time of 1 or 2us. As your pulse width is about 1ms you will always have plenty of time using the conventional procedure. No need to miss an edge either. On the first edge you can check whether or not the second one already occured. If not, you start a counter and wait for that edge, otherwise you found the time
Reply to
petrus bitbyter

berichtnews: snipped-for-privacy@mid.individual.net...

It is possible. But you need to drive the clock of the uC from the ramp timer clock source, or have the uC clock spectrally very clean (no digital frequency "loop" fudging and stuff, like many modern uC do). I have done something similar to synchronize several ADCs where the requirements were in the tens of picoseconds. Ok, there was a DSP on board because the client wanted to do all this in the frequency domain at first but that thing became nearly unemployed, a uC could have done that with ease. The only reason we left the DSP on there was that is only cost around $5, not much to be saved by going to a uC and they didn't want to re-layout that area.

Martin and Senor Bitbyter have already said it, for 8-bit accuracy inside timings in the millisecond range a uC is more than adequate. You have to get interrupt priorities right but the horsepower is certainly there.

If you ever need ramps and current sources I suggest not to use current diodes. It's boutique stuff, expensive, often hard to purchase. Basically glorified JFETs. I'd make a real current source.

--
Regards, Joerg

http://www.analogconsultants.com/

"gmail" domain blocked because of excessive spam.
Use another domain or send PM.
Reply to
Joerg

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.