Ports of MSP430

Pinterest icon

MSP430 microcontroller series usually has 8 bit ports numbered as Port 1,Port2,Port3 etc.Ports 1 and 2 have interrupt capabilities and can have internal pullup resistors.Please note that there is no Port 0.

ports of MSP430

In the case of MSP430G2553, two 8 bit ports Port1 and Port 2 are available. The pins P2.6 and P2.7 of Port 2 are also used for connecting the 32Khz watch crystal for the internal oscillator, So if you are using an external crystal, pins P2.6 and P2.7 will become unavailable.

Direction and I/O
In MSP430 the register PxDIR controls the direction of the port(Input or Output),here x refers to the Port number eg P1DIR for Port1.Writing a 1 will make the corresponding Port bit as output and a Zero would make corresponding Port bit as input.The register PxOUT contains the data that is available to the outside pins. Writing a 1 will make the corresponding Port bit high and a zero would make the corresponding Port bit low.The two registers are both bit and byte addressable.

Setting the direction in IAR EW

P1DIR  =  0xF0 // Port1,upper nibble output, lower nibble input
P1DIR |=  BIT0 + BIT2 + BIT3 // Port1,P1.0,P1.2,P1.3 Output
P1DIR &= ~BIT0 + BIT2 + BIT3 // Port1,P1.0,P1.2,P1.3 Input

Setting the Port Output in IAR EW

P1OUT  =  0xF0 // Port1,upper nibble high, lower nibble low
P1OUT |=  BIT0 + BIT2 + BIT3 // Port1,P1.0,P1.2,P1.3 = 1
P1OUT &= ~BIT0 + BIT2 + BIT3 // Port1,P1.0,P1.2,P1.3 = 0

Now lets create a small program to light up an LED connected to P1.0.Here I am using IAR embedded Workbench for compiling and downloading the code (the code will also run on Code Composer Studio). If you are new to IAR,check out this short tutorial on how to use IAR EW.


#include “msp430g2553.h” //header file for the specific controller
void main(void)
  WDTCTL WDTPW WDTHOLD// stop the watch dog timer
  P1DIR |= BIT0;            // P1.0 direction output
  P1OUT |= BIT0;            // P1.0 high

Before Compiling and downloading the program makes sure that correct settings are selected.

Please make sure that the correct microcontroller is selected.

Selecting the correct msp430 controller on IAR embedded work bench

Set Debugger as “FET Debugger” in the IAR settings.

FET debugger selection for MSP430

Compile the code and download it to your Launchpad development board containing MSP430G2553 using IAR EW.Make sure that P1.0 LED jumper is Set in the Launchpad Board.The LED will light up now when you run the program.

After the MSP430 is Reset ,its watch dog timer starts the count down and resets the controller within a specified time period.
The first line
WDTCTL WDTPW WDTHOLD; stops the timer before it has time to reset the controller.


Port Interrupts in MSP430
MSP430 is designed as an ultra low power microcontroller which stays in sleep for most of its working life to conserve power. The controller wakes up periodically to react to changes in the external environment and goes back to sleep after it has done its job. There are 5 different types of sleep modes also known as Low Power Modes named from 0 to 4 ( LPM0 –LPM4). Due to the unique nature of its architecture interrupts play an important role in the working of the MSP430 controller. Almost all the peripherals inside the MSP430 can generate interrupts and wake up from its slumber. Here we are going to set up the port interrupts of MSP430.

Ports 1 and 2 of MSP430G2553 has interrupt capabilities. The Ports also has built in internal pullup resistors which can be enabled by software. There is also an option to select the edge(falling edge or rising edge) which will trigger the interrupt.If you are using launchpad development board you can see that P1.3 is connected to a tactile switch. Depending upon the version of Launchpad a pullup resistor (R34?) may or may not be present in the board. Refer the circuit which comes with your board and the Launchpad PCB to confirm the existence of the resistor. If the resistor is present on the board there is no need to enable the internal pull up resistors.

Now you have to set the following registers to get the interrupt to work.

PxDIR → 8 bit register you have seen earlier. Should be set as input in this case.

PxIE → 8 bit register to enable the interrupts for all the 8 port pins. A '1' enables the interrupts and a Zero disables the interrupts of the corresponding port pins.

