Serial Communication using MSP430 UART(USCI_A)

Pinterest icon

In MSP430 serial communication is handled by an on chip peripheral called USCI (Universal Serial Communications Interface​).The peripheral is designed in such a way that it can handle multiple serial communication formats ,synchronous as well as asynchronous like SPI,I2C,IrDA,UART etc.MSP430G2553 has two USCI modules names as USCI_A0 and USCI_B0 for handling multiple communication formats. USCI_A0 can be configured to handle LIN,IrDA,SPI and asynchronous serial communication (UART) while USCI_B0 can handle SPI and I2C.

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​) and receive data(UCA0RXD​) .Asynchronous serial communication is widely used for communicating with external devices like PC/Laptops,GSM modems,GPS modules etc.It also forms the basis for many standard protocols like RS232,RS422,RS485 etc.

You can see the (very) simplified block diagram of USCI_A0 in UART mode below.

Block diagram of USCI_A0 in UART mode

In this example we are going to configure the MSP430 UART to transmit and receive at 9600bps with 8 databits,no Parity and 1 Stop bit .In the above figure you can see that the USCI can source clock from either SMCLK or ACLK to generate BRCLK which is then used to generate the required timings.UC0CLK is external clock sourced from outside through MSP430 pins.

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 system register and DCO control registers like this

if (CALBC1_1MHZ == 0xFF) // If calibration constant erased
{    
    while(1);            // do not load, trap CPU!!

DCOCTL  = 0;             // Select lowest DCOx and MODx settings
BCSCTL1 = CALBC1_1MHZ;   // Set range
DCOCTL  = CALDCO_1MHZ;   // Set DCO step + modulation 

CALBC1_1MHZ is a constant defined in the header file msp430g2553.h,inside the header file you can find the constants for configuring the DCO to operate at 1,8,12 and 6MHz (CALBC1_1MHZ,CALBC1_8MHZ,CALBC1_12MHZ,CALBC1_16MHZ).

 

Registers Required for UART 
After configuring the system clock to 1MHz, we are going to look into the registers and bits that are important for configuring the UART.Please note that only relevant registers and bits will be explained .

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),character length,no of stop bits,modes of serial transmission.

The  important bits are

UCPEN Used for enabling or disabling parity (In our case no parity so UCPEN = 0)
UCPAR Used for selecting between EVEN or ODD parity (not used since we are disabling parity)
UCMSB Controls the direction of receive and transmit shift register(here LSB first)
UC7BIT Selects the character length 7/8 bit(we are using 8 bit so 0)
UCSPB Number of stopbits (here 1 so UCSPB = 0)
UCMODEx 2 bits used to select the asynchronous mode when UCSYNC = 0.Here we are using the default UART mode.Other modes are used with multiprocessor serial communication and for automatic baudrate detection 
UCSYNC Selecting between asynchronous(UART) and synchronous modes(SPI)

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

 

2) UCAxCTL1 here  UCA0CTL1-The two important bits contained in this register are 

UCSSELx These bits are used to select the clock source to the USCI module.In our case we are going to select SMCLK @1MHz.
UCSWRST This bit is used to put the USCI module in reset state .It is recommended to put the USCI module in reset state before making any changes to the registers.

 

3) UCAxSTAT here  UCA0STAT-The two important bits contained in this register are

UCLISTEN This bit is used to select the internal loop back mode.In loop back mode UCAxTXD is internally fed back to the receiver.
UCBUSY  Indicates if a transmit or receive operation is in progress.

4) UCAxBR0 (UCA0BR0) and UCAxBR1 (UCA0BR1) are two  8 bit registers which are used to set the clock prescalar value for the Baud rate generator.

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

6) UCAxRXBUF (UCA0RXBUF) 8 bit data register that stores the received byte.

 

Configuring the Baud rate 
MSP430 data sheet has an indepth description about configuring the baud rate for various modes.Easiest  way to configure the baud rate of a MSP430 device is to use the Baud rate selection table listed in the usermanual.Just find out the UCBRx values corrosponding to your BRCLK frequency and the baud rate you wish to use .After finding out values just put them in the UCA0BR0 and UCA0BR1 register to configure the baud rate.

In our case we are using SMCLK @ 1MHz as BRCLK and 9600bps as the baud rate .Now check the table for the corresponding UCBRx value (here 104) and put them in UCA0BR0 and UCA0BR1 registers to configure the baud rate.You can find the below table in the MSP430x2xxx usermanual.

Baud rate selection table for MSP430G2xxx microcontrollers from TI

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 

