Accessing individual I/O pin on MSP430
In the MSP430, accessing individual pins is commonly written using defines and bitwise operators:
P2OUT &= ~BIT1; /* Pin P2.1 = 0 */
P2OUT |= BIT1; /* Pin P2.1 = 1 */
P2OUT ^= BIT1; /* Toggle Pin P2.1 */
Be sure to include the proper header file for your specific chip that contains the port and pin (BIT#
) defines.
TI Discussion board had a very informative discussion about the same thing.
In short, this is defined by the generic msp430.h
header used in CCS (which is linked to the specific target chip msp430xxxx.h
on compile). It does not have support for PxOUT.BITy
style assignments.
IAR's io430.h does have support for that, but the consensus is that msp430.h
is the better header (as the msp430xxxx.h
headers are written by TI employees, io430.h
is written by IAR employees)
PxOUT |= BITy
style is the best way to set a single bit.
PxOUT &= ~BITy
style is the best way to clear a single bit.
The following code is a workaround for individual pin access using Code Composer (with a little tweak can be ported on any compiler). The example is a modified version of the Code Composer Basic Example Blink the LED. In this example instead of writing the usual statement LED=1 to turn on the LED you will write LED(HI).
//***************************************************************************
//
// MSP432 main.c template - P1.0 port toggle
//
//***************************************************************************
#include "msp.h"
#define LO 0x00
#define HI 0x01
#define BIT_0 0x01
#define BIT_1 0x02
#define BIT_2 0x04
#define BIT_3 0x08
#define BIT_4 0x10
#define BIT_5 0x20
#define BIT_6 0x40
#define BIT_7 0x80
#define LED_BIT BIT_0
#define LED_PORT P1OUT
#define LED(x) (x==HI)?(LED_PORT |= LED_BIT):(LED_PORT &= ~LED_BIT)
void main(void)
{
volatile uint32_t i;
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
// The following code toggles P1.0 port
P1DIR |= BIT0; // Configure P1.0 as output
while(1)
{
//blink LED1
LED(HI);
_delay_cycles(100000);
LED(LO);
_delay_cycles(100000);
}
}