This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/MSP430FR6989: UART Comm. Reading and writing a string to terminal

Part Number: MSP430FR6989

Tool/software: Code Composer Studio

Hi, Im trying to write a string to the terminal, however the output gets stuck in a bunch of random characters (garbage). To try and prevent this I tried different way of dealing with the string. 

I tried: 

1) Using a string buffer.

2) Using an specific amount of characters, then setting a null terminator after. 

3) Using 2 and 3.

4) Simply creating a string (i.e string = "random")

I really dont know how I can stop this stream of random characters being read by my function. Why is the null terminator not being read by my function?

Here's my code:

#include <msp430fr6989.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define redLED BIT0 // Red at P1.0
#define greenLED BIT7 // Green at P9.7
#define BUT1    BIT1 // Button S1 at P1.1
#define BUT2    BIT2 // Button S2 at P1.2

#define FLAGS UCA1IFG // Contains the transmit & receive flags
#define RXFLAG UCRXIFG // Receive flag
#define TXFLAG UCTXIFG // Transmit flag
#define TXBUFFER UCA1TXBUF // Transmit buffer
#define RXBUFFER UCA1RXBUF // Receive buffer

void Initialize_UART(void);
void uart_write_char(unsigned char ch);
unsigned char uart_read_char(void);
void uart_write_unit16(unsigned int n);
void uart_write_string(char *str);

int main(void)
{
    WDTCTL = WDTPW | WDTHOLD; // Stop WDT
    PM5CTL0 &= ~LOCKLPM5; // Enable GPIO pins

    P1DIR |= redLED;
    P1OUT &= ~redLED;
    P9DIR |= greenLED;
    P9OUT &= ~greenLED;

    int i;
    char newLine = '\n';
    char carriage = '\r';
    char *myStringBuffer;
    // 5 + 1 to add a null terminator
    myStringBuffer = malloc(sizeof(char)*(5 + 1));
    // Using strcpy
    strcpy(myStringBuffer, "abcde");
    // Adding the null terminator
    myStringBuffer[5] = '\0';

    // eUSCI Module Configuration
    Initialize_UART();

    // Transmit string myString
    for (i = 0; i < 30000; i++){}
    P1OUT |= redLED;
    uart_write_string(myStringBuffer);
    uart_write_char(newLine);
    uart_write_char(carriage);
    for (i = 0; i < 30000; i++){}
    P1OUT &= ~redLED;

    while (1)
    {
        if (uart_read_char() == '1')
        {
            P9OUT ^= greenLED;
        }

        if (uart_read_char() == '2')
        {
            P9OUT ^= greenLED;
        }

    }

}


// Configure UART to the popular configuration
// 9600 baud, 8-bit data, LSB first, no parity bits, 1 stop bit
// no flow control
// Initial clock: SMCLK @ 1.048 MHz with oversampling
void Initialize_UART(void){
    // Divert pins to UART functionality
    P3SEL1 &= ~(BIT4|BIT5);
    P3SEL0 |= (BIT4|BIT5);

    // Use SMCLK clock; leave other settings default
    UCA1CTLW0 |= UCSSEL_2;

    // Configure the clock dividers and modulators
    // UCBR=6, UCBRF=13, UCBRS=0x22, UCOS16=1 (oversampling)
    UCA1BRW = 6;
    UCA1MCTLW = UCBRS5|UCBRS1|UCBRF3|UCBRF2|UCBRF0|UCOS16;

    // Exit the reset state (so transmission/reception can begin)
    UCA1CTLW0 &= ~UCSWRST;
}

// Transmit a byte over UART
// When byte is copied to trasmit buffer, transmit flag = 0 automatically
// When transmission finishes, the transmit flag goes back to one
void uart_write_char(unsigned char ch){

    // Wait for any ongoing transmission to complete
    while ( (FLAGS & TXFLAG)==0 ) {}

    // Write the byte to the transmit buffer
    TXBUFFER = ch;
}

// Receives a byte over UART
// Returns the byte; if none received, returns NULL
// Byte is received, the receive flag becomes 1
// When copy the byte from receive buffer, the receive flag = 0 automatically
unsigned char uart_read_char(void){

    unsigned char temp;

    // Return NULL if no byte received
    if( (FLAGS & RXFLAG) == 0)
        return '\0';

    // Otherwise, copy the received byte (clears the flag) and return it
    temp = RXBUFFER;

    return temp;
}

void uart_write_unit16(unsigned int n)
{
    int digit;
    // 5 digits
    if (n > 9999)
    {
        digit = n / 10000;
        uart_write_char('0' + digit);
    }
    // 4 digits
    if (n > 999)
    {
        digit = (n / 1000) % 10;
        uart_write_char('0' + digit);
    }
    // 3 digits
    if (n > 99)
    {
        digit = (n / 100) % 10;
        uart_write_char('0' + digit);
    }
    // 2 digits
    if (n > 9)
    {
        digit = (n / 10) % 10;
        uart_write_char('0' + digit);
    }

    digit = n % 10;
    uart_write_char('0' + digit);
    return;
}

void uart_write_string(char *str)
{
    int i;
    for (i = 0; *str != '\0'; i++)
        uart_write_char(str[i]);

    return;
}

**Attention** This is a public forum