You are on page 1of 9

xanthium enterprises

embedded systems and robotics for all


Home

Store

Products

Tutorials

Source Codes

Contact

Home Serial Communication using MSP430 UART(USCI_A)

Serial Communication using MSP430


UART(USCI_A)
Submitted by Rahul.Sreedharan on 7 June 2015 - 9:39am

In MSP430 serial communication is handled by an on chip peripheral called USCI(Universal Serial


CommunicationsInterface).Theperipheralisdesignedinsuchawaythatitcanhandlemultipleserial
communication formats ,synchronous as well as asynchronous like SPI,I2C,IrDA,UART
etc.MSP430G2553 has two USCI modules names as USCI_A0and USCI_B0 for handling multiple
communicationformats.USCI_A0canbeconfiguredtohandleLIN,IrDA,SPIandasynchronousserial
communication(UART)whileUSCI_B0canhandleSPIandI2C.

Products

In this article we we will configure USCI_A0 in MSP430G2553 to handle asynchronous serial


communication or commonly known as UART mode.The UART mode uses two pins to
transmit(UCA0TXD)andreceivedata(UCA0RXD).Asynchronousserialcommunicationiswidelyused
for communicating with external devices like PC/Laptops,GSM modems,GPS modules etc.It also
formsthebasisformanystandardprotocolslikeRS232,RS422,RS485etc.
Youcanseethe(very)simplifiedblockdiagramofUSCI_A0inUARTmodebelow.

MSP430MotorControlBoosterPack

USBtoSerial/RS232/RS485
Converter

Xanthiumenterprises
Computers/TechnologyBangalore
356likes

LikePage

ShopNow

Bethefirstofyourfriendstolikethis

InthisexamplewearegoingtoconfiguretheMSP430UARTtotransmitandreceiveat9600bpswith
8databits,noParityand1Stopbit.IntheabovefigureyoucanseethattheUSCIcansourceclock
from either SMCLK or ACLK to generate BRCLK which is then used to generate the required
timings.UC0CLKisexternalclocksourcedfromoutsidethroughMSP430pins.
Here I am going to configure the DCO to generate 1MHz and use SMCLK as UART clock.To
generate accurate 1MHz clock I am going to load the caliberation constants into the Basic clock
systemregisterandDCOcontrolregisterslikethis

Contact Us

if(CALBC1_1MHZ==0xFF)//Ifcalibrationconstanterased

Phone

{
while(1)//donotload,trapCPU!!

+918105565924

Address

DCOCTL=0//SelectlowestDCOxandMODxsettings
BCSCTL1=CALBC1_1MHZ//Setrange

#5,NearFidelityFinancial

DCOCTL=CALDCO_1MHZ//SetDCOstep+modulation

ElectronicsCityPhase2
HosurRoad
Bangalore560100
India

CALBC1_1MHZisaconstantdefinedintheheaderfilemsp430g2553.h,insidetheheaderfileyoucan
find the constants for configuring the DCO to operate
(CALBC1_1MHZ,CALBC1_8MHZ,CALBC1_12MHZ,CALBC1_16MHZ).

at

1,8,12

and

6MHz

Recent comments
thanks for your help, i will2 weeks 2 days
ago
Dear Sara,2 weeks 3 days ago
how can i download zip le of source
code2 weeks 3 days ago
try compiling as C project2 weeks 4 days
ago

RegistersRequiredforUART
Afterconfiguringthesystemclockto1MHz,wearegoingtolookintotheregistersandbitsthatare
importantforconfiguringtheUART.Pleasenotethatonlyrelevantregistersandbitswillbeexplained
.

1)UCAxCTL0 (USCI_Ax Control register0) here we are dealing with USC1_A0 so UCA0CTL0
This register controls the settings for Parity selection,direction of data transmission(LSB or MSB
first),characterlength,noofstopbits,modesofserialtransmission.
Theimportantbitsare

errors in compiling your codes 2 weeks 5


days ago
Nice one1 month 15 hours ago

UCPEN

Usedforenablingordisablingparity(InourcasenoparitysoUCPEN=0)

UCPAR

Used for selecting between EVEN or ODD parity (not used since we are
disablingparity)

UCMSB

Controlsthedirectionofreceiveandtransmitshiftregister(hereLSBfirst)

UC7BIT

Selectsthecharacterlength7/8bit(weareusing8bitso0)

UCSPB

Numberofstopbits(here1soUCSPB=0)

UCMODEx

2bitsusedtoselecttheasynchronousmodewhenUCSYNC=0.Hereweare
using the default UART mode.Other modes are used with multiprocessor
serialcommunicationandforautomaticbaudratedetection

UCSYNC

