MSP430 Introduction¶
The MSP430 series by Texas Instruments is a very popular low-power microcontroller which has many features yet is designed to run with very low power. TI sells individual chips, but the best way to prototype with the MSP430 is to use their launchpads (see below).
Key Specs- 16-bit CPU
- 0.5Kb to 16Kb Flash (Program memory)
- UART, I2C, SPI, DMA, USB depending on model
- Various low-power modes, down to 500nA at 1.8V
- 1.8-3.6V operation
- Several clock sources, up to 25MHz
Note: This tutorial describes how to use the MSP430 with Code Composer Studio, an ASM/C/C++ based IDE. TI also has Energia, an Arduino-like development environment designed specifically for the launchpads. Please see this page for a tutorial on how to use Energia and the MSP430 launchpads.
Choosing an MSP430 device¶
TI manufactures MSP430 devices within the following categories:
- Value Line (MSP430Gxxx): Limited or combined peripherals, low pin count (similar to Atmel ATtiny series)
- 5xx/6xx : SMT package, many peripherals (similar to Atmel ATmega or Xmega)
- Other lines designed for LCD, motor control, or other applications
- MSP-EXP430G2: Includes MSP430G2553 and MSP430G2452 microcontrollers and 20DIP socket to accommodate other MSP430G series devices.
http://www.ti.com/ww/en/launchpad/launchpads-msp430-msp-exp430g2.html - MSP-EXP430F5529LP: Includes MSP430F5529 microcontroller which has many peripherals, including 2xI2C, 4xSPI, 2xUART, and USB 2.0
http://www.ti.com/ww/en/launchpad/launchpads-msp430-msp-exp430f5529lp.html - MSP-BNDLFR5969LCD: Includes MSP430F5969LP microcontroller and LCD
http://www.ti.com/ww/en/launchpad/launchpads-msp430-msp-bndl-fr5969lcd.html
The launchpads have an onboard FET to program the MSP430. Depending on the launchpad, the board will also create a virtual COM port for serial communication between the PC and UART via serial, and a small "flash-drive like" storage space for files. These separate lines for communication can operate simultaneously.
Robotics club keeps some launchpads that you can use with a small refundable deposit. They are also sold by TI and other distributors for $10-$30.
MSP430 Programming basics¶
Interrupts
DMA Controller
Flash Memory Controller
Digital I/O
Hardware Multiplier
Timer_A
Timer_B
Universal Serial Interface (USI)
Universal Serial Communication Interface (USCI)
Operational Amplifier
Comparator_A+
ADC 10bit
ADC 12bit
DAC 12bit
Links to tutorials on specific peripherals
Following are some tutorials common to all MSP430 devices
MSP430 Documentation¶
There are two important documents to reference: First, the device specific datasheet, found if you search for the device part number (e.g. MSP430F5529) on [[www.ti.com]]. This document lists the peripherals available on the device, but does not explain how to use the peripheral. Second, Family User's Guide, under the User Guide section. The user guide explains peripheral registers and operation for all devices. For example, the MSP430G2553 has a USCI module for UART, I2C, SPI, but the MSP430G2452 has a USI module that also has SPI and I2C, but is more limited. The user guide has sections for both the USCI and USI so make sure to know which peripheral is on your device.
Recommended Code Style¶
It is generally important to make your code as readable as possible, especially when you need to ask for help. This is particularly important for embedded programming where the code manipulates registers with cryptic names. To disable the watchdog timer, for example, it is possible to do the following:WDTCTL = 0x5a80;
However, you cannot easily tell what control bits are being set or cleared. Instead, use the macros in "msp430.h" and the device-specific library, e.g. "msp430g2553.h":WDTCTL = WDTPW | WDTHOLD;
Alternatively,WDTCTL = WDTPW + WDTHOLD;
In "msp430g2553.h", we see#define WDTHOLD (0x0080)
#define WDTPW (0x5a00)
Since the binary bit patterns do not overlap, you can use either an addition or OR operator.
The macros account for bit shifting (unlike AVR). Most macros follow the control bit name in the datasheet. Multibit macros are the bit name followed by an underscore and the decimal value of the bit values:UCA0CTL1 |= UCSSEL_2;
This selects the bit pattern UCSSEL[1:0] = 0b10 = 2
Interrupt vectors are slightly different from the interrupt name. See Interrupts.
For any macro, hovering your cursor over the syntax will usually show the macro definition. Alternatively, look in the header files, msp430.h, and the device specific file.
Header files¶
At the top of your main.c or main.h file, include the following header files:
For any MSP430 device, include the msp430.h file:#include <msp430.h>
Also include the device-specific header file, for example the msp430g2553:#include "msp430g2553.h"
Watchdog Timer¶
By default, the watchdog timer on the MSP430 is enabled. It is a hardware timer that should be periodically reset in software, so that if the software hangs, the watchdog timer will reach max and reset the chip. You most likely want to disable the watchdog timer:WDTCTL = WDTPW | WDTHOLD;
Clock System¶
The MSP430 has a complex clock system designed to reduce power consumption. Instead of using a single clock to run the entire device, peripherals can run on slower clocks than the master clock running the CPU, to reduce power consumption. Certain clocks can also be turned off using the Low Power Modes to reduce power consumption. Note that details about the clock module depends highly on family and less on device, so check the user guide and datasheet.
The MSP430 has several options for a clock source- LFXT1CLK: Crystal or ceramic resonator at 32.768kHz or 400kHz to 16MHz
- XT1CLK: Crystals or ceramic resonator at 32.768kHz or 4MHz to 32kHz
- XT2CLK: Crystals or ceramic resonator at 400kHz to 16MHz
- DCOCLK: Internal digitally controlled oscillator (DCO)
- VLOCLK: Internal 10/12kHz oscillator designed for low power
- REFOCLK: Internal 32.76kHz oscillator
- MCLK: Master Clock used by the CPU
- ACLK: Auxiliary Clock selected by peripherals
- SMCLK: Sub-main clock selected by peripherals
Each clock line can run at a frequency equal, half, quarter, or eight, of the clock source. Note that clock sources and lines differ by device, so check the datasheet and user guide.
The default clock source is the DCO. Select the necessary clock source below, then select the clock line used by each peripheral individually.
Internal Very Low Power Low Frequency Oscillator (VLO)¶
The VLO uses an internal 12kHz oscillator (Value devices) or 10kHz oscillator (5xx/6xx devices). Select it as follows:BCSCTL1 &= XTS;
BCSCTL3 = LFXT1S_2;//Set LFXTS = 0b10
LFXT1 Oscillator¶
The LFXT1 can be driven by an external 32.768kHz crystal (LF mode) or high speed crystal (HF mode). The crystal requires external capacitors on XIN and XOUT, see user guide for details.
If in LF mode, clear XTS:BCSCTL1 &= XTS;
BSCTL3 = LFXT1S_0 | XCAP_x;//Set LFXT1S = 0b00, select capacitor with XCAP_x
If in HF mode, set XTS:BCSCTL1 &= XTS;
BSCTL3 = LFXT1S_x | XCAP_0;//Set LFXT1S_x to oscillator range, set XCAP = 0b0
Digitally Controlled Oscillator (DCO)¶
The DCO uses an internal RC oscillator to generate a clock signal. On some devices, it can also be sourced from an external RC oscillator, crystal, or ceramic resonator. The DCO has been calibrated in factory at 1MHz, 8MHz, 12MHz, and 16MHz. If the device flash has been completely erased, this data will not exist and read as 0xff. if(CALBC1_16MHZ == 0xff || CALDCO_16MHZ == 0xff); check if calibration data in flash has been erased
while(1);
BCSCTL1 = CALBC1_16MHZ;
DCOCTL = CALDCO_16MHZ;
See user manual for additional details.
Low Power Modes¶
The defining feature of the MSP430 is its low power modes. The low power modes shut down parts of the chip to reduce power usage, up to 500nA (In comparison with other brands that operate at a few milliamps). For example, the CPU is shut down until the UART interrupt is set, then the CPU performs some operation and is again shutdown.Low Power modes (check with datasheet for specific device):
- Active Mode (AM): All clocks active
- Low-power mode 0 (LPM0)
- CPU disabled
- ACKL, SMCLK active; MCLK disabled
- Low-power mode 1 (LPM1)
- CPU disabled
- ACLK, SMCLK active; MCLK disabled
- DCO disabled
- Low-power mode 2 (LPM2)
- CPU disabled
- MCLK, SMCLK disabled
- ACLK active
- Low-power mode 3 (LPM3)
- CPU disabled
- MCLK, SMCLK disabled
- DCO disabled
- ACLK active
- Low-power mode 4(LPM4)
- CPU disabled
- ACLK, MCLK, SMCLK disabled
- DCO disabled
- Crystal oscillator stopped
More modes on other devices
Additional Resources¶
Video Tutorials (9-parts) for G-series launchpad from TI: http://www.youtube.com/watch?v=VW1LhWJd3UM