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.

MSP430AFE253 UART garble

Other Parts Discussed in Thread: MAX232, MSP430AFE253

Hello everyone,

I am using the sample code from TI setting up UART at 115200 Baud. However, when I echo back characters in hyperTerminal (or TeraTerm) I get gibberish for some and the right chars back for others. 

I'm guessing this is a sync problem. However, I am not proficient enough with these controllers to fix it. 

Maybe I need to be at 9600 baud to slow things down? Here's the code in case anyone is interested. 

 

void main(void)

{

  volatile unsigned int i;

 

  WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT

  P1SEL |= BIT3+BIT4;                       // P1.3,1.4 = USART0 TXD/RXD

  do

  {

      IFG1 &= ~OFIFG;                       // Clear OSCFault flag

      for (i = 0x47FF; i > 0; i--);         // Time for flag to set

  }

  while ((IFG1 & OFIFG));                   // OSCFault flag still set?

 

  ME1 |= UTXE0 + URXE0;                     // Enable USART0 TXD/RXD

  U0CTL |= CHAR;                            // 8-bit character

  U0TCTL |= SSEL1;                          // UCLK= SMCLK

  U0BR0 = 10;                               // 1MHz 115200

  U0BR1 = 0x00;                             // 1MHz 115200

  U0MCTL = 0x00;                            // 1MHz 115200 modulation

  U0CTL &= ~SWRST;                          // Initialize USART state machine

  IE1 |= URXIE0;                            // Enable USART0 RX interrupt

  P1SEL2 |= BIT0;                           // Set SMCLK at P1.0

//  while(1)

//  {

//   while (!(IFG1 & UTXIFG0));

//   TXBUF0 = 'a';  

//  }

  //_BIS_SR(LPM0_bits + GIE);                 // Enter LPM0 w/ interrupt

}

 

#pragma vector=USART0RX_VECTOR

__interrupt void USART0_RX (void)

