# MSP430 - UART Interface

Sometimes when I am developing and debugging a system I need to monitor some intermediate result. I do this by sending ASCII text serially to a "dumb terminal" such as HyperTerm. Believe me, I have a PC on my workbench running COMM on MSDOS just for this purpose.

I also have a microcontroller based "dumb terminal" using an mcu driving a 2x16 LCD character display. This is what I used when I was testing the LM35 to ADC output.

Here is the code to transmit serial data via the UART:

Code:
// MSP430 - UART Example
// 2013.06.20 - MrChips
#include "io430.h"

#define DELAY 1000
#define CAL      451

#define LF 10
#define CR 13

short v;
char d[5];

void bin2bcd(unsigned short v)
{
short i;
for (i = 0; i < 5; i++)
{
d[i] = v % 10;
v = v/10;
}
}

void init(void)
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
P1OUT = 0;
P1DIR = 0x7F;
}

{
ADC10CTL1 = INCH_7 + CONSEQ_2; // use P1.7 (channel 7)
}

void initUART(void)
{
// initialize USCI = UART
// TXD is on P1.2, select secondary peripheral function
P1SEL_bit.P2 = 1;
P1SEL2_bit.P2 = 1;
UCA0CTL1 |= UCSSEL_2;
UCA0BR0 = 104;                // set for 9600 baud
UCA0BR1 = 0;
UCA0CTL1_bit.UCSWRST = 0; // release RESET
}

void putc(unsigned char c)
{
while (UCA0STAT_bit.UCBUSY);
UCA0TXBUF = c;
}

void delay(unsigned long d)
{
unsigned long i;
for (i = 0; i < d; i++);
}

void main( void )
{

init();
initUART();

while (1)
{

v = v * CAL;
v = v >> 7;

bin2bcd(v);

putc(d[4] + '0');
putc(d[3] + '0');
putc(d[2] + '0');
putc(d[1] + '0');
putc('.');
putc(d[0] + '0');
putc(CR);
putc(LF);

delay(DELAY);

}

}
Notes:

1) The UART on the MSP430G2553 is named UCA0. Transmit Data, TXD is on port P1.2, pin-4. The UART is configured for 8 bits, no parity, one stop bit, 9600 baud.

2) The TXD signal is a logic level voltage 0 to 3.6V. You will need a RS-232 driver chip in order to generate RS-232 compatible voltages.

3) You can slow down the output repetition rate by increasing the value of DELAY. The current rate is about 60 times per second.

4) I have written the code to send one character at a time using the function putc( ). Later I will show a puts( ) function to output a character string.

This leads to the next project: A "dumb terminal" example using the MSP430, LCD and UART.

Author
MrChips
Views
3,128
Last update