O'Reilly logo

Microcontroller Programming and Interfacing Texas Instruments MSP430 by Daniel J. Pack, Steven F. Barrett

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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
specific 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) configuration.
In this example, we also configure 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[4] is hardwired to the amplifier input. For
this specific application, we configure all port P4 pins to output and configure 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.
//function prototypes
void initialize_ports(void);
//main function
void main(void)
{
:
initialize_ports( );
:
}
//function body
void initialize_ports(void)
{
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: Configuring ports.
A.3.4 PROGRAM CONSTANTS
The #define statement is used to associate a constant name with a numerical value in a program. It
can be used to define 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 defined within a program:

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required