Before configuring clocks on the MSP430, you might wonder what a clock is used for on a microcontroller and why should you care? Well, the clock is used for timing by the processor and peripherals. For example, if you set the master clock to 16 MHz, the processor will be going really fast and consuming full power, however if you set it to 1 MHz, it will be running slower and using less power. It is also used by the peripherals (such as the serial interface) to coordinate when they should send/receive data.
The MSP430 has three clocks. They are called the MCLK (master clock), SMCLK (sub-main clock), and ACLK (auxiliary clock). Each of these clocks must be assigned to a clock source. There are four possible clock sources: DCOCLK (internal digitally controlled oscillator), LFXTCLK1 (external clock source such as a crystal), XT2CLK (high frequency external clock source), VLOCLK (internal low frequency oscillator). To further complicate matters, not all clock sources are available to all clocks.
First I wondered why there is a need for three different clocks. Wouldn’t one be enough? Well, it appears that the reason for this is so that different peripherals can operate at different speeds. The two high speed clocks, MCLK and SMCLK, are used for high speed peripherals and the processor. MCLK is used for the processor and SMCLK is used for peripherals. The lower speed ACLK is used for slower peripherals. Furthermore, the reason for having two high speed clocks I assume is because one is normally used only for controlling the processor speed (MCLK) and the other one (SMCLK) is used for peripherals, which may need to be high speed, but not necessarily the same speed as the the processor.
Next you may wonder why there are different sources that the clocks can base their speed off of. The purpose of this is so that you can have different accuracy in your clocks. It is possible to use a highly accurate crystal if you need precise timing (such as if you were building an electronic wristwatch), however in many cases it will be adeqate to use the internal clocks which are somewhat inaccurate and vary their speed based on the chip’s temperature and other things.
Of the four clock sources, DCOCLK and VLOCLK are both internal to the microcontroller. You don’t have to add anything to your board to use these. Of the two external clock sources, XT2CLK is not in any microcontroller that I was able to find. It is an optional feature. However, you will probably have LFXT1CLK (the documentation states that only MSP430G22x0s don’t have it).
If you have the MSP430 Launchpad, along with the board and two sample chips you will have received a 32768 Hz crystal. On one of my boards I soldered it on, but for the rest I left it off. By soldering the crystal on to your Launchpad, you can have a accurate clock, if that is important to you. It is used for one of the lessons that are included with the Launchpad, however if I recall correctly, it interferes with the Anaren Wireless BoosterPack, so don’t solder it on if you are using that.
The configuration of the clocks is controlled through four registers: DCOCTL and BCSCTL1 through 3. By default, the MCLK and SMCLK are set to use the DCO as a clock source, and the DCO is configured for 1 MHz. The ACLK is set to use LFXT1, an external crystal. If these settings work for you, I’d suggest not trying to change them since the process is somewhat cumbersome.
Probably the most frequent things you will want to change are the master clock speed and the ACLK source.
First, let’s talk about the ACLK source. To change it from an external crystal to the internal 12 kHz VLO, in C you would do “BCSCTL3 = LFXT1S_2;”. However, according to the documentation, the VLO is not available in all the MSP430s. Apparently it is only available in certain models. To find out whether your MSP430 supports the VLO (or XT2CLK), check the datasheet. On my G2553 datasheet, which is named SLAS735E, I found it on page 2, under the section titled Description. There is a table which shows the features of each chip and under the Clock column you can find out what you have. Mine reads “LF, DCO, VLO” which tell me the three choices I have for a clock source.
Second, you might want to change the master clock speed. If you try reading the MSP430 Family User’s Guide to figure out how to do this, you might get a headache. It seems really complicated to me. Fortunately there are some preprocessor definitions for some standard settings which have been calibrated from the factory. The header files are located under C:Program Files (x86)Texas Instrumentsccsv4msp430include if you are on Windows with Code Composer 4. In msp430g2553.h, I found the calibrated MHz settings at line 830 (search for the phrase “Info Mem”). Here I found that there are four calibrated settings: 1 MHz, 8 MHz, 12 MHz, and 16 MHz.
To change the speed, you have to configure the DCO. This is done through the DCOCTL and BCSCTL1 registers. For example, to set my chip to 8 Mhz, I did this:
DCOCTL = CALDCO_8MHZ; // DCO frequency set to 8 MHz
BCSCTL1 = CALBC1_8MHZ; // DCO range set to 8 MHz
Besides what I’ve discussed so far, there are some additional features you should be aware of. One is that clock sources can be further modified by dividers. The default divider is 1 (no division), but you could reduce the frequency by dividing it by 2, 4, or 8. Also there is an ability to send the clock output to a pin, in case it is needed externally. Finally, there is a clock oscillator fault detector which you can enable. This will cause an interrupt in the case of a crystal malfunctioning on your board.
As you can see, clocks are a very complex subject. (Or at least it seems complex to me, as someone trying to learn.) I have not even gotten into the possibility of turning off clocks by going into a low power mode. That will have to be a different post.
What I have found most helpful for learning clocks is the end of chapter 5 in MSP430 Microcontroller Basics, the MSP430 Family User’s Guide (chapter 5 “Basic Clock Module+”), and experimenting with the Grace plugin for Code Composer. Although Grace is not working for me due to an inexplicable low voltage error, I found it is still helpful because you can go to the Power User button under BCS+ and visually configure the clocks. After you’ve done that you can click on the Registers button to get an idea of how the registers should be setup. I then go into a text editor on my other PC and code the register settings in C and compile the code with MSPGCC. This is sort of a weird way of doing it but it is better than nothing. I don’t know why Grace isn’t working for me (it was working earlier), but I’ll investigate it more later.
Configuring Clocks with Texas Instruments’ Grace
If anyone finds any mistakes in this post, please mention it in the comments below!