Chapter 6
[Link] purpose Input/Output
This chapter starts with a description of one of the simplest
integrated peripherals of the MSP430 the General Purpose 8-bit
nput !utput "GP!#$ The nput%!utput "%!# ports can be
configured as interruptible or non-interruptible$ &dditionall'( the port
pins can be indi)iduall' configured for general-purpose use( or as
special function %!s( such as *S&+Ts( comparator signals and &,-s$
.ollowing the technical details( there is the first in a series of hands-
on laboratories$ The laborator' uses the input lines to read the state
of push button switches and uses the output lines to feed light
emitting diodes "/0,s#$
Topic Page
[Link] Copyright 2009 Texas Instruments, All Rights Resere! "
#eneral purpose Input$%utput
6.1 Introduction...................................................................6-3
6.2 Registers........................................................................6-4
6.2.1 irection Registers !P"IR#......................................6-4
6.2.2 Input Registers !P"I$#..............................................6-4
6.2.3 Output Registers !P"O%T#.........................................6-4
6.2.4 Pull-up / Pull-do&n Resistor 'na(le Registers
!P"R'$# ..............................................................................6-)
6.2.) *unction +elect Registers, !P"+'-# and !P"+'-2#......6-)
6.2.6 Interrupti(le ports !P1 and P2#.................................6-)
Interrupt 'na(le !P"I'#...............................................6-6
Interrupt 'dge +elect Registers !P"I'+#......................6-6
Interrupt *lag Registers !P"I*G#..................................6-6
6.3 -a(orator. 2, *lashing -'.............................................6-6
6.3.1 -a(2/, 0lin1ing the -'............................................6-2
6.3.2 -a(20, 0lin1ing the -' hal3 the speed......................6-4
6.3.3 -a(2C, Toggle the -' state (. pressing the push
(utton .................................................................................6-5
6.3.4 -a(2, 'na(le / disa(le -' (lin1ing (. push (utton
press ...............................................................................6-11
6.4 6ui7..............................................................................6-12
6.) */6s.............................................................................6-13
2 Copyright 2009 Texas Instruments, All Rights Resere! [Link]
&A's
6.1 Introduction
,epending on the MSP430 de)ice( there can be up to ten 8-bit
digital nput%!utput "%!# ports( named P1 to P10$
T'picall'( the MSP430 %! ports P1 and P2 ha)e interrupt capabilit'$
0ach interrupt on these %! lines can be indi)iduall' configured to
pro)ide an interrupt on a rising edge or falling edge of an input
signal$ &ll %! lines with interrupt capacit' use a single interrupt
)ector$
,epending on the hardware de)elopment tool used( the a)ailable
digital %! pins are3
e4430-.2013 MSP430 *S5 Stic63 10 pins - Port P1 "8 bits# and
Port P2 "2 bits#7
e4430-+.2800 MSP430 *S5 Stic63 32 pins - Ports P1 to P4 "8
bits#7
MSP430.G4918%.2013 0:perimenter board3 80 pins Ports P1
to P10 "8 bits#$ Ports P;%P8 and P<%P10 can be accessed as 19-
bit )alues "words# as ports P& and P5 respecti)el'$
0ach of these %! ports has the following capacit'3
ndependentl' programmable7
-ombined input( output( and interrupt conditions7
0dge-selectable interrupt inputs for all the 8 bits of ports P1 and
P27
+ead%write access to port-control registers supported b' all two-
or one-address instructions7
0ach %! has an indi)iduall' programmable pullup%pulldown
resistor "2:: famil' onl'#$
&dditionall'( and depending on the de)ice "see specific datasheet for
details#( the port pins can be indi)iduall' configured as special
functions %!( for e:ample3
*S&+T *ni)ersal S'nchronous%&s'nchronous
+ecei)e%Transmit7
-omparati)e signals7
&nalogue-to-,igital con)erter7
&mongst others=
[Link] Copyright 2009 Texas Instruments, All Rights Resere! 3
#eneral purpose Input$%utput
6.2 Registers
ndependent of the %! port t'pe "non-interruptible3 P3 and others#
or interruptible "P1 and P2#( the configuration of the port operation
is defined in software using the following registers3
6.2.1 irection Registers !P"IR#
+ead%write 8-bit registers7
Selects the direction of the corresponding %! pin( regardless of
the selected function of the pin "general purpose %! or as a
special function %!#7
!ther module functions must be set as re>uired b' the other
modules7
P:,+ configuration3
5it ? 13 The port pin is set up as an output7
5it ? 03 the port pin is set up as an input$
6.2.2 Input Registers !P"I$#
0ach bit of these read-onl' registers reflects the input signal at
the corresponding %! pin "pin configured as general purpose
%!#7
P:@ configuration3
5it ? 13 The input is high7
5it ? 03 The input is low7
Tip3 &)oid writing to these read-onl' registers because it will
result in increased current consumption$
6.2.3 Output Registers !P"O%T#
The output registers are read-write$ 0ach bit of these registers
reflects the )alue written to the corresponding output pin$
P:!*T configuration3
5it ? 13 The output is high7
5it ? 03 The output is low$
The 2:: famil' pro)ides the additional feature that each %! has
a pullup%pulldown resistor that can be indi)iduall' programmed$
f the pinAs pullup%pulldown resistor is enabled( the
corresponding bit in the P:!*T register selects the pull-up or
pull-down3
5it ? 13 The pin is pulled up7
4 Copyright 2009 Texas Instruments, All Rights Resere! [Link]
&A's
5it ? 03 The pin is pulled down$
[Link] Copyright 2009 Texas Instruments, All Rights Resere! (
#eneral purpose Input$%utput
6.2.4 Pull-up/Pull-do&n Resistor 'na(le Registers !P"R'$#
&pplies to the 2:: famil' onl'$
0ach bit of this register enables or disables the pullup%pulldown
resistor of the corresponding %! pin$
P:+0@ configuration3
5it ? 13 Pullup%pulldown resistor enabled7
5it ? 03 Pullup%pulldown resistor disabled$
6.2.5 *unction +elect Registers, !P"+'-# and !P"+'-2#
Some port pins are multiple:ed with other peripheral module
functions "see the de)ice-specific datasheet#7
The bits3 "P:S0/# and "P:S0/2 2:: famil' and some de)ices of
the 4;:":# famil'#( are used to select the pin function3 %!
general-purpose port or peripheral module function$
P:S0/ configuration3
5it ? 03 %! function is selected for the pin7
5it ? 13 Peripheral module function is selected for the pin$
The 2:: famil' de)ices pro)ide the P:S0/2 bit to configure
additional features of the de)ice$ The P:S0/ and P:S0/2
combination pro)ides the following configuration of the 2::
de)ices3
5it ? 03 %! function is selected for the pin7
5it ? 13 Peripheral module function is selected for the pin$
Table )*". +in ports 2xx ,amily !ei-es -on,iguration by +x./0 an! +x./02 -ombination.
P"+'- P"+'-2 Pin *unction
0 0 Selects general-purpose %! function
0 1 Selects the primar' peripheral module function
1 0 +eser)ed "See de)ice-specific data sheet#
1 1 Selects the secondar' peripheral module function
@ote3 Bhen the P1 and P2 ports are configured for peripheral
module function "P:S0/ ? 1 and%or P:S0/2#( the interrupts are
disabled$
6.2.6 Interrupti(le ports !P1 and P2#
0ach pin of ports P1 and P2 is able to generate an interrupt re>uest
"pin is interruptible# and is configured using the P:.G( P:0( and
P:0S registers$ The port ma6es use of all the same configuration
registers as non-interruptible ports "as described abo)e#( but with
three additional registers3
) Copyright 2009 Texas Instruments, All Rights Resere! [Link]
&A's
Interrupt 'na(le !P"I'#
+ead-write register to enable interrupts on indi)idual pins7
P:0 configuration3
5it ? 13 The interrupt is enabled7
5it ? 03 The interrupt is disabled$
0ach P:0 bit enables the interrupt re>uest associated with the
corresponding P:.G interrupt flag7
Briting to P:!*T and%or P:,+ can result in setting P:.G$
Interrupt 'dge +elect Registers !P"I'+#
This read-write register selects the transition on which an
interrupt occurs for the corresponding %! pin "if P:0 and G0
are set#7
P:0S configuration3
5it ? 13 nterrupt flag is set on a high-to-low transition7
5it ? 03 nterrupt flag is set on a low-to-high transition$
Interrupt *lag Registers !P"I*G#
The bit of this read-write register is set automaticall' when the
programmed signal transition "edge# occurs on the
corresponding %! pin( pro)ided that the corresponding P:0 bit
and the G0 bit are set7
0ach P:.G flag can be set b' software( enabling an interrupt
generated b' software7
0ach P:.G flag must be reset with software7
P:.G configuration3
5it ? 03 @o interrupt is pending7
5it ? 13 &n interrupt is pending$
6.3 -a(orator. 2, *lashing -'
The first hands-on laborator' consists of configuring the %! ports(
setting up the input lines to read push buttons and the output lines
to feed /0,s$ The following e:ercises ha)e been de)eloped for the
three hardware de)elopment tools$
The first to be discussed is the MSP-0CP430.G4918 0:perimenterAs
board$ Modifications are later made to suit the other de)elopment
boards$ The main differences between the boards are related to the
specific ports in which the buttons and /0, are "or can be#
[Link] Copyright 2009 Texas Instruments, All Rights Resere! 1
#eneral purpose Input$%utput
connected$ .or the de)elopment of this laborator'( Co!e Composer
/ssentials 3 has been used$
This laborator' will be e:panded into /abs 2&( 25( 2- and 2,$ t will
start without using an' input or interrupt$ Then( the initial lab "2&#
will be impro)ed until it is using inputs( interrupts and low power
modes$ &lso( the watchdog timer is used in these e:ercises$
5ecause the e4430-.2013 does not include a push button( /ab2-
and /ab2, will not support this hardware de)elopment tool$
6.3.1 -a(2/, 0lin1ing the -'
Project files
- source files3 Chapter 6 8 -a(29: 8 -a(2/9student.c
Solution file3 Chapter 6 8 -a(29: 8 -a(2/9solution.c
Bhere : corresponds to the folder of the selected hardware
de)elopment tool3
e44303 e4430-.2013 MSP430 *S5 Stic6
+.28003 e4430-+.2800 MSP430 *S5 Stic6
0:pDboard3 MSP430.G4918%.2013 0:perimenterAs board
Procedure
5' anal'sis of the schematics( determine which %! port pin is
connected to the /0, on the board3
-onsult the MSP430.G4918%.2013 0:perimenterAs 5oard
*serEs Guide Fslau213a$pdfG7
-onsult the e4430-.2013 ,e)elopment Tool *serEs Guide
Fslau1;9b$pdfG7
-onsult the e4430-+.2800 ,e)elopment Tool *serEs Guide
Fslau22;a$pdfG7
!r the schematics shown in -hapter 3( depending on the
de)ice$
Port control registers3
Set the /0, port pin as an output7
Switch the port state between low and high during program
e:ecution$
-onfigure the watchdog timer to pre)ent a P*- during the
program e:ecution7
*se an infinite loop to modif' the state of the port7
*se a software dela' loop to generate the pause inter)al$ "a
long software dela' loop is used here for simplicit' - in real
applications( a timer would be used#7
2 Copyright 2009 Texas Instruments, All Rights Resere! [Link]
&A's
Tr' different )alues for the counter( until 'ou can obser)e a
)isible blin6$
[Link] Copyright 2009 Texas Instruments, All Rights Resere! 9
#eneral purpose Input$%utput
MSP-0CP430.G4918 +O-%TIO$
*sing the MSP-0CP430.G4918 ,e)elopment Tool and the
MSP430.G4918 de)ice( blin6 /0,1$
/0,1 is connected to Port 2$2
Port control registers3
P2,+3 Port 2$2 is set as an output3 P2,+ H? 0:04 Ito force
the pin setting$ t is uses an !+ operation " H # with P2,+
and 0:04J
P2!*T3 To switch the port state between low and high state$
t uses an C!+ operation " K # between P2!*T and 0:04
The watchdog timer must be pre)ented from generating a P*-$
Brite 0:8& to the eight MS5s of the Batchdog timer control
register( B,T-T/ "&dditional details are gi)en in -hapter 8#7
B,T-T/ ? B,TL!/, H B,TPB7
5ecause no cloc6 is defined( the de)ice will use the 32$;98 6LM
watch cr'stal$ n order for a rate of one blin6ing /0, state
transition each second( the software dela' loop should count to
appro:imatel' 30000 I30000%32;98 1 secJ7
e4430-+.2800 +O-%TIO$
*sing the e4430-+.2800 ,e)elopment Tool blin6 the red /0,$
/0,1 is connected to Port 1$1
The programming code follows the same se>uence as gi)en
abo)e( re>uiring onl' configuration of this port$
e4430-.2013 +O-%TIO$
*sing the 0M430-.2013 ,e)elopment Tool blin6 the /0,$
/0, is connected to Port 1$0
The programming code follows the same se>uence as gi)en
abo)e( re>uiring onl' configuration of this port$
6.3.2 -a(20, 0lin1ing the -' hal3 the speed
Project files
- source files3 Chapter 6 8 -a(29: 8 -a(209student.c
Solution file3 Chapter 6 8 -a(29: 8 -a(209solution.c
"0 Copyright 2009 Texas Instruments, All Rights Resere! [Link]
&A's
Bhere : corresponds to the folder of the selected hardware
de)elopment tool3
e44303 e4430-.2013 MSP430 *S5 Stic6
+.28003 e4430-+.2800 MSP430 *S5 Stic6
0:pDboard3 MSP430.G4918%.2013 0:perimenterAs board
Procedure
*sing the pre)ious e:ample( independentl' of the hardware
de)elopment tool( reduce the )alue of the software dela' to half
its pre)ious )alue$
6.3.3 -a(2C, Toggle the -' state (. pressing the push (utton
Project files
- source files3 Chapter 6 8 -a(29: 8 -a(2C9student.c
Solution file3 Chapter 6 8 -a(29: 8 -a(2C9solution.c
Bhere : corresponds to the folder of the selected hardware
de)elopment tool3
+.28003 e4430-+.2800 MSP430 *S5 Stic6
0:pDboard3 MSP430.G4918%.2013 0:perimenterAs board
Procedure
5' anal'sis of the schematics( determine to which port pin the
push button is connected3
-onsult the MSP430.G4918%.2013 0:perimenterAs 5oard
*serEs Guide Fslau213a$pdfG7
-onsult the e4430-+.2800 ,e)elopment Tool *serEs Guide
Fslau22;a$pdfG7
Schematics are gi)en in -hapter 3$
Ports control registers3
Set push button pin port as an input7
0nable interrupts to this pin port7
-onfigure the watchdog timer to pre)ent a P*- during the
program e:ecution7
-onfigure a low power mode$
[Link] Copyright 2009 Texas Instruments, All Rights Resere! ""
#eneral purpose Input$%utput
MSP-0CP430.G4918 +O-%TIO$
*se the S1 button of the de)elopment tool to toggle the state of
/0,1$ Nou must use interrupts to detect the button press and 'ou
should ensure that the MSP430 is in /ow Power Mode 3 when it is
not in use$
The /0,1 configuration is gi)en in /ab 2&7
5utton S1 is connected to Port 1$0
Port control registers3
P1,+3 Port 1$0 is set as an input3 P1,+ O? P0:01 Ito
force the pin setting to 0$ t is uses an &@, operation " O #
between P1,+ and 0:.0J
P103 0nable interrupt to port 1$07
P0S3 -all the port interrupt on a high-to-low transition7
-onfigure the Batchdog timer7
0nable Global nterrupts7
-onfigure low power mode 37
-reate a interrupt ser)ice routine( that includes3
Toggle /0,1 pin port7
,ela' for button debounce7
-lear interrupt flag$
e4430-+.2800 +O-%TIO$
*se the button of the de)elopment tool to toggle the state of the
red /0,$ Nou must use interrupts to detect the button press and
'ou should ensure that the MSP430 is in /ow Power Mode 3 when it
is not in use$
.ollow the same se>uence as gi)en abo)e( ta6ing into
consideration that3
5utton S1 is connected to Port 1$27
Port control registers3
The e4430-+.2800 uses a de)ice in 2:: famil'( so 'ou need
to additionall' configure the button as pull-up or pull-down(
in the P1+0@ register$
"2 Copyright 2009 Texas Instruments, All Rights Resere! [Link]
&A's
6.3.4 -a(2, 'na(le/disa(le -' (lin1ing (. push (utton press
Project files
- source files3 Chapter 6 8 -a(29: 8 -a(29student.c
Solution file3 Chapter 6 8 -a(29: 8 -a(29solution.c
Bhere : corresponds to the folder of the selected hardware
de)elopment tool3
+.28003 e4430-+.2800 MSP430 *S5 Stic6
0:pDboard3 MSP430.G4918%.2013 0:perimenterAs board
Procedure
mpro)e the last e:ercise "/ab 2-# b' enabling the /0, blin6ing
b' button press7
*se the pre)ious configuration7
nclude a control flow program )ariable that detects if the /0, is
blin6ing or not( when the button is pressed$
MSP-0CP430.G4918 +O-%TIO$
*se the S1 button of the de)elopment tool to start and stop the
/0, blin6ing$ t is not necessar' to use /ower Power Modes in this
e:ercise$
.ollow the same se>uence as presented in /ab 2-7
,etect of the button is pressed3 if "Q"P1@ O 0:01##
,efine a )ariable that indicates whether the /0, is blin6ing7
Set the program flow depending on the state of the )ariable$
e4430-+.2800 +O-%TIO$
*se the button of the de)elopment tool to start and stop the /0,
blin6ing$ t is not necessar' to use /ower Power Modes in this
e:ercise$
.ollow the same se>uence as gi)en in /ab 2- for this hardware
de)elopment tool and the additional steps pro)ided abo)e$
[Link] Copyright 2009 Texas Instruments, All Rights Resere! "3
#eneral purpose Input$%utput
6.4 6ui7
1. n the MSP430 s'stem( all %! ports3
"a# &re initiall' set as outputs when the s'stem powers up7
"b# +emain constant as the applications program proceeds7
"c# Rar' with each step of the program7
"d# &re initiall' set as inputs when the s'stem powers up$
2. To set an %! port pin as output( the3
"a# P:,+ direction register bit is set7
"b# P:S0/ function-select register bit is set7
"c# P:@ register bit is set7
"d# P:!*T register bit is set$
3. &n input pin on the MSP430 %! port configuration re>uires3
"a# +eset the direction register bit7
"b# Set the P:@ register bit to whate)er the input data dictates7
"c# P:!*T register bit inacti)e7
"d# &ll of abo)e$
4. .or the 2:: famil' hardware de)elopment tools "e4430-.2013
and e4430-+.2800#( an output pin is pulled down when3
"a# P:,+ and P:+0@ are set7
"b# P:,+ is set( P:+0@ is reset7
"c# P:,+( P:+0@ and P:!*T are set7
"d# P:,+ and P:!*T are reset$
). The e4430-.2013 Port 1 %! pin as Sigma%,elta &,- "S,19D&#
positi)e analogue input &0 re>uires3
"a# Set P1S0/$0 "P1S0/ ? 0:01h# and P1,+$0 "P1,+ ? 0:01h#7
"b# Set P1!*T$1 "P1!*T ? 0:02h# and reset P1,+$1
"P1,+ ? 0:00h#7
"c# Set P1@$2 "P1@ ? 0:04h# and reset P1,+$2 "P1,+ ? 0:00h#7
"d# Set P1S0/$1 "P1S0/ ? 0:02h# and reset P1,+$0
"P1,+ ? 0:00h#$
"4 Copyright 2009 Texas Instruments, All Rights Resere! [Link]
&A's
6. To configure %! Port 2 to generate an interrupt re>uest during a
low-to-high transition( 'ou must configure3
"a# Set the P2,+( P20 and P20S associated bits7
"b# Set the P20 and reset the P2,+ and P20S associated bits7
"c# Set the P20 and P2,+ and reset the P20S associated bits7
"d# @one of the abo)e$
6.5 */6s
1. Bhat happens when the read-onl' +egisters P:@ are writtenS
Briting to these read-onl' registers results in increased current
consumption while the write attempt is acti)e$
2. s it appropriate to set P:+0@ ? 1 when P:S0/ ? 1S
This is not recommended$ t ma' result in unwanted current flow
through the internal resistor$
3. Bhat will be the status of P1 and P2 interrupts when peripheral
module function is enable "P:S0/ ? 1#S
+egardless of the state of the corresponding P10 or P20 bit( signals
on the associated pins will not generate interrupts( because the pinAs
corresponding interrupt function is disabled$
4. Bhat will be the state of the P:.G flags when P:!*T or P:,+
are changed "onl' for the interruptible ports( Port 1 and Port 2#S
Briting to P1!*T( P1,+( P2!*T( or P2,+ can result in setting the
corresponding P1.G or P2.G flags$
). Bhat should be the minimum length of time of an e:ternal %!
pin interrupt e)entS
t should be acti)e for a minimum time period e>ual to 1$8 : M-/T(
to ensure that it is recogniMed and that the corresponding interrupt
flag is set$
[Link] Copyright 2009 Texas Instruments, All Rights Resere! "(
#eneral purpose Input$%utput
6. Bhat happens to the P:.G: status when writing to P:0S:S
t depends on the P:@ status$ f P:@ is reset and there is a low-to-
high transition on P:0S:( then the flag ma' be set$ f P:@ is set
and a high-to-low transition occurs on the P:0S:( the flag also ma'
be set$
2. Low should the unused port pins be configuredS
The' should be configured as %! function( output direction( and left
unconnected on the P- board$ This pre)ents floating inputs and
reduces power consumption$ .or the 2:: famil' of de)ices( an
integrated pull-up%pull-down resistor can be enabled b' setting the
P:+0@ bit of the unused pin( which pre)ents the input from floating$
") Copyright 2009 Texas Instruments, All Rights Resere! [Link]