Professional Documents
Culture Documents
Clocks and Timers-Sources & Controls-I
Clocks and Timers-Sources & Controls-I
Introduction
Clocks are at the heart of synchronous digital systems and processors. CPUs require clocks to
run to ensure the orderly fetching and execution of instructions. In PCs, the selection of clock
speeds is determined by various factors. Unless you are overclocking, you will never deal with
them directly. Microcontrollers, on the other hand, are usually very flexible with clocks and
require that you to specify what clocks will be used and at what speeds. This means you have
to understand your application and what clocks are needed to satisfy your requirements.
One of the most obvious effects of clocks is the speed at which the CPU is processing data. If
the clock is slow, algorithms will take longer to complete and the application can violate
timing requirements. Another good example is the timing required for UART. UART is covered
in a later chapter, but being asynchronous means that the reliability of the data being
transmitted depends on the input clock, as is the speed at which it transmits data. If the clock
is unstable and the baudrate is inaccurate, errors will be detected at the receiver.
Clock Generation
As with any system, clock generation contains many tradeoffs between size, accuracy and
cost. More expensive solutions typically tend to produce better and cleaner clock signals that
have lower jitter and drift. In the MSP430, the two main clock generation mechanisms are
internal RC type oscillators and internal oscillators using external crystals.
The MSP430 can contain several internal oscillators. Of particular note are the Digital
Controlled Oscillator and the VLO low frequency oscillator. Both of these are based on an RC
network. RC networks can be rather inaccurate in generating precise clocks, so TI has trimmed
and calibrated them to reach close to 1% accuracy. These type of oscillators are heavily
affected by temperature and cannot be always be used for interfaces requiring accurate
timing such as UART without causing errors. The DCO is digitally controlled because its
frequency be changed from several hundred kHz up to 25MHz.
The MSP430 can also use external crystals with internal oscillator circuitry to generate both
low frequency (32kHz) and high frequency (Up to 25MHz) clocks that are as accurate as the
crystal used. One issue with crystals is that they are not tunable, so the only possible option
is to divide them, but they are far more accurate, reaching 50ppm accuracy or better. A
downside of using crystals is that they occupy more space on the board and increase the BOM
cost, but when doing UART or needing accurate timing measurement they are practically
obligatory.
Crystals are accurate and they have low error due to aging. In general, every crystal is used in
a parallel configuration, which is the case in the MSP430 and many other microcontrollers,.
This requires capacitors to provide the load capacitance needed for the oscillator to oscillate
at the right frequency. These capacitors are usually connected to the two crystal pins as
shown in the figure above. The effective loading required is specified by the crystal
manufacturer, not by the microcontroller manufacturer (although the microcontroller
manufacturer could make recommendations or force you to use a certain loading because of
built in capacitors). Any change in the effective loading changes the crystal’s effective
frequency, which is an error in itself. However, it isn’t only the capacitors that add
capacitance. The pins of the microcontroller as well as traces between the microcontroller
and the crystal and the internal circuitry of the MSP430 oscillator also act as capacitors. To
have an accurate frequency, a designer needs to take all of these elements into account. We
will go through a simple exercise to demonstrate how to calculate the capacitors we should
use.
Let’s assume we need a 4MHz crystal and have selected one with 7pF of loading capacitance.
C1 and C2, the loading capacitors, are usually selected to be the same, so we have:
Because both capacitors are the same, C1 = C2, we get 14pF of capacitance. Therefore, two
capacitors of 14pF will provide us with the required load. This is quite simplistic and doesn’t
take into account the other parasitics. The oscillator inside the microcontroller introduces two
capacitances generally called CXIN and CXOUT. Each appears in parallel with one of the
external loading capacitors. In addition to this, we also have the parasitics due to the PCB
itself which are in parallel to the other capacitances.
Cpar is not always easy to measure. Because of this we typically assume a value of 2-5pF which
works in most cases.
In some cases, external loading capacitors are actually not required. In the MSP430, several
devices incorporate internal loading capacitors for the Low Frequency 32kHz watch crystals.
The MSP430G2553 has 4 options for integrated effective load capacitance for low
frequencies. You can choose between 1pF, 5.5pF, 8.5pF and 11pF, and these values also
include the parasitic bond and package capacitance, which are around 2pF per pin. Note that
you can still integrate external capacitors to try and compensate for parasitics, but this often
causes more problems because the capacitors themselves have parasitics.
Whether you are using the integrated oscillators or external crystals, it is possible and
advisable to measure the output frequency after a buffer. The MSP430 enables several clock
sources to be output to a pin so the frequency can be measure using an oscilloscope or more
accurately frequency counter.
Clock Sources
Up to four sources are available for the clock, depending on the family and variant:
• Low- or high-frequency crystal oscillator, LFXT1: Available in all devices. It is usually
used with a low-frequency crystal (32 KHz) but can also run with a high-frequency
crystal (typically a few MHz) in most devices. An external clock signal can be used
instead of a crystal if it is important to synchronize the MSP430 with other devices in
the system.
• High-frequency crystal oscillator, XT2: Similar to LFXT1 except that it is restricted to
high frequencies. It is available in only a few devices and LFXT1 (or VLO) is used instead
if XT2 is missing.
• Internal very low-power, low-frequency oscillator, VLO: Available in only the more
recent MSP430F2xx devices. It provides an alternative to LFXT1 when the accuracy of
a crystal is not needed.
• Digitally controlled oscillator, DCO: Available in all devices and one of the highlights of
the MSP430. It is basically a highly controllable RC oscillator that starts in less than 1μs
in newer devices.
Internal DCO
Upon reset, the MSP430 does not know whether any external crystals are available, and what
they are, so it defaults to using the internal DCO to power MCLK and other clocks. We
mentioned before that the MSP430 will start up around 1.2MHz for the G2553.
Features of DCO
• One of the aims of the original design of the MSP430 was that it should be able to start
rapidly at full speed from a low-power mode, without waiting a long time for the clock
to settle.
• Available in all devices.
• It is basically a highly controllable RC oscillator that starts in less than 1 µA in newer
devices.
• There are no erratic pulses: The output from the DCO starts cleanly after this delay.
• The frequency can be controlled through sets of bits in the module’s registers at three
levels.
Control of the Clock Module through the Status Register
The clock module is controlled by 4 bits in the status register as well as its own peripheral
registers. This is because of the intimate connection between clocks and low-power modes.
All bits are clear in the full-power, active mode. This is the main effect of setting each bit in
the MSP430F2xx:
• CPUOFF disables MCLK, which stops the CPU and any peripherals that use MCLK.
• SCG1 disables SMCLK and peripherals that use it.
• SCG0 disables the DC generator for the DCO (disables the FLL in the MSP430x4xx
family).
• OSCOFF disables VLO and LFXT1.
Registers to configure the MSP430 clock module
There are four registers to configure the MSP430 clock module:
• DCOCTL: DCO control
• BCSCTL1: Basic clock system control 1
• BCSCTL2: Basic clock system control 2
• BCSCTL3: Basic clock system control 3
#include <msp430.h>
void main()
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
Example Program to make P2.6 and P2.7 set for accepting a crystal at the input:
#include <msp430.h>
void main()
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P2SEL |= (BIT6 | BIT7); // Set P2.6 and P2.6 SEL for XIN, XOUT
P2SEL2 &= ~(BIT6|BIT7); // Set P2.6 and P2.7 SEL2 for XIN, XOUT