Crystal Accuracy

Go To Last Post
34 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hmm, My 10ppm crystal looses like 10 seconds a day. That's like 116 parts per million. Perhaps I have wrong capacitors?

Epson TSX-3225 16.0000MF18X-AC3 16MHz

22pf capacitors.

xMega8e5.

Clock setup code:

void CrystalClock16(void)
{
	OSC.XOSCCTRL=OSC_FRQRANGE_12TO16_gc | OSC_XOSCSEL_XTAL_16KCLK_gc;
	OSC.CTRL|=OSC_XOSCEN_bm;
	uint8_t n=(CLK.PSCTRL & (~(CLK_PSADIV_gm | CLK_PSBCDIV1_bm | CLK_PSBCDIV0_bm))) | CLK_PSADIV_1_gc | CLK_PSBCDIV_1_1_gc;
	CCP=CCP_IOREG_gc;
	CLK.PSCTRL=n;

	// Wait for the external oscillator to stabilize
	while ((OSC.STATUS & OSC_XOSCRDY_bm)==0);

	// Select system clock source: External Osc. or Clock
	n=(CLK.CTRL & (~CLK_SCLKSEL_gm)) | CLK_SCLKSEL_XOSC_gc;
	CCP=CCP_IOREG_gc;
	CLK.CTRL=n;

	//internal 32kHz, PLL
	OSC.CTRL&= ~(OSC_RC2MEN_bm | OSC_RC32MEN_bm | OSC_RC32KEN_bm | OSC_PLLEN_bm);
}

Setting up my 1hz interrupt

void SetUpClock(void)
{
						// Crystal is 16000000 Hz
	TCD5.CTRLA = 7 ;	// 16000000/1024
	TCD5.CTRLB = 0 ;	
	TCD5.CTRLC = 0 ;	
	TCD5.CTRLD = 0 ;
	TCD5.CTRLE = 0 ;
	TCD5.PER = 15625 ;
	TCD5.INTCTRLA = 1 ; // Low level interrupt on overflow
	TCD5.INTCTRLB = 0 ;
}

Maybe I need my "PER" to be 15626 or 15627? 15625*1024=16000000. 1 count difference here would be about 64ppm. Have I miscounted fenceposts?

Fenceposts: A reference to an old math question: If a farmer wants to build a straight piece of fence 100 ft long with posts spaced every 10 ft, how many posts does he need? (11)

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Surely this is the same thing as CTC timers in tiny/mega? The oft overlooked issue there is that "0 counts" so if you want a count of 15625 you have a counter count 0..15624 and the upper compare value is therefore 15624 not 15625.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Was just realizing that, but the church's router was down. Yes, if the clock is running slow, the count is long, not short, and 15625 counts would be 0-15624.

Will fix Monday Morning.

I thought it would be a "fencepost problem."

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Off-by-one with the digital count yes.

Also it seems your crystal is specified for 9pF load if I have tracked down the part number correctly. Two 22pF capacitors create already a 11pF load for the crystal, just add PCB stray capacitance and IO pin capacitance. Extra capacitance will slow it down a bit.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So I need more like 15pf? Not sure what other small value SMD caps I have,

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Torby wrote:
So I need more like 15pf? Not sure what other small value SMD caps I have,

15pf seems like a good starting place if you have some hanging around.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Loosing time means the frequency is low. Smaller caps would raise the frequency. At least that is the right direction.

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Another option is, don't change the capacitors, just change the amount of how many crystal ticks resemble a certain amount of real-world time. Downside is you have to calibrate it, but the good thing is the software can be made more accurate than the crystal. For example interpreting 15999999 ticks as 1 second instead of 16000000 ticks gives you 0.0625 ppm tuning...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, I just adjusted the counter from 15625 to 15625-1

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Torby wrote:
Hmm, My 10ppm crystal looses like 10 seconds a day. That's like 116 parts per million. Perhaps I have wrong capacitors?

Use a trimmer capacitor so you can adjust it to the most precise setting.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Correcting the period of the timer has it to about 2 seconds slow in 12 hours, which is still out of spec, but I might live with it.

Perhaps I'll make a "seconds per day slow" parameter and set the time to that vale instead of 0 at midnight.

Goodness, many people making clocks all of a sudden.

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't know what this a for, but why don't you use the AC's 50 or 60 Hz ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Lose. Loose is a different word!

Four legs good, two legs bad, three legs stable.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That would work too - simple.

I would have suggested that since 15625 is divisible by 5, there is a 5Hz interrupt instead of 1Hz interrupt. Then accumulate amount of real-world time which in your case must run faster than crystal. Therefore, most of the time the seconds get updated every 5 interrupts, but sometimes after 4 interrupts.

If this is a watch someone most likely does not notice that sometimes seconds update after 1000ms and sometimes after 800ms. If this is a problem then use a faster timer like 125Hz, and so seconds change every 1000ms or 992ms.

It all boils down to a) how many crystal ticks there really is per one human second or one human minute or whatever, and b) how often or with what jitter you update the perceived seconds.

Do you display seconds, or just minutes?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I display seconds, for want of anything better to do. I could, naturally, make it a pernicious clock. The actual goal is to keep the time of day from drifting. It's replacing a system where it drifted about 1 minute per week and was a pain in the you-guess-where to set. This looks to drift about a minute in 4 weeks, and is quite easy to set.

