Continue to Site

# Accuracy and Repeatability Calculation

Status
Not open for further replies.

#### Urahara

##### Member
Hi All

It's been a while.

In an EPE magazine in Sep 07, there was a diy project (Flexitimer) which said that it has a timing accuracy of better than +/- 0.02%. If I remember correctly, it uses a PIC MCU with a 4MHz crystal.

I plan to have a PIC MCU and an external 4MHz crystal which I would like to do some counting either in 1sec and 0.1sec resolution. How does one calculate timing accuracy and repeatability?

Thks!

#### Hakachukai

##### New Member
There are lots of factors that go into that calculation.

How you write the code matters
How accurate your crystal is matters
How accurate the PIC is matters

Possibly a few other things.

It's not possible for us to just give you a simple formula, because too many details are missing.

#### Urahara

##### Member
There are lots of factors that go into that calculation.

How you write the code matters
How accurate your crystal is matters
How accurate the PIC is matters

Possibly a few other things.

It's not possible for us to just give you a simple formula, because too many details are missing.

I am using a PIC 18F4520.

My code essentially sets up timer1 for timing interrupt at say 0.1sec, and then loops continuously to see if the interrupt flag has been set. Have also taken some care to ensure that the codes executed within the loop is well below 0.1sec.

Assuming codes and PIC are accurate, how can I determine the accuracy and repeatability with information obtained abt the crystal? I know crystal is specified by ppm. So if a 4MHz crystal is spec'ed at say, +/- 20ppm, is there a way to determine accuracy and repeatability?

Thks!

#### blueroomelectronics

##### Well-Known Member
Why would you poll an interrupt flag, why not just use the interrupt instead.

#### Hakachukai

##### New Member
I am using a PIC 18F4520.

My code essentially sets up timer1 for timing interrupt at say 0.1sec, and then loops continuously to see if the interrupt flag has been set. Have also taken some care to ensure that the codes executed within the loop is well below 0.1sec.

Assuming codes and PIC are accurate, how can I determine the accuracy and repeatability with information obtained abt the crystal? I know crystal is specified by ppm. So if a 4MHz crystal is spec'ed at say, +/- 20ppm, is there a way to determine accuracy and repeatability?

Thks!

ppm stand for Parts Per Million.

so 4mhz * 20/1,000,000 = 8*10^-5 mhz = 80hz

So your crystal will run somewhere between 3,999,920hz and 4,000,080hz.
you can then take those 2 extreme numbers and figure in any other inaccuracies that you have, to generate new extreme numbers.

#### kchriste

##### New Member
Forum Supporter
The crystal will drift with age and temperature.
The temperature drift can be compensated for by the type of capacitors you choose for the oscillator circuit. Or, if you wanted to get really trick, you could add a temp sensor to the PIC and compensate in code though that would require you to first log and study the typical drift of the circuit and create a compensation table from the data.
As for age drift, there is not much you can do about that, though frequency drift from age is usually far less than what you get from temperature changes.

#### Urahara

##### Member
ppm stand for Parts Per Million.

so 4mhz * 20/1,000,000 = 8*10^-5 mhz = 80hz

So your crystal will run somewhere between 3,999,920hz and 4,000,080hz.
you can then take those 2 extreme numbers and figure in any other inaccuracies that you have, to generate new extreme numbers.

+/- 80hz/4,000,000hz = +/- 0.002%
So I assume that's accuracy based solely on the crystal itself. I re-read the EPE article and it said the circuit should be better than +/- 0.02%. Maybe a factor of 10 is used?

How abt repeatability? Is there a way to calculate based solely on the crystal specs?

Hi Bill, didn't set up an interrupt service routine as my pgm is not doing anything while waiting anyway. So took the easy way out and poll on the interrupt flag.

Hi KChristie, I use normal ceramic caps. Am not so anal abt that level of accuracy, but guess it is worth knowing.

Thks!

#### Mr RB

##### Well-Known Member
There is some good info here;
Zero-error 1 second timing algorithm

