Also:
Basics:
In C or any High Level Language you read or write a port the same as you write to any variable. In ASM, you read or write to the port just as you would a register; each port, in fact, has a register. There are several registers that control each ports settings and value.Each pin of a port corresponds to each bit of that ports register. Bits 0 to 7 of that register are pins 0 to 7 of that port.
P1DIR controls the direction of EACH bit of the port independently. An '1' means the pin works as output, an '0' means the pin works as input. If you write 0x0F (or binary 00001111 or decimal 15) to P1DIR, that means pins P1.0 through P1.3 will be outputs, while P1.4 through P1.7 will be inputs.
P1OUT writes a value on the output pins.
P1SEL selects for each pin if it will be used as a digital I/O or for its secondary funcion (like analog input, PWM output, etc).
P1IN reads the value present at the ports inputs.
The MSP430 does have weak pull-ups for most ports. Set the port as an input and write a 1 to PxOUT, to activate the weak pull-up.
Because there are seperate input and output registers for each IO port, the MSP430's avoid Read-Modify-Write problems that plague other uC's
Sample C code
P1DIR = 0x01; // specified as output port. 0x00 would make it input
* This selects P1.0 as output, all other P1 pins as input.PIOUT = 0x00; //initialize output port to ground
* here you are writing a '0' to each output pin , that is P1.0 to P1.7;PISEL = 0x00; // use all pins as digital IO?
* Here you are selecting all P1 pins to be used as digital I/O.
If you just use = then you set all the bits on the port in one command.You can use &= (AND EQUAL) and |= (OR EQUAL) to set or clear some bits while preserving the other bits.
Setting a bit should be accomplished by using the |= so that any bit you want to set gets a 1 in the mask
P1OUT |= 0x01; // turn bit zero of port 1 on. P1.0 is now high
To clear a bit, you use &= with a mask but the original mask has to be inverted using the ~ operator. The following are functionally equivalent
P1OUT &= ~0x01; //turn bit zero of port 1 off. P1.0 is now low P1OUT &= 0xFE; //turn bit zero of port 1 off. P1.0 is now low
Animated diagram:
Questions: