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.

Launchpad USCI UART - HELP! I already RTFM

Other Parts Discussed in Thread: MSP430G2553

Hello,

I'm trying to setup UART using the USCI peripheral on the MSP430g2553 chip. I've tried two nights in a row to set things up by the manual, but it's still not working. There might be other problems present, but the code compiles. I am trying to source MCLK and ACLK from the 32kHz xtl. I know the xtl works. Trying to get BR=1200 using UCBRX 27 and UCBRSX 2.

Thank you in advance. It takes most of the fun out of it when it doesn't work as you understand it to.

Program is very simple? Just trying to send an 'H' over the UART on the launchpad.

#include <msp430g2553.h>

long i = 0;

#define LED1 BIT0
#define LED2 BIT6

void ConfigWDT(void);
void ConfigPIN(void);
void ConfigCLK(void);
void ConfigUSCI(void);

void main(void) {
    ConfigWDT();
    ConfigPIN();
    ConfigCLK();
    ConfigUSCI();
    for(;;){
        P1OUT ^= LED1;
        __delay_cycles(15000);
        if((IFG2 & UCA0TXIFG) != 0) {
            UCA0TXBUF = 'H';
        }
        __delay_cycles(32000);
    }    
} //main

void ConfigWDT(void)
{
    WDTCTL = WDTPW + WDTHOLD;
} //ConfigWDT

void ConfigPIN(void)
{
    P2OUT = 0;
    P2DIR  = LED1 | LED2;
    P1SEL  |= (BIT1 + BIT2); //P1.1 = UCA0RXD
    P1SEL2 |= (BIT1 + BIT2); //P1.2 = UCA0TXD (controlled by bit1 and bit2)
} //ConfigPIN

void ConfigCLK(void)
{
    BCSCTL2 =  (SELM_3);
    BCSCTL3 |= (XCAP_3);
    while((IFG1 & OFIFG) != 0) {
        for (i=32000; i>0; i--) {
        }
        IFG1 ^= OFIFG;
        P1OUT ^= LED2;
    }
} //ConfigCLK

