I. Background knowledge: Use of logical operators
When the program initializes, it is recommended to use direct assignment for registers with uncertainties in the reset state (such as Pxout), and it is best to modify the registers using logical operators in other cases.
Direct Assignment
REGISTER =0xF0;
"Turn on" a bit (1), keep the other bits unchanged
// turn bit x on // both on
"Close" a bit (0), keep the other bits unchanged
// turn bit x off // both off
"Flip" a bit (reverse), keep the other bits unchanged
// Toggle bit x // Toggle Both
Two. Register for Gpio
As shown in the following table.
Register |
short Form |
Register Type |
Initial State &nb Sp |
Input |
pxin |
Read only |
- |
Output |
pxout |
read/write |
unchanged |
Direction |
pxdir |
read/write |
Reset with puc |
pullup/pulldown resistor Enable |
Pxren |
Read/write |
Reset with PUC |
pxdir: Setting the direction of the IO Pin
-Bit = 0: input (default)
-Bit = 1: Output
pxren: Enable pin internal pull-up/pull-down, typical pull-up/pull-down resistor resistance 35KOHM
-bit=0: Disable the upper/lower pull resistor function (default)
-Bit=1: Enable up/down resistor function
pxin: reflects the level on the PIN
-Bit=0: input is low level
-Bit=1: input is high level
pxout:
-When the up/down resistor is disabled, the function is to set the output level high and low
--bit=0: Output High Level
--bit=1: Output Low Level
-When the Enable up/down resistor, the function is to select the pull up or pull down
--bit=0: Drop down
--Bit=1: Pull-up
Three. Initialization program example
Example 1: Setting P1.0 to Output
1#include"io430.h"2 3 voidMainvoid)4 {5 //Stop watchdog timer to prevent time out reset6Wdtctl = Wdtpw +Wdthold;7 8P1out =0;//Initialize the output state before changing the pin to an output9P1dir |= BIT0;//P1.0 Output 0Ten One while(1) A { - //code ... - } the -}
Example 2: Setting P1.3 as a pull-up input
1#include"io430.h"2 3 #definePUSH2 BIT34 5 voidMainvoid )6 {7 //Stop watchdog timer to prevent time out reset8Wdtctl = Wdtpw +Wdthold;9 TenP1out =0; OneP1out |= PUSH2;//Initialize the Pullup state AP1ren |= PUSH2;//Enable internal Pullup -P1dir &= ~push2;//set P1.3 to input mode (default) - //State on P1.3: (p1in & PUSH2) = = PUSH2 the - while(1) - { - //code ... + } - +}
Resources
Msp430x2xx Family User ' s Guide
msp430g2553 Datasheet
MSP-EXP430G2 Launchpad Setting Gpio