A.3. ANATOMY OF A PROGRAM 369
determines if the P2 pin will be used for digital input/output (0) or the alternate function of the
speciﬁc pin (1). For an input pin, the P2REN and P2OUT registers are used to enable the internal
resistor and select it for either pullup (1) or a pulldown (0) conﬁguration.
In this example, we also conﬁgure port P4 to connect LEDs to pins 6, 5 and 3 and motors
to pins 2 through 0. On the MSP430F5438 experimenter board, the majority of the port P4 pins
are routed to the Port X,Y header connector. Port P4 is hardwired to the ampliﬁer input. For
this speciﬁc application, we conﬁgure all port P4 pins to output and conﬁgure the associated port
registers as shown in Figure 10.5.
During some of the early steps of a program, a function is called to initialize the ports as input,
output, or some combination of both. This is illustrated in Figure A.3.
P2DIR = 0x01; //configure port P2
P2SEL = 0x01;
P2OUT = 0xFE;
P2REN = 0xFE;
P4DIR = 0xFF; //configure port P4
P4SEL = 0x00;
P4OUT = 0x00;
P4REN = 0x00;
Figure A.3: Conﬁguring ports.
A.3.4 PROGRAM CONSTANTS
The #deﬁne statement is used to associate a constant name with a numerical value in a program. It
can be used to deﬁne common constants such as pi. It may also be used to give terms used within a
program a numerical value. This makes the code easier to read. For example, the following constants
may be deﬁned within a program: