Digital Input and Output with the MSP430

Digital Input and Output with the MSP430
OpenStax-CNX module: m40643
Digital Input and Output with the
Matthew Johnson
This work is produced by OpenStax-CNX and licensed under the
Creative Commons Attribution License 3.0†
This module goes over the basics of digital I/O and the digital interface between a microcontroller
and the outside world.
1 Basic Digital I/O in the Real World
In this lab you'll go over the basics of how to setup and use the GPIO on the MSP430. This will allow you to
get data from the outside world, run some processing on it, and then output it again as useful information.
You only have one task this week:
1. Coding in MSP430 assembly, write a simple I/O echo program. Setup the GPIO pins and poll
the input switches for any changes. On a change, take the input and display it to the output. Step
through this program to observe how it behaves. Assignment Details (Section 3: Assignment Details)
2 Digital I/O Basics
2.1 GPIO
• The MSP430 uses a limited number of GPIO hardware pins that are assignable to several functions
depending on your specic model and your program's needs. Our version, the MSP430G2231, can have
the Port_1 pins act as digital output, digital input, or ADC input.
• The pins are organized into ports, with each port usually one byte (8 bits/pins) wide. On larger
versions of the processor (dierent format chips with physically many more pins...) you can encounter
several ports, but in this lab you will only be using Port_1 and Port_2
• You can set each pin's function independently (input or output) by modifying some memory mapped
I/O registers. Since we want to do both, we will divide P1 into half inputs and half outputs as needed.
• The I/O ports are memory mapped into the top of the MSP430 address space.
∗ Version
1.1: Aug 16, 2011 1:47 pm -0500
OpenStax-CNX module: m40643
• There are several registers associated with each port. For now, you only need to worry about four
(P1IN, P1OUT, P1DIR, and P1REN).
The P1IN register is located at address 0x0020 in memory, which you can also refer to using the
C symbol &P1IN
The register holds the values the MSP430 sees at each pin, regardless of the pin direction setting.
To read the register, it is good practice to use a mov.b instruction to avoid accidentally reading
adjacent registers
tip: If you are looking to test or read just the pins set to input, you will have to mask
the P1IN register to zero out the other unwanted/output pins. Reading P1IN reads the
entire port, regardless of pin direction.
The P1OUT register is located at address 0x0021 in memory, which you can also refer to using
the C symbol &P1OUT
If their direction bits in P1DIR are set to output/ "1", the corresponding pins will output the
values set in P1OUT.
If a pin's direction bits are set to input in P1DIR and its resistors are enabled in P1REN, P1OUT
controls the pin's connection to the pull-up resistor. Setting P1OUT to "1" enables the pull-up,
while setting it to "0" leaves the input to oat in a high impedance state.
To set P1OUT, use a mov.b instruction to set several pins at once. To set individual bits to "1",
you can use an or.b instruction with a "1" in the positions you want to set. To clear individual
bits/ set them to zero, use an and.b instruction with mostly "1"s except for a "0" for the bits
you want to clear.
The P1DIR register is located at address 0x0022 in memory, which you can also refer to using
the C symbol &P1DIR
The value of the bits in P1DIR determines whether the MSP430 hardware leaves the pin in a high
impedance state where it responds to external voltage changes (which you can read at P1IN), or in
a low impedance state where the MSP430 drives the output voltage to a certain value determined
by P1OUT.
To set the bit directions all at once, use a mov.b instruction, but to change individual bits
regardless of the others, use an and.b or a or.b
Set the corresponding bits to "0" to set pins to input mode, or to "1" to set them
to output mode.
The P1REN register is located at address 0x0027 in memory, which you can also refer to using
the C symbol &P1REN
P1REN controls whether the MSP430 Launchpad enables the integrated pull-up resistor for a
given pin.
The pull-up resistors allow the use of single pole switches. They prevent the input signals from
oating randomly while the switches are open by loosely tying the inputs to Vcc. When the switch
is closed though, the much stronger connection to ground wins out, pulling the inputs down to
Set the corresponding bits to "1" to enable a pin's pull-up resistor, or to "0" to
disable it (disabled by default).
OpenStax-CNX module: m40643
2.2 So What?
In this lab we're going to use the MSP430's GPIO pins, combined with some external switches and an LED
display, to build a basic I/O system for our board. Because of how things t together on the board, it makes
sense to use P1.0-P1.3 (the rst three Port_1 GPIO Pins) to read the input switches and P1.4-P1.7 for the
output signals.
Setting up the outputs is easy simply set the upper four bits (bits 4-7) of &P1DIR to "1", and then write
the output to the upper four bits of &P1OUT. That means you'll have to shift your data left 4 positions before
output, but you should already know a simple technique to do so!
aside: You'll notice that when you change the output, the corresponding input bits also change.
This happens because the input hardware always reads the status of the line, regardless if it is set
to input our output. Changing the &P1DIR values only connects or disconnects the driving circuitry
built into the MSP430. In advanced applications this can be used to analyze potential faults in the
circuitry outside the chip.
Inputs are also "easy," but there are a few hardware concepts you'll need before you understand how they
2.2.1 A Little Bit About Wires
As mentioned briey in class, binary digital logic has two valid states, plus one third mystery state. That
third state, "The High Impedance State," (High-Z) just means that the wire isn't connected to anything.
You've already talked about using so called tri-state buers to negotiate who can talk on a shared bus the
listening components enter the high impedance state, allowing the transmitting component's signal to drive
the bus with no conicts.
Impedance is a generalized form of the classical Resistance concept. Impedances can be
real or complex valued, and apply too signals expressed in complex exponential form (whether
constant or variable!). To learn more about impedance, check out Dr. Johnson's sections from
the Elec 241 course notes.1
A Basic Switch
Figure 1
In order to read useful input from your switches, you need them to be "0" in one state, and "1" in
the other. Yet knowing what you know about the third state, the switch shown above will actually give
a "0"/"1" (depending on what you connect it to) when closed and "High-Z" when open. Because there's
nothing else driving the sensor input besides our switch, the input value will be random when the
switch is open. In digital logic this is called oating, and it is a very very bad thing.
1 "The Impedance Concept" <>
OpenStax-CNX module: m40643
One simple solution is the Pull-Up (or Pull-Down) Resistor. Connecting the oating side of the
switch to a logic level through a large resistor will tie down the oating input when the switch is open, but
won't eect the read value much when the switch is closed.
Figure 2: As you can see, when the switch is closed, the input is shorted to ground and reads zero.
When the switch is open, the pull-up resistor holds the previously oating end at Vcc.
Pull-Ups in the MSP430
For better or for worse, the MSP430 actually has pull up resistors already built into the chip's hardware.
Conguring them takes several steps, but once setup they provide all the functionality above without the
extra external connections.
∗ Set the Pin Direction for P1.0-P1.3 to input. (Set bits 0-3 of &P1DIR to "0")
∗ Enable the resistors themselves. (Set bits 0-3 of &P1REN to "1")
∗ Congure the resistors to be pull-up. (Set bits 0-3 of &P1OUT to "1")
The most confusing part of the whole process is the double function of P1OUT. Because of the hardware implementation on the MSP430, &P1OUT controls the outputs as well as the
connections to the pull up resistors. You will need to ensure that every time you output a
value, you KEEP the lower four bits "1". The easiest way to do this is just by ORing your
raw output with the constant #0Fh before you write to P1OUT. The MSP430 does not have a specic
"or" instruction by name, but bis does the same thing. For more info on bis and its inverse bic,
see next week's lab.2
OpenStax-CNX module: m40643
Figure 3: Notice that congured this way, the MSP430 GPIO pin takes the form of the simplied
Pull-Up gure above.
2.3 Polling
ˆ A traditional single threaded polling scheme consists of a main loop that runs continuously. Within
that loop, the processor periodically checks for changes, and if there are none, continues looping. Once
a change is detected, the program moves to a new section of code or calls a new subroutine to deal
with the changes.
ˆ Polling has advantages and disadvantages it keeps program execution linear and is very easy to code
and implement, but it also is not incredibly responsive. Since polling only checks values at certain
points in the main run loop, if the loop is long or changes occur quickly, a polling scheme can miss
input data. For now though it will suce.
3 Assignment Details
Your task is to code a simple input to output echo program for the MSP430. Your program should consist
A setup section that runs once and congures the GPIO pins
A main loop that runs innitely
Code inside your loop to read the state of the GPIO input pins
A separate section of code to write the changes to the output pins and then return to the main loop
OpenStax-CNX module: m40643
You should already know the basics of masking from class, but it becomes very important
when dealing with I/O. Since dierent pins do dierent things in the same port (P1), you the
programmer will have to be careful not to accidentally modify the wrong bits even though your
instructions will operate on the entire register.
All images drawn by Matt Johnson, Rice ECE
Was this manual useful for you? yes no
Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Download PDF