Selectingbetweenasynchronous(UART)andsynchronousmodes(SPI)

Usually after you close the1 month 2


weeks ago
Hi I downloaded your zip it1 month 2
weeks ago
try adding backslashes like1 month 3
weeks ago
Can't open port COM1 month 3 weeks ago
Yes USB2Serial V2.0 is1 month 3 weeks
ago
Is your USB to RS4851 month 3 weeks ago
We are planning on providing1 month 3
weeks ago
VisualBasic support1 month 3 weeks ago
Cool Booster Pack,thanks2 months 1 week
ago

User login
Username *

In our case ,all the bits in UCA0CTL0 are zero so we won't be configuring that register in our
program.

Password *

2)UCAxCTL1hereUCA0CTL1Thetwoimportantbitscontainedinthisregisterare
Create new account
Request new password

UCSSELx

Thesebitsareusedtoselecttheclocksource to the USCI module.In our


casewearegoingtoselectSMCLK@1MHz.

UCSWRST

ThisbitisusedtoputtheUSCImoduleinresetstate.Itisrecommendedto
put the USCI module in reset state before making any changes to the
registers.

Log in

3)UCAxSTAThereUCA0STATThetwoimportantbitscontainedinthisregisterare

UCLISTEN

This bit is used to select the internal loop back mode.In loop back mode
UCAxTXDisinternallyfedbacktothereceiver.

UCBUSY

Indicatesifatransmitorreceiveoperationisinprogress.

4)UCAxBR0(UCA0BR0)andUCAxBR1 (UCA0BR1) are two 8 bit registers which are used to


settheclockprescalarvaluefortheBaudrategenerator.

5) UCAxTXBUF (UCA0TXBUF) 8 bit data register for holding the byte to be transmitted by the
MSP430UART.

6)UCAxRXBUF(UCA0RXBUF)8bitdataregisterthatstoresthereceivedbyte.

ConfiguringtheBaudrate
MSP430 data sheet has an indepth description about configuring the baud rate for various
modes.EasiestwaytoconfigurethebaudrateofaMSP430deviceistousetheBaudrateselection
table listed in the usermanual.Just find out the UCBRx values corrosponding to your BRCLK
frequencyandthebaudrateyouwishtouse.AfterfindingoutvaluesjustputthemintheUCA0BR0
andUCA0BR1registertoconfigurethebaudrate.
InourcaseweareusingSMCLK@1MHzasBRCLKand9600bpsasthebaudrate.Nowcheckthe
table for the corresponding UCBRx value (here 104) and put them in UCA0BR0 and UCA0BR1
registerstoconfigurethebaudrate.YoucanfindthebelowtableintheMSP430x2xxxusermanual.

Another register you should consider while configuring the baud rate is the UCAxMCTL
(UCA0MCTL) which is used to select the modulation stage.Put the UCBRSx value (here 1)
corresponding to the selected baud rate in the UCA0MCTL register to configure the modulation
stage

InterruptsofMSP430UART
MSP430G2553 has a transmit interrupt as well as a data received interrupt with seperate interrupt
locations.The transmit and reception intterrupts have to be enabled in the IE2 register before they
can be used.Two flags in the IFG2 register are used to indicate whether data is transmitted or
received.

IE2

UCA0TXIE

Usedtoenable/disablethetransmitinterrupt.

UCA0RXIE

Usedtoenable/disablethereceiveinterrupt.

IFG2

UCA0TXIFG

USCI_A0transmitinterruptflagissetwhenUCA0TXBUFisempty

USCI_A0 receive interrupt flag is set when UCA0RXBUF have received a


UCA0RXIFG

complete character.you should remember to clear the flag inside the


interruptserviceroutinemanually.

WritingtheCode
One problem with writing code for communicating with two devices is that ,it is quite difficult to pin
point the problem when something goes wrong .The problem could be with your code ,or the
communication cable or the code running on the other device etc.In order to make it easy for
developing UART code,MSP430 has Loop Back mode.In Loop back mode the output of the
transmitterisinternallyconnectedtotheinputofthereceiver,sowhatevertheMSP430transmitsis
receivedback.
Inthisexamplewearegoingtowritesimplecommunicationprogramwhichusestheloopbackmode
andcommunicatesat9600bps(8N1).HereIamusingIARembeddedworkbenchforMSP430from
IAR.IfyouarenotfamiliarwithIAREWyoucancheckoutashorttutorialhere.
The code given below will configure the UART and transmit an ascii character 'A' .The UART is
configuredinloopbackmodeso'A'isreceivedbackandisstoredinUCA0RXBUF.LEDconnectedto
P1.0 is lighted when 'A' is transmitted and LED connected to P1.6 is lighted when 'A' is received
back.

