Author Topic: Getting an accurate clock with a crystal  (Read 11205 times)

0 Members and 1 Guest are viewing this topic.

Offline PCChazterTopic starter

  • Contributor
  • Posts: 23
  • Country: ca
Getting an accurate clock with a crystal
« on: May 14, 2017, 11:05:31 pm »
Hello forum,

I am trying to set up a TMS320F28027 C2000 microcontroller to act as an RTC for use in other functions on the chip. I have a crystal hooked up, and set up Timer2 to use the crystal. It seems to work, but I am not getting the accuracy that I am looking for.

It uses a 12 Mhz crystal that is supposed to be 15ppm, and I calculated the load capacitors to be 6pF using the formula C = 2(CL - Cstray), assuming 5pF Cstray, CL is 8pF.

The timer has a 11999 prescaler, which means the timer should count every 1ms * period, which means it should trigger every second, which just adds 1 to a 32 bit number. It seems that after 20 hours, it is 20 seconds out, which is 0.0556% or 556ppm, not even close to the expected 15ppm!

This is the first time I have implemented a crystal in my design, so I'm not 100% sure I am doing it right, or really what accuracy I should be able to expect.

Anyways, does anyone have any ideas or advice as to why I'm not getting the accuracy I need, or is my expectation unreal, and I should just get a dedicated clock chip? I would prefer to not have a separate chip if at all possible.


The crystal: https://www.digikey.ca/product-detail/en/avx-corp-kyocera-corp/CX3225SB12000D0GZJC1/1253-1696-1-ND/5995243

The Capacitors: https://www.digikey.ca/product-detail/en/tdk-corporation/C1608C0G1H060D080AA/445-5037-1-ND/2093652

The (relevant) board layout:


The code:

Code: [Select]
uint32_t clock_period = 1000; //in ms
uint32_t clock_time_s = 0; //in s
uint32_t clock_time_s_offset = 0;

void initClockTimer()
{
    myTimer2 = TIMER_init((void *)TIMER2_BASE_ADDR, (size_t)sizeof(TIMER_Obj));

    EALLOW;
    PIE_registerSystemIntHandler(myPie, PIE_SystemInterrupts_TINT2, (intVec_t) &clockInterrupt);
    EDIS;

    TIMER_stop(myTimer2);

    CLK_setTimer2Src(myClk, CLK_Timer2Src_ExtOsc);
    TIMER_setPeriod(myTimer2, clock_period);
    TIMER_setPreScaler(myTimer2, 11999);
    TIMER_reload(myTimer2);
    TIMER_setEmulationMode(myTimer2, TIMER_EmulationMode_RunFree);

    TIMER_enableInt(myTimer2);

    TIMER_start(myTimer2);

    CPU_enableInt(myCpu, CPU_IntNumber_14);

    Clock_setPosixTime(1447459200);
}

void Clock_setPosixTime(uint32_t t)
{
    clock_time_s_offset = t - clock_time_s;
}

#pragma CODE_SECTION(clockInterrupt, "ramfuncs");
__interrupt void clockInterrupt(void)
{
    GPIO_toggle(myGpio, GPIO_Number_0);

    clock_time_s++;
}

« Last Edit: May 14, 2017, 11:09:06 pm by PCChazter »
 

Offline pigrew

  • Frequent Contributor
  • **
  • Posts: 680
  • Country: us
Re: Getting an accurate clock with a crystal
« Reply #1 on: May 14, 2017, 11:16:49 pm »
In designs I'd been doing, I've also had trouble having the crystals be in specification at first. I've been tweaking the capacitor values until the clock is correct. Usually a larger capacitor will make the clock run more slowly.

5pF of stay capacitance sounds too high. I'd expect more like 1 or 2 pF.

I measure the frequency using a frequency counter. I never probe the crystal directly, I program a PWM signal as a clock divider, and use that to measure the clock by proxy.

I see that you have a acute angle in your layout. It's best to avoid them. Also, the ground layout doesn't look so great. I'd add some ground vias around the crystal and load caps to connect to the micro's ground more directly.

I'm not sure about your micro, but make sure you are properly enabling the crystal, and not using an internal oscillator.

Also, the high frequency clock is going to be power hungry. Generally people use a lower frequency oscillator for accurate timing, as they are lower power.
« Last Edit: May 14, 2017, 11:26:37 pm by pigrew »
 

Offline PCChazterTopic starter

  • Contributor
  • Posts: 23
  • Country: ca
