AN INTRODUCTION TO CHOOSING AND USING CLOCK SOURCES
The clock for the AVR is its heartbeat. It basically defines when a processor will do an instruction. Normal instructions for the chip take between one to three clock cycles to complete; a clock cycle is the low-hi-low transition of the clock. So the faster the clock runs, the faster the chip works. This is why a Pentium 1 GHz chip is a LOT faster than a Pentium 100 MHz chip – many more instructions are executed per second. However be careful – the speed of the clock doesn’t actually mean a lot. One processor could take 1 clock cycle to move data from one register to another, but another processor could take 2 clock cycles to do the same thing. This means that processor number 2 will have to run at double the speed as processor 1 to do the same instruction in the same amount of time! The key here is a little rating called MIPS (Million of Instructions Per Second), which is surprisingly (not) how many instructions the chip could do in a second at a specified clock frequency. Anyway the point is that you need something that tells the processor when it will be doing instructions (the clock). Even THEN that’s not a perfect indicator of speed, as stuff like on-board cache, bus width and loads of other things make a huge difference!
Selecting a Clock Source
There are a few different ways to generate a clock for the AVR. You can use an external clock that feeds in the low-hi pulse stream, a crystal or a ceramic resonator. A few of the chips even have a built-in RC oscillator! We’ll talk about properly interfacing them in a second, first lets see the advantages, disadvantages of each.
This one is fairly easy – all you do is have some circuit that creates TTL compatible logic pulses, and feed those into pulses into pin XTAL1. Your external clock could be a cesium-based atomic clock, or a chipmunk spinning a wheel, with a unit counting the RPM of the wheel. The external clock should be very clean and fall and rise times should be fast.
The crystal is a very accurate and (in theory) simple way to get a clock. As well crystals do not change their value (much) with a changing external temperature. Crystals are available in many frequencies and sizes, and needs two external ‘load capacitors’ to work.
Two Pin Ceramic Resonator
A ceramic resonator is similar to a crystal, except that it is much cheaper. However as a consequence it is also not as accurate and the value can change at different temperatures. An advantage is that they are less fragile than a crystal – so in high-vibration environments they can perform (fairly) well. The ceramic resonator may not be accurate enough for reliable serial data transfer (using the UART), but sometimes it is (depends on quality). If you need reliable data transfer, a crystal is a better choice. The two pin ceramic resonator needs two external ‘loading capacitors’.
Three Pin Ceramic Resonator
The three pin ceramic resonator is similar to the two pin – except that it doesn’t need the external loading capacitors as they are built in. One pin of the resonator goes to ground, one to XTAL1 and one to XTAL2 (it doesn’t matter which pin goes to XTAL1 or XTAL2, but the pin to ground matters!).
A few of the chips are given an internal RC oscillator. The new ‘Mega’ series almost all have an internal oscillator. This oscillator can vary from different chips, but one of the faults of them is that the frequency will change with changing voltage on VCC pin and temperature. However you can often calibrate them for relative accuracy (still not near a crystal and not quite as good as a resonator). To use them you have to enable the proper fuse.
Important note: Many chips have this selected by default, so even if you connect a 16 MHz crystal to your AtMega128-16AC it may only be running at 1 MHz! Make sure to select the proper fuse bits if you are using a Mega series chip (as opposed to the AT90S series).
So how do you select the proper one? Well it depends on application. If you need high accuracy, then external clock or crystal is the way to go. If you need low cost, then internal oscillator or ceramic resonator is the way to go. If you want the easiest solution, then internal oscillator or three pin ceramic resonator is the best choice.