Just thinking a clever bit of code might hold within a minute all summer long (May through October).

Could provide a "drift" figure in like hundredths of seconds per day and use that figure to set the interrupt count and timer count at midnight each day, but that might be more work than needed as the whole idea is to be able tell people "2:30," and not mistakenly tell them when it has passed, not when it's about to happen.

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It's now gaining 1 second a day. I think that will do.

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Synchronize it to the first stroke of the church bells every sunday morning. That will keep it really accurate!

Charles Darwin, Lord Kelvin & Murphy are always lurking about!
Lee -.-
Riddle me this...How did the serpent move around before the fall?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

By the grace of God.

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There's actually a lot to be said for that sort of strategy!

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What is Your time base reference source?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
What is Your time base reference source?

The beginning of the first day 6000 years ago! :)

Seriously, if you want it really accurate you could syncopate your clock with the 1 PPS from almost any low cost GPS engine.
To set clocks around or check the longer term accuracy of clocks, I usually use WWVH on 5.0,10.0 or 15.0 Mhz. I just love that signal, been listening to it for over 55 years(but not continuously of course :lol: ).

Charles Darwin, Lord Kelvin & Murphy are always lurking about!
Lee -.-
Riddle me this...How did the serpent move around before the fall?

Last Edited: Wed. Nov 27, 2013 - 10:10 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Torby wrote:
Hmm, My 10ppm crystal looses like 10 seconds a day. That's like 116 parts per million. Perhaps I have wrong capacitors?

Epson TSX-3225 16.0000MF18X-AC3 16MHz

Provided your maths is right, the Crystal is always the best judge of capacitance.

Torby wrote:

Correcting the period of the timer has it to about 2 seconds slow in 12 hours, which is still out of spec, but I might live with it.

That is still 46ppm, but if you do not want to change or Trim the C's to make this correct, you can apply a digital correction.

One part in 15625 is ~64ppm, so you could move the count by a further 1 and get to within 17.7ppm (now fast)

That gives you two timer solutions : 46.296ppm slow and 17.704ppm fast

If you then modulate between those two divisions, you can go well below crystal drift.

An easy modulation timebase is 8 bits, so we find

1-((256-185)*(1+46.296u)+185*(1-17.704u))/256 = -4.6e-8

ie 185 of those 256, you choose one Div, the rest the other, and the average time error is now 46ppb

add some lines to adjust that /256 to /235 and a fractional match here is better

1-((235-170)*(1+46.296u)+170*(1-17.704u))/235 = 1.872ppb

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How about using varactor/varicap trimming on the oscillator, driven by PWM output.
You could calibrate against 1 PPM signal from GPS & save value in EEPROM.
The OSCCAL algorithm could be adapted to build the calibrate routine. Just an idea!

Charles Darwin, Lord Kelvin & Murphy are always lurking about!
Lee -.-
Riddle me this...How did the serpent move around before the fall?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So I need to keep a second count 0-255. If this count is less than 185, set the count to 15625-1, and if >= 185, use 15625-2.

Since it's now running about 1 second per day fast, it's probably plenty accurate. Any closer is just for fun.

I'll give it a try. Keep in mind, I'm checking its accuracy by letting it run about a day and comparing it to the display at http://www.time.gov So we're probably aiming for accuracy better than my ability to measure

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That is what MSF or DCF77 clocks do. Free run at good-enough accuracy. Correct themselves every few minutes from the radio signal.

I don't think that they adjust their free-run speed according to the difference, but they could do.

This is what you do when you calibrate the RC oscillator of the AVR.

Likewise, you can 'tune' a crystal oscillator for a few Hz by adjusting the capacitance in the resonant circuit. In practice, this will give you a good accuracy if the temperature is reasonably constant.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Using Who-Me's technique, the error is smaller than I can measure without cooking up some more complex instrumentation. Very pleased.

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

After a week, it's still "On the nose."

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Does Anyone Really Know What Time It Is -- Chicago Transit Authority

...
As I was walking down the street one day
A man came up to me and asked me
What the time was that was on my watch, yeah
And I said

Does anybody really know what time it is?
Does anybody really care?
If so I can't imagine why
We've all got time enough to cry
...

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

http://www.time.gov

The main java display has been broken since I started this project, so click the "Time widget" link down below.

So, you see, I can't really observe milliseconds, I just put the two displays together and see that after a week they still say the same time and are still changing together.

Of course, a second a day is more than accurate enough for the purpose

Now working on the second part of this project. The clock communicates with a hidden controller that makes the volcano erupt on queue.

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Torby wrote:
The clock communicates with a hidden controller that makes the volcano erupt on queue.
How many people have to be in the queue before the volcano goes off on cue? :twisted:

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Virgins. We only queue up virgins for the volcano :twisted:

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I recall there was a project here on AVR Freaks to measure and calibrate AVR projects. It used a modified NTP daemon or something, that received one second pulses from AVR (instead of GPS) and kept track of AVR time related to the NTP servers (not affecting the NTP servers). I never used it though.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Torby wrote:
Using Who-Me's technique, the error is smaller than I can measure without cooking up some more complex instrumentation. Very pleased.

Torby wrote:
After a week, it's still "On the nose."

Glad to hear it worked nicely for you :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It's been running for weeks, even through a power failure and it's still on the nose.

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut.