# Very accurate time measurement

I'm going to measure very short times as accurately as possible. The device will get first one pulse and within 150 microseconds six more, each on their own wires. The time between first signal and each of the six other signals must be measured as accurately as possible. The accuracy should be at least 100 nanoseconds but more is better.

Which microcontroller would be best for this? I've found this. They seem to have a timer with period of 4 nanoseconds. It would be accurate enough for me.

Would some other microcontroller be better to do this? Can this be done with AVR?

-
I think you're talking about precision, not accuracy. What if you have nanosecond precision, but the underlying clock is off by 10%? Then the measurements aren't accurate. –  Kaz Feb 26 '13 at 17:53
How far apart are the six pulses? Do they arrive on a common input or on six separate ones? –  starblue Feb 26 '13 at 20:09
They have all their own inputs. Pulses can sometimes come all at the same time but usually they come at different times. –  Kestis Feb 27 '13 at 4:52

The speed of the microcontroller is not always the limiting factor. The MSP430 can be an appropriate solution, not because of the fact it only runs up to 25MHz, but because several MSP430 devices have the Timer D peripheral which enables up to 4ns time period (256MHz). This is faster than almost all microcontrollers out there. Even the STM32 in its latest versions (F4) can only do 180MHz.

Timer D is available on MSP430F51x1 and MSP43051x2 devices such as the MSP430F5131.

However, that only solves being able to capture the time. The big question is what do you do with it because the processing itself will be slower. You could sample the time between two pulses, but you won't be able to do any processing between them, if that's what you mean.

-
My bad, hadn't fully RTFM of the MSP430, mainly because TI's website triggers pavlovian convulsions of hate. –  John U Feb 26 '13 at 18:15
@JohnU: Never had that reaction. The truth is the OP didn't specify much information about what he's doing so it's hard to give a proper suggestion. –  Gustavo Litovsky Feb 26 '13 at 18:57
Processing isn't problem. There is many seconds to do the calculations once the times have been captured and the algorithms aren't even complex. Can one MSP430 handle all six pulses or should there be more of them? –  Kestis Feb 26 '13 at 20:01
@Kestis: The Timer D contains 6 capture compares registers, and some MSP430 have more than just one Timer D module, so it should fit your requirement. –  Gustavo Litovsky Feb 26 '13 at 20:13

Timing to a resolution of 100ns requires a timer running at 10MHz. Many microcontrollers should be capable of running a timer that fast.

The problem comes when you are trying to time the arrival of 6 signals. Are these signals all on the same wire, or each on a different wire?

If they're all on the same wire, then is might be possible to do this accurately on any MCU with a single 10MHz timer. Naively, the code to do this would look something like this:

wait for trigger signal
reset timer

wait for first signal
save timer value
reset timer

....

wait for sixth signal
save timer value
reset timer


The problem is that it takes a finite amount of time to reset the timer. This causes two problems:

1. The times measured would be wrong by a few 100ns, depending on your implementation. However, they should be consistently wrong. I.E. wrong by exactly the same amount every time. This means you can easily compensate for it by adding a small amount to each measurement.

2. There would be a minimum time you could measure. If the any pulse arrived 100ns after the the previous one, then you'll probably miss it. I don't know if there's anything you can do about that on in software. You'll have to find a microcontroller which can handle multiple pulses in hardware.

Which microcontroller can handle multiple pulses in hardware? The Cypress PSoC! This is a microcontroller which also contains configurable digital blocks, meaning you can easily have 6 separate timers running, each at 60MHz, giving you a better than 20ns resolution.

Here's an example I knocked up quickly to show you the sort of things you could do with it. I've got 6 separate timers, all running off the bus clock, which can go up to 67MHz. There's a trigger pin which starts all of the timers running, and 6 other pins, each of which causes a capture event in the timer. A status register allows your code to monitor which timers have captures a pulse. The code can read the values out of the timers.

-
That PSoC seems very interesting. However I would prefer AVR or MSP430 because they're much more common. –  Kestis Feb 26 '13 at 19:52

Revised answer: A fast digital storage oscilloscope or possibly frequency counter device.