Author Topic: This is interesting...  (Read 4163 times)

0 Members and 1 Guest are viewing this topic.

Offline technixTopic starter

  • Super Contributor
  • ***
  • Posts: 3508
  • Country: cn
  • From Shanghai With Love
    • My Untitled Blog
This is interesting...
« on: September 12, 2016, 03:16:05 pm »
I got an ATmega32 which have two sets of oscillator inputs, one set for the system clock and the other for a 32768Hz watch crystal when one of the onboard timers is operating in asynchronous mode. And I got an interesting measurement:

Code: [Select]
Ready.
tock: 998
tock: 1998
...
tock: 10998
tock: 11998
tock: 12999
tock: 13999
...
tock: 31999
tock: 32999
tock: 34000
tock: 35000
...
tock: 53000
tock: 54000
tock: 55001
tock: 56001
...
tock: 74001
tock: 75001
tock: 76002
tock: 77002
...
tock: 95002
tock: 96002
tock: 97003
tock: 98003
...
tock: 116003
tock: 117003
tock: 118004
tock: 119004
...
tock: 137004
tock: 138004
tock: 139005
tock: 140005
...
tock: 158005
tock: 159005
tock: 160006
tock: 161006
...
tock: 179006
tock: 180006
tock: 181007
tock: 182007
...
tock: 199007
tock: 200007
tock: 201008
tock: 202008
...

Those "tock" messages are outputed when Timer 2 overflows, which is once per second. The number is milliseconds since initialization of an Arduino-style timer based on Timer 0 which is running at system core clock speed.

What is causing those oscillators to drift apart like that, one millisecond every 20 seconds or so? I am using two crystals, one 2-pin 32768Hz watch crystal, and a 16MHz 4-pin crystal oscillator can.
« Last Edit: September 12, 2016, 03:18:19 pm by technix »
 

Offline PA0PBZ

  • Super Contributor
  • ***
  • Posts: 5520
  • Country: nl
Re: This is interesting...
« Reply #1 on: September 12, 2016, 03:29:09 pm »
What is causing those oscillators to drift apart like that..

Why do you think they would be synchronized?
Keyboard error: Press F1 to continue.
 
The following users thanked this post: Kilrah

Offline technixTopic starter

  • Super Contributor
  • ***
  • Posts: 3508
  • Country: cn
  • From Shanghai With Love
    • My Untitled Blog
Re: This is interesting...
« Reply #2 on: September 12, 2016, 03:33:50 pm »
What is causing those oscillators to drift apart like that..

Why do you think they would be synchronized?

I am not expecting synchronization, but I do want to know if this drift is significant.
 

Offline Kilrah

  • Supporter
  • ****
  • Posts: 2042
  • Country: ch
Re: This is interesting...
« Reply #3 on: September 12, 2016, 03:41:13 pm »
Seems within the tolerances of 2 randomly picked crystals working completely independently...
 

Offline technixTopic starter

  • Super Contributor
  • ***
  • Posts: 3508
  • Country: cn
  • From Shanghai With Love
    • My Untitled Blog
Re: This is interesting...
« Reply #4 on: September 12, 2016, 03:45:23 pm »
If your timer's ISR takes too long to execute before it resets the timer, then your timer interval is effectively compare/period/clock+isr_time, and here the isr_time is your majority source of error.

For instance:

#pragma ISR timer0_vector
void t0isr()
{
    do_something();
    t0_reset();
}

takes longer time to reset the timer compared to:

#pragma ISR timer0_vector
void t0isr()
{
    t0_reset();
    do_something();
}

Even for the first case, ISR entrance delay also plays a role in drifting. To get around with it, set the timer to reset itself (continuous mode) and generate interrupts without user code's help.

I used timer overflow interrupts in Fast PWM mode (effectively simple timer mode with PWM instead of comparator output) so there is no timer resetting involved.
 

Offline Chris Mr

  • Regular Contributor
  • *
  • Posts: 157
  • Country: gb
  • Where there's a will there's a way
Re: This is interesting...
« Reply #5 on: September 12, 2016, 04:17:01 pm »
Quote
What is causing those oscillators to drift apart like that, one millisecond every 20 seconds or so?

Lemme see..

20 seconds and 0.001 seconds out, is like 20,000:1 or to put it another way, 50ppm ((1/20000) * 1,000,000).

Your two crystals might be +/- 100ppm (for example) so what you have noticed is entirely reasonable.


A better explanation would be that whilst you have worked out precisely what the times should be using the crystal frequencies, the +/-100ppm variability of the crystals means those calculations are not as the hardware actually is.  Crystals are temperature sensitive so you could heat one up and watch the discrepancy get either larger or smaller.

If you have a scope, even better is to output two square waves and watch them drift.  Say 512Hz so you get plenty of updates.
 

Offline tatus1969

  • Super Contributor
  • ***
  • Posts: 1277
  • Country: de
  • Resistance is futile - We Are The Watt.
    • keenlab
Re: This is interesting...
« Reply #6 on: September 12, 2016, 04:21:09 pm »
those watch crystal can be very nasty and temperature dependent. Take a chance and put your finger on it, I bet that you can see it at your output.
We Are The Watt - Resistance Is Futile!
 

Offline technixTopic starter

  • Super Contributor
  • ***
  • Posts: 3508
  • Country: cn
  • From Shanghai With Love
    • My Untitled Blog
Re: This is interesting...
« Reply #7 on: September 12, 2016, 04:25:51 pm »
those watch crystal can be very nasty and temperature dependent. Take a chance and put your finger on it, I bet that you can see it at your output.
So my general design policy of only using DS3231 TCXO for RTC is a good idea?
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->