{

  while (!(IFG1 & UTXIFG0));                // USART0 TX buffer ready?

  TXBUF0 = RXBUF0;                          // RXBUF0 to TXBUF0

}

 

  • Hi,

    From the table of commonly used baud rates in the 2xx users guide. Here is the line for 1,000,000 Hz and 115200 baud.

    BRCLK
    frequency[Hz]   BaudRate  UCBRx UCBRSx UCBRFx     Max. TX Error [%]         Max. RX Error [%]

    1,000,000        115200          8             6                0 -                -7.8     6.4 -                  -9.7      16.1

     

    So to me it looks like it should be set up as U0BR0 = 8, UCBR1 = 0, U0MCTL =0x0C (UCBRSx = 6).  One thing to notice is that the error rate is pretty bad at 16.1%.  I think bumping up the clock speed to 4 or 8 MHz would be a good idea, or going with a lower baud rate.

    Another thing I noticed is that in your program there is nothing to prevent the program from just exiting. Perhaps adding a while(1); would be in order.

     

    HTH,

    Barry

     

  • Okay, so I set my BAUD rate to 9600: UxBR0 = 0x6D, UxBR1 = 0, UxMCTL = 0x03, but this time it's even worst...I can only detect an echo on character 'p' at which point the degree character is echoed.

    As for the while(1), I didn't include part of the code. In fact the controller enters LPM with interrupts, so The program is continuously running. 

     

  • If this helps: I'm using a MAX232 level shifter. I connected T1in to R1out just to make sure that the MAX232 wasn't the problem and characters were echoes fine. I.e. the levels are okay so the problem is purely in software. 

  • Is your SMCLK 1 MHz or 1048576 Hz?  The value you've selected for the UCBRx registers is for the latter.  Your MCTL register should be set to 0x04 I believe.

  • In the user guide for MSP430x2xx, it states the clock rate is 1 048 576 Hz. So I set it to the values stated in that guide. But setting U0MCTL = 0x04 still doesn't solve the problem. 

  • If it helps also, I can correctly echo the following characters: U, o, k, m. 

    U = 10101010

    k = 1101011

    m = 1101101

    o = 1101111

    Weird?

  • Lawrence Said said:

      P1SEL2 |= BIT0;                           // Set SMCLK at P1.0

    Have you checked this output with a scope to see if your SMCLK is really 1 048 576 Hz?

     

  • Oddly enough, I plugged the scope into P1.0, but I don't see a nice square wave at all. In fact, the scope can't even detect a stable frequency on the channel...Unless I'm completely confused, all I had to do was connect signal to P1.0 and GND to GND and I should see a square wave, but I don't.

    Is there something wrong?

  • OK, I ran another piece of code to simply measure the SMCLK frequency. Turns out its around 1.27 Mhz. Given this value, and assuming I want to transmit at 9600 Baud, this would mean that.

    1. U0BR0 = 132 = 0x84

    2. U0BR1 = 0

    3. U0MCTL = 0x24

    I tried that, but it didn't work.

     

  • Looking at the datasheet for the processor, it appears that it doesn't have an XT1 input for a 32KHz crystal, so you can't use that as a reference clock. It does have two calibrated clock frequencies of 8 and 12 MHz.  I would suggest setting you DCO to run at 8MHz and then your SMCLK will also be set for 8MHz.

    DCOCTL = 0;

    BCSCTL1 = CALBC1_8MHZ;

    DCOCTL = CALDCO_8MHZ;

    Then try and set the baud rate using 8MHz as your clock.

  • Okay, well I tried to set the clock at 8Mhz: no luck...Although, I feel like I should be able to get rs232 communication with the 1.27 Mhz clock that I measured.  I again tested my hardware at 115200 baud and found that, by shorting T1in and R1out, I get gibberish again, and only if the baud rate is 9600 am I able to echo characters correctly. All this to say that at baud rates other than 9600, we can't be sure that the issue is purely software. However, at 9600 baud, the problem is persistently in software.

    Just so that we are sure I coded the 8Mhz case correctly, I included the code:

    #include "msp430afe253.h"

     

    void main(void)

    {

      volatile unsigned int i;

     

      WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT

      P1SEL |= BIT3+BIT4;                       // P1.3,1.4 = USART0 TXD/RXD

      do

      {

          IFG1 &= ~OFIFG;                       // Clear OSCFault flag

          for (i = 0x47FF; i > 0; i--);         // Time for flag to set

      }

      while ((IFG1 & OFIFG));                   // OSCFault flag still set?

      DCOCTL = 0;

      BCSCTL1 = CALBC1_8MHZ;

      DCOCTL = CALDCO_8MHZ;

      ME1 |= UTXE0 + URXE0;                     // Enable USART0 TXD/RXD

      U0CTL |= CHAR;                            // 8-bit character

      U0TCTL |= SSEL1;                          // UCLK= SMCLK

      U0BR0 = 833;                               // 1MHz 115200

      U0BR1 = 0x00;                             // 1MHz 115200

      U0MCTL = 0x92;                            // 1MHz 115200 modulation

      U0CTL &= ~SWRST;                          // Initialize USART state machine

      IE1 |= URXIE0;                            // Enable USART0 RX interrupt

      P1SEL2 |= BIT0;                           // Set SMCLK at P1.0

     

      _BIS_SR(LPM0_bits + GIE);                 // Enter LPM0 w/ interrupt

    }

     

    #pragma vector=USART0RX_VECTOR

    __interrupt void USART0_RX (void)

    {

      while (!(IFG1 & UTXIFG0));                // USART0 TX buffer ready?

      TXBUF0 = RXBUF0;                          // RXBUF0 to TXBUF0

    }

  • U0BR0 is byte so it can't take value greater than 255.  Try U0BR0 = 0x41 and U0BR1 = 0x03 and U0MCTL = 0x09.  These are the settings for 8MHz, 9600 baud.

    Discovered a cool tool to calculate the register values here.  http://mspgcc.sourceforge.net/baudrate.html

     

  • Nope, still nothing....

    With these settings I cannot echo any characters at all. I tried using the calculator with the original SMCLK (1.27 Mhz) and still nothing. If I aim for a baud rate of 115200 I always get something in the terminal. At 9600 I never get anything. Could I have been wrong and the problem is in hardware?!

  • SOLVED!!

    Solution:

    1. Thank you for the online calculator, it's amazing and should definitely be linked on the TI site

    2. Changes the MAX232 for an ADM3202 and everything worked.

    Thank you so much for your support in trying to diagnose this problem,

  • How about try adding this to your code:

    P1DIR |= 0x01;

    P1SEL |= 0x01;

    P1SEL2 |= 0x01;

    This should get the SMCLK to output on P1.0. This hopefully will allow you to scope P1.0 and see if the SMCLK is what it is supposed to be.

**Attention** This is a public forum