Re: Getting an accurate clock with a crystal
« Reply #2 on: May 14, 2017, 11:42:00 pm »
I guess I should have mentioned, it is 20 seconds slow, so I don't think a larger cap would help. I have also tried taking the caps right off, as well as using a 10pF ,and a 10pF with a 6pF in parallel, none of it seemed seem to change much. I also had another crystal that specifies 18pF CL, and that did not seem to change it enough to get it to stop being slow.

Unfortunately, I don't have an oscilloscope (yet, it's been on my list for a long time), so the best way I have to check it is to let it run for 10+ hours and see how off my time is.

Like I said, I haven't used a crystal before, so this is a learning experience for me! I used 5pF because that's just what a few pages I looked at said. I would hope that it would only be 1 or 2 pF, but I probably have a problem in my design, cause it almost seems like more...

You say acute angles are bad, which ones in particular look bad to you?

I'm fairly certain I am turning on and using the crystal correctly, but I can go through my code and make sure. Wouldn't that be a pain to find out I've actually been using the internal oscillator...
 

Offline pigrew

  • Frequent Contributor
  • **
  • Posts: 680
  • Country: us
Re: Getting an accurate clock with a crystal
« Reply #3 on: May 15, 2017, 12:01:30 am »
I guess I should have mentioned, it is 20 seconds slow, so I don't think a larger cap would help. I have also tried taking the caps right off, as well as using a 10pF ,and a 10pF with a 6pF in parallel, none of it seemed seem to change much. I also had another crystal that specifies 18pF CL, and that did not seem to change it enough to get it to stop being slow.

10 or 13 pF should be about right, I'd think.

Could the crystal have been damaged during soldering? They are somewhat heat-intolerant.

Unfortunately, I don't have an oscilloscope (yet, it's been on my list for a long time), so the best way I have to check it is to let it run for 10+ hours and see how off my time is.

In this case, an oscilloscope isn't the answer. A frequency counter is what you would want.

You say acute angles are bad, which ones in particular look bad to you?

In your track right above pad 1 of the crystal.

I'm fairly certain I am turning on and using the crystal correctly, but I can go through my code and make sure. Wouldn't that be a pain to find out I've actually been using the internal oscillator...

I just browsed the chip's datasheet. It seems somewhat complicated with two internal oscillators, and an option for two different types of external clocks. Figure 6-9 of the datasheet seems to suggest a series damping resistor on the X2 pin? I don't understand it, but maybe it's to force the ESR to be in an acceptable range for the oscillator circuitry.

You should be able to view system registers with the debugger to see which oscillator is in use.

I've also run into micro issues where the period was off-by-one, and I had to program a period of 9 to actually get a period of 10, though this wouldn't offset the frequency by as much as you've seen.
 

Offline StillTrying

  • Super Contributor
  • ***
  • Posts: 2850
  • Country: se
  • Country: Broken Britain
Re: Getting an accurate clock with a crystal
« Reply #4 on: May 15, 2017, 12:04:49 am »
If you could arrange for a LED to flash (or toggle) every 10 or 60 seconds you could compare with an accurate clock and detect if it's drifting without having to wait the full 10 to 20 hours.
.  That took much longer than I thought it would.
 

Offline pigrew

  • Frequent Contributor
  • **
  • Posts: 680
  • Country: us
Re: Getting an accurate clock with a crystal
« Reply #5 on: May 15, 2017, 12:15:40 am »
Figure 6-9 of the datasheet seems to suggest a series damping resistor on the X2 pin? I don't understand it, but maybe it's to force the ESR to be in an acceptable range for the oscillator circuitry.

Looking through the TI message boards, Rd is used to reduce the drive level of the crystal, and they also say that it's generally unnecessary at above 10 MHz.
 

Offline PCChazterTopic starter

  • Contributor
  • Posts: 23
  • Country: ca
Re: Getting an accurate clock with a crystal
« Reply #6 on: May 15, 2017, 12:50:43 am »
Could the crystal have been damaged during soldering? They are somewhat heat-intolerant.

This is a possibility, I used solder paste and a hot air gun to solder it on.

In this case, an oscilloscope isn't the answer. A frequency counter is what you would want.

Don't have one of these either, but my interrupt code pretty much does this for me.

In your track right above pad 1 of the crystal.

I see, I will refrain from this in the future

I just browsed the chip's datasheet. It seems somewhat complicated with two internal oscillators, and an option for two different types of external clocks. Figure 6-9 of the datasheet seems to suggest a series damping resistor on the X2 pin? I don't understand it, but maybe it's to force the ESR to be in an acceptable range for the oscillator circuitry.

