Accurate time measurement

micrometal

New Member
I want to measure a roughly three second time period to a precision of 0.0001%. The event is repeatable so measuring the time for, say, one hundred such events (about five minutes) and doing the calculation would not be a problem and would help achieve the accuracy.

I expect to use some form of external time-base. I thought that I might use the "count" or "pulsout" commands in some way, but I cannot see how to extend these to the period that I want to measure.

How can I go about this?
 

oracacle

Senior Member
To even get close to the level of precision you would need tempurature compensation. You also Don't mention the resolution you need.
The entire timing circuit would need to be tempurature compensted. You maybe able to use a temputure compensated crystal with a 28x2 or 40x2 to get the picaxe to get close but i don't know how the picaxe will handle that or if the internal PLL will cause issues with the timing too.
You would also tun into issue using it to just generate a clock signal to be counted as the picaxe would have to count the pulses over a given period of time, thusly the picaxe would have to be tempurature compensted too.
The DS3231 is tempurature compensated, but only has 1s resolution.
 

AllyCat

Senior Member
Hi,

So basically you need a counter-timer with clock period of around 3 us, a maximum count of >1 million and an accuracy of 1 in 106 (equivalent to 1 second in > 11 days)? In practice, you'd probably use a 1 us (1 MHz) clock and count to over 3 million. Very unlikely to be possible with "software" counters such as COUNT or PULSIN, but the "repeated" events might help towards a solution:

First measure the "time period" approximately (to better than 1%) with a software command such as PULSIN. Not directly possible because it will normally timeout at 0.655 second but there should be a workaround. ;) You may also need to defeat the PIC(axe)'s "Watchdog" which may kick in at about 2.3 seconds.

Then measure the time period with an "available" hardware 16-bit timer. The M2s have a "Timer 1 Gate" feature that I've previously described, but there might be others with the X2s (that I never use). Beware that the PICaxe Basic interpreter may "interfere" with Timer 1. The 16-bit timer will overflow numerous times, but in principle you can calculate the number of overflows from the first measurement and hence the number of missed clock pulses. You'll probably need a few "32-bit" subroutines but there may be some in a "Code Snippet" and for example the * and ** operators allow multiplication of two 16-bit words to give a 32-bit "Long Word" result.

Then as soon afterwards as possible, use the same hardware to measure a precise 3-second (or some multiple) external event period (which is very unlikely to give a result of exactly 3 million). Then you can correlate the two time periods to give your required "precise time period". An X2 with external crystal oscillator should give an adequate short-term stability; I don't know if the M2's internal oscillator is stable enough, but they do have some (largely undocumented) methods to use an external crystal. As for the 3-second Reference time (with 1 part in 1 million accuracy) you could try a good RTC with a precise 1 second pulse output such as the DS3231, or perhaps a GPS receiver.

Cheers, Alan.
 
Last edited:

micrometal

New Member
Thank you for these replies. I have been working on a tool that takes a long sample but that has its inconveniences and I wanted to make sure that I had not missed something better. I can see now that if the "count" function runs around into another cycle then that, in itself, is no handicap - it would be possible to infer the number of cycles. But I am probably better off sticking to my original plan.
 

AllyCat

Senior Member
Hi,

Hmm, I'm not sure what the "Original Plan" was, but there are two fundamental issues: Firstly you need a time Reference with an "Absolute Accuracy" of 1 part in 106 and I'm not sure what that is. Secondly, the idea of taking multiple measurements (in association with an intentional and random degree of "noise") is a technique known as "Oversampling". The problem is that AFAIK, the number of required measurements increases as the power of the improved resolution, i.e. to increase the resolution by a factor of 10 needs 100 measurements, etc..

Cheers, Alan.
 

micrometal

New Member
Okay, I might have overstated the precision - values better than 1 in 100,000 would be useful much of the time.

I'm not sure what the "Original Plan" was ...
The original (and still current) plan is to connect with a smartphone through Bluetooth and use UTC time (or "GPS" time as you mentioned in an earlier post). The downside is that I have to transmit an integer count value so there is a loss of precision, although it will usually, I hope, give me a usable indicator or trend. This much is working on the test-bench; I thought that I would just check that I had not overlooked some better way before building a "production" box.
 

oracacle

Senior Member
You could look at GPS chips PPS (pulses per second) output. Something like GT-U7 can give upto 1khz PPS for the most common and cheaper variant, and upto 10Mhz for the elusive and expensive variant. Most GPS chips do not give any sort of access to this, i certainly haven'y found a way of accessing anything smaller than s from my phone - but then i haven't looked that hard
Data sheet: 91tuvtrO2jL.pdf (ssl-images-amazon.com)
This would give an accurate clock source and 40ns accuracy, providing you can get it to work with PICAXE
Every project that I have seen these used with, and used to keep track of time have used tempurature regulated crystals for the controller
Even if you use the PPS from the GPS into hippys' timing curcuit you have the tempurature variability of the picaxe itself. When you are getting towards the timing standards you are looking for a few fractions of a degree are going to make a difference and get you of your 1 in 100,000 target. It not even just the crystal itself, but the PLL circuit that is often used to get higher clocks from slower crystals.

