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.

MSP430G2553 UART communication example

Other Parts Discussed in Thread: MSP430G2553

Can anyone offer me an example of  MSP430G2553 UART communicating with PC? I have been trapped in this problem for 2 days and I find no solution for it. Thank you.

  • go to the website below and read it & there is an ex program also

    http://homepages.ius.edu/RWISMAN/C335/HTML/msp430SerialComm.htm

  • Why don't you use one of the examples TI offers you for your controller? There are several small programs written for almost every function of almost every MCU. Of course these programs just cover the basic implementation of one specific functionality but it is easy to adapt it to your specific requirements.


    You can find the code examples for the G2553 here:

    www.ti.com/lit/zip/slac485

    This is the example code for UART communication @ 9600 baud (msp430g2xx3_uscia0_uart_01_9600.c) - the program just sends the received character back to the host (PC). It's on you to change the functionality to do something with the received character and to send back the information you want to instead of just an echo of the received symbol.

    #include <msp430.h>
    
    int main(void)
    {
      WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
      if (CALBC1_1MHZ==0xFF)					// If calibration constant erased
      {											
        while(1);                               // do not load, trap CPU!!	
      }
      DCOCTL = 0;                               // Select lowest DCOx and MODx settings
      BCSCTL1 = CALBC1_1MHZ;                    // Set DCO
      DCOCTL = CALDCO_1MHZ;
      P1SEL = BIT1 + BIT2 ;                     // P1.1 = RXD, P1.2=TXD
      P1SEL2 = BIT1 + BIT2 ;                    // P1.1 = RXD, P1.2=TXD
      UCA0CTL1 |= UCSSEL_2;                     // SMCLK
      UCA0BR0 = 104;                            // 1MHz 9600
      UCA0BR1 = 0;                              // 1MHz 9600
      UCA0MCTL = UCBRS0;                        // Modulation UCBRSx = 1
      UCA0CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**
      IE2 |= UCA0RXIE;                          // Enable USCI_A0 RX interrupt
    
      __bis_SR_register(LPM0_bits + GIE);       // Enter LPM0, interrupts enabled
    }
    
    //  Echo back RXed character, confirm TX buffer is ready first
    #pragma vector=USCIAB0RX_VECTOR
    __interrupt void USCI0RX_ISR(void)
    {
      while (!(IFG2&UCA0TXIFG));                // USCI_A0 TX buffer ready?
      UCA0TXBUF = UCA0RXBUF;                    // TX -> RXed character
    }

  • Thank you, Dennis .However, whatever I send to the msp430, it gives me no response. Also, I have tried this example, but still gets nothing. Can anyone tells me why?

    #include <msp430.h>
    
    const char string1[] = { "Hello World\r\n" };
    unsigned int i;
    
    int main(void)
    {
      WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
      P1DIR = 0xFF;                             // All P1.x outputs
      P1OUT = 0;                                // All P1.x reset
      P2DIR = 0xFF;                             // All P2.x outputs
      P2OUT = 0;                                // All P2.x reset
      P1SEL = BIT1 + BIT2 ;                     // P1.1 = RXD, P1.2=TXD
      P1SEL2 = BIT1 + BIT2 ;                     // P1.1 = RXD, P1.2=TXD
      P3DIR = 0xFF;                             // All P3.x outputs
      P3OUT = 0;                                // All P3.x reset
      UCA0CTL1 |= UCSSEL_1;                     // CLK = ACLK
      UCA0BR0 = 0x03;                           // 32kHz/9600 = 3.41
      UCA0BR1 = 0x00;                           //
      UCA0MCTL = UCBRS1 + UCBRS0;               // Modulation UCBRSx = 3
      UCA0CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**
      IE2 |= UCA0RXIE;
      IE2 |= UCA0TXIE; 						    // Enable USCI_A0 RX interrupt
      UCA0TXBUF = string1[i++];
    
      __bis_SR_register(LPM3_bits + GIE);       // Enter LPM3 w/ int until Byte RXed
    }
    
    #pragma vector=USCIAB0TX_VECTOR
    __interrupt void USCI0TX_ISR(void)
    {
      UCA0TXBUF = string1[i++];					// TX next character
      P1OUT |= BIT6;
      while (!(IFG2&UCA0TXIFG));
      _delay_cycles(20);
      P1OUT &= ~BIT6;
      if (i == sizeof string1 - 1)              // TX over?
        IE2 &= ~UCA0TXIE;                       // Disable USCI_A0 TX interrupt
    }
    
    #pragma vector=USCIAB0RX_VECTOR
    __interrupt void USCI0RX_ISR(void)
    {
      if (UCA0RXBUF == 'u')                     // 'u' received?
      {
        i = 0;
        IE2 |= UCA0TXIE;                        // Enable USCI_A0 TX interrupt
        UCA0TXBUF = string1[i++];
      }
    }
    

  • Crist Young said:
    However, whatever I send to the msp430, it gives me no response. Also, I have tried this example, but still gets nothing. Can anyone tells me why?

    Perhaps you have hardware problem then. Check LaunchPad User's Guide and see how jumpers shall be oriented for UART communication.

  • Hi Crist!

    There are some things in your program you definitely should take care about:

    First using a variable that is changed during an interrupt should be declared as volatile because your compiler does not know that it's value can change suddenly during the normal program flow. This is your "int i". You should also give it a value at program start because in line 24 of your code you are using i without knowing which value it is filled with. It may be 0 at startup, but it could also be something else between 1 and 65535.

    Your initialization of the UART seems to be OK at a first sight, but if you are using ACLK with the watch crystal - is it populated on your launchpad? Because as I remember it comes with the crystal not already soldered on the board.

    Then in line 22 you enable receive interrupts - that's OK. To be safe you could clear a erroneous set flag before enabling the interrupt, but normally there is no need to because it is '0' after start.

    In line 23 you also enable transmit interrupts - be careful! The TX IFG is set after startup because the buffer is, of course, empty. If you now enable the GIE your program will immediately jump into the TX ISR. You normally don't want that. In your case it is better to clear the pending flag before enabling the interrupts.

    Really bad is your ISR for TX. Never!!! put a while loop in an ISR! And never!!! put a delay-loop in an ISR! You have to completely rewrite your ISR. That is not how it works! OK, it is a little bit bad, that the code example I've posted from TI uses a while() in ISR. But remember - these small programs just show the one functionality it is used for and for that it is OK. If you know want to do more with your program - don't do it that way. But maybe for you as a beginner this is good to start and learn.

    And now to the point why you are not receiving anything - you are using LPM3. I'm not sure, but is ACLK still active in LPM3? I have to look myself - I really don't know at the moment. You should take a look in the documents of the controller, too. Maybe it is because there is no clock active.

  • OK, just had a look - it remains active in LPM3.

  • I'm not sure, but was'nt there anything with differences in hardware between older and newer launchpads? Depending application UART and hardware UART?


    OK, just seen that IIlmars linked in the right information.

  • Ilmars said:

    However, whatever I send to the msp430, it gives me no response. Also, I have tried this example, but still gets nothing. Can anyone tells me why?

    Perhaps you have hardware problem then. Check LaunchPad User's Guide and see how jumpers shall be oriented for UART communication.

    [/quote]

    Thank you! Thank you so much, IImars, IT IS THE JUMPERS' ISSUE.  After I cross pin 4 and 5, every thing goes correctly!  

  • Thanks for your detailed instruction for my massed up codes. It's not easy to go through such a program. I will keep your expostulation in mind. Thank you!

**Attention** This is a public forum