Interrupts of MSP430 UART
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 Used to enable/disable the transmit interrupt. 
UCA0RXIE Used to enable/disable the receive interrupt.

IFG2

UCA0TXIFG USCI_A0 transmit interrupt flag is set when UCA0TXBUF is empty
UCA0RXIFG USCI_A0 receive interrupt flag is set when UCA0RXBUF have received a complete character.you should remember to clear the flag inside the interrupt service routine manually.

 

Writing the Code 
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 transmitter is internally connected to the input of the receiver,so what ever the MSP430 transmits is received back.

In this example we are going to write simple communication program which uses the loop back mode and communicates at 9600bps (8N1).Here I am using IAR embedded workbench for MSP430 from IAR.If you are not familiar with IAR EW you can check out a short tutorial here .

The code given below will configure the UART and transmit an ascii character 'A' .The UART is configured in loop back mode so 'A' is received back and is stored in UCA0RXBUF.LED connected to P1.0 is lighted when 'A' is transmitted and LED connected to P1.6 is lighted when 'A' is received back. 

#include "msp430g2553.h"
void main(void)
{
  WDTCTL = WDTPW + WDTHOLD; // Stop the Watch dog
     
  //------------------- Configure the Clocks -------------------//
        
  if (CALBC1_1MHZ==0xFF)   // If calibration constant erased
     {    
        while(1);          // do not load, trap CPU!!
     } 

   DCOCTL  = 0;             // Select lowest DCOx and MODx settings
   BCSCTL1 = CALBC1_1MHZ;   // Set range
   DCOCTL  = CALDCO_1MHZ;   // Set DCO step + modulation 
      
  //---------------- Configuring the LED's ----------------------//
      
   P1DIR  |=  BIT0 + BIT6;  // P1.0 and P1.6 output
   P1OUT  &= ~BIT0 + BIT6;  // P1.0 and P1.6 = 0
      
  //--------- Setting the UART function for P1.1 & P1.2 --------//
      
   P1SEL  |=  BIT1 + BIT2;  // P1.1 UCA0RXD input
   P1SEL2 |=  BIT1 + BIT2;  // P1.2 UCA0TXD output
      
  //------------ Configuring the UART(USCI_A0) ----------------//
      
   UCA0CTL1 |=  UCSSEL_2 + UCSWRST;  // USCI Clock = SMCLK,USCI_A0 disabled
   UCA0BR0   =  104;                 // 104 From datasheet table-  
   UCA0BR1   =  0;                   // -selects baudrate =9600,clk = SMCLK
   UCA0MCTL  =  UCBRS_1;             // Modulation value = 1 from datasheet
   UCA0STAT |=  UCLISTEN;            // loop back mode enabled 
   UCA0CTL1 &= ~UCSWRST;             // Clear UCSWRST to enable USCI_A0
      
  //---------------- Enabling the interrupts ------------------//
      
   IE2 |= UCA0TXIE;                  // Enable the Transmit interrupt
   IE2 |= UCA0RXIE;                  // Enable the Receive  interrupt
   _BIS_SR(GIE);                     // Enable the global interrupt
      
   UCA0TXBUF = 'A';                  // Transmit a byte 
      
   _BIS_SR(LPM0_bits + GIE);         // Going to LPM0
}

  //-----------------------------------------------------------------------//
  //                Transmit and Receive interrupts                        //
  //-----------------------------------------------------------------------//

  #pragma vector = USCIAB0TX_VECTOR
  __interrupt void TransmitInterrupt(void)
  {
    P1OUT  ^= BIT0;//light up P1.0 Led on Tx 
  }

  #pragma vector = USCIAB0RX_VECTOR
  __interrupt void ReceiveInterrupt(void)
  {
    P1OUT  ^= BIT6;     // light up P1.6 LED on RX 
    IFG2 &= ~UCA0RXIFG; // Clear RX flag 
  }

After the WDT is stopped .The DCO is configured to produce clock at 1MHz using the calliberation constants.The ports P1.0 and P1.6 are configured as outputs to light up the respective LED' during transmission and reception.

In MSP430G2553, P1.1 and P1.2 are configured as UCA0RXD and UCA0TXD using their respective PXSEL registers.The clock for USCI_A0 in UART mode is sourced from SMCLK and other parameters are configured.

The UART module is configured to act in loop back mode using  UCA0STAT |=  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,Transmit interrupt is generated which lights up the P1.0 LED.

The character is received back UCA0RXBUF and a recceive interrupt is generated which lights up the P1.6 LED.

