First of all, we set the DCO to the calibrated 1 MHz frequency. Pretty straight-forward here; the MSP430 MCLK timing the CPU will operate at very close to 1 MHz. What isn't as obvious is that the sub-main clock (SMCLK) is also driven by the DCO, and so also operates at 1 MHz.
The timer is then configured to run with the SMCLK divided by 8, or at 125 kHz. The timer counts to TACCR0 and then triggers an interrupt, wherein the current through the speaker (or current to the LEDs if you changed it to that mode) is switched. The frequency of the switching drives the speaker to create a particular tone.
In between switches, the MSP430 enters LPM0. We can see what happens to the chip when entering this mode by looking again at the diagram and table on page 2-15 of the x2xx Family Guide: the CPUOFF bit in the Status Register is set to 1. The CPU turns off, as does MCLK, leaving SMCLK and ACLK running. If we run LPM1, then the CPUOFF bit is set as in LPM0, and in addition SCG0 is set to 1. We can see that this mode has the added benefit of turning off the DCO if the SMCLK is not being used. In our case, we're using SMCLK to drive Timer A, so the DCO is not turned off for LPM1. Essentially, for this program, there is no difference between LPMs 0 and 1.
If we move to LPM2, however, we set CPUOFF and SCG1, which has a much different effect: in addition to the CPU and MCLK, SMCLK is disabled, allowing the DCO to also be disabled. In our program, then, when we enter LPM2 we turn off the clock driving Timer A; Timer A never reaches TACCR0, and an interrupt is never triggered to cause the MSP430 to exit the LPM. In LPM2, however, ACLK is allowed to continue running. If we drive Timer A with ACLK, then, we could use deeper modes and consume less power overall! Less power means more battery life and more efficient use of the resources in the MSP430 design.
ACLK is typically driven at low frequencies (comparatively), and so draws less power than does DCO, further improving our goal of reducing power consumption. By default, it is set to use a 32,768 Hz quartz crystal. Your LaunchPad kit came with a crystal that you can solder onto the board to use here. Alternatively, you can configure the LFXT1 clock source to run from an internal oscillator, similar to the DCO, that operates at a very low frequency (typically 12 kHz). This oscillator is referred to simply as the VLO.
Configuring LFXT1 and ACLK
If you're planning to use the crystal oscillator on LFXT1, there's likely little that needs to be done. The crystal that comes with the LaunchPad needs 12.5 pF capacitors for stability, and you might notice the SMD solder pads near the crystal location for capacitors. You can use discrete capacitors if you like, but the MSP430 is also configured with a few capacitances internally, and 12.5 pF is one of the choices. Using the crystal is a simple matter of soldering it onto the board and configuring the BCS+ module correctly. There are a lot of switches and controls involved here, so we'll save crystal control for a later tutorial, and assume for now that we're going to use the VLO to source the Auxiliary Clock.
Section 5.3 in the x2xx Family Guide (starting on page 5-13) holds the necessary information for configuring the BCS+ module. The registers we need for now are BCSCTL1 and BCSCTL3. In terms of setting up the VLO, we need the XTS bit in BCSCTL1 (bit 6) and the LFXT1Sx bits in BCSCTL3 (bits 5 and 4). The XTS bit sets the LFXT1 clock source to low or high frequency. Many MSP430s in the x2xx family let you use a high frequency crystal for this source; the value line devices, however, only support low frequency (ie. 32,768 Hz) crystals. When this bit is cleared to 0 we have configured the chip for low frequency ranges. Some devices, including the value line chips that come with the LaunchPad, allow us to use the VLO. This mode is selected by setting the LFXT1Sx bits to 0b10, as described on page 5-16. To do this in C, we again make use of a header file definition:
BCSCTL3 |= LFXT1S1; // sets LFXT1Sx to 0b10, VLO mode
We don't need to explicitly change the XTS bit, as it defaults to the low frequency setting. This line is sufficient for setting up the VLO.
With LFXT1 now oscillating at 12 kHz, ACLK can be used for the peripherals. You may recall from Tutorial 08-a that ACLK only sources from LFXT1, using either a crystal or the VLO. We can, however, alter the frequency for ACLK by dividing the VLO by 2, 4, or 8. This is done in BCSCTL1 with the DIVAx bits (5 and 4) with the configurations given on page 5-14. For our purposes, we'll set ACLK to run at about 3 kHz by dividing the VLO by 4. We do this with the following code:
BCSCTL1 |= DIVA_2; // ACLK divide by 4
Keep in mind that you can also divide the clock in the peripheral itself, or further divide inside the peripheral. For example, we can run Timer A at 750 Hz by dividing by an additional factor of 4:
TACTL = TASSEL_1 + ID_2 + MC_1 + TACLR; // use ACLK, div 4, up mode, clear
To summarize, we can modify the limboG2211.c code to use the following:
BCSCTL1 = CALBC1_1MHZ; // Running at 1 MHz
DCOCTL = CALDCO_1MHZ;
BCSCTL3 |= LFXT1S1; // VLO mode
TACCR0 = 14; // With the Timer using ACLK (12 kHz), this
// value gives a frequency of 12000/(TACCR0+1) Hz.
// For TACCR0 = 14, that's 800 Hz.
TACCTL0 = CCIE; // Enable interrupts for CCR0.
TACTL = TASSEL_1 + MC_1 + TACLR; // ACLK, up mode, clear timer.
_BIS_SR(LPM3_bits + GIE); // Enter LPM3 and enable interrupts
This code sets ACLK to run from the VLO (at ~12 kHz) and then uses Timer A to switch the speaker at ~800 Hz. Most importantly, since we are now sourcing the timer which causes interrupts from ACLK, we can use LPM2 and 3 in our program. Note that LPM4 would turn off the ACLK; using LPM4 is only feasible if we have some external interrupt source, such as watching for a signal on one of the GIO pins.
We'll return to looking at the low power modes soon; the first real scientific project we'll do will analyze and compare the power consumption of a simple MSP430 design using the various LPM settings. To do this, however, we're going to need to measure an analog signal and record data. The next tutorial will start us in this direction by looking at analog to digital conversion.
Reader Exercise: Suppose your LaunchPad has the crystal soldered onto it, and is being used in a project that does not use ACLK at all. The crystal will continue to oscillate, and use power (albeit a very small amount) that is not contributing to the overall design. Note that none of the standard LPMs disable ACLK and the crystal oscillator except LPM4. Write up the needed line(s) of code that would create a new LPM that keeps MCLK, SMCLK, and the DCO running, but disables ACLK and the crystal oscillator.