Calibrating a Crystal OSC with the Digilent Analog Discovery and µC ? - Page 1
Forum section: electronics / general

author: Doomkeks
author_rank: Newbie
timestamp: February 21, 2015, 01:15:54 pm
content: Heello ForumThe (32768Hz) crystal is connected to a µC (16F1508):The firmware generates a 64Hz square wave (prescaled from the 32768Hz) on a DO pin of the µC which is probed by the Analog Discovery.(You can't probe a crystal directly without affecting it's frequency.)The idea is to precisly measure the frequency and adjust the trimmer cap until it's 64.00000....Hz.To do this you would run a frequency counter for some minutes and then divide the elapsed time through the #of pulses to get a very precise average.The only thing I have is the Analog Discovery. Suprisingly, I could not find a frequency counter function yet.(It should be easy for the devs to implement one.)It comes with an frequency average tool in the scope section, but you can only use it for relatively small time spans and not in a minute range.So it is not precise enough.The record feature of the scope section would be helpful, but you can't trigger data entries with the signal edges. (Recording only with fixed sampling rates. => not helpful)I can get good results with averaging just some ms with the scope feature but I need a little more precision. (see screencap)Does anyone have an idea how to do this with the Analog Discovery?I'm sure there is a way...Thank you!(Background: binary clock project)(NOTE: Of course it makes no sense to calibrate the circuit on a breadboardThis is just for testing purposes, the circuit will be calibrated on the final pcb.)

author: babysitter
author_rank: Frequent Contributor
timestamp: February 21, 2015, 03:45:59 pm
content: A relatively modern soundcard can sample the 32768 Hz, record for a longish time and count the zero crossings. Or use one of those spectrum analysis tools. Analog discovery might sleep during that or work on better things.

author: Doomkeks
author_rank: Newbie
timestamp: February 21, 2015, 08:26:10 pm
content: Danke!I did not think of this.The soundcard method should do the trickProblem: my signal looks like this:(pulsating dc)...but my soundcard needs this:(ac)Is there an easy way to generate a -2.5V offset?

author: retrolefty
author_rank: Super Contributor
timestamp: February 21, 2015, 08:31:58 pm
content: Quote from: Doomkeks on February 21, 2015, 08:26:10 pmSo capacity couple the signal.

author: edavid
author_rank: Super Contributor
timestamp: February 21, 2015, 08:50:47 pm
content: You can't really use the Analog Discovery or a computer to calibrate the oscillator for your clock, because neither of them has an accurate oscillator to use as a standard. Why not just build your clock and then see how much it drifts in a day compared to an accurate time source (NTP)? Then you will know how much you need to adjust the oscillator. (An even better way is to leave the oscillator alone, and do the calibration in the clock software.)

author: Doomkeks
author_rank: Newbie
timestamp: February 21, 2015, 10:38:06 pm
content: Quote from: retrolefty on February 21, 2015, 08:31:58 pmQuoteYes, you might be right...The problem is I would not even know in which direction I'd have to adjust the trimmercap (It has no stop). A live monitoring device would be helpful.My clock is based on Timer1 overflow, which triggers an interrupt every second. The Timer1 clock tick comes from the 32768Hz crystal.Calibrating it via software would require pausing the Timer / incrementing it in the ISR. This seems to be a little dodgyAdjusting the trimmercap via trail and error should be the best solution for me. I will post some pics when it sits in the final box.Thank You

author: dannyf
author_rank: Super Contributor
timestamp: February 21, 2015, 10:41:51 pm
content: QuoteYou will need a more accurate timing source, like a ocxo or a tc-vcxo -> they can do 2.5ppm or better.Short of that, try WWV - but it can be messy.Another approach is to use a gps to generate a 1pps signal.

author: edavid
author_rank: Super Contributor
timestamp: February 21, 2015, 11:32:34 pm
content: Quote from: Doomkeks on February 21, 2015, 10:38:06 pmThere are a lot of other ways. You could have the tick interrupt add a fixed point value which represents the measured tick duration to a software accumulator, then increment the time when that accumulator overflows.