As an idea I have been working another project using a AS5600 encoder

here is how I decided to be able to calculate things for RPM
Code:
/*
    //clock1 = ARM_DWT_CYCCNT;       //read system clock - disabled during micros testing
    clock1 = micros();                            //read micros() variable
    ReadRawAngle();                             //read angle from AS5600
    clock2 = micros();                            // read micros again
    //clock2 = ARM_DWT_CYCCNT;     //read system clock - disabled while using micros
    Serial.println(clock1);                      //send before and after clock to serial monitor
    Serial.println(clock2);
    difference = clock2-clock1;            //calcualte difference
    Serial.println(difference);                //print it
    //using micro() we get 202 micros second to read angle over i2c
    //using arm clock 121122 - 121253 cycles 0.00020187 - 0.000202088s @600MHz
    //1rpm = 6 degrees a second
    //0.00036 dgeree per m/s or or 0.07272 degrees for 202ms
    //1,000,000us per second, 202 fits 4950.495045950 times
    delay(500);
    Serial.println();
  */
this line here is the most telling
Code:
//using arm clock 121122 - 121253 cycles 0.00020187 - 0.000202088s @600MHz
a variation of 31 clock cycles when continuously reading the same i2c device (and this was in tempurature stable room too). GPS can remove that, in theory, if you just read that data stream from the GPS chip, assume that each read takes same amount of time, providing you can find a GPS that will let you access the nanosecond scale. And then from the specs i have seen most don't get much below 20ns resolution. With that said i suspect you are going to run out of mathmatical space very quickly. The next section of that code, added up a set of reading and divided by the total number of readings taken to give an average output.

There is something else that cropped up while looking through various other timing thread on various forums, the rate that the I/O operated at, the chip used for the above although runs it core @600mhz the I/O runs at around 150mhz. I don't know if this withstands for interrupts or not but is something else you are goin to have to think about along side rise and fall times.
 

hippy

Ex-Staff (retired)
Even if you use the PPS from the GPS into hippys' timing curcuit you have the tempurature variability of the picaxe itself.
Because the PICAXE uses on-chip hardware it should be immune to temperature and other variations in speed of execution.

The hardware is used as a 16-bit counter and overflow flag and will accurately count the pulses. So long as the PICAXE reads and clears the overflow flag, keeps the running count updated, before the next overflow it should be as accurate as the clock incrementing the counter. Those overflows only happen every 65,536 clock pulses which has quite some time between them; 6.5ms even at 10MHz.

Handling larger than 16-bit numbers poses challenges for a PICAXE but, depending on what's wanted, it may be possible to greatly simplify the maths, chose a counting regime or over-sampling quantity which helps with that.

I suppose the question is; what is done with the '3 second time' measurement when it is completed ?
 

micrometal

New Member
Thanks for your ideas, oracacle, but I am not a hardware man and this looks well beyond my pay-grade.

I did get a new idea last night, though, where I think that I can transfer UTC status from my smartphone (at millisecond precision) to the Picaxe. My timing method at the moment simply counts pulses over a long but accurately measured period and suffers from the "fence-post effect". If I can transfer a UTC datum to the Picaxe to better establish the position of the first and last fence-post (measured pulse) relative to the edges of the timing window I will get a huge improvement in accuracy - much more than an order of magnitude I think.

Also I need to look at hippy's suggestions. This is a bit of a struggle for me, but I have not given up yet.
 

oracacle

Senior Member
If you want to go the route of hippy's timer I built a time based on it using done logic gates for to start stop triggers

 

hippy

Ex-Staff (retired)
I need to look at hippy's suggestions. This is a bit of a struggle for me, but I have not given up yet.
If you can detail what you are attempting to achieve, the reason and end purpose of it, it may be possible to determine what the best way forward will be.

As a starter; why do you need to measure a 3 second pulse to 3us accuracy ? What's the pulse used for ?
 

micrometal

New Member
As a starter; why do you need to measure a 3 second pulse to 3us accuracy ? What's the pulse used for ?
Hi hippy. I have thought seriously about your questions. I know that I have been a bit obscure from the outset, but that was deliberate. I follow this forum regularly. Some of the threads turn into very long discussions, I feel, and I did not want this to be one of them. I realise that many of you are "old friends" and this is like meeting down at the pub, but that is not for me.

Meanwhile I have entirely met my objective. My present approach has drawbacks and I wanted to test if I had overlooked some more direct method. It seems that I have not, and anything more direct is likely to be hardware intensive which would be difficult for me to implement. But I have come up with a new idea that might be a major step forward and in any case will be interesting to investigate.

So I am happy to leave things here, although I might be back later. I hope that you understand.
 
Top