Chapter 9 - DigitalIO
Chapter 9 - DigitalIO
潘政皓 3B
9.1. Describe the procedure to set up a digital I/O pin on the MSP430.
9.2. Design a program to write logic levels to an output pin on the MCU.
9.3. Design a program to read logic levels from an input pin on the MCU.
Fig. 9.1
Digital I/O breakout on the MSP430FR2355TPT package
Each of the digital I/O from the MSP430FR2355TPT MCU is broken out to a variety of external
circuits on the MSP430FR2355 LaunchPad™ board. Figure 9.2 shows the details of the MCU digital I/O
breakout on the LaunchPad™.
9.1 The MSP430 Digital I/O System • 247
Fig. 9.2
Digital I/O breakout on the MSP430FR2355 LaunchPad™ development kit
The MSP430 provides the ability to access ports using 16-bit operations using the labels PA, PB,
and PC. PA represents the combination of P1:P2. PB represents the combination of P3:P4. PC
represents the combination of P4:P5. In this text, we will always access the ports using 8-bit operations
(.b) as it is much simpler to understand their operation and write programs for.
Each bit within every port is independently configurable with a handful of useful settings. First, each
bit can be configured to either be an input or an output. Additionally, bits that are programmed to be an
input can have an optional pull-up or pull-down resistor. Ports 1–4 can also contain edge-triggered
interrupts with selectable edge sensitivity (LOW-to-HIGH or HIGH-to-LOW). Note that interrupts will be
covered in Chap. 11, so we will not go into those details now. The digital I/O system contains a set of
registers for each port that facilitate configuration, reading, and writing. The terminology used within the
data sheets for the MSP430 attempts to generalize the register names so that they apply to all of the
ports. A lower-case “x” is used to denote a number that can take on any value between 1 and 5 and
represents a port number. For example, if the documentation stated “Px can be configured to be both an
input and output”, this means all ports from P1 to P5 can be configured to be both an input and output.
The rationale for this terminology will become clearer as the configuration registers are explored. There
248 • Chapter 9: Digital I/O
are six key configuration registers for each port within the MSP430FR2355 MCU: PxDIR, PxIN, PxOUT,
PxREN, PxSEL0, and PxSEL1.
When a port bit is configured as an input, the PxOUT register has a secondary role, which is to
dictate the polarity of an optional pull-up/down resistor (see next section).
Fig. 9.3
The use of pull-up resistors with SPST switches
Whether or not a port input has a pull-up/pull-down resistor is dictated by the bits within the PxREN
register. The bits within PxREN control the corresponding bit location within PxIN (i.e., bit 0 of PxREN
controls whether bit 0 of PxIN has a pull-up/pull-down resistor). The logic for PxREN is as follows:
When PxREN enables a pull-up/pull-down resistor, the PxOUT register is used to dictate whether
the resistor is a pull-up or pull-down. If PxDIR ¼ 0, making the port bit an input, and PxREN ¼ 1, enabling
the pull-up/pull-down resistor, then the PxOUT register has the following logic:
more than two functions assigned to most of its pins, so it requires two bits to control the function
selection. There are two registers, PxSEL1 and PxSEL0 that hold the two selection bits. The logic for
these registers is shown in Table 9.1. Note that each signal pin has digital I/O as its default selection (i.e.,
PxSEL1:PxSEL0 ¼ 00). To see what the secondary and tertiary functions are for each pin, you must look
in the device-specific data sheet for the MCU.
Table 9.1
Port Register Select Logic (PxSEL1:PxSEL0)
Programmers will often take advantage of the reset conditions of the configuration registers when
setting up digital I/O. As a case in point, consider setting up a signal pin as a digital input without a pull-up/
pull-down resistor. Upon reset, the default value of PxDIR ¼ 0, which defaults the pin to an input. Upon
reset, the default value of PxREN ¼ 0, which defaults the pin to not have a pull-up/pull-down resistor.
Finally, upon reset the values of PxSEL1/PxSEL0 ¼ 00, which defaults the function select for the pin to
be digital I/O. So, all that a programmer actually needs to do is clear the LOCKLPM5 bit in the PM5CTL0
register to take the digital I/O system out of low power mode and all pins are configured as inputs without
pull-up/pull-down resistors.
The level of explicit configuration of digital I/O can vary upon coding expectations, typically set by
the programmer’s organization. Some organizations create coding standards that require each configu-
ration bit to be explicitly set or cleared upon startup. This is done regardless of whether the explicit
altering of the configuration bit matches its reset value (i.e., the reset value is 0, but we still do a bit clear
to ensure it is a 0). This can be advantageous for two reasons: first, there is no risk that an oversight in
understanding the default values in the data sheet occurs; second, it makes the program highly readable
by subsequent programmers. This approach can be simultaneously disadvantageous. The explicit
instructions that make sure every bit is configured as desired, regardless of whether the desired value
9.1 The MSP430 Digital I/O System • 251
matches the reset value, can lead to excessive startup times. Additionally, the program code can
become excessively large and use up the program memory.
In this text, an approach is taken that balances the two initialization extremes while emphasizing the
core concepts of the digital I/O system. We will always configure the PxDIR register to explicitly show that
we are setting the direction of a bit to be used by the program. We will only configure PxREN if the input
uses a pull-up/pull-down resistor. That way if a resistor is used it is obvious, and if it doesn’t, there is no
mention of PxREN in the program. We will not explicitly configure PxSEL1:PxSEL0 to select digital I/O as
the function for the pin as it is a common assumption that upon reset, all MCU pins default to the digital
I/O function.
CONCEPT CHECK
CC 9.1 Why is it a good idea for the digital I/O system to be disabled and in low-power
mode upon startup?
A) It isn’t. All it does is cause the programmer to spend time figuring out how to
turn it on. I would prefer if by default all the pins were configured as outputs
and just worked.
B) Since the MCU can be used in any type of application, we never know what is
going to be physically connected to the pins of the package. Disabling the
digital I/O system upon startup avoids accidently damaging external circuitry
by inadvertently driving a logic level into it.
C) It makes it difficult to program so the developers are forced to take an
embedded systems class.
D) The entire MCU is disabled upon startup, which includes the digital I/O.
252 • Chapter 9: Digital I/O
ㄍ∵
- P1DIR bit 0 ¼ 1 ; Configure P1.0 as an OUTPUT
Note: For an output, this is all we need to do (relying on PxSEL1:PxSEL0 ¼ 00).
• Clear LOCKLPM5 in PM5CTL0 register.
If we open the msp430fr2355.h header file, we can see that there are literals defined for the P1DIR
and PM5CTL0 register addresses. We can also see that there is a bit mask defined for BIT0 (0x0001)
and LOCKLPM5 (0x0001). Using the names defined in the header file, the assembly code to accomplish
this configuration is:
After these two lines of code, the output is now ready to be used. From the msp430fr2355.h header
file we can see that the register name for the port outputs is P1OUT. This name can be used when writing
to the port. Since literal names from the header file will be directly substituted into the [Link] as their
numeric values, we need to use an & in front of the names to treat the numbers as absolute addresses.
Follow Example 9.1 to see the actual code to turn on and off LED1 on the LaunchPad™ board. Note that
upon reset, P1OUT is indeterminant. That means that the first time through the main loop, LED1 may be
in either the ON or OFF states.
9.2 Digital Output Programming • 253
Example 9.1
Using a digital output to drive LED1 on the LaunchPad™ board
The CCS debugger allows the port registers to be viewed during debug. This is a useful tool when
configuring a digital I/O pin for the first time. Often when a digital I/O is not configured correctly, the result
is that the I/O pin simply does not work. When this occurs, the only way to figure out what is going on is to
go into the Register Viewer and make sure the configuration registers are set up as desired. Follow
Example 9.2 to experiment with the configuration registers in the Register Viewer.
254 • Chapter 9: Digital I/O
Example 9.2
Viewing port registers in the CCS register viewer
CONCEPT CHECK
CC 9.2 Is it possible to experiment with different port configuration settings by manually
typing in values into the Register Viewer?
A) Yes. Any register that shows up in the Register Viewer in the CCS debugger
can be manually altered by typing in values directly in their fields. You just
need to remember to hit return after you enter the value for it to take effect.
B) No. That is just unnatural. Registers should only be configured by the
program, not the debugger.
9.3 Digital Input Programming • 255
Fig. 9.4
Push-button switch circuitry on the LaunchPad™ board
Let’s specifically look at reading a logic level from S1. One of the simplest ways to read from an input
pin is through a process called polling. Polling consists of creating a program loop that will continually
check the value of the input and only exit the loop if the value changes. Applying the concept of polling to
reading S1, we can create a program loop that will continually check the switch to see if it has been
pressed. Based on the circuit diagrams in Fig. 9.4, we should stay in the loop as long as the value of
S1 ¼ 1, meaning that the switch has not been pressed. If S1 ever equals 0, then that indicates that the
switch has been pressed and we can exit the loop and perform some actions using other instructions.
After the action is taken, the program re-enters the polling loop to wait for the next press. Let’s create a
program that will poll S1 and if it is pressed, toggle LED1. The flow chart for this program is shown in
Fig. 9.5.
Fig. 9.5
Flow chart for polling SW1
256 • Chapter 9: Digital I/O
是否為 1
,
是的話 ,
Z = 0
ψ # 0000001 ob
囂
—
8000000
檢查⾄ 三 x 00
②
]b
000000
# 0000000 ± 0
Xor
= 紫
暗
| ! :
③ 0000000 :
… 1 o
Example 9.3
Polling the input S1
9.3 Digital Input Programming • 257
When running the program from Example 9.3 you probably noticed that pressing S1 did not always
result in LED1 toggling. You may have also noticed that when you held down S1, LED1 appeared dimmer
than when it was on and S1 was not pressed. These issues have to do with the timing of the polling loop.
The polling loop is testing the S1 bit many millions of times per second. This means no matter how fast
you press and release S1, the program will exit the polling loop and toggle LED1 thousands of times. The
reason that LED1 is not always toggled when you press and release S1 is because you can never tell
what value LED1 is at when you release the button. The reason that LED1 appears dimmer when you
hold down S1 is because it is being continually turning LED1 on and off millions of times each second
while the program checks S1, exits the polling loop, and performs the XOR toggle operation. One
common technique to address the issues encountered when applying polling to the relatively slow
human interaction with the MCU is to insert a delay loop after the event of interest occurs. In our case
the event of interest is the human finger pressing S1. The delay will give time for the human to remove
their finger from S1 before reentering the polling loop. The amount of delay depends on how responsive
you want the button to be. Consider the new logic for polling S1 in Fig. 9.6 that inserts some delay after
the LED1 toggling action.
Fig. 9.6
Flow chart for polling SW1 with delay
258 • Chapter 9: Digital I/O
Now follow Example 9.4 to see the impact of adding delay after the polling loop.
Example 9.4
Polling the Input S1 with delay
Summary • 259
Summary
v Digital I/O gives an MCU the ability to read pull-down using the logic: 0 ¼ pull-down;
and write logic levels to the pins of a device. 1 ¼ pull-up.
This system is highly versatile because it can v PxSEL1 and PxSEL0 select the function to
be used for nearly any application. be used on each pin. The default value for
v MCUs share functionality on their pins to PxSEL1:PxSEL0 ¼ 00, which selects the
save space on the package. Digital I/O is digital I/O function. The secondary and ter-
typically the first option on each pin. tiary functions for each pin are listed in the
v The MSP430FR2355TPT has six digital I/O MSP430FR2355 device-specific data sheet.
ports named P1 (8-bit), P2 (8-bit), P3 (8-bit), v Upon power-up or reset, the digital I/O sys-
P4 (8-bit), P5 (5-bit), and P6 (7-bit). All 44 I/O tem puts all pins of the digital I/O system into
are brought out to pins on the MCU package. a high-impedance input mode with Schmitt
v 16-bit access to the ports can be accom- triggers in order to reduce power consump-
plished using the labels PA (P1:P2), PB tion and avoid errant current flow. To take the
(P3:P4), and PC (P5:P6). digital I/O system out of this low-power mode,
v All 44 I/O of MSP430FR2355 MCU are the programmer needs to clear the
brought out to pins, LEDs, connectors, or LOCKLPM5 bit in the PM5CTL0 register.
sensors on the LaunchPad™ board. v The procedure to initialize the ports is to first
v There are six registers for each port in the configure the PxDIR, PxREN, PxOUT
MSP430 that configure the port operation. (if using a pull-up/pull-down resistor), and
They are PxDIR, PxIN, PxOUT, PxREN, PxSEL1:PxSEL0 registers. Next, the
PxSEL1, and PxSEL0. LOCKLPM5 bit in the PM5CTL0 register is
v PxDIR configures each bit within a port to cleared to take the digital I/O system out of
either an input or output. The logic for low-power mode. After this, the PxIN or
PxDIR is: 0 ¼ input (default); 1 ¼ output. PxOUT registers are ready for use by the
v PxIN contains the logic values for each pin of main program.
a port configured as an input. v Programmers can take advantage of the
v PxOUT can be written to in order to set the default settings of some configuration
logic values for any signals configured as registers upon startup; however, explicitly
outputs. PxOUT has a secondary role when configuring some bits makes your code
a port bit is configured as an input and uses a more readable. It is common to explicitly con-
pull-up/pull-down resistor. In this case, figure PxDIR so it is obvious how the pin is
PxOUT controls the polarity of the resistor. being used. It is also common to accept the
v PxREN is used to control whether an optional default values for PxSEL1:PxSEL0 ¼ 00,
pull-up/pull-down resistor is added to pins which selects digital I/O as the pin function.
configured as inputs. The logic is: 0 ¼ no v Each new [Link] file created by CCS
resistor (default); 1 ¼ resistor enabled. includes a msp430.h header file. This header
When the resistor is enabled, the PxOUT file points to a device specific header file, in
register dictates whether it is a pull-up or our case the msp430fr2355.h. The device-
260 • Chapter 9: Digital I/O
specific header file contains literal names for check the value of the pin. The program will
each configuration register in the memory stay in the polling loop as long as no event
map plus some common bit masks. Using has occurred (i.e., the switch has not been
the literal definitions from the header files pressed). When an event occurs (i.e., the
makes a program easier to understand and switch has been pressed), the program
also potentially portable to another exits the polling loop and performs some
MSP430 MCU. task. It then reenters the polling loop to wait
v The LaunchPad™ board contains two user for the next event.
LEDs labeled LED1 and LED2. These are v When polling a human interaction such as a
connected to P1.0 and P6.6 of the MCU button press, the mismatch in speed
respectively. These LEDs can be driven between the MCU bit checking and the
directly as digital outputs to turn them on human motion can create glitchy behavior.
and off (0 ¼ OFF, 1 ¼ ON). This is because the MCU can poll the input
v The LaunchPad™ board contains two push- millions of times per second. Even the fastest
button switches labeled S1 and S2. These human button press can be observed by the
are connected to P4.1 and P2.3 of the MCU polling loop as thousands of presses.
respectively. These switches are SPST with v To avoid the glitchy behavior, a delay loop
their inputs connected to ground. This can be inserted after the program exits the
provides a logic LOW when pressed. A pull- polling loop to give time for the human to
up resistor is required to be enabled on the remove their finger from the button.
MCU pin in order to provide a logic HIGH
when the switch is not pressed.
v Polling is the process of continually checking
the value of an input. This is accomplished by
creating a program loop that will continually
Exercise Problems
Section 9.1: The Digital I/O System the only two configuration steps that are
needed to initialize a pin to be an output?
9.1.1. What does the PxDIR register configure?
9.2.2. Give the assembly program code to configure
9.1.2. What is the logic for the PxDIR register?
P1.4 as an output. Use the literal definitions
9.1.3. What is the function of the PxIN register? from the msp430.h header file for the register
9.1.4. What is the primary function of the PxOUT names and bit masks.
register? 9.2.3. Give the assembly program code to configure
9.1.5. What is the secondary function of the PxOUT P2.0 as an output. Use the literal definitions
register? from the msp430.h header file for the register
names and bit masks.
9.1.6. What does the PxREN register configure?
9.2.4. Give the assembly program code to configure
9.1.7. What is the logic for the PxREN register?
P3.7 as an output. Use the literal definitions
9.1.8. What do the PxSEL1:PxSEL0 registers from the msp430.h header file for the register
configure? names and bit masks.
9.1.9. What function will be selected for an MCU pin if 9.2.5. Give the assembly program code to configure
PxSEL1:PxSEL0 ¼ 00? P4.5 as an output. Use the literal definitions
9.1.10. What is the default value for PxSEL1:PxSEL0 from the msp430.h header file for the register
after power-on or reset? names and bit masks.
9.1.11. What does the msp430.h header file provide 9.2.6. Give the assembly program code to drive a
for the programmer? logic LOW to P1.4. Use the literal definitions
from the msp430.h header file for the register
9.1.12. Why is it a good idea to use the literals from the
names and bit masks.
msp430.h header file?
9.2.7. Give the assembly program code to drive a
Section 9.2: Digital Output Programming logic LOW to P2.0. Use the literal definitions
from the msp430.h header file for the register
9.2.1. If you rely on the function select registers names and bit masks.
default value choosing digital I/O as a pins
function (PxSEL1:PxSEL0 ¼ 00), what are 9.2.8. Give the assembly program code to drive a
logic HIGH to P3.7. Use the literal definitions
References • 261
from the msp430.h header file for the register 9.3.2. If an SPST switch is connected to an MCU pin
names and bit masks. and has its input tied to the power supply, does
9.2.9. Give the assembly program code to drive a the MCU need to enable a pull-up or pull-down
logic HIGH to P4.5. Use the literal definitions resistor? Why or why not?
from the msp430.h header file for the register 9.3.3. What is the concept of polling?
names and bit masks. 9.3.4. Why does delay help with polling when reading
an input with human interaction?
Section 9.3: Digital Input Programming 9.3.5. What is the downside of putting too large of a
9.3.1. If an SPST switch is connected to an MCU pin delay after the polling loop is exited?
and has its input tied to GND, does the MCU
need to enable a pull-up or pull-down resistor?
Why or why not?
References
1. Texas Instruments Inc. (2019). MSP430FR4xx and MSP430FR2xx Family User’s Guide, Literature Number:
SLAU445I, October 2014 – Revised March 2019. Retrieved from: [Link]
[Link]
2. Texas Instruments Inc. (2019). MSP430FR235x, MSP430FR215x Mixed-Signal Microcontrollers [device-
specific data sheet], Literature Number: SLASEC4D, May 2018 – Revised December 2019. Retrieved from:
[Link]
3. Texas Instruments Inc. (2019). MSP430FR235x LaunchPad™ Development Kit User’s Guide, Literature
Number: SLAU680, May 2018. Retrieved from: [Link]