A microcontroller needs a clock to keep time for all of its hardware components and peripherals. This is because a microcontroller contains a finite-state machine to execute code (as introduced in
Sectionย 2.9) and finite-state machines are synchronous sequential circuits.
A
clock signal is a square wave that oscillates between 0ย V and VCC at regular intervals. The frequency (
\(f\)) of a clock describes the number of oscillations it makes every second and is measured with a unit of Hz. (For a square wave clock signal, an oscillation corresponds to subsequent rising edges of the signal or to subsequent falling edges of the signal.) The period (
\(T\)) of a clock describes the number of seconds it takes to complete one oscillation. These two quantities are inversely related to each other as defined by
(9.0.1).
\begin{equation}
T = \frac{1}{f}\tag{9.0.1}
\end{equation}
There are different technologies that can be used to generate clock sources. This includes piezoelectric materials whose internal atomic or molecular vibrations generate small fluctuations in an electric field. The vibrational frequency varies based on the size, shape, and material. Ceramic resonators are typically a low-cost piezoelectric material but may not be accurate enough to use for precise applications. Crystal oscillators are piezoelectric and are highly accurate for use in high-precision applications such as timekeeping. A piezoelectric material does not generate a square wave on its own. To use a ceramic or crystal oscillator as a clock source for a microcontroller, that microcontroller will need to include circuitry to generate an appropriate square wave.
Another clock source can come from a circuit that is capable of generating a square wave. Examples include an RC circuit, amplifier, or 555 timer. (Refer to
[16.23] for practical clock sources used in hobbyist digital circuits.)
An understanding of a microcontrollerโs capabilities is important before selecting a clock source. First, a microcontroller will likely have an operating range of frequencies. Exceeding that range may cause the microcontroller to perform in an unexpected manner. (For example, the ATmega328P is only rated up to 16ย MHz.) Second, itโs possible that external capacitors or other conditioning circuitry may be required to obtain proper signal timing. Refer to
[16.18] when selecting a clock source to use with an AVR microcontroller such as the ATmega328P.
Microcontrollers may also have the capability to change the clock frequency.
Clock multipliers increase the clock frequency by some integer value. (For example, the DA series of AVR microcontrollers features a clock multiplier. However, the ATmega328P microcontroller does not include this functionality.) The advantage of this is to have faster code execution. However, all microcontrollers have maximum frequencies beyond which they will not work reliably, which must be taken into consideration before multiplying the frequency of any clock signal. Increasing the clock frequency also has the side effect of increasing the power consumption of the microcontroller.
Clock dividers (prescalers) decrease the clock frequency by an integer value. Prescaling is accomplished by toggling the clock after a counter counts to a particular power of two (2, 8, 1024, etc.). Slower clocks indicate a longer time to execute code but use less power. A microcontroller with a flexible clock unit may allow for individual peripherals to be independently prescaled to accomplish a particular function without impacting the overall system clock.