# MSP430 Pin Variables

I am trying to program an MSP430FR5969 for the first time. I successfully executed and modified a program to blink one of the LEDs connected to P1.0. The relevant predefined variable is P1OUT.

The other LED is connected to P4.6 but I do not know which variable corresponds to it. P4OUT doesn't seem to work, and I am not sure it is the right one because there are several pins: P4.4, P4.5, P4.6 and the variable P4OUT doesn't seem to differentiate between them.

What is the variable naming scheme? More importantly, what reference document would answer similar questions?

• If you're blinking P1OUT then you're blinking all the pins on port 1, not just the zeroth pin. – Samuel Jul 13 '14 at 7:13
• yes, thanks, i've learned that! the original sample program from TI had the line P1OUT ^= 0x01; // Toggle P1.0 using exclusive-OR which has a lot more nuance in it than i could appreciate at first sight :) – necromancer Jul 13 '14 at 7:22

the . means a particular bit of a multi-bit output. P4 is not really one pin but a bank of pins! :)

    P4DIR |= BIT6;  /* set P4.6 to output */
P4OUT |= BIT6;  /* set P4.6 to 1 */
P4OUT &= ~BIT6; /* set P4.6 to 0 */


The MSP430 does not technically have individual pin control. Instead, you have to control the entire register at once. P1OUT is the name of the register, not the pin.

In order to turn on, let's say, P1.0 and P1.6 (both are in the P1OUT register), you would first have to set them to output (P1DIR = 0b01000001; //or 0x41) and then set the value of the register (P1OUT = 0b01000001; /or 0x41). Notice that the value you are assigning to the port is 8 bits, which corresponds to the 8 pins connected to the P1OUT register.

If you want to, for example, toggle just one of the pins without changing the rest of the port, you can either set the value manually, or you can always use bitwise operations, like P1OUT ^= 0b00000001;. This toggles bit 0 without affecting any of the other pins. Besides exclusive OR (^), you have bitwise AND (&), bitwise OR (|), etc. You use these to change the individual pins without changing the entire port value. If you look in the header file, you may also find defines, such as:

#define BIT0                   (0x0001)
#define BIT1                   (0x0002)
#define BIT2                   (0x0004)


So you can say P1OUT ^= BIT0; to toggle bit 0.

If you would like to have the illusion that you're controlling one pin at a time, you can use the following functions:

#include <stdint.h>

typedef enum {
LED_GREEN = BIT6,
LED_RED = BIT0
} PIN_Def;

static inline void set_pin(uint8_t volatile * const port, PIN_Def pin) {
*port |= pin;
}
static inline void clear_pin(uint8_t volatile * const port, PIN_Def pin) {
*port &= ~pin;
}
static inline uint8_t test_pin(uint8_t volatile * const port, PIN_Def pin) {
return *port & pin;
}
static inline void toggle_pin(uint8_t volatile * const port, PIN_Def pin) {
*port ^= pin;
}


Then, to toggle BIT0, all you would need to do is call the following from inside main:

toggle_pin(P1OUT, LED_RED);


You can use the functions to set, clear, check, and toggle pin states.

Source: TI Forums