author: tggzzz
author_rank: Super Contributor
timestamp: February 21, 2015, 11:43:47 pm
content: One way or another, you will end up comparing two frequencies - one from your circuit and one "golden" reference.Your circuit's frequency cannot be more accurately set than the reference frequency.You could use the Analog Discovery's waveform generator as the reference frequency, in which case the accuracy is determined by the onboard MEMS resonator.A traditional way to compare frequencies using an analogue scope is to apply one frequency to channel 1, the other to channel 2, and set the scope display in XY mode. If the frequencies are identical then the trace will seem stationary. If there is a frequency offset the trace will rotate clockwise or anticlockwise. So you simply twiddle your calibrator until the rotation slows down and ideally stops.I haven't tried that with a digital scope such as the Analog Dscovery, but traditionally digital scopes XY displays are "tricky" to the extent that some use it as a way of demonstrating the benefits of analogue scopes.

author: dannyf
author_rank: Super Contributor
timestamp: February 22, 2015, 01:05:49 am
content: QuoteI looked at the manual and it does have a clock generation function - 20Mhz clock.So one way to do it, if you think the discovery's clock is more accurate, is to program the PIC to measure the discovery's clock generation, with the 32Khz crystal as the gate.Adjust the trimmer until the read-out matches your expectation.If you don't want to learn to program the PIC, you can use a counter as well.

author: babysitter
author_rank: Frequent Contributor
timestamp: February 22, 2015, 09:23:05 am
content: If you are living close to the rhein main area, come over, you can do the adjustment here.

author: PA0PBZ
author_rank: Super Contributor
timestamp: February 22, 2015, 10:01:16 am
content: Divide the output by 64, let it count seconds for as long as you can, compare with a real clock, adjust, repeat.

author: Stigaard
author_rank: Contributor
timestamp: February 22, 2015, 10:22:59 am
content: Quote from: edavid on February 21, 2015, 08:50:47 pmIf the computer is sync'ed with ntp and he is counting pulses, he does not need to use the soundcard sample clock, therefore he will only by relying on the pc's watch accuracy which with a ntp client should be more than good enough for the synchronization of another watch, only issue is that trimming will be far from realtime so that part might be very difficult. It may in fact be easier to change between fixed caps and keeping notes on errors for each value on a piece of paper, this should allow for reasonable fast trimming using educated guesses based on prior values.

author: Doomkeks
author_rank: Newbie
timestamp: February 22, 2015, 12:34:09 pm
content: QuoteGood Idea! The Analog Discovery has a xy feature. I could try that. Although the frequency of the Analog Discovery would propably not be accurate enough.QuoteUnterfrankenQuoteYes it has a function generator, but I was looking for a feature which counts rising edges on the Input for a long period of time.QuoteThats of course the holy grail, but it need more periphery and is harder to implement in software. Same with GPS. Maybe in a future projectAnother sweet way to build an extremely precise clock is using the mains AC frequency as a reference. It is synced to the Coordinated Universal Time (UTC). (My country does this.)Although it fluctuates more than a crystal, the long time average is very precise. It just needs a small transformer, a fuse for safety and a diode.My binary clock is running for some time now and I'm really happy with the accuracy. (less than 1s in 5 hours compared to a radio clock.)Bevore I got the crystal I tried to use just the internal Osc of the Pic but the accracy was very bad. ~1s /h fluctuation no matter what.My clock will be a wallmount design, around 210 x 110mm in size. I'm using those cheap, white 5mm strawhat LED's from China. They are amazing for diffsional applications.(Ebay link)Each Led will light up a 30x 30mm chamber behind a frosted acrylic glassplate. All in a wooden cabinet.

author: babysitter
author_rank: Frequent Contributor
timestamp: February 22, 2015, 12:56:49 pm
content: Quote from: Doomkeks on February 22, 2015, 12:34:09 pm"Bayrischer Untermain" ? Slowly homing in... to be exact, Aschaffenburg!