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
P1OUT ^= 0x01; // Toggle P1.0 using exclusive-OR
which has a lot more nuance in it than i could appreciate at first sight :) \$\endgroup\$ – necromancer Jul 13 '14 at 7:22