You should be able to view system registers with the debugger to see which oscillator is in use.

I went through my code, assuming the libraries were configuring the registers correctly, and it looked good. You're right, it can seem to be over complicated, but in my code, I am using internal oscillator 1 to drive the main code PLL, and the crystal to drive timer2. All other peripherals run off the PLL loop, and internal oscillator 2 is turned off. To add to confidence I am using the right oscillator source for timer2, I just removed the crystal completely, and the timer stopped counting, so it is not a case of mistakenly using the internal oscillator.

I've also run into micro issues where the period was off-by-one, and I had to program a period of 9 to actually get a period of 10, though this wouldn't offset the frequency by as much as you've seen.

This is also something that I considered at first. The prescaler for the timer is exactly the way you describe, so I have it set to 11999 instead of 12000 to get 1ms timer intervals, which is then counted down from 1000 on the timer so that my interrupt triggers (or should trigger!) every 1 second.

If you could arrange for a LED to flash (or toggle) every 10 or 60 seconds you could compare with an accurate clock and detect if it's drifting without having to wait the full 10 to 20 hours.

I do like this idea. I can watch it in the debugger, but this would give a good quick reference to see if it is off.
 

Offline cncjerry

  • Supporter
  • ****
  • Posts: 1282
Re: Getting an accurate clock with a crystal
« Reply #7 on: May 15, 2017, 01:24:12 am »
is it always the same amount off?  20 seconds seems like a lot.  If it was a crystal problem then temperature changes over the measurement period would vary the PPM up/down. My experience with designing clocks based on timers is the instruction timing is usually the culprit once you get the crystal close.
 

Offline PCChazterTopic starter

  • Contributor
  • Posts: 23
  • Country: ca
Re: Getting an accurate clock with a crystal
« Reply #8 on: May 15, 2017, 01:40:45 am »
is it always the same amount off?  20 seconds seems like a lot.  If it was a crystal problem then temperature changes over the measurement period would vary the PPM up/down. My experience with designing clocks based on timers is the instruction timing is usually the culprit once you get the crystal close.
It does vary a slight amount, but it is always slow. I just haven't kept note of how much it varies. My problem is that I can't rule out the crystal oscillating correctly, but from what I can tell, my timer is set up right, and as long as my isr completes before the next interrupt, it should not affect the timer. If you see a problem in my code that I overlooked, let me know, it's in the main post

Sent from my XT1563 using Tapatalk

 

Offline pigrew

  • Frequent Contributor
  • **
  • Posts: 680
  • Country: us
Re: Getting an accurate clock with a crystal
« Reply #9 on: May 15, 2017, 01:41:17 am »
If you could arrange for a LED to flash (or toggle) every 10 or 60 seconds you could compare with an accurate clock and detect if it's drifting without having to wait the full 10 to 20 hours.
I would have it flash every second, and display an accurate (NTP-based?) clock on the computer. You should be able to see the blinking of the computer's second counter and LED go in and out of phase over a period of an hour, but you could see the phase change after less than ten minutes.

EDIT: Or, not. The NIST time service page says not to use it as a frequency standard.
« Last Edit: May 15, 2017, 01:58:47 am by pigrew »
 

Offline David Hess

  • Super Contributor
  • ***
  • Posts: 16615
  • Country: us
  • DavidH
Re: Getting an accurate clock with a crystal
« Reply #10 on: May 15, 2017, 01:55:26 am »
In this case, an oscilloscope isn't the answer. A frequency counter is what you would want.

Don't have one of these either, but my interrupt code pretty much does this for me.

A frequency counter would be the right tool for this however if a known good reference frequency is applied to one of the microcontroller's capture inputs, the microcontroller can time its own clock frequency.  This is commonly done for self calibration during production.

 

Offline Rerouter

  • Super Contributor
  • ***
  • Posts: 4694
  • Country: au
  • Question Everything... Except This Statement
Re: Getting an accurate clock with a crystal
« Reply #11 on: May 15, 2017, 02:23:43 am »
I have to say your layout isn't that nice, those caps grounds should be referenced to the oscillator ground pin and only join the plane at that pin, equally some rotation could tidy it up quite a fair amount

So possibly noise coupling, in general the wrong caps can only pull it +- 80ppm at most,

Equally the crystal you bought, what was the initial accuracy tolerance, crystals are stable, but you can buy them in 1% initial accuracy on the cheaper ones, which may explain your error, a low ppm per year crystal with a poor initial accuracy.
 