#include"msp430g2553.h"
voidmain(void)
{
WDTCTL=WDTPW+WDTHOLD//StoptheWatchdog

//ConfiguretheClocks//

if(CALBC1_1MHZ==0xFF)//Ifcalibrationconstanterased
{
while(1)//donotload,trapCPU!!
}
DCOCTL=0//SelectlowestDCOxandMODxsettings
BCSCTL1=CALBC1_1MHZ//Setrange
DCOCTL=CALDCO_1MHZ//SetDCOstep+modulation

//ConfiguringtheLED's//

P1DIR|=BIT0+BIT6//P1.0andP1.6output
P1OUT&=~BIT0+BIT6//P1.0andP1.6=0

//SettingtheUARTfunctionforP1.1&P1.2//

P1SEL|=BIT1+BIT2//P1.1UCA0RXDinput
P1SEL2|=BIT1+BIT2//P1.2UCA0TXDoutput

//ConfiguringtheUART(USCI_A0)//

UCA0CTL1|=UCSSEL_2+UCSWRST//USCIClock=SMCLK,USCI_A0disabled
UCA0BR0=104//104Fromdatasheettable
UCA0BR1=0//selectsbaudrate=9600,clk=SMCLK

UCA0MCTL=UCBRS_1//Modulationvalue=1fromdatasheet
UCA0STAT|=UCLISTEN//loopbackmodeenabled
UCA0CTL1&=~UCSWRST//ClearUCSWRSTtoenableUSCI_A0

//Enablingtheinterrupts//

IE2|=UCA0TXIE//EnabletheTransmitinterrupt
IE2|=UCA0RXIE//EnabletheReceiveinterrupt
_BIS_SR(GIE)//Enabletheglobalinterrupt

UCA0TXBUF='A'//Transmitabyte

_BIS_SR(LPM0_bits+GIE)//GoingtoLPM0
}
////
//TransmitandReceiveinterrupts//
////
#pragmavector=USCIAB0TX_VECTOR
__interruptvoidTransmitInterrupt(void)
{
P1OUT^=BIT0//lightupP1.0LedonTx
}
#pragmavector=USCIAB0RX_VECTOR
__interruptvoidReceiveInterrupt(void)
{
P1OUT^=BIT6//lightupP1.6LEDonRX
IFG2&=~UCA0RXIFG//ClearRXflag
}

After the WDT is stopped .The DCO is configured to produce clock at 1MHz using the calliberation
constants.TheportsP1.0andP1.6areconfiguredasoutputstolightuptherespectiveLED'during
transmissionandreception.
InMSP430G2553,P1.1andP1.2areconfiguredasUCA0RXDandUCA0TXDusingtheirrespective
PXSEL registers.The clock for USCI_A0 in UART mode is sourced from SMCLK and other
parametersareconfigured.
TheUARTmoduleisconfiguredtoactinloopbackmodeusingUCA0STAT|=UCLISTEN
Here I am using both transmit and receive interrupts which are enabled in IE2 .GIE bit is also
enabled.
UART transmits a character 'A' by writing it into UCA0TXBUF.After the character is
transmitted,TransmitinterruptisgeneratedwhichlightsuptheP1.0LED.
The character is received back UCA0RXBUF and a recceive interrupt is generated which lights up
theP1.6LED.
YoushouldremembertocleartheUCA0RXIFGflaginsidetheISRmanually.
After you have successfully run the above program in Launchpad you can remove the loop back
modebycommentingoutUCA0STAT|=UCLISTEN.Afterthatlineisremoved,RXDandTXDpins
arenowroutedtoP1.1andP1.2.YoucanconnectP1.1andP1.2andruntheprogram,thetwoLED
connectedtoP1.0andP1.6shouldlightup.

InterfacingMSP430UARTwiththeRealWorld
After you have managed to successfully communicate with the launchpad, its time to interface the
launchpadtoexternaldeviceslikeaPC,GSMmodemoraGPSmodulewhichsupportsaUART.In
thisexamplewewillinterfacetheMSP430launchpadwithaPC/Laptop.
OnewaytoconnectyourlaunchpadtoaPCistousestandardDB9RS232serialport,oneproblem
withDB9portisthatithasbecomelegacyhardwareandmostofthenewerPC'sdonothaveit.When
you are connecting a PC with a microcontroller board like Launchpad ,you have to use a Null
ModemConnection.
InanullmodemconnectiontheTXDpinfromPCisconnectedtotheRXDpinofthemicrocontroller
(here UCA0RXD) and the RXD pin of PC is connected to the TXD pin of the microcontroller (here
UCA0TXD).So whatever PC transmits through its TXD pin goes straight to the receive pin of
microcontrollerandviceversa.
NowwhenyouareinterfacingwithaPCRS232serialport,yourequireatleast3linesRXD,TXDand
Ground to communicate with the Launchpad board.The RS232 signals coming from the PC serial
serial port is in the range of +15 to 15V so you can't connect it directly to the microcontroller
pins.YouhavetobuildaRS232levelconverter(foregMAX232)toconvertbetweenRS232andTTL
signallevels.ThebelowfigureshowstheconnectiondiagrambetweenanMSP430Launchpadand
PC DB9 serial port.You can see that UCA0RXD (Blue Wire) the receive pin of microcontroller is
connectedtotheTransmitpinofPCserialport.

