Accessing UART in MSP430F5418

UART is the traditional serial communication protocol used. It offers only two pins RX and TX for sending and receiving characters serially. A microcontroller can have more than one UART. For instance MSP430F5418 microcontroller has two UART’s (UART and enhanced UART). Just read the desired controller datasheet for getting the info.

There are two methods of using UART.

  • Polling mode

  • Interrupt mode

Commonly interrupt will be used for reception and polling is used for transmission. Here, I will use both methods. Code used in this article is for MSP430F5418 microcontroller. But, this can be ported to any microcontroller series with minor adjustments. Changes had to be made in the registers and interrupt handlers.

The USCI (Universal Serial Communication Interface) module in MSP430F5418 is responsible for serial communications. It has support for both synchronous protocols (SPI, I2C) and asynchronous protocols (UART, eUART). This controller comes with two USCI modules A and B.

  • USCI A controls UART, IrDA, SPI

  • USCI B controls SPI, I2C

IrDA is Infrared encoder/decoder and eUART is Enhanced UART which supports auto baud rate detection.


Before sending/transmitting through UART there are various things which needs to be done. It includes selecting the RX/TX pins, USCI reset, selecting clock source, determining baud rate and finally enabling the USCI module…Let us look into the software part for more detail.

Configure the Clock source

Select the pins where the crystal is connected, using the PSEL instruction. After that, select the clock source. !!!yep I can your voice, what is the use of clock in asynchronous mode? The reason is that clock source is necessary for the generation of baud rate to synchronize both transmitter and receiver. I had selected SMCLK. You can select as per your wish. The available clocks are SMCLK, ACLK, UCAxCLK.

Here, the clock source is selected as the high frequency crystal oscillator connected to PORT7 pins 0 and 1. As you can see from the code above, the drive strength is selected as per the crystal oscillator frequency. I had used 4MHz crystal, but you can connect up to the maximum allowed frequency.

Note: While using the external crystal it is necessary to check the oscillator fault flags. This will avoid you a lot of troubles. If your fault flag is set even you had connected the crystal properly, do check the soldering point of the crystal. A small dry soldering will spoil your day.

Initializing UART

First step is to select the RX/TX pin on Port3. Using PSEL enables us to select the UART pins as they are also used as general purpose I/O pins. Then, clearing UCSWRST bit in UCA0CTL1 register resets UART. Baud rate can be generated using external clock source (UCAxCLK) and internal clock sources (SMCLK, ACLK) depending upon the selection of UCSSELx bit. Then, the baud rate generator is configured for 9600 baud from 4MHz SMCLK. UCA0MCTL register is used to provide the necessary modulation. Finally, the UCSWRST is reset to initialize the UART module with the selected configuration.

Note: The UCSWRST bit should be set before configuring the USCI registers. Finally, it should be reset to enable UART.

Polling mode

In this mode the interrupts are not used. Instead the interrupt flags are polled in order to transmit and receive. The interrupt flags are always set during TX and RX irrespective of enabling or disabling interrupts.

Transmitting Characters

Transmission can be enabled by writing to UCA0TXBUF. If the buffer is empty UCTXIFG flag will be set, when it is full it will bet reset. Here, the flag is polled untill it is set. Once, it is set the data will be written to the buffer and the data will be transferred to the Transmit shift register then transmitted via TX pin. The TX flag will automatically reset once the data is loaded into buffer and will be set when it is transmitted.

Receiving Characters

Method used for transmission is used for reception also. The UCRXIFG is polled until it is set. When there is data in UCA0RXBUF the flag will be set, once the data is read it will be automatically reset.

Interrupt mode

Using interrupts is the recommended way over polling, because polling causes the time of the controller to be wasted. Hence, interrupts are used to save the processor time. Also, if you were not aware of the timing of the receiving character, using  interrupt will be handy. MSP430F5418 uses same interrupt handler for both transmission and reception. If the TX and RX interrupt are enabled the interrupt request will be generated during transmission and reception.

Uncomment the last two lines in uart_init_9600() function to enable interrupt. It is to be noted that the interrupt is enabled only for reception.

Interrupt handler

When a byte is arrived it will be stored in the UCA0RXBUF buffer, then the interrupt flag will be set and the RX ISR will be called. Inside the ISR, the data byte will be read, then the UCRXIFG flag will be automatically reset.

Delay function

Using the accurate delay in your code is recommended. When it comes to delay, many of us will use the for loop for generating the delay. But, that will not be accurate. TI doesn’t provides any delay function in their libraries. So, I had written a delay function, which will be far more accurate than the delay generated using for loop.

I used for loop too :_)


No Comment

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *