Oct 242020
 

https://www.argenox.com/library/msp430/msp430-uart-peripheral-chapter-8/

 

MSP430 UART PERIPHERAL

Physical Interface

From a pinout perspective, UART signals only require one line for unidirectional communications, although two are typically used for bi-directional transmit and receive. Being asynchronous, UART signals don’t require any other clock line because the two UART devices agree on a common baud rate, stop, start and data bits. This makes the receiver capable of decoding the data. UART is connected by crossing the TX and RX lines, as shown below:

Connecting UART between two devices such as an MSP430 and another generic productUART transmits bits sequentially by pulling the UART line low for a fixed amount of time determined by the baud rate. The UART line when idle is high at the I/O level, 3.3V or whatever the VCC of the MSP430 is set. RS232, on the other hand, uses similar signaling to UART but also incompatible voltage levels (the voltages are high and some are negative) that will damage the MSP430. Therefore, never connect an RS232 device directly to the MSP430. Use a MAX232 or similar device to convert between the two signal levels.

The smallest element of transmission is the UART frame or character, which consists of the Start bit/s, data bits, stop bits and optional parity bits, as shown below:

Bit number 1 2 3 4 5 6 7 8 9 10 11
Start bit Start bit Data Bit Data Bit Data Bit Data Bit Data Bit Data Bit Data Bit Data Bit Stop Bit Stop Bit

The start bits alerts the receiver that data is coming. Without it, if the first bit was a ‘1’, it would be seen as an idle line since an idle UART line is also high. The number of data bits is typically 8, but it can be configured for 7 bits as well. Although some UART receivers can use a different number of bits, only 8 or 8 bits are supported by the MSP430. After the data bits stop bits are sent along with an optional parity bit.

MSP430 families contain different peripherals capable of UART communications. Among these are USCI, USART and eUSCI modules. UART can also be generated using timers or even bit-banged. Some peripherals have sophisticated options that we will not cover since they are rarely used.

Configuring the MSP430 for UART operation

We previously covered the issue of signal multiplexing. In order to use UART, specific pins that are connected to the UART module must be used, and the pin muxing must choose the Primary Peripheral mode. On the MSP430G2553 that is on the MSP430 Launchpad, UCA0 pins are present on pins P1.1 and P1.2 as UCA0RXD and UCA0TXD, respectively. Setting the pin muxing is simple since TI provides the information for the bit settings in the datasheet of the device. In this case, UART requires both PxSEL and PxSEL2 to be set to ‘1’. PxDIR does not need to be configured.

Configuring MSP430 Pin Muxing for UART

 
P1SEL |= (BIT1 | BIT2);
P1SEL2 |= (BIT1 | BIT2);


With the pins configured, we must configure the clocks for UART. The baudrate generation requires a clock of a certain precision. Although baudrates vary, 9600 and 115200 baud are the most common and we will focus on them. In order to generate a baud rate, we must feed a large enough clock so that it can be divided by the baud rate generator for the actual baud rate. For 9600 baud we can use both the slow 32.768kHz crystal if one is present, or a faster source such as the internal DCO or an external crystal. 115200 baud requires a fast clock, so only the DCO and a fast external clock can be used. Below we have the code needed to configure USCI_A0 for UART operation at 9600 baud:

Configuring MSP430 Pin Muxing for UART

 
#include <msp430.h>

int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT

/* Use Calibration values for 1MHz Clock DCO*/
DCOCTL = 0;
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;

/* Configure Pin Muxing P1.1 RXD and P1.2 TXD */
P1SEL = BIT1 | BIT2 ;
P1SEL2 = BIT1 | BIT2;

/* Place UCA0 in Reset to be configured */
UCA0CTL1 = UCSWRST;

/* Configure */
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 104; // 1MHz 9600
UCA0BR1 = 0; // 1MHz 9600
UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1

/* Take UCA0 out of reset */
UCA0CTL1 &= ~UCSWRST;

/* Enable USCI_A0 RX interrupt */
IE2 |= UCA0RXIE;

__bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts enabled
}

/* Echo back RXed character, confirm TX buffer is ready first */
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = UCA0RXBUF; // TX -&gt; RXed character
}


The clock source for the UART baud generator is SMCLK sourcing the DCO running at 1MHz. Before we can configure the UART peripheral we need to place it in reset mode. Not all registers require this but it is best to do so when first configuring USCI, whether it’s for UART or any other mode. Notice that we use the assignment operator, so all the other bits are set to zero. With the reset in place, we make SMCLK the clock source for the UART. Being flexible, there are other possible options for the UCSSELx:

  • 00b = UCAxCLK (external USCI clock)
  • 01b = ACLK
  • 10b = SMCLK
  • 11b = SMCLK

UART can actually use a cloc

 Posted by at 6:45 am

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)