As I have already mentioned DB9 ports are being replaced with USB ports in most PC's and
Laptops,SoanotherwaytointerfacetheLaunchpadwithPCisbyusinganUSBtoSerialconverter
board.USBtoSerialConverterboardsprovideavirtualserialportwhichyoucanreadandwritelikea
standardserialport.Whenyoureadorwritetothevirtualserialport,thedatayousendisthenpassed
theough the standard USB port to the Converter chip (eg FT232) which translates to serial data
stream.
HerIamusingUSB2SERIALUSBtoSerial/RS232/RS485convertertointerfacethelaunchpadwith
my Laptop.The TTL outputs of TX and RX pins of FT232 are available outside which are then
connected to UCA0RXD and UCA0TXD pins of MSP430G2553 respectivly as shown in the below
table.USB2SERIALboardshas3.3V/5Vsignallevelselectwhichmakesitsafetointerfacedirectly
withMSP430(3.3Vlogic)pins.

While communicating,you should have some sort of terminal program like PuTTY (on PC) for
listeninginonthebytessendbythelaunchpadboard.Open"DeviceManager"onWindowstofind
outtheCOMportnumberofyourvirtualserialportandconfigurePuTTYtocommunicatewiththat
port.IfyouareinterestedinsettingupserialconnectionwithotherOperatingsystemslikeLinux,you
cancheckoutmyserialcommunicationtutorials.PuTTYisavailablleforLinuxalso.

UsingMSP430LaunchpadSerialPort
InLaunchpad,theTUSB3410chip(insidetheemulatorsection)canactasaUSBtoSerialconverter
too.If you check under "Device Manager" you can see that as "MSP430 Application UART
COMxx",highlighted in red (here COM13).The one below that is the COM port number
correspondingtotheUSBtoSerialConverter(USB2SERIAL).

IfyouareaLinuxuser,Launchpadserialport(basedonTUSB3410chip)isdetectedbytheLinuxOS
butIwasunabletocommunicatewithit.

IwouldrecommendanyFT232basedUSBtoserialconverterslikeUSB2SERIALforcommunicating
withLinuxsystem.
Inorder to use the Launchpad Serial port you chould configure the jumpers on the board first.Near
the Emulator section there are 5 jumpers connecting the Emulator section to the MSP430G2553
chip.YoushouldputtheRXDandTXDpinsintheHardwareUARTmode(HWUART)asshownin
thebelowfigure.

Afteryouhaveconfiguredthejumpersintheboard,LaunchPuTTYoranyotherterminalprogramof
your choice.Put the COM port number corresponding to your launchpad serial port (here COM13)
,selectthebaudrate(here9600)andopentheconnection.

Now download the program we discussed above and reset the board.The MSP430 will transmit a
character 'A" which will be dispalyed on the PuTTY terminal as shown in the figure below.(I have
reseted the board a few times). If you press any key on the keyboard,it will be transmitted to the
MSP430boardandtheLEDconnectedtoP1.6willtoggle.

SothereitsiscommunicatingbetweenaPCandaLaunchpaddevelopmentboard.
One problem with serial communication format like RS232 and USB is there limited range,these
standardsarenotsuitablefortransmittingdataoverseveral100'sofmeters.Ifyouwanttotransmit
dataoverlongdistancesusingtwistedpaircablesyouhavetouseRS485protocol.
IfyouareinterestedyoucancheckournextsectionwherewearegoingtointerfaceaMAX485chip
withtheLaunchpadboard.
Tags:
MSP430G2553
Log in or register to post comments

Comments
Nice

Anonymous (not veri ed)


14 December 2015 - 11:14pm
permalink

Nice
Log in or register to post comments

159

Like

3
Tweet

20142016www.xanthium.in.Allrightsreserved.
Alltrademarksandservicemarksarethepropertiesoftheirrespectiveowners.

2016 xanthium enterprises- This is a Free Drupal Theme


Ported to Drupal for the Open Source Community by Drupalizing, a Project of More than (just) Themes. Original design by Simple Themes.

You might also like