void ConfigUSCI(void)
{
    UCA0CTL1 = 0;            //resets the USCI control register
    UCA0CTL0 = 0;            //UART mode, No parity, LSB first, 8 data, 1 stop
    UCA0CTL1 |= UCSSEL_1;    //Sets it to run off of ACLK leaving reset present
    UCA0BR0 = 0x1B;            //lower byte of UCBR0. Setting it at dec27 I think
    UCA0BR1 = 0x0;            //upper byte of UCBR0. Don't need anything in that byte
    UCA0MCTL = 0x4;            //sets UCBRSx to 2 while keeping UCOS16=0
    UCA0CTL0 &= ~BIT0;        //Turns off reset
}

  • Robert Henderson said:
    UCA0CTL1 = 0;            //resets the USCI control register

    No. UCA0CTL1 |= UCSWRST; resets the USCI. While the UCSWRST bit is clear, changes to many config options are simply ignored.

    The last instruction of the init then must clear this bit: UCA0CTL1 &= ~UCSWRST;

  • You're quite right about the UCSWRST bit in UCA0CTL1. I think the last line I had in that section with the UCA0CTL1 &= ~Bit0 would have worked to clear it due to the fact that UCSWRST == BIT0. I modified the code to include your suggestion, but it still doesn't send serial data.

    I figure there is a bigger problem here because the LEDs on the launchpad don't turn on. That is a dead give away something else is incorrect. I would guess it would be maybe something with the Psel bits, but I don't know.

    Thank your for your response. It was helpful to know that the device will disregard changes with the UCSWRST bit cleared.

  • Since this matters to me enough and because I have tried unsuccessfully several nights in a row to get a USCI UART connection between my MSP430g2553 and my computer, I am offering $20USD to the first person who can deposit a well document rework of the program I started writing above. To clarify, 20 dollars to the person who modifies the above code into one that communicates with a computer through UART using the hardware USCI module. For the experienced person, I do not think it should take too long. Please document your code through comments as it's my ultimate goal to learn from my errors. 

    If your code indeed works and sends serial data via the USCI module, start a message with my by clicking my name and ,on the right, clicking the option to start a message.

    I'm just trying to expedite things, and I won't have a chance to work on it this weekend because I am going hiking. I'll be back about 0212012. 

    Regards and best of luck.

  • Robert Henderson said:
    I think the last line I had in that section with the UCA0CTL1 &= ~Bit0 would have worked to clear it due to the fact that UCSWRST == BIT0.

    Yes, bu tsince the very first line already cleared it...

    Also, your last line was trying to clear the bit in UCA0CTL0, while the UCSWRST bit is in UCA0CTL1 :)

    Robert Henderson said:
    It was helpful to know that the device will disregard changes with the UCSWRST bit cleared.

    not exactly disregard. Changing th ebits when not in reset mode may well have effects - just not the ones you desire. It's like changing constants while the program is already running and working with them. Anything may happen.

    Robert Henderson said:
    I figure there is a bigger problem here because the LEDs on the launchpad don't turn on.

    Inedeed, you configure the LEDs on P2DIR, but then toggle P1OUT :)

  • I was having the exact same issue.  I think they printed RX/TX wrong on the board, or I made up my cable wrong???  Either way, your code works as is apart from the lights which is a simple port issue.  Something to consider is this comes out as TTL, not standard RS232 format, maybe you had overseen that?

  • Hi Robert,

    have you tried the code examples that came with the msp430g2553 device? The entire code example package can be found on the msp430g2553 product folder.

    There's one example that most closely matches what you're trying to accomplish, with the exception of the UART baudrate being 9600baud instead of 1200baud (you can substitute your own values as you listed in one of the previous posts, and it should work).

    The code does UART echo, which TXs what it RXes. If you simply want to TX, just use

      while (!(IFG2&UCA0TXIFG));                 // USCI_A0 TX buffer ready?
      UCA0TXBUF = 'H';                     // TX -> RXed character

    Notice, [only] if you are using the LaunchPad Rev1.5 or later, make sure the 2 UART jumpers are in HW positions (horizontal) instead of SW positions (vertical).

    Hope that will get you up and running quickly. Looking forward to hearing the results and seeing the $20 check from you soon.

    Regards,

    Dung

    //******************************************************************************
    //   MSP430G2xx3 Demo - USCI_A0, Ultra-Low Pwr UART 9600 Echo ISR, 32kHz ACLK
    //
    //   Description: Echo a received character, RX ISR used. Normal mode is LPM3,
    //   USCI_A0 RX interrupt triggers TX Echo.
    //   ACLK = BRCLK = LFXT1 = 32768Hz, MCLK = SMCLK = DCO ~1.2MHz
    //   Baud rate divider with 32768Hz XTAL @9600 = 32768Hz/9600 = 3.41
    //* An external watch crystal is required on XIN XOUT for ACLK *//
    //
    //                MSP430G2xx3
    //             -----------------
    //         /|\|              XIN|-
    //          | |                 | 32kHz
    //          --|RST          XOUT|-
    //            |                 |
    //            |     P1.2/UCA0TXD|------------>
    //            |                 | 9600 - 8N1
    //            |     P1.1/UCA0RXD|<------------
    //
    //   D. Dang
    //   Texas Instruments Inc.
    //   February 2011
    //   Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10
    //******************************************************************************
    #include  "msp430g2553.h"
    
    void 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;                          // Enable USCI_A0 RX interrupt
    
      __bis_SR_register(LPM3_bits + GIE);       // Enter LPM3, 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
    }
    

  • Hello Dung,

    Just for knowing where to look and for providing the code sample, the reward is yours to have. I just need a paypal email address sent to me in my private message box.

    I didn't know there was a code sample packet for the G2xx3 series right on ti's website, and I'm thankful to you for pointing me in the right direction.

    Best regards,

    Robert

  • Did you verify it on your launchpad? I'm not saying you should, if you haven't, but I was just curious. I knew I didn't have a level converter, so I tried to send it through the launchpad's usb. That did not seem to work, and since I don't have a scope, I couldn't tell if anything was being sent out of the pins in a meaningful fashion.

    Thanks for your response.

    Robert

  • Hello All. A solution to the problem was found!

    Thank you to all those who have helped along the way. Turns out I was being a little bit of a muppet.

    The LED problem was correctly identified by Jens-Michael Gross. Don't really know why I did that other than it was late and I was frustrated.

    Also, the USCI device was never set free because I was trying to clear the wrong register (UCA0CTL0). The register should have been UCA0CTL1 (Also J-MG).

    After changing those two problems in the original code and switching jumpers to horizontal (Dung Dang's suggestion), it worked!

    If either of you want to claim the reward, please PM me a paypal email address.

    Both of your efforts solved the problem.

    It's beautiful now that it's ticking and clicking...

    Working code is below.

    Spits out an 'H' about every second using the hardware USCI UART at 1200bps 8N1 with no handshaking. Interesting because the USCI clock is based off an Xtl

    /*-------------------------------------------------------------------------------*/

    #include <msp430g2553.h>

    long i = 0;

    #define LED1 BIT0
    #define LED2 BIT6

    void ConfigWDT(void);
    void ConfigPIN(void);
    void ConfigCLK(void);
    void ConfigUSCI(void);

    void main(void) {
        ConfigWDT();
        ConfigPIN();
        ConfigCLK();
        ConfigUSCI();
        for(;;){
            P1OUT ^= LED1;
            __delay_cycles(15000);
            if((IFG2 & UCA0TXIFG) != 0) {
                UCA0TXBUF = 'H';
            }
            __delay_cycles(32000);
        }    
    } //main

    void ConfigWDT(void)
    {
        WDTCTL = WDTPW + WDTHOLD;
    } //ConfigWDT

    void ConfigPIN(void)
    {
        P1OUT = 0;
        P1DIR = 0xFF;
        
        P2OUT = 0;
        P2DIR = 0xFF;
        
        P3OUT = 0;
        P3DIR = 0xFF;
        
        P1SEL  |= (BIT1 + BIT2); //P1.1 = UCA0RXD
        P1SEL2 |= (BIT1 + BIT2); //P1.2 = UCA0TXD (controlled by bit1 and bit2)
    } //ConfigPIN

    void ConfigCLK(void)
    {
        BCSCTL2 =  (SELM_3);
        BCSCTL3 |= (XCAP_3);
        while((IFG1 & OFIFG) != 0) {
            for (i=32000; i>0; i--) {
            }
            IFG1 ^= OFIFG;
            P1OUT ^= LED2;
        }
    } //ConfigCLK

    void ConfigUSCI(void)
    {
        UCA0CTL1 |= UCSWRST;    //resets the USCI control register
        UCA0CTL0 = 0;            //UART mode, No parity, LSB first, 8 data, 1 stop
        UCA0CTL1 |= UCSSEL_1;    //Sets it to run off of ACLK leaving reset present
        UCA0BR0 = 0x1B;            //lower byte of UCBR0. Setting it at dec27 I think
        UCA0BR1 = 0x0;            //upper byte of UCBR0. Don't need anything in that byte
        UCA0MCTL = 0x4;            //sets UCBRSx to 2 while keeping UCOS16=0
        UCA0CTL1 &= ~UCSWRST;    //Clears bit 0 of UCA0CTL1
    }
       

  • Robert Henderson said:
    If either of you want to claim the reward, please PM me a paypal email address.

    Thanks for the offer. But keep your money. :)

    Well, if everyone who marked one of my posts as verified answer would have paid me, I'd go into vacation for a year or more. But since nobody did (and only one or two offered), I don't see why I should make an exception for you :)

    Robert Henderson said:
    Spits out an 'H' about every second using the hardware USCI UART at 1200bps 8N1 with no handshaking. Interesting because the USCI clock is based off an Xtl

    What's the surprising part in this? 32768Hz are fast enough for a good 1200Bd timing. Only a few % timing error.
    That's the microcontroller world. Here we squeeze something useful from every single Hz we have. :)

  • Dear Sir,

    Jens-Michael Gross said:
    Well, if everyone who marked one of my posts as verified answer would have paid me, I'd go into vacation for a year or more. But since nobody did (and only one or two offered), I don't see why I should make an exception for you :)

    Your sense of humour iz super duper :-)

    best wishes

    janmay

  • Hi Robert,

    good to see you got it to work. As JMG gracefully declined the payment, I'll gladly take up on the offer. That is if you insist, please make the payment in the form of buying yourself 4 more LaunchPads, couple of BoosterPacks, or even an FRAM experimenter board if you feel adventurous. And happy coding!

    ~Dung

**Attention** This is a public forum