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.

MSP430FR5739: UART not working when running at other Clock Frequencies when using recommended register values

Part Number: MSP430FR5739
Other Parts Discussed in Thread: MSP430FR2311, MSP-EXP430FR5739

I have configured the MSP430FR5739 UART when running with a clock frequency of 8MHz, using the Registry entries as per the MSP430FR57xx Family Users Guide, using Table 18-5. Recommended Settings for Typical Crystals and Baudrates.  This works for a Clock at 8MHz.  However, when I try another frequency using the values in the table, UART spits out rubbish.  Could someone please advise if there is some other setting that is required?  See code below:

// UART Test

#include <msp430.h>

// Define Constant values
//#define MCU_CLK_8MHZ // Comment this out to use 16MHz Clock
#define UART_TXD BIT0 //Pin 19: P2.0 UART TXD
#define UART_RXD BIT1 //Pin 20: P2.1 UART RXD

#ifdef MCU_CLK_8MHZ // Values from User Guide for 8MHz
#define UART_BR 52
#define UART_BRF 1
#define UART_BRS 0x49
#define UART_OS16 1
#else // Values from User Guide for 16MHz
#define UART_BR 104
#define UART_BRF 2
#define UART_BRS 0xD6
#define UART_OS16 1
#endif

// Function declaration and Variables
void UART_TX(char *TXData);
char* TXString; // Transmit string for UART

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

// Configure Clocks
CSCTL0_H = 0xA5; // Unlock register
#ifdef MCU_CLK_8MHZ
CSCTL1 |= DCOFSEL0 + DCOFSEL1; // 8MHz
#else
CSCTL1 |= DCOFSEL_0 + DCORSEL; // 16MHz
#endif

CSCTL2 = SELA_3 + SELS_3 + SELM_3; // set ACLK = vlo; SMCLK = DCO, MCLK = DCO
CSCTL3 = DIVA_0 + DIVS_0 + DIVM_0; // set all dividers
CSCTL0_H = 0x01; // Lock Register

// Configure Ports
P2SEL1 |= UART_TXD + UART_RXD; // Configure UART pins P2.0 & P2.1
P2SEL0 &= ~(UART_TXD + UART_RXD);

// Configure UART
UCA0CTL1 |= UCSWRST + UCSSEL_2; // Configure UART 0
UCA0BR0=(UART_BR&0xff);
UCA0BR1=(UART_BR>>8);
UCA0MCTLW = UART_BRF|UART_BRS|UART_OS16;

while(1)
{
TXString = "Testing \r\n";
UART_TX(TXString);
__delay_cycles(20000);
}
}


void UART_TX(char *TXData) // Define a function which accepts a character pointer to an array
{
// Function to Transmit characters

unsigned int i=0;

UCA0CTL1 &= ~UCSWRST; // release from reset
while(TXData[i]) // Increment through array, look for null pointer (0) at end of string
{
while (!(UCA0IFG & UCTXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = TXData[i]; // Send out element i of tx_data array on UART bus
i++; // Increment variable for array address
}
}

  • Basically, I am trying to use UART while having a 16MHz Clock, no external 32.768KHz external crystal. I have used all the formulas relating to calculating Baud Rate in the user guide, and none of them seem to produce an unscrambled UART transmission. Could someone please assist with a valid UCBRx, UCBRFx and UCBRSx ? ANy baud rate will do: 9600, 115200 etc. Thanks
  • The numbers in table 18-5 specify the values of the fields in the register, which is not the same as the value you're writing to the register for fields that do not start at the lowest bit.

    Instead of "1", use "UCBRF_1". Instead of "0x49", use "(0x49 * UCBRS0)". Instead of "1", use "UCOS16".
    And UCA0BR can be accessed as a 16-bit register.

  • Thanks for your response Clemens.  I tried the values as suggested and it works for 8MHz Clock producing 9600 baud.  According to table 18-5, the same values can be used for a 16MHz clock to produce 19200 baud, however, it does not produce a valid result.  For the register entries I am using the following:

    CSCTL1 |= DCOFSEL_0 + DCORSEL;             // Set Clock to 16MHz

    UCA0BRW = 52;
    UCA0MCTLW = UCBRF_1 + (0x49 * UCBRS0) + UCOS16;

    Is this the correct way to populate these registers for 16MHz clock and 19200 baud?  If not, please advise.

    I really appreciate your input since to me it is very confusing and I am really needing to get UART working.  I don't care what baud - but any baud at 16MHz.  Any example working code for this would be helpful.

    Thanks

  • CSCTL1 |= DCOFSEL_0 + DCORSEL;             // Set Clock to 16MHz

    This actually sets the clock to 24 MHz. (By default, both FSEL bits are set; and "|=" does not clear any bits.)

  • Thanks for that. I changed to CSCTL1 = DCOFSEL_0 + DCORSEL; and now I have UART working at 9600. I have tried many other values from the table to see if I can get other baud rates working - but none of them work. Could you please show a quick example of say 16MHz and 38400 baud? What would the code be to set the registers correctly?

    I.e. I use the following values (from Table 18-5) using 16MHz for 38400 and it does not work.
    UCA0BRW = 26;
    UCA0MCTLW = UCBRF_0 + (0xB6 * UCBRS0) + UCOS16;

    And I use the following values (from Table 18-5) using 16MHz and it does work.
    UCA0BRW = 104;
    UCA0MCTLW = UCBRF_2 + (0xD6 * UCBRS0) + UCOS16;

    Am I using these values incorrectly when setting the registers? If so, what is the correct method.

    Again, I am very grateful for your input.
  • I do not know where table18-5 got those values from. The USCI UART Calculator says BRW = 26, MCTLW = UCOS16 + UCBRF_1 + 0 * UCBRS0 in oversampling mode, but that results in higher errors than without oversampling, so you should instead use BRW = 416, MCTLW = UCBRF_0 + 6 * UCBRS0.

  • Hi Clemens,

    Thanks again for your reply.  I tried both sets of numbers and they both do not work.  I also tried other values from the UART calculator (thanks for the link), and they do not work either.  Example of what I tried using the values you suggested - with a 16MHz clock:

    UCA0BRW = 416;
    UCA0MCTLW = UCBRF_0 + 6 * UCBRS0;
    UCA0CTLW0 |= UCSWRST + UCMODE_0 + UCSSEL_2;

    Would there be something else that would cause this to not work?  Surely the online calculator should give valid values.  Could you please send me the code that would work?

    Thanks

  • It works with 9600 baud, so we know that the only remaining problem is with the clock.

    What do you mean with "spits out rubbish"? Usually, this indicates that the clocks of the sender and receiver do not exactly match.
    Can you confirm that the clock source runs at exactly 16 MHz?
  • I have an external crystal with 2 x 12pF capacitors to the XIN and XOUT pins, and using the following code to set the clocks to run at 16MHz:
    CSCTL0_H = 0xA5; // Unlock register
    CSCTL1 = DCOFSEL0 + DCOFSEL1; // 8MHz
    CSCTL1 = DCOFSEL_0 + DCORSEL; // 16MHz
    CSCTL2 = SELA_0 + SELS_3 + SELM_3; // set ACLK = DCO; SMCLK = DCO, MCLK = DCO
    CSCTL3 = DIVA_0 + DIVS_0 + DIVM_0; // set all dividers
    CSCTL0_H = 0x01; // Lock Register

    I assume this is correct for running the MCU at 16MHz. Does this look ok to you?
  • This does not use the crystal.

    See the *CS.c files in the code example package for how to start up the crystal.
  • Hi Clemens,

    I looked at each of the 3 *CS.c files for the MSP430FR57xx and none of them use an external crystal.  So, I will ignore the External Crystal and focus on getting the internal crystal working - using the clock settings I mentioned above.  When I set up UART as per the values you gave me, I do get a response on the terminal and it looks like this:

    i?I�)?)i?)I?)?)?I?)?)?II�)?I?)�)Ii?I�)?I?I?I�i?I�i?)�)

    Coul.d you please advise if there is anything else I can try to get this working?  Would you have an example of code that uses 16MHz Crystal and 38400 (or any other baud except 9600)?

    Thanks

  • There is no internal crystal. The DCO/VLO are not crystals but R/C oscillators; this is why they are not very accurate.
    According to the datasheet, the DCO frequency trimmed for "16 MHz" typically runs at 16.2 MHz, and has errors up to 20000 ppm at normal temperatures.

    If possible, you should derive the UART clock from a crystal. CS_03.c shows how to start up an external crystal on XT1 in low-frequency mode. (What crystal do you have?)

    Alternatively, try to compute the baud rate registers for a base clock of 16.2 MHz (or measure what the frequency actually is on your chip).

  • The XTAL I have is a 7A-16.000MAHE-T. I now have it driving from the MCU and I measured it on a CRO and it measures exactly 16.00000MHz (amazingly). I have then tried the register values you gave me, and no luck, then the values from the user guide - and still no luck. The code now looks like:

    GPIO_setAsPeripheralModuleFunctionInputPin(
    GPIO_PORT_PJ, GPIO_PIN4 + GPIO_PIN5, GPIO_PRIMARY_MODULE_FUNCTION);
    CS_setDCOFreq(DCORSEL,DCOFSEL_2); // Set DCO Frequency to 16MHz
    CS_initClockSignal(CS_ACLK,CS_XT1CLK_SELECT,CS_CLOCK_DIVIDER_1); // Initialise ACLK=XT1 and CLK DIVIDER to 1
    CS_initClockSignal(CS_SMCLK,CS_DCOCLK_SELECT,CS_CLOCK_DIVIDER_1); // Initialise SMCLK=DCOCLK and CLK DIVIDER to 2
    CS_initClockSignal(CS_MCLK,CS_DCOCLK_SELECT,CS_CLOCK_DIVIDER_1); // Initialise MCLK=DCOCLK and CLK DIVIDER set to 1
    CS_setExternalClockSource(16000000,0); // Set XT1 frequency
    CS_turnOnXT1(CS_XT1_DRIVE_0); // Start XT1 external crystal

    P2SEL1 |= UART_TXD + UART_RXD; // Configure UART pins P2.0 & P2.1
    P2SEL0 &= ~(UART_TXD + UART_RXD);
    UCA0BRW = 416;
    UCA0MCTLW = UCBRF_0 + 6 * UCBRS0;
    UCA0CTLW0 = UCSWRST + UCMODE_0 + UCSSEL_1;
    UCA0CTL1 &= ~UCSWRST; // release from reset

    I am not sure what else to try with this. Does this code look correct?
  • In theory, a 16 MHz crystal requires drive strength 1 or 2.

    What happens now? Nothing? Garbage? How does the TxD signal look like?
  • Thx. I changed to CS_turnOnXT1(CS_XT1_DRIVE_1) to drive harder. The result is now:
    €…æ…b€…æ…b€…æ…b€…æ…b€…æ…b€…æ…b€…æ…b€…æ…b€…æ…b€…æ…b€…æ…b€…æ…b€…æ…b€…æ…b€…æ…b€…æ…b€…æ…b

    If you cannot find any other issue with the code above, and you think it should work, then I am really at a loss. I have even tried swapping out the UART init part with the code from the CCS example eusci_a_uart.c, and I get the same result.
    Is this really supported in this chip - or am I missing something since the code above to me seems correct?
    Thx again for you help. I do really appreciate your time with this. It is getting a little frustrating though :(
  • You get such garbage when the clocks of the sender and the receiver don't match.
    What is the actual ACLK frequency? (Configure P2.0 to output ACLK, and measure that.)

    In theory, this does not look like erratum USCI44. But to be safe, configure both ACLK and MCLK to be sourced from XT1.

  • I have set MCLK to = ACLK as per the following code.  This code works for the 9600 baud, but no other baud rate.

    GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_PJ,GPIO_PIN4 + GPIO_PIN5,
    GPIO_PRIMARY_MODULE_FUNCTION
    );

    CS_setDCOFreq(DCORSEL,DCOFSEL_2);                                                                        // Set DCO Frequency to 16MHz
    CS_initClockSignal(CS_ACLK,CS_XT1CLK_SELECT,CS_CLOCK_DIVIDER_1);                 // Initialise ACLK=XT1 and CLK DIVIDER to 1
    CS_initClockSignal(CS_SMCLK,CS_XT1CLK_SELECT,CS_CLOCK_DIVIDER_1);              // Initialise SMCLK=DCOCLK and CLK DIVIDER to 2
    CS_initClockSignal(CS_MCLK,CS_XT1CLK_SELECT,CS_CLOCK_DIVIDER_1);                // Initialise MCLK=DCOCLK and CLK DIVIDER set to 1
    CS_setExternalClockSource(16000000,0);                             // Set XT1 frequency
    CS_turnOnXT1(CS_XT1_DRIVE_0);                                     // Start XT1 external crystal

    I have also configured P2.0 to output the ACLK signal and it is reading 15.997MHz, so ACLK seems to be running ok.

    Any other suggestions?

  • Hi Clemens,

    I am at a point where I am not sure if the MSP430FR5739 will operate UART at anything other than 9600 baud. I have gone through all the TI examples and even used the devicelib code, and still cannot get it working other than 9600 baud. I have written code for the MSP430FR2311 and have set various baud rates and which use the eUSCI, and they DO WORK. I apply the same values to the MSP430FR5739 - and all I get is strange characters. Could you - or someone else at TI please confirm, and if possible send some working code) that this MCU operates other than 9600 baud?
    The code below works for 9600 baud, but not 19200 baud @ 16MHz clock:

    Code:
    #elif MCLK_FREQ_MHZ == 16
    #if UART_BAUD_RATE == 9600 // Using 16MHz CLK, 9600 baud
    UCA0BR0 = 104; // 16000000/16/9600
    UCA0BR1 = 0x00;
    UCA0MCTLW = 0xD600 | UCOS16 | UCBRF_2;
    #elif UART_BAUD_RATE == 19200 // Using 16MHz CLK, 19200 baud
    UCA0BR0 = 52; // 16000000/16/19200
    UCA0BR1 = 0x00;
    UCA0MCTLW = 0x4900 | UCOS16 | UCBRF_1;

    Result with 19200 baud.
    ¥‡„ Dó¡…G£¤†…㤆¥‡t ó�…£¤†…£¤†¥DbàDó

    Please can someone advise - or provide working code with baud other than 9600 (16MHz clock). Thanks.
  • Output a bunch of 0x55 bytes. What is the exact frequency of the TXD output? Is there any jitter?
  • As suggested, I set the output to 0x55.

    At 16MHz, baud: 9600, Frequency measured: 4.86kHz. No Jitter. The output looks like: UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
    At 16MHz, baud: 19200, Frequency measured: 9.724kHz. No Jitter. The output looks like: €àÀ€àÀ€àÀ€àÀ€àÀ€àÀ€àÀ€àÀàÀ€àÀÀ€àÀ€àÀ€àÀ€àÀ€àÀ€àÀ€àÀ€àÀ€àÀ€àÀ€àÀ€€àÀ€ðàÀ€àÀ€àÀ€àÀ€àÀ€àÀ€À€àÀ€àÀ€àÀ€àÀ€à€À€àÀ€àÀ€àÀ€àÀ€àÀÀ€àÀàÀ€ðàÀàÀ€àÀ€àÀ€àÀ€àÀ€àÀ€àÀ€àÀ€àÀàÀ€àÀ€àÀ€àÀ€àÀàÀ€àÀ€àÀ€àÀÀ€àÀ€À€àÀ€àÀ€àÀ€àÀàÀàÀ€àÀ€àÀ€àÀ€àÀ€àÀ€àÀ€àÀ€àÀÀ€àÀ€àÀ€àÀ€àÀ€àÀ€à€À€àÀ€àÀ€àÀ€àÀ€àÀ€ð
    At 16MHz, baud: 38400, there are no values output.

    I am using the MSP-EXP430FR5739, so I would not expect any jitter.

    Would there be any code that would work with this EXP board using a separate baud?
  • In both cases, the measured frequency is off by about 1.3 %. It's likely that this prevents correct reception at higher baud rates.

    Please try without oversampling.

    If you want to ensure that the UART works at high baud rates, use a baud rate crystal (e.g., 14.7456 MHz) instead.

  • I tried removing the oversampling, and it still does not work. So far I have tried all your suggestions - apart from the external 14.7456MHz crytal - and none of them has worked. I have also tried the external 16.0000MHz crystal - and that did not make a difference either.
    Conclusion: The MSP-EXP430FR5739 Dev board only works at 8MHz clock, 9600 baud or 16MHz clock, 9600 baud - with or without an external crystal. All the UART example code from TI is for 9600 baud only. Also, I am able to get other baud rates working on a lesser MCU (MSP430FR2311) - using the same determined register values for UCAxxx.

    Could TI please provide code that works at a different baud rate, or am I to conclude that the MSP430FR57xx series / or the Launchpad does not support other baud rates?
  • Hi Clemens,

    So far I have tried all your suggestions - apart from the external 14.7456MHz crytal - and none of them has worked. I have also tried the external 16.0000MHz crystal - and that did not make a difference either. 
    Conclusion: The MSP-EXP430FR5739 Dev board only works at 8MHz clock, 9600 baud or 16MHz clock, 9600 baud - with or without an external crystal. All the UART example code from TI is for 9600 baud only. Also, I am able to get other baud rates working on a lesser MCU (MSP430FR2311) - using the same determined register values for UCAxxx.

    Could TI please provide code that works at a different baud rate than 9600, or am I to conclude that the MSP430FR57xx series / or the Launchpad does not support other baud rates?

    Please advise.

    Thanks

  • Hi Timothy, you can use an oscilloscope to see whether the hardware worked as you expected, first you can set a IO to a particular frequency,the you can measure it if the measured frequency is expected, then you can short your TX pin and RX pin, send a byte, to check the TX waveform , and to check the value you received from TX.
  • Thanks for that. The hardware is the TI EVM board - so are you saying that there may be an issue with it? I am still not convinced that it is a hardware issue, since I have measure the clock with an oscilloscope and the frequency was fine. I would first like to make sure that the software is correct - with the correct values used for the registers etc. Once this is confirmed, then I can rule that out - since I would expect the EVM board from TI should?? work.
    In my posts, I have asked for a piece of sample code that is known to be working for a baud rate other than 9600, and clock of 8 or 16MHz, but I cannot seem to find anyone (at least on this posting) that has this. If anyone has a small sample of working UART code, please let me know.
    Thanks
  • Hi, Timothy, I do not mean it is a hardware issue, I just suggested you check the system step by step.If the system does not work as you expected, It is caused either by soferware or hardware. The USART is an asynchronous communication, It is also determined by the accuracy of the oscillator, so I suggested you short the TX pin and RX pin, to see whether you can receive a right value and to chek the waveform.

**Attention** This is a public forum