CRYSTAL OSCILLATOR
Context
● You are developing an embedded application using one or more members of the 8051 family of microcontrollers.
● You are designing an appropriate hardware foundation for your application.
Problem
When and how should you use a quartz crystal to create an oscillator for use with members of the 8051 family of microcontrollers?
Background
Quartz is a common mineral and is the main component of most sand grains. It has the useful quality that it is piezoelectric in nature, which means that if we apply pres- sure to a piece of quartz, it will generate an electric current at a particular frequency. In some materials, the converse is also true: application of an electric field will cause a mechanical deflection in the material.
We can use this behaviour as the basis of a useful oscillator by using an electric field (generated by plating some contacts on the surface of the mineral and applying a cur- rent) to set up mechanical oscillations in the crystal which are, in turn, converted into measurable voltage fluctuations at the surface of the crystal. We can precisely control the frequency of these fluctuations by cutting the quartz to a particular size and shape: a par- ticular form of cut, known as the ‘AT’ cut, is reasonably inexpensive to produce and can create high-frequency crystals with good temperature stability at reasonable cost.
To create a complete oscillator, some further components are required. Figure 4.1 shows how crystals may be used to generate a popular form of oscillator circuit known as a Pierce oscillator.
A variant of the Pierce oscillator is common in the 8051 family. To create such an oscillator, most of the components are included on the microcontroller itself: these components are, together, sometimes referred to as the oscillator inverter. The user of this device must generally only supply the crystal and two small capacitors to com- plete the oscillator implementation. We discuss this further in the solution section of this pattern.
Note that, in some circumstances, it may be preferable to use a complete, self- contained external crystal oscillator module (based on a circuit like that illustrated in Figure 4.1) and use this to drive the microcontroller. We discuss this possibility in ‘Reliability and safety implications’.
The link between oscillator frequency and machine cycle period When selecting an appropriate oscillator for an 8051–family device, the choice of oscillator frequency is really incidental to our real concern: the machine cycle period. That is, we are concerned with the speed at which instructions will execute.
As we discussed in Chapter 3, the various members of the 8051 family have different relationships between the oscillator cycle period and the machine cycle period. For example, in the original members of the 8051 family, the machine cycle takes 12 oscil- lator periods. In later family members, such as the Infineon C515C, a machine cycle takes six oscillator periods; in more recent devices such as the Dallas 89C420, only one oscillator period is required per machine cycle. As a result, the later members of the family operating at the same clock frequency execute instructions much more rapidly.
In general, the improved performance of modern implementations of the 8051 is ‘A Good Thing’: however, in situations where timing is critical, care must be taken to ensure that any timer-related calculations are implemented correctly on a particular device: see HARDW ARE DELA Y [page 194], and CO – OPERA TIVE SCHEDULER [page 255] for further details.
Why you should keep the clock frequency as low as possible As a general rule, the speed at which your application runs is directly determined by the oscillator frequency: in most cases, if you double the oscillator frequency, the application will run twice as fast.
In our experience, many developers select an oscillator / resonator frequency that is at or near the maximum value supported by a particular device. For example, the Infineon C505/505C will operate with crystal frequency of 2–20 MHz and many people automatically choose values at or near the top of this range, in order to gain maximum performance.
This can be a mistake, for the following reasons:
● Many applications do not require the levels of performance that a modern 8051 device can provide.
● In most modern (CMOS-based) 8051s, there is an almost linear relationship between the oscillator frequency and the power supply current. As a result, by using the lowest frequency necessary it is possible to reduce the power require- ment: this can be useful in many applications.
● When accessing low-speed peripherals (such as slow memory or LCD displays), programming and hardware design can be greatly simplified – and the cost of peripheral components, such as memory latches, can be reduced – if the chip is operating more slowly.
● The electromagnetic interference (EMI) generated by a circuit increases with clock frequency.
In general, you should operate at the lowest possible oscillator frequency compatible with the performance needs of your application.
0 MHz operating frequencies?
Several modern 8051 family members can be operated at speeds down to 0 Hz: for example, the Atmel 89C52 device has an operating range from 0 to 24 MHz. This facility can allow significant power savings, through operating the system at very low frequencies (in kiloHertz or even Hertz, rather than in megaHertz). We make use of these features in ONE – YEAR SCHEDULER [page 919].
In some applications, even 0 Hz can be useful. At first glance, this may not make sense: at 0 Hz, the device is not operating and no code will execute. However, in devices designed for low-frequency operation, the system state will be maintained even if the clock frequency is reduced. This means that the clock frequency can be reduced to 0 to save power. In addition it means that, if the clock temporarily fails (for what- ever reason) and then recovers, your system has a better chance of recovering, too.
Solution
The aim of this pattern is to help you decide if you should use a quartz crystal with your 8051 microcontroller and, if so, how to connect such a device. This section directly addresses these issues.
Stability issues
A key factor in selecting an oscillator for your system is the issue of oscillator stability. In most cases, oscillator stability is expressed in figures such as ‘±20 ppm’: ‘20 parts per million’.
To see what this means in practice, consider that there are approximately 32 mil- lion seconds in a year.8 In every million seconds, your crystal may gain (or lose) 20 seconds. Over the year, a clock based on a 20 ppm crystal may therefore gain (or lose) about 32 × 20 seconds, or around ten minutes.
Standard quartz crystals are typically rated from ±10 to ±100 ppm and so may gain (or lose) from around 5 to 50 minutes per year. Note that this figure also applies to external oscillator modules. If you require greater accuracy than this, refer to ‘Related patterns’.
Cost issues
Crystals cost around twice the price of a ceramic resonator, with prices linked to the crystal stability.
How to connect a crystal to a microcontroller
Basic connections for a crystal oscillator are given in Figure 4.2.
The values of the capacitors will vary, depending on the microcontroller and the crystal frequency. We will provide examples of recommended capacitor values for a range of different 8051 devices in the examples that follow; please refer to the data sheet describing your chosen microcontroller for further information. In the absence of specific information, a capacitor value of 30 pF will perform well in most circumstances.
Hardware resource implications
Use of a crystal oscillator has no direct implications for the CPU or memory require- ments in your application in most cases. However, if you choose to make temperature measurements in order to increase the stability of your oscillator (see ‘Reliability and safety issues’), this will have a CPU and memory overhead.
Note also that, as discussed in the background section, the performance of your application is directly related to the crystal frequency. If your application cannot per- form sufficiently rapidly, consider increasing the oscillator frequency. Alternatively, consider using a more modern 8051 design, from Dallas or Infineon (for example) that uses fewer clock cycles to carry out each instruction.
1. (365 days) × (24 hours) × (60 minutes) × (60 seconds) = 31,536,000 seconds.
Reliability and safety implications
We consider some reliability and safety issues related to the use of crystal oscillators in this section.
System heartbeat
The oscillator forms the ‘heartbeat’ of any digital computer. If this heartbeat stops, your system will stop. If this heartbeat varies, timing loops, delays, generated wave- forms etc. will vary too. Correct operation of your embedded system relies therefore on the provision of a robust and regular clock input.
Heart of glass
Quartz is similar to glass in some physical characteristics: in particular, it is fragile.
If you require an oscillator that will operate in an environment where there is sig- nificant vibration, then quartz may not be the ideal choice. If you use a quartz crystal in these circumstances, you will need to package your application to avoid vibration influencing the operation of your system.
Time taken for oscillator to start
If the start of the (crystal) oscillator in your circuit is delayed, then the reset cycle may be completed before the oscillation begins. If this happens, the chip will not be reset.9 The time taken for a crystal oscillator to start operating depends on its being mounted correctly and having appropriate capacitors. Typical start-up times are 0.1 to
10 ms (Mariutti, 1999).
Using an external crystal oscillator module
As we noted in ‘Background’, it is possible to use a self-contained external crystal oscillator module (based on a circuit like that illustrated in Figure 4.1) to drive the microcontroller. This technique has the considerable advantage that the oscillator is guaranteed to start. This can make it a good solution if your system must operate very reliably.
Connecting an oscillator module is very straightforward. Figure 4.3 shows a circuit that will work with all members of the 8051 family. Note that, as shown in the figure, pin XTAL1 should be driven, while XTAL2 is left unconnected.
Particularly where higher clock frequencies (> 12 MHz) are being used, then modules may improve your system reliability. However, oscillator modules do have several drawbacks:
● Oscillator modules cost around twice the price of a crystal oscillator and four times as much as a ceramic resonator.
● Oscillator modules typically draw currents comparable to that of an 8051 micro- controller: 15–35 mA. This may represent a very significant power drain in battery-powered applications.
2. See RC RESET [page 68] for further details.
● Oscillator modules are not always easy to obtain in ‘odd’ frequencies, such as
11.059 MHz. This frequency is very useful in 8051-based designs involving a serial interface, as discussed in ‘Related patterns and alternative solutions’.
Improving the stability of a crystal oscillator
As we have discussed, typical crystal oscillators have a stability of around ±20–100 ppm. If we use this device to control a real-time clock we may gain or lose up to 50 mins per year: that is, up to ~1 minute / week. This result is not specific to the 8051 family: the result of this behaviour is evident even in expensive servers for desktop computer networks. By contrast, most ‘quartz’ wristwatches use crystal oscillators, cost very little and keep very good time. This is because they have a sophisticated temperature control system attached, which keeps them operating at a temperature of 35°C for about 16 hours every day. The temperature control system is your wrist (and attached biological mechanisms).
If you want a general crystal-controlled embedded system to keep accurate time, you can choose to keep the device in an oven (or fridge) at a fixed temperature and fine-tune the software to keep accurate time. This is, however, rarely practical. Instead, ‘temperature compensated crystal oscillators’ (TCXOs) are available that pro- vide – in an easy-to-use package – a crystal oscillator and circuitry that compensates for changes in temperature. Such devices provide stability levels of up to ±0.1 ppm (or more): in a clock circuit, this should gain or lose no more than around 1 minute every 20 years. Such levels of accuracy are adequate for all but the most demanding of applications. However, there is a catch. TCXOs can cost in excess of $100.00 per unit and may even cost several times this amount. This price puts them well out of reach of most embedded projects.
One practical alternative is to determine the temperature-frequency characteristics for your chosen crystal and include this information in your application. For the cost of a small temperature sensor (around $2.00), you can keep track of the temperature and adjust the timing as required. This is the basis of ST ABLE SCHEDULER [page 932].
Another alternative is to use an atomic clock. For example, a caesium beam clock uses atomic transitions as the reference for a crystal oscillator and can provide accu- racy at a level of a few parts per million million (that is, around 1 in 1012). This translates into an accuracy of around 1 minute every million years. Use of such a device may sound like an outlandishly expensive solution, but you can now access the atomic clocks in various satellites using global positioning system (GPS) receivers or GPS chip sets. This is the approach increasingly used by the mobile phone (cell phone) companies that include such technology on their base stations.
Portability
These techniques can be, and are, used with a wide range of microcontrollers and microprocessors.
Note that, as discussed in the ‘Solution’ section, the value of capacitors to be used depends on both the crystal frequency and the microcontroller used. The manufac- turer’s data sheet for the microcontroller will provide recommended values.
Overall strengths and weaknesses
Crystal oscillators are stable. Typically ±20–100 ppm = ±50 mins per year (up to ~1 minute / week).
The great majority of 8051-based designs use a variant of the simple crystal- based oscillator circuit presented here: developers are therefore familiar with crystal-based designs.
Quartz crystals are available at reasonable cost for most common frequencies. The only additional components required are usually two small capacitors. Overall, crystal oscillators are more expensive than ceramic resonators.
Crystal oscillators are susceptible to vibration. The stability falls with age.
Related patterns and alternative solutions
An alternative solution
The main alternative to an external crystal oscillator is an external ceramic resonator: see CERAMIC RESONA TOR [page 64].
Using an on-chip oscillator
As we saw in Chapter 3, Small 8051 devices, such as the popular Atmel 89C4051, are designed as flexible, cost-effective replacements to discrete circuits (assembled from transistors, resistors, capacitors etc.). However, these devices still require external oscillator (and reset) circuits. As the oscillator and reset components can, together, cost as much as these small microcontrollers and greatly increase the board size, it
would seem sensible to include oscillator (and reset) circuits within the microcon- troller itself.
This is now possible, with 8051-family devices such as the Philips 87LPC764. This 20-pin device includes an on-board reset circuit (see Chapter 5). It also includes an on-chip resistor-capacitor (RC) oscillator. It can therefore be used without any exter- nal components.
Increasingly, RC oscillators (also known as ‘relaxation oscillators’) are becoming available as on-chip components. A simple implementation of such an oscillator is illustrated in Figure 4.4.
Other implementations of this simple oscillator are possible using, for example, small numbers of logic gates. However – whatever the implementation – the problem with the RC solution is that the oscillator can never be very stable, largely due to the variation of the resistor values with temperature. This is apparent in many practical implementations: for example, the RC oscillator on the 87LPC764 (and similar devices) has a stability of only ±25%. This is not sufficient for many applications: for instance, if used to generate baud rates for a serial interface, this level of stability would mean that the communication was unlikely to be effective.
Timing issues
Do not assume that just because your microcontroller will operate over a wide range of frequencies that you are free to choose any frequency in this range. The choice of oscilla- tor frequency will have a major impact on any time-related aspects of your application.
For example, you will see numerous designs for 8051-based systems which use crys- tal frequencies of 11.0592 MHz. The reason why this frequency is used is that, with standard 8051 devices, this crystal frequency may be easily used to generate standard baud rates (such as 9600 baud) from the built-in serial port: with other frequencies
(e.g. 10 MHz, 12 MHz) it is more difficult to produce these standard baud rate values. This issue is discussed in greater depth in Chapter 18.
Similarly, the oscillator frequency dictates the rate at which the hardware timers in your application will be incremented. If you need, for example, to schedule a task to run precisely every one minute, this can be difficult to achieve if you have selected an inappropriate oscillator frequency. (See Chapter 14 for further details.)
Note that such peculiar numbers are not restricted to the 8051 family. ‘Quartz’ dig- ital wristwatches use a frequency of 32.768 kHz, since, by dividing this frequency by 215, you obtain a 1 Hz ‘tick’ (215 = 32,768).
Example: Attaching a crystal to an Atmel 89C2051
Recommended capacitor values for connecting most quartz crystals to an Atmel 89C2051 are shown in Figure 4.5.
Example: Attaching a crystal to dual-processor board
It should be noted that most crystals or oscillator modules will drive more than one (typically up to five) microcontrollers. The data sheet will specify this ‘fan out’ value. This can be useful where a multiprocesor design is planned, not least because this means that both microcontrollers (assuming they are both 8051s) will always be ‘in step’ (see Part F for further details).
Figure 4.6 illustrates how the two boards should be connected to the same crystal. Note that the same approach can be applied to any combination of microcontrollers: if the two boards require different capacitor values, then select a mid-range value.
Further reading
Refer to the manufacturer’s data sheet for your chosen microcontroller to ensure you use the required capacitor values in your crystal oscillator circuit.