MSP430 Tutorial


Configuring GPIOs

   The pins which have GPIO functionality (see device pinout) can be selected by configuring the below register settings: PxSEL should be set to zero for the bit corresponding to the port desired in this register.

If you are configuring an output for that pin you will:

  • PxDIR should be set to 1 for output for the bit corresponding to the port desired in this register.
  • PxOUT should be set to 1 for output high or zero for low output state for the bit corresponding to the port desired in this register.

If you are configuring an input for that pinyou will:

  • PxDIR should be set to 0 for input for the bit corresponding to the port desired in this register.
  • PxIN can then be read.

Each register represents a port, and each port has up to 8 pins available for I/O. So P1.x is represented with the registers P1DIR, P1SEL, P1OUT, and P1IN. Each bit in each port register represents a specific pin (i.e. for port 1, each bit represents each of P1.0, P1.1, ... , P1.7). The nomenclature for each register is as follows from MSB to LSB:
Px.7 Px.6 Px.5 Px.4 Px.3 Px.2 Px.1 Px.0
So if you wanted to set P1.4 to output direction you would set P1DIR |= 0x10;
If you are configuring the pin for it's peripheral functionality...
Let's use the example of the F1611, where P5.7 can be either a GPIO or a SVSOUT peripheral function.
Let us configure this pin to it's peripheral functionality.
The P5SEL register controls whether or not the pin is set to GPIO or it's alternate peripheral functionality. Because of this we need to set the P5SEL bit corresponding to P5.7, since we want the peripheral functionality (not GPIO functionality). To summarize, enabling P5.7 as SVSOUT requires the following statement:
P5SEL |= 0x80; // which corresponds to the MSB of P5.x, which is P5.7


		
//******************************************************************************
//   MSP430F552x Demo - Software Toggle P2.0, P2.2
//
//   Description: Toggle P2.0, P2.2 by xor'ing P2.0, P2.2 inside of a software loop.
//   ACLK = 32.768kHz, MCLK = SMCLK = default DCO~1MHz
//
//                MSP430F552x
//             -----------------
//         /|\|                 |
//          | |                 |
//          --|RST              |
//            |                 |
//            |             P2.0|-->LED
//			    P2.2|-->LED
//
//******************************************************************************
#include 

int main(void)
{
  volatile unsigned int i;

  WDTCTL = WDTPW+WDTHOLD;		// Stop WDT
  //P1DIR |= BIT0;			// P1.0 set as output
  P2DIR |= BIT0;			// P2.0 set as output
  P2DIR |= BIT2;			// P2.2 set as output
  while(1)				// continuous loop
  {
	//P1OUT ^= BIT0;                          // XOR P1.0
	P2OUT ^= BIT0;
	P2OUT ^= BIT2;
	for(i=50000;i>0;i--);                   // Delay
  }
}