1. Select pin function -- PxSEL, PxSEL2
PxSEL2 PxSEL pin function
0 0 Used as IO port
0 1 Used as the first function pin
1 0 Reserved, refer to the manual of the specific model
1 1 Used as the second function pin
When setting a pin as a peripheral function, the chip will not automatically set the input and output direction of the pin. The user needs to set the direction register according to the function.
PxDIR.
2. Select pin input/output direction -- PxDIR
Bit = 0: Input
Bit = 1: Output
3. Select whether to enable pull-up and pull-down resistors on the pin -- PxREN
Bit = 0: Disable
Bit = 1: Enable
4. Output register -- PxOUT
Bit = 0: Output low level or pull down
Bit = 1: Output high level or pull-up
5. Pin Status Register -- PxIN
Bit = 0: The pin is currently low
Bit = 1: The pin is currently high
P1DIR|=BIT1+BIT3+BIT4; // Set P1.1, P1.3, and P1.4 as outputs (set to 1)
P1DIR &=~ (BIT5+BIT6+BIT7); // P1.5, P1.6, P1.7 are set as input (set to 0)
P1OUT ^= BIT1; //Invert
|