If you look down the page there is a technique for generating very accurate 1 second periods, which will get you much better accuracy than the inherent xtal speed; if you adjust the variable after testing that xtal you can get it to a few PPM especially if used indoors where the temperature doesn't change that much.

(I cut and pasted);
---------------------------------------------------------
Clock xtal super-fine calibration.

The int period and 1second period value can both be multiplied by the same number, giving an increase in the timer resolution much greater than the original resolution of the xtal value in Hz.

This next example uses a PIC with 4Mhz xtal (1MHz timer), and testing against a GPS receiver over a month shows the PIC clock is 9 seconds fast. So the 1second correction value is calculated as;
(1month+9secs) / 1month which in seconds is; 2678409 / 2678400
Which is 1.0000033602
Multiply by 1Mhz to give the correct 1 second period; = 1000003.3602 ticks

Since we are using an unsigned long variable for the bresenham accumulator the same code will take a value up to 4.29 billion with no issues. So we just multiply both the periods by 1000, which gives 1000 times finer resolution to calibrate the clock but still uses the same simple C code;

Code:
	//C code for PIC 4Mhz xtal (1MHz timer) super-fine calibration
// uses 1 variable; unsigned long bres
// gets here every TMR0 int (every 256 ticks)

bres += 256000;		// add (256*1000) ticks to bresenham total

if(bres >= 1000003360)	// if reached 1 second!
{
bres -= 1000003360;	// subtract 1 second, retain error
do_1sec_event();	// update clock, etc
}
---------------------------------------------------------

If you use that code you can calibrate your 1MHz timer to .000000001 MHz resolution, and the code is very simple.

Last edited:

#### Urahara

##### Member
Hi Mr RB, thks for your info! Certainly something worth considering if I can get my hands on an accurate clock reference.

With regards to repeatability, I am wondering if I could use the freq stability tolerance of a crystal to calculate? All things being equal, and assuming a duration of a short period (say an hour) during which timing is being made repeatedly, wouldn't this be the "only" thing that varies in a simple circuit? Any thoughts?

#### Mr RB

##### Well-Known Member
You can't calculate based on the tolerance of the xtal because it is a manufacturing variable and is unknown.

The best way is to make the clock/timer etc with your xtal, check it over X hours against a known good source like a GPS, a telephone talking clock, radio clock standard, satellite TV set top box clock etc. A 24 hour period will do fine, most clocks lose or gain a few seconds in 24 hours. Then just adjust that bresenham accumulator value so your clock is accurate.

Then "repeatability over an hour" of your clock will be excellent provided you operate the clock at the same temperature during the hour as it was in the 24 hour cailibration test.

If you want temperature compensation, you can check out the Dallas "DS32kHz" temp compensated 32768Hz clock reference. I use one of these in my house master clock and it's great. The web page I posted has bresenham code for generating a 100Hz event (stopwatch etc) from the 32768Hz source (which doesn't divide by 100). However it might not be useful to you if you need fine resolution (ie uS) stopwatch measurements.

Or you can make your xtal into a OXCO that keeps constant temperature, see here;
https://www.romanblack.com/xoven.htm

or just buy a commercial OXCO xtal module.

As for ageing of the xtal causing freq change, that is usually shown in the xtal datasheet but it is so slow you don't need to worry with multiple tests in one hour!

#### blueroomelectronics

##### Well-Known Member
You could also use a cheaper TCXO (digikey.com) $6 A true OCXO starts at over$120US.

#### kchriste

Forum Supporter
A true OCXO starts at over $120US. Not to mention the amount of power that it'll draw. #### Mr RB ##### Well-Known Member$120?? That's why I made one.

Mine only uses some milliwatts too.

The link is working for me, for now anyway... Maybe my page just had a server down for a while.

Status
Not open for further replies.

Replies
9
Views
1K
Replies
12
Views
2K
Replies
16
Views
862
Replies
3
Views
623
Replies
37
Views
2K