Offline PCChazterTopic starter

  • Contributor
  • Posts: 23
  • Country: ca
Re: Getting an accurate clock with a crystal
« Reply #12 on: May 15, 2017, 03:10:53 am »
I have to say your layout isn't that nice, those caps grounds should be referenced to the oscillator ground pin and only join the plane at that pin, equally some rotation could tidy it up quite a fair amount

So possibly noise coupling, in general the wrong caps can only pull it +- 80ppm at most,

As far as layout goes, is this better, or do you have some recommendations? I have been doing research over the past week to best practices on crystal layout, but input would be great! I have taken the crystal and capacitors off of the main ground plane, and have run just a trace from the nearest MCU GND pin, which is conveniently right next to the XTAL pins. There is a keepout area on the top layer for these components that goes to the other side of the MCU pins, and a separate ground plane attached at only one point under this keepout, with a via to connect the bottom ground plane to the top ground trace.

Is this overthinking it, or is this standard layout for any crystal?

Also, pigrew, you pointed out an acute angle in my first design, for the grounds on these 2 caps, would you leave it this way, make a 90ยบ trace bend, or point them towards the crystal?

Top layer:


Bottom layer:



Equally the crystal you bought, what was the initial accuracy tolerance, crystals are stable, but you can buy them in 1% initial accuracy on the cheaper ones, which may explain your error, a low ppm per year crystal with a poor initial accuracy.

It states +/- 15ppm for tolerance and +/- 15ppm for temperature stability
 

Offline james_s

  • Super Contributor
  • ***
  • Posts: 21611
  • Country: us
Re: Getting an accurate clock with a crystal
« Reply #13 on: May 15, 2017, 03:15:45 am »
Most of the time if you need precise timing you want one of the two loading capacitors on the crystal to be a variable capacitor that can be trimmed. It's certainly possible to get good accuracy, I have a Cathode Corner scope clock that keeps excellent time. It took a few iterations but I tweaked it until it keeps time to within a few seconds between DST cycles.
 

Offline PCChazterTopic starter

  • Contributor
  • Posts: 23
  • Country: ca