You should remember to clear the UCA0RXIFG flag inside the ISR  manually.

After you have successfully run the above program in Launchpad you can remove the loop back mode by commenting out UCA0STAT |=  UCLISTEN; .After that line is removed, RXD and TXD pins are now routed to P1.1 and P1.2.You can connect P1.1 and P1.2 and run the program,the two LED connected to P1.0 and P1.6 should light up. 

Image showing how to connect P1.1 and P1.2 pins of MSP430 uart on a launch pad board

 

Interfacing MSP430 UART with the Real World
After you have managed to successfully communicate with the launchpad, its time to interface the launchpad to external devices like a PC ,GSM modem or a GPS module which supports a UART.In this example we will interface the MSP430 launchpad with a PC/Laptop. 

One way to connect your launchpad to a PC is to use standard DB9 RS232 serial port,one problem with DB9 port is that it has become legacy hardware and most of the newer PC's do not have it.When you are connecting a PC with a microcontroller board like Launchpad ,you have to use a Null Modem Connection.

In a null modem connection the TXD pin from PC is connected to the RXD pin of the microcontroller (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 microcontroller and vice versa. 

Now when you are interfacing with a PC RS232 serial port ,you require atleast 3 lines RXD ,TXD and 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.You have to build a RS232 level converter(for eg MAX232) to convert between  RS232 and TTL signal levels.The below figure shows the connection diagram between an MSP430 Launch pad and PC DB9 serial port.You can see that UCA0RXD (Blue Wire) the receive pin of microcontroller is connected to the Transmit pin of PC serial port. 

 

interfacing msp430 launchpad with PC DB9 serial port using RS232 protocol

As I have already mentioned DB9 ports are being replaced with USB ports in most PC's and Laptops,So another way to interface the Launchpad with PC is by using an USB to Serial converter board.USB to Serial Converter boards provide a virtual serial port which you can read and write like a standard serial port.When you read or write to the virtual serial port,the data you send is then passed theough the standard USB port to the Converter chip (eg FT232) which translates to serial data stream.

Her I am using USB2SERIAL USB to Serial/RS232/RS485 converter to interface the launchpad with 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 .USB2SERIAL boards has 3.3V /5V signal level select which makes it safe to interface directly with MSP430 (3.3V logic) pins.

MSP430G2553 INTERFACED WITH PC USING USB TO SERIAL CONVERTER

While communicating,you should have some sort of terminal program like PuTTY (on PC) for listening in on the bytes send by the launchpad board.Open "Device Manager" on Windows to find out the COM port number of your virtual serial port and configure PuTTY to communicate with that port.If you are interested in setting up serial connection with other Operating systems like Linux , you can check out my serial communication tutorials.PuTTY is availablle for Linux also.

Using MSP430 Launchpad Serial Port 
In Launchpad, the TUSB3410 chip( inside the emulator section) can act as a USB to Serial converter 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 corresponding to the USBto Serial Converter (
USB2SERIAL).

Device manager showing the COM port number corresponding to the launchpad development board

If you are a Linux user,Launchpad serial port (based on TUSB3410 chip) is detected by the Linux OS but I was unable to communicate with it.

launch pad UART based on TUSB3410 detected by Linux

I would recommend any FT232 based USB to serial converters like USB2SERIAL for communicating with Linux system.

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.You should put the RXD and TXD pins in the Hardware UART mode (HW UART ) as shown in the below figure.

configuring the launchpad jumpers to use the launch pad uart

After you have configured the jumpers in the board ,Launch PuTTY or any other terminal program of your choice.Put the COM port number corresponding to your launchpad serial port (here COM13) ,select the baudrate (here 9600) and open the connection.

configuring putty to use the launchpad development board

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 MSP430 board and the LED connected to P1.6 will toggle.

putty displaying received characters

So there its is communicating between a PC and a Launchpad development board.

One problem with serial communication format like RS232 and USB is there limited range,these standards are not suitable for transmitting data over several 100's of meters.If you want to transmit data over long distances using twisted pair cables you have to use RS485 protocol.

If you are interested you can check our next section where we are going to interface a MAX485 chip with the Launchpad board.

Comments

Nice

hello,

I've done everything as is written. Both leds lights up, but when I try to communicate with my pc using the Emulator of the launchpad, I can not see the "A" in putty. I already moved the jumpers as you said and connected RX and TX together. I can connect with through putty but can't see the results. Any hint?

I loaded this program along with every serial communications test I could find. None of them allow communications. I believe this is a driver issue of some kind.