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/MSP430FR2512: How to get my UART communication working

Part Number: MSP430FR2512

Tool/software: Code Composer Studio

Hi I'm not sure which part of my code is wrong as I cannot get the Uart to communicate through USB serial connection into the tera term software for it to display the hello world. is it I set the wrong baud rate. or the wrong pin and register? im new to this firmware coding thing... 

#include <stdio.h>
#include <msp430fr2512.h>
#define TXLED BIT0
#define RXLED BIT6
#define TXD BIT0
#define RXD BIT1
const char string[] = { "Hello World\r\n" };
unsigned int i; //Counter

/**
* hello.c
*/


int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
// DCOCT_L = 0; // Select lowest DCOx and MODx settings<
// BCSCTL1 = CALBC1_1MHZ; // Set DCO //******************************not sure about this
// DCORSEL = CALDCO_1MHZ;

// Disable the GPIO power-on default high-impedance mode to activate
// previously configured port settings
PM5CTL0 &= ~LOCKLPM5;

P2DIR |= TXD; // All P2.x outputs<
P2DIR &= RXD;

P2OUT &= 0x00; // All P2.x reset
P2SEL0 |= RXD + TXD ; // P2.1 = RXD, P2.0=TXD
P2SEL1 &= ~RXD ; // P2.1 = RXD, P2.0=TXD
P2SEL1 &= ~TXD ;
P1DIR |= RXLED + TXLED;
P1OUT &= 0x00;

UCA0CTLW0_L |= UCSSEL_2; // SMCLK //edited this to add the _L cuz 8 bit previously 16BIT
UCA0BR0 = 0xF7; // 1MHz 115200
UCA0BR1 = 0x00; //

UCA0MCTLW = UCBRS2 + UCBRS0; // Modulation UCBRSx = 5
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**

UCA0IE |= UCRXIE; // Enable USCI_A0 RX interrupt
UCA0IE |= UCTXIE;

// __bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ int until Byte RXed
while (1)
{
UCA0TXBUF = string[i++]; // TX next character
if (i == sizeof string - 1) // TX over?
i=0;
//UCA0IE &= ~UCTXIE; // Disable USCI_A0 TX interrupt
return 0;
}
}

#pragma vector=USCI_A0_VECTOR
__interrupt void USCI_A0_ISR(void)
{
P1OUT |= RXLED;
if(UCA0IFG&UCRXIFG)
{
if (UCA0RXBUF == 'a') // 'u' received?
{
i = 0;
UCA0IE |= UCTXIE; // Enable USCI_A0 TX interrupt
UCA0TXBUF = string[i++];
}
}
else if(UCA0IFG&UCTXIFG)
{
UCA0TXBUF = string[i++]; // TX next character
if (i == sizeof string - 1) // TX over?
UCA0IE &= ~UCTXIE; // Disable USCI_A0 TX interrupt
}

P1OUT &= ~RXLED;
}

//#pragma vector=USCI_A0TX_VECTOR
//__interrupt void USCI_A0TX_ISR(void)
//{
// P1OUT |= TXLED;
// UCA0TXBUF = string[i++]; // TX next character
// if (i == sizeof string - 1) // TX over?
// UCA0IE &= ~UCTXIE; // Disable USCI_A0 TX interrupt
// P1OUT &= ~TXLED;
//}

  • Hi Yu,

    Yu Hong Wu said:
    Hi I'm not sure which part of my code is wrong as I cannot get the Uart to communicate through USB serial connection into the tera term software for it to display the hello world. is it I set the wrong baud rate. or the wrong pin and register? im new to this firmware coding thing... 

    I would suggest starting with our one of our UART code examples. Don't make any changes, and it should work. Then, you can use what you've shared above and incrementally add additional code/functionality piece-by-piece. If you make too many changes at once, it's much more challenging to figure out what caused the issue whereas if a single change causes an issue, you know exactly what happened.

    MSP430FR2512 Peripheral Examples

    I'd also recommend reading through the Solutions to Common eUSCI and USCI Serial Communication Issues on MSP430™ MCUs app note. Also, for future posts, please add your code using the context highlighter button shown below. Thanks.

    MSP430FR2xxx_euscia0_uart_03.c

    /* --COPYRIGHT--,BSD_EX
     * Copyright (c) 2014, Texas Instruments Incorporated
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * *  Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     *
     * *  Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     *
     * *  Neither the name of Texas Instruments Incorporated nor the names of
     *    its contributors may be used to endorse or promote products derived
     *    from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     *
     *******************************************************************************
     * 
     *                       MSP430 CODE EXAMPLE DISCLAIMER
     *
     * MSP430 code examples are self-contained low-level programs that typically
     * demonstrate a single peripheral function or device feature in a highly
     * concise manner. For this the code may rely on the device's power-on default
     * register values and settings such as the clock configuration and care must
     * be taken when combining code from several examples to avoid potential side
     * effects. Also see www.ti.com/grace for a GUI- and www.ti.com/msp430ware
     * for an API functional library-approach to peripheral configuration.
     *
     * --/COPYRIGHT--*/
    //******************************************************************************
    //   MSP430FR2422 Demo - USCI_A0 External Loopback test @ 115200 baud
    //
    //  Description: This demo connects TX to RX of the MSP430 UART
    //  The example code shows proper initialization of registers
    //  and interrupts to receive and transmit data. If data is incorrect P1.0 LED is
    //  turned ON.
    //  ACLK = n/a, MCLK = SMCLK = BRCLK = DCODIV ~1MHz.
    //
    //                MSP430FR2422
    //             -----------------
    //         /|\|                 |
    //          | |                 |
    //          --|RST              |
    //            |                 |
    //            |                 |
    //            |     P1.4/UCA0TXD|----
    //            |                 |   |
    //            |     P1.5/UCA0RXD|----
    //            |                 |
    //            |            P1.0 |--> LED
    //            |                 |
    
    //   Ling Zhu
    //   Texas Instruments Inc.
    //   May 2017
    //   Built with IAR Embedded Workbench v6.50 & Code Composer Studio v7.0.0
    //******************************************************************************
    #include <msp430.h>
    
    unsigned char RXData = 0;
    unsigned char TXData = 1;
    
    int main(void)
    {
        WDTCTL = WDTPW | WDTHOLD;                 // Stop watchdog timer
    
        PM5CTL0 &= ~LOCKLPM5;                     // Disable the GPIO power-on default high-impedance mode
                                                  // to activate previously configured port settings
        P1DIR |= BIT0;
        P1OUT &= ~BIT0;                           // P1.0 out low
    
        // Configure UART pins
        P1SEL0 |= BIT4 | BIT5;                    // set 2-UART pin as second function
    
        // Configure UART
        UCA0CTLW0 |= UCSWRST;                     // Put eUSCI in reset
        UCA0CTLW0 |= UCSSEL__SMCLK;               
        // Baud Rate calculation
        UCA0BR0 = 8;                              // 1000000/115200 = 8.68
        UCA0MCTLW = 0xD600;                       // 1000000/115200 - INT(1000000/115200)=0.68
                                                  // UCBRSx value = 0xD6 (See UG)
        UCA0BR1 = 0;
        UCA0CTLW0 &= ~UCSWRST;                    // Initialize eUSCI
        UCA0IE |= UCRXIE;                         // Enable USCI_A0 RX interrupt
    
        while (1)
        {
            while(!(UCA0IFG & UCTXIFG));
            UCA0TXBUF = TXData;                   // Load data onto buffer
    
            __bis_SR_register(LPM0_bits|GIE);     // Enter LPM0
            __no_operation();                     // For debugger
        }
    }
    
    #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
    #pragma vector=USCI_A0_VECTOR
    __interrupt void USCI_A0_ISR(void)
    #elif defined(__GNUC__)
    void __attribute__ ((interrupt(USCI_A0_VECTOR))) USCI_A0_ISR (void)
    #else
    #error Compiler not supported!
    #endif
    {
        switch(__even_in_range(UCA0IV,USCI_UART_UCTXCPTIFG))
        {
            case USCI_NONE: break;
            case USCI_UART_UCRXIFG:
                  UCA0IFG &=~ UCRXIFG;            // Clear interrupt
                  RXData = UCA0RXBUF;             // Clear buffer
                  if(RXData != TXData)            // Check value
                  {
                      P1OUT |= BIT0;              // If incorrect turn on P1.0
                      while(1);                   // trap CPU
                  }
                  TXData++;                             // increment data byte
                  __bic_SR_register_on_exit(LPM0_bits); // Exit LPM0 on reti
                  break;
            case USCI_UART_UCTXIFG: break;
            case USCI_UART_UCSTTIFG: break;
            case USCI_UART_UCTXCPTIFG: break;
        }
    }

    Regards,

    James

  • Hi james, but my chip is MSP430FR2512 is it compatible with this FR2422

  • Those projects should run fine. You'll need to just check the device settings in the CCS project.

    Regards,

    James