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.

MSP430FR2311: UART Commincation: Unable to Transmit Serial Data

Part Number: MSP430FR2311


Hey, 

I am new to embedded programming and I want to set up UART communication on the MSP430FR2311 to send data to my serial port. I have attached code below and basically I am trying to using the TX buffer register: UCA0TXBUF to read a value of 2 I assigned to TX. However, when I open my COM port on my computer at a baud rate of 4800 I don't see a value of 2. What do I need to do in order to see that value? 

#include <msp430.h>
#include <stdint.h>

unsigned char TX = 2;
void Init_GPIO();


int main(void)
{
  WDTCTL = WDTPW | WDTHOLD;                 // Stop watchdog timer

  // Configure GPIO
  Init_GPIO();

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

  // Configure UART pins
  P1SEL0 |= BIT6 | BIT7;                    // set 2-UART pin as second function

  // Configure UART
  UCA0CTLW0 |= UCSWRST;                     //Sets softare reset enable
  UCA0CTLW0 |= UCSSEL_1;               // set SMCLK as BRCLK

  // Changing baud rate to 115200
  // Baud Rate calculation. Referred to UG 17.3.10
  // (1) N=32768/4800=6.827
  // (2) OS16=0, UCBRx=INT(N)=6
  // (4) Fractional portion = 0.827. Refered to UG Table 17-4, UCBRSx=0xEE.
  UCA0BR0 = 6;                              // INT(32768/4800)
  UCA0BR1 = 0x00;
  UCA0MCTLW = 0xEE00;

  // UCA0BR0 = 8;                              // 1000000/115200 = 8.68
  // UCA0MCTLW = 0xD600;                       // 1000000/115200 - INT(1000000/115200)=0.68
  // UCA0BR1 = 0x00;                              // UCBRSx value = 0xD6 (See UG)

  UCA0CTLW0 &= ~UCSWRST;                    // Initialize eUSCI
  UCA0IE |= UCRXIE;                         // Enable USCI_A0 RX interrupt

  __bis_SR_register(LPM3_bits|GIE);         // Enter LPM3, interrupts enabled
  __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:
      while(!(UCA0IFG&UCTXIFG));
      UCA0TXBUF = TX;
      __no_operation();
      break;
    case USCI_UART_UCTXIFG: break;
    case USCI_UART_UCSTTIFG: break;
    case USCI_UART_UCTXCPTIFG: break;
    default: break;
  }
}

void Init_GPIO()
{
    P1DIR = 0xFF; P2DIR = 0xFF;
    P1REN = 0xFF; P2REN = 0xFF;
    P1OUT = 0x00; P2OUT = 0x00;
}

  • Hey Mani,

    You should never set UCA0RXBUF to a value since it contains the information received through the UCA0RXD pin. You don't see UCA0RXBUF assigned to the value of RXData because you are never entering the USCI ISR, which occurs when valid information is received into UCA0RXBUF and sets the UCRXIFG (you have RXIE and GIE enabled). This can only come through the UCA0RXD pin. I recommend that you further review the UART examples provided by TI.

    Regards,
    Ryan
  • Hey Ryan, thanks for the reply. So if I attach GND that is on the launchpad to the UCA0RXD pin (on my MPS430FR2311 RX is Pin 1.6). If i attach a jumper cable from GND to Pin 1.6 will I be able to receive serial data? I tried using the code provided in on of the examples but I was wondering how I would change it. I tried changing Pin 1.6 to an input by setting P1DIR = 0xDF instead of 0xFF but it didnt work as well. Any help would be appreciated thanks. How do I read the value of Pin 1.6 so that it could be set to UCA0RXBUF and transmitted via the serial port. 

    #include <msp430.h>
    #include <stdint.h>
    
    //volatile uint8_t RxData = 8;
    void Init_GPIO();
    
    
    int main(void)
    {
      WDTCTL = WDTPW | WDTHOLD;                 // Stop watchdog timer
    
      // Configure GPIO
      Init_GPIO();
    
      PM5CTL0 &= ~LOCKLPM5;                     // Disable the GPIO power-on default high-impedance mode
                                                // to activate 1previously configured port settings
    
      // Configure UART pins
      P1SEL0 |= BIT6 | BIT7;                    // set 2-UART pin as second function
    
      // Configure UART
      UCA0CTLW0 |= UCSWRST;                     //Sets softare reset enable
      UCA0CTLW0 |= UCSSEL_1;               // set SMCLK as BRCLK
    
      // Changing baud rate to 115200
      // Baud Rate calculation. Referred to UG 17.3.10
      // (1) N=32768/4800=6.827
      // (2) OS16=0, UCBRx=INT(N)=6
      // (4) Fractional portion = 0.827. Refered to UG Table 17-4, UCBRSx=0xEE.
      UCA0BR0 = 6;                              // INT(32768/4800)
      UCA0BR1 = 0x00;
      UCA0MCTLW = 0xEE00;
    
      // UCA0BR0 = 8;                              // 1000000/115200 = 8.68
      // UCA0MCTLW = 0xD600;                       // 1000000/115200 - INT(1000000/115200)=0.68
      // UCA0BR1 = 0x00;                              // UCBRSx value = 0xD6 (See UG)
    
      UCA0CTLW0 &= ~UCSWRST;                    // Initialize eUSCI
      UCA0IE |= UCRXIE;                         // Enable USCI_A0 RX interrupt
    
      __bis_SR_register(LPM3_bits|GIE);         // Enter LPM3, interrupts enabled
      __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:
          while(!(UCA0IFG&UCTXIFG));
          UCA0TXBUF = UCA0RXBUF;
          __no_operation();
          break;
        case USCI_UART_UCTXIFG: break;
        case USCI_UART_UCSTTIFG: break;
        case USCI_UART_UCTXCPTIFG: break;
        default: break;
      }
    }
    
    void Init_GPIO()
    {
        P1DIR = 0xFF; P2DIR = 0xFF;
        P1REN = 0xFF; P2REN = 0xFF;
        P1OUT = 0x00; P2OUT = 0x00;
    }
    

  • Use the following example code and connect P1.6 to P1.7: 

    /* --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--*/
    //******************************************************************************
    //   MSP430FR231x 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.
    //
    //                MSP430FR2311
    //             -----------------
    //         /|\|                 |
    //          | |                 |
    //          --|RST              |
    //            |                 |
    //            |                 |
    //            |     P1.7/UCA0TXD|----
    //            |                 |   |
    //            |     P1.6/UCA0RXD|----
    //            |                 |
    //            |            P1.0 |--> LED
    //            |                 |
    //
    //   Darren Lu
    //   Texas Instruments Inc.
    //   Oct. 2015
    //   Built with IAR Embedded Workbench v6.30 & Code Composer Studio v6.1
    //******************************************************************************
    #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 |= BIT6 | BIT7;                    // 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, Ryan

  • Hi, Ryan I am confused on some other things as well. In this example code unsigned char RXData = 0; unsigned char TXData = 1;
    Does this indicate that the RXData sent to the MSP430 is always 0 and the TXData is also 0 so the LED doesnt turn on? Also, why do we connect pins 1.6 and 1.7. I am still confused on how this UART communication works for this code. I opened up a terminal and i sent random data values and the LED didn't turn on but when I disconnected the two pins, it turned on after i sent something. Is RXdata 0 or what I was sending?
  • No at all, it just initializes the variables. RxData will change to 1 once UCA0RXBUF receives the value from the UCA0TXBUF and assigns it accordingly in the USCI ISR. The LED is not meant to come on so long as RXData and TXData match, which is the case as the example endlessly sends a 1 through TXBUF which is properly received by RXBUF.

    But then you disconnected P1.6 from P1.7 and instead connected the terminal to P1.6 which sent a random value to force the UCRXIFG and call the ISR, since this received value from UCA0RXBUF is now not 1 then RXData != TXData and the LED is set and forces a forever while loop to trap the CPU.

    Regards,
    Ryan

**Attention** This is a public forum