PxIES → 8 bit register to select the signal edge that would trigger the interrupt. A '1' means that interrupt will be triggered in the falling edge (high to low) and a Zero means that interrupt will be triggered in the rising edge(low to high).

PxIFG → 8 bit flag register which shows which pin has triggered the interrupt.

PxREN → 8 bit flag register which enables the pull up resistors of the Port Pins.

Now lets create a small program that would toggle the LED connected to P1.0 to toggle when the switch connected to the P1.3 is pressed.
In this case I am using the tactile switch connected to P1.3 of MSP430G2553 to trigger the interrupt. The port pin P1.3 is pulled up to VCC(3V) by enabling the nternal pull up resistors (if there is an external pullup resistor, there is no need to use the internal ones) and a high to low transition is selected to trigger the port interrupt.

  WDTCTL = WDTPW + WDTHOLD//Stop the Watch dog
  P1DIR &= ~BIT3//P1.3 i/p
  P1REN |=  BIT3//P1.3 enable pullup resistor
  P1IES |=  BIT3//P1.3 high to low transition
  P1IFG &= ~BIT3//P1.3 clear interrupt flag
  P1IE  |=  BIT3//enable P1.3 interrupt
  P1DIR |=  BIT0//P1.0 o/p
  P1OUT &= ~BIT0//clear P1.0

  _BIS_SR(LPM0_bits + GIE); //enter LPM0 with interrupts enabled

The above program shows how to initialize the port P1.3 to get the interrupt mode working.After the initialization is complete we put the controller in sleep mode more specifically in LPM0 mode with interrupts enabled using
_BIS_SR(LPM0_bits + GIE);

In LPM0 mode the CPU and clock to the CPU (MCLK) is disabled while the clocks to the peripherals (SMCLK and ACLK) are active.

GIE is a bit inside the Status Register (SR) of the MSP430 processor core. It is used to enable or disable the maskable interrupts of the MSP430.If this bit is not set the P1.3 interrupt will not happen. Sometimes people will configure everything and forget to set the GIE bit and then start hunting through myriad of register options to troubleshoot the problem.

Please note that GIE bit is used to control all the maskable interrupts of the MSP430.The Status Register is also used to put the MSP430 in various Low Power Mode's like LPM0, LPM1, LPM2 etc. MSP430 can enter various Low Power Mode's by configuring various registers like CPUOFF, OSCOFF in the Status Register. Instead of individually setting the bits we can use the compiler directive LPM0_bits to achieve the same(here we are putting the controller in LPM0).A detailed explanation of Low Power Modes is present in the user manual of the respective controller along with the power consumption data for each modes.

When an interrupt happens processor wakes up from the Low power mode and goes to the interrupt vector location. If you are coding using assembly language you have to provide the address of the location in hex. Since we are using C, we can do it as

#pragma vector = PORT1_VECTOR
__interrupt void Port1(void)
  P1IFG &= ~BIT3//clear P1IFG
  P1OUT ^= BIT0;  //toggle LED at P1.0

Here PORT1_VECTOR is the interrupt vector location, It is defined in the header file “msp430g2553.h” if you open the file you can see the memory location corresponding to the PORT1_VECTOR. void Port1(void) is the user defined name for the interrupt service routine.

When an interrupt happens the corresponding flag in P1IFG register is set(here BIT3 of P1IFG), You should clear the flag immediately inside the interrupt service outine.

P1IFG &= ~BIT3;

After that you can toggle the LED connected to the P1.0 by xoring BIT0 of P1OUT.

Selecting the Pins
In MSP430 the port pins are shared among various peripherals like ADC inputs, Timers,Comparator inputs etc. To select the various functions associated with the same pins MSP430has two 8 bit registers called PxSEL and PxSEL2.
Not all functions requires you to configure the PxSEL and PxSEL2 register, some times selecting or activating the peripheral would automatically configure the pins for eg ADC.In other cases like enabling I2C or Capacitive touch sensing you have to explicitly configure the PxSEL registers to enable the pins for performing that function.
The information on how to configure the port selection registers is present in the datasheet of the controller .In MSP430G2553 that table is buried in the Port Schematics section at the end of the whole datasheet. Here is a sample of the Pin Selection table of MSP430g2553.

MSP430 Datasheet Port selection section


Back to Main Page