Re: Getting an accurate clock with a crystal
« Reply #14 on: May 15, 2017, 04:26:35 am »
Something interesting is that on the launchpad (TI's dev board), there is a spot for a 1M resistor parallel to the crystal (R7). What is the purpose of this resistor, and is it crucial to the crystal's accuracy? There is no mention of this resistor in the datasheet. They also show 36pF caps, which seems quite large to me.

source: http://www.ti.com/lit/ug/sprui11/sprui11.pdf


edit: oops, that's for the F28069, but the F28027 has the same value resistor
« Last Edit: May 15, 2017, 04:31:01 am by PCChazter »
 

Offline tombi

  • Regular Contributor
  • *
  • Posts: 163
  • Country: au
Re: Getting an accurate clock with a crystal
« Reply #15 on: May 15, 2017, 05:31:10 am »
Just a guess but I wonder if latency in the ISR could effect this? Like I wonder if the timer reload is somehow dependent on the ISR completing? Or the timer reload time is somehow a factor?

So if you change the timer so instead of going off every 1ms it goes off every 5ms does this change the error? 20 hours - 7.2 x 10^6 triggers - only need a small time error per timeout to get 20s error.

Tom
 

Offline rs20

  • Super Contributor
  • ***
  • Posts: 2318
  • Country: au
Re: Getting an accurate clock with a crystal
« Reply #16 on: May 15, 2017, 05:43:35 am »
Something interesting is that on the launchpad (TI's dev board), there is a spot for a 1M resistor parallel to the crystal (R7). What is the purpose of this resistor, and is it crucial to the crystal's accuracy? There is no mention of this resistor in the datasheet. They also show 36pF caps, which seems quite large to me.

When you make an oscillator using a simple NOT gate (input and output corresponding to X1 and X2 in your diagram), then I believe that 1M resistor is there to ensure that the oscillations start (otherwise, the NOT gate could start off with a low input and a high output, with no energy in the crystal and not enough leakage resistance in the crystal for anything to happen). The resistor ensures that the crystal drive staying high or low for long periods of time is impossible, and AFAICT it is assumed to have negligible importance or effect once the oscillator is up and running (hence the relatively enormous value).

So perhaps that chip just has a NOT gate internally, or equivalent?  :-//
 

Offline StillTrying

  • Super Contributor
  • ***
  • Posts: 2850
  • Country: se
  • Country: Broken Britain
Re: Getting an accurate clock with a crystal
« Reply #17 on: May 15, 2017, 09:43:47 am »
Despite its claims of Real time, Background, and Unobtrusive, are you sure the debugger doesn't stop the internal clocks for a few cycles every time it reads the internal registers.

I think the new layout would be worse, all those long tracks and especially gnd would have quite a bit of inductance.

Edit: Something like this appears to have the shortest 12MHz tracks.
« Last Edit: May 17, 2017, 01:08:34 am by StillTrying »
.  That took much longer than I thought it would.
 
The following users thanked this post: Someone

Offline Rerouter

  • Super Contributor
  • ***
  • Posts: 4694
  • Country: au
  • Question Everything... Except This Statement
Re: Getting an accurate clock with a crystal
« Reply #18 on: May 15, 2017, 10:44:10 am »
It will be listed in the chip datasheet, but in general the 1 Meg resistor is for 2 purposes, the first is to ensure it starts up reliably and in the time frame specified in the datasheet, and to reduce dc biasing issues.

The second is to limit the gain of the oscillator, as its getting feedback through the resistor it doesn't need to swing as hard, which reduces the power consumption somewhat, and reduces the amplitude of the higher harmonics.

The next best place to look for what is going wrong is compare the "drive strength" of the chip, to the same on the crystal, driving the crystal with too much power can cause gross calibration issues, essentially the crystal is swinging too hard and detuning itself somewhat. if the drive strength is too high, you may need to add a series resistor to reduce the drive strength.
 

Offline David Hess

  • Super Contributor
  • ***
  • Posts: 16615
  • Country: us
  • DavidH
Re: Getting an accurate clock with a crystal
« Reply #19 on: May 15, 2017, 03:47:39 pm »
Something interesting is that on the launchpad (TI's dev board), there is a spot for a 1M resistor parallel to the crystal (R7). What is the purpose of this resistor, and is it crucial to the crystal's accuracy? There is no mention of this resistor in the datasheet. They also show 36pF caps, which seems quite large to me.

I agree with rs20.  The resistor biases the inverter between X1 and X2 into its linear range.  Otherwise leakage currents could result in the oscillator not starting.  Some designs include this resistor as part of the integrated circuit so no external resistor is required.  The proper resistor has no effect on accuracy.

Parallel resonate crystals (this is a parallel resonate oscillator) have their frequency specified with a specific load capacitance so the total capacitance values including parasitic capacitance depend on the crystal specification.  One of the capacitors may be a trimmer to allow adjusting the crystal frequency.
 

Offline technix

  • Super Contributor
  • ***
  • Posts: 3507
  • Country: cn
  • From Shanghai With Love
    • My Untitled Blog
Re: Getting an accurate clock with a crystal
« Reply #20 on: May 16, 2017, 12:35:35 am »
If you need accurate RTC timing you can invest in one DS3231 TCXO RTC chip. That thing is guaranteed to stay within 3 seconds per year. And if you need a faster accurate clock you can trim the internal RC oscillator against this chip (effectively a digital feedback PLL.)

Or if you want to, your micro can be clocked off an atomic clock's 10MHz output.
 

Offline james_s

  • Super Contributor
  • ***
  • Posts: 21611
  • Country: us
Re: Getting an accurate clock with a crystal
« Reply #21 on: May 16, 2017, 03:01:27 am »
The DS3231 do indeed work very well, and they're dirt cheap from China. I'm curious where those come from, unlike many cheap gray market semiconductors from China, the DS3231s I've had work very well.
 

Offline CatalinaWOW

  • Super Contributor
  • ***
  • Posts: 5231
  • Country: us
Re: Getting an accurate clock with a crystal
« Reply #22 on: May 16, 2017, 05:29:29 am »
You have said that your clock is off.  Is it slow or fast?  I am with the others who suspect software overhead, but this could be totally eliminated if the clock is fast.
 

Offline rs20

  • Super Contributor
  • ***
  • Posts: 2318
  • Country: au
Re: Getting an accurate clock with a crystal
« Reply #23 on: May 16, 2017, 05:37:22 am »
You have said that your clock is off.  Is it slow or fast?  I am with the others who suspect software overhead, but this could be totally eliminated if the clock is fast.

I guess I should have mentioned, it is 20 seconds slow,...
 

Offline bson

  • Supporter
  • ****
  • Posts: 2270
  • Country: us
Re: Getting an accurate clock with a crystal
« Reply #24 on: May 16, 2017, 08:03:04 am »
Have you tried using timer 0 or 1?
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf