Project

General

Profile

MSP430 Digital IO

Controlling IO pins on the MSP430 is very similar to other microcontrollers. Pins are grouped into ports that are controlled by registers. To address a single pin, use the BITx macros. However, for readability, you should redefine the pin with its functionality. For example, if P1.0 controls an LED, we can define LED as follows, and replace BIT0 with LED in all examples below.
#define LED BIT0 //P1.0

Set Direction

GPIO are input by default. To make a GPIO an output, set the corresponding bit in its port direction register:
P1DIR |= BIT0;//Make P1.0 output

To change a GPIO to input:
P1DIR &= ~BIT0;//Make P1.0 input

Set Output value

To make an output pin high, set the corresponding bit in its port output register:
P1OUT |= ~BIT0;//P1.0 = VCC

To make an output pin low, clear the corresponding bit in its port output register:
P1OUT &= ~BIT0;//P1.0 = VCC

Alternate port functions

Peripherals such as the USCI can also have access to the pins. Unlike AVR, you have to explicitly give the peripheral access to the pin using the Port Select registers. In the device-specific datashet, there is a table of Pin Functions for every port. This table lists the various functions each pin of the port can perform, and how to set the select registers to enable each function. For example, on the MSP430g2553, P1.1 is by default an I/O input, but also can be the RX of the UART. To select P1.1 for RX of the UART, set the P1SEL.1 and P1SEL2.1 control bits:
#define RXD BIT1 //P1.1 used for RXD of UART
//Put following in main() or setup functions
P1SEL |= RXD;
P1SEL2 |= RXD;

To change the pin function back to I/O:
P1SEL &= ~RXD;
P1SEL2 &= ~RXD;