In microcontroller circuits, quartz crystals provide the highest accuracy for keeping everything on frequency. With frequency and time measurement (and for commissioning master clocks) fine adjustment of crystal oscillators may also be necessary, so we will now investigate in detail how crystal frequencies can be ‘pulled’. Although we have taken the ATtiny25 AVR microcontroller from Atmel as our example, the methods indicated can, in fact, be applied to just about all microcontrollers.
The oscillator in a microcontroller consists of an inverter that is timed externally by just a quartz crystal and two capacitors (Pierce oscillator). The value of the capacitance is matched accurately to the selected crystal, in order that any deviation from the nominal frequency is contained to the minimum possible (see controller data sheet). Crystals can display some tolerance, however, and to compensate for this effect we have to increase the two (parallel) capacitances significantly to drag down the frequency. To make this adjustment possible a trimmer capacitor is fitted in series with the crystal. We select the two parallel capacitors (C1 and C4) so as to be large enough to make the oscillator operate below its nominal frequency at maximum series capacity (C2 and C3). Adjusting the trimmer capacitor (C2) then allows us to pull the crystal upwards.
Carrying out this adjustment in a practical manner calls for a frequency counter of course. In this case, its test probe must not be connected to the inverter input of the oscillator (XTAL1)! The capacity of the test probe would alter the frequency and in fact, this effect can even be detected at the oscillator output (XTAL2), even if not so pronouncedly. The best solution is to load the microcontroller (or expand the firmware correspondingly) with a program that produces a square wave signal on one port.
The following little program in C needs only five steps for one cycle in the main loop. Therefore a signal appears at port PB0 with a frequency that is one-tenth of the crystal frequency.
But why set the frequency manually when the microcontroller can do this equally well? The relevant preset parameters can be retained in an EEPROM for example. To simplify the circuitry we do this by varying the parallel capacitance at the oscillator input (even though this is less effective than altering the series capacity). Capacitor C1 is replaced by a varactor diode, which means we now require a control voltage for this diode in order to set the capacitance and hence the crystal frequency. The controller is programmed so that at its PWM output we get a squarewave signal with an adjustable pulse width (the AVR can do this without having to execute a line of the program). An R-C element (R2 and C6) smooths the pulses into a DC voltage that is fed to the diode via R1. In our circuit, the varactor diode used is a 1N5819 Schottky rectifier diode, which functions impeccably! That said, the supply voltage must remain at 5 volts to ensure an adequate adjustment range. If you are happy to rely on manual adjustment alone the circuit will also work off 3.3 volts.
In this second circuit, the fixed series capacitor C3 pushes the crystal frequency upwards. The programmable capacitor D1 pulls the frequency downwards, together with the second parallel capacitor C4. The sole task of capacitor C7 is to isolate the DC control voltage from the oscillator input. For this reason the control voltage level should be significantly higher than the supply voltage!
In our (experimental) circuit we need some ‘user input’ in order to tell the controller which control voltage to produce (as before the actual calibration is carried out manually). For this, we simply hook up a trimpot to an A/ D converter input. The digitized potentiometer setting is transferred directly into the register that determines the pulse width of the PWM signal.
Once more we measure the crystal frequency on port PB0, although this time the firmware no longer outputs a tenth of the crystal frequency. Using a couple of NOP commands the frequency relationship is trimmed to one twentieth. In the example illustrated we would, therefore, expect to see 600 kHz at this output.
The values for the capacitors surrounding the oscillator depend primarily on the actual crystal selected (the values in the photos should be taken as generic standard values). Some ‘suck it and see’ fiddling around will also be hard to avoid when selecting the varactor diode.
All source code and hex files of the microcontroller programs can be downloaded free from a dedicated Elektor web page or the author’s project pages. )