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.

MSP-EXP430FR2311: UART Baudrate configuration

Part Number: MSP-EXP430FR2311

I am having trouble setting up for a baud rate of 9600 to communicate from my msp430 to a zigbee module, I am getting garbage data on the other module. I have my xbee module connected to pins 1.6 & 1.7, 3.3V and GND. 
Any help on what the UCA0MCTL2 value should be set to, or anything else that maybe an issue?

Thanks


#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__ACLK; // Baud Rate calculation UCA0BR0 = 3; // 32768/9600 = 3.41333 UCA0MCTLW = 0x06; // 3.41333 - INT(3.4133)=0.4133 // UCBRSx value = ? (not sure) 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; } }

  • Hi Sparrow,

    Did you check this example code whether can work on your board?  

    /* --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 -  eUSCI_A0 UART echo at 9600 baud using BRCLK = 8MHz.
    //
    //  Description: This demo echoes back characters received via a PC serial port.
    //  SMCLK/ DCO is used as a clock source and the device is put in LPM3
    //  The auto-clock enable feature is used by the eUSCI and SMCLK is turned off
    //  when the UART is idle and turned on when a receive edge is detected.
    //  Note that level shifter hardware is needed to shift between RS232 and MSP
    //  voltage levels.
    //
    //  The example code shows proper initialization of registers
    //  and interrupts to receive and transmit data.
    //  To test code in LPM3, disconnect the debugger.
    //
    //  ACLK = REFO = 32768Hz, MCLK = DCODIV = SMCLK = 8MHz.
    //
    //                MSP430FR2311
    //             -----------------
    //         /|\|                 |
    //          | |                 |
    //          --|RST              |
    //            |                 |
    //            |                 |
    //            |     P1.7/UCA0TXD|----> PC (echo)
    //            |     P1.6/UCA0RXD|<---- PC
    //            |                 |
    //
    //   Darren Lu
    //   Texas Instruments Inc.
    //   Oct. 2015
    //   Built with IAR Embedded Workbench v6.30 & Code Composer Studio v6.1
    //******************************************************************************
    
    #include <msp430.h>
    
    void Init_GPIO();
    void Software_Trim();                       // Software Trim to get the best DCOFTRIM value
    #define MCLK_FREQ_MHZ 8                     // MCLK = 8MHz
    
    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
    
      __bis_SR_register(SCG0);                 // disable FLL
      CSCTL3 |= SELREF__REFOCLK;               // Set REFO as FLL reference source
      CSCTL1 = DCOFTRIMEN_1 | DCOFTRIM0 | DCOFTRIM1 | DCORSEL_3;// DCOFTRIM=3, DCO Range = 8MHz
      CSCTL2 = FLLD_0 + 243;                  // DCODIV = 8MHz
      __delay_cycles(3);
      __bic_SR_register(SCG0);                // enable FLL
      Software_Trim();                        // Software Trim to get the best DCOFTRIM value
    
      CSCTL4 = SELMS__DCOCLKDIV | SELA__REFOCLK; // set default REFO(~32768Hz) as ACLK source, ACLK = 32768Hz
                                               // default DCODIV as MCLK and SMCLK source
    
      // Configure UART pins
      P1SEL0 |= BIT6 | BIT7;                    // set 2-UART pin as second function
    
      // Configure UART
      UCA0CTLW0 |= UCSWRST;
      UCA0CTLW0 |= UCSSEL__SMCLK;
    
      // Baud Rate calculation
      // 8000000/(16*9600) = 52.083
      // Fractional portion = 0.083
      // User's Guide Table 17-4: UCBRSx = 0x49
      // UCBRFx = int ( (52.083-52)*16) = 1
      UCA0BR0 = 52;                             // 8000000/16/9600
      UCA0BR1 = 0x00;
      UCA0MCTLW = 0x4900 | UCOS16 | UCBRF_1;
    
      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
    }
    
    void Software_Trim()
    {
        unsigned int oldDcoTap = 0xffff;
        unsigned int newDcoTap = 0xffff;
        unsigned int newDcoDelta = 0xffff;
        unsigned int bestDcoDelta = 0xffff;
        unsigned int csCtl0Copy = 0;
        unsigned int csCtl1Copy = 0;
        unsigned int csCtl0Read = 0;
        unsigned int csCtl1Read = 0;
        unsigned int dcoFreqTrim = 3;
        unsigned char endLoop = 0;
    
        do
        {
            CSCTL0 = 0x100;                         // DCO Tap = 256
            do
            {
                CSCTL7 &= ~DCOFFG;                  // Clear DCO fault flag
            }while (CSCTL7 & DCOFFG);               // Test DCO fault flag
    
            __delay_cycles((unsigned int)3000 * MCLK_FREQ_MHZ);// Wait FLL lock status (FLLUNLOCK) to be stable
                                                               // Suggest to wait 24 cycles of divided FLL reference clock
            while((CSCTL7 & (FLLUNLOCK0 | FLLUNLOCK1)) && ((CSCTL7 & DCOFFG) == 0));
    
            csCtl0Read = CSCTL0;                   // Read CSCTL0
            csCtl1Read = CSCTL1;                   // Read CSCTL1
    
            oldDcoTap = newDcoTap;                 // Record DCOTAP value of last time
            newDcoTap = csCtl0Read & 0x01ff;       // Get DCOTAP value of this time
            dcoFreqTrim = (csCtl1Read & 0x0070)>>4;// Get DCOFTRIM value
    
            if(newDcoTap < 256)                    // DCOTAP < 256
            {
                newDcoDelta = 256 - newDcoTap;     // Delta value between DCPTAP and 256
                if((oldDcoTap != 0xffff) && (oldDcoTap >= 256)) // DCOTAP cross 256
                    endLoop = 1;                   // Stop while loop
                else
                {
                    dcoFreqTrim--;
                    CSCTL1 = (csCtl1Read & (~DCOFTRIM)) | (dcoFreqTrim<<4);
                }
            }
            else                                   // DCOTAP >= 256
            {
                newDcoDelta = newDcoTap - 256;     // Delta value between DCPTAP and 256
                if(oldDcoTap < 256)                // DCOTAP cross 256
                    endLoop = 1;                   // Stop while loop
                else
                {
                    dcoFreqTrim++;
                    CSCTL1 = (csCtl1Read & (~DCOFTRIM)) | (dcoFreqTrim<<4);
                }
            }
    
            if(newDcoDelta < bestDcoDelta)         // Record DCOTAP closest to 256
            {
                csCtl0Copy = csCtl0Read;
                csCtl1Copy = csCtl1Read;
                bestDcoDelta = newDcoDelta;
            }
    
        }while(endLoop == 0);                      // Poll until endLoop == 1
    
        CSCTL0 = csCtl0Copy;                       // Reload locked DCOTAP
        CSCTL1 = csCtl1Copy;                       // Reload locked DCOFTRIM
        while(CSCTL7 & (FLLUNLOCK0 | FLLUNLOCK1)); // Poll until FLL is locked
    }
    
    
    #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;
    }
    

    Best regards,

    Cash Hao

  • Hello,

    This example  echos back  data received via the PC serial port, however I want to send data directly from the msp430 via UART to the zigbee connected to it.

    However, I did try to use the baud rate configuration from this program to my own program and I am not receiving any data still. 

    #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 = 52;                              // 32768/9600 = 3.41333
        UCA0BR1 = 0x00;
        UCA0MCTLW = 0x4900 | UCOS16 | UCBRF_1;                       // 3.41333 - INT(3.4133)=0.4133
                                                  // UCBRSx value = ? (not sure)
    
        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;
        }
    }

  • Sparrow Tech said:
    while(!(UCA0IFG & UCTXIFG)); UCA0TXBUF = TXData; // Load data onto buffer

    This will not provide "visible data until your TXData variable reaches ASCII 33 '!'. Just make sure you are watching this.

    Also, how sure are you the zigbee module is working? If it supports AT commands, you might try testing it with a terminal to confirm its functionality.

  • I have verified that my zigbees are working correctly with the zigbee software. I have put values over ASCII 33 and I am now receiving random variables but not the ones I am sending.
  • Hi Sparrow,
    Try this configuration,
    UCA0CTLW0 |= UCSSEL__ACLK;
    // Baud Rate calculation
    UCA0BR0 = 3; // 32768/9600 = 3.41333
    UCA0BR1 = 0x00;
    UCA0MCTLW = 0x9200; // 3.41333 - INT(3.4133)=0.4133 Based on User's guide Table 21-4

    Best regards,
    Cash Hao

**Attention** This is a public forum