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.

MSP430 UART

Other Parts Discussed in Thread: MSP430F5438A

Hi to all,

          I am new for msp430, and i have gone through the register configuration config and sample code which is given ti website. Now i want to transmit a data from msp430 to pc through uart. But it is not happening in my program, I am attaching my code below. kindly let me know is there any mistake.

#include<msp430f5438a.h>

void main(void)
{
WDTCTL = WDTPW + WDTHOLD;

P5SEL |= BIT6 + BIT7; // TXD AND RXD
P1DIR |= BIT0;
P1OUT &= ~BIT0;

UCA1CTL1 |= UCSWRST; // ENABLE RESET
UCA1CTL1 |= UCSSEL_2; // CLOCK SELECTION SMCLK
UCA1BR0 = 9; // 1MHZ = 115200
UCA1BR1 = 0; // 1MHZ = 115200
UCA1MCTL |= UCBRS_1 + UCBRF_0;
UCA1CTL1 &= ~UCSWRST; // RELEASE RESET

UCA1IE = UCTXIE;
__bis_SR_register(LPM0_bits + GIE); // Enter LPM4, Enable interrupts

}

#pragma vector=USCI_A1_VECTOR
__interrupt void USCI_A1_ISR(void)

{
P1OUT ^= BIT0; // TOGGLE LED
while (!(UCA1IFG&UCTXIFG));
UCA1TXBUF = 0x01; // TX -> RXed character
}

thank you all...

 and my ultimate aim is to transfer the adc data to pc through uart. kindly give me some idea for that... thank you

  • What sample code did you start with? What did you change, and why?
  • ya, i am attaching it below :

    #include <msp430.h>

    int main(void)
    {
    WDTCTL = WDTPW + WDTHOLD; // Stop WDT

    P3SEL = 0x30; // P3.4,5 = USCI_A0 TXD/RXD
    UCA0CTL1 |= UCSWRST; // **Put state machine in reset**
    UCA0CTL1 |= UCSSEL_2; // SMCLK
    UCA0BR0 = 9; // 1MHz 115200 (see User's Guide)
    UCA0BR1 = 0; // 1MHz 115200
    UCA0MCTL |= UCBRS_1 + UCBRF_0; // Modulation UCBRSx=1, UCBRFx=0
    UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
    UCA0IE |= UCRXIE; // Enable USCI_A0 RX interrupt

    __bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts enabled
    __no_operation(); // For debugger
    }

    // Echo back RXed character, confirm TX buffer is ready first
    #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,4))
    {
    case 0:break; // Vector 0 - no interrupt
    case 2: // Vector 2 - RXIFG
    while (!(UCA0IFG&UCTXIFG)); // USCI_A0 TX buffer ready?
    UCA0TXBUF = UCA0RXBUF; // TX -> RXed character
    break;
    case 4:break; // Vector 4 - TXIFG
    default: break;
    }
    }

    Instead of Receiver interrupt, i have been used transmit interrupt enable. And i am trying to transmit a character 'A' repeatedly. how??

  • Dear
    Clemens Ladisch

    Actually i found it was a baud rate issue.. According to the table given in SLAU208 Pg.no16. Initially i set for 115200 with SMCLK but it is not working for this combination. But it is working for 9600 baud rate combination.
    can you please tell me why???
    thank you
  • As shown in table 36-4, the maximum timing error is 11.5 %, in addition to the error introduced by REFO. For reliable UART operation, you should try to stay below 1 %.

    To get better timing, use a more precise clock source (i.e., a crystal) with a high enough frequency.

  • thanks for your information sir,
    in below code, i am unable to send the character '9' continuosly, sometimes random data also coming.... i don't know why??
    and tell me how to get two digit and three digit numbers(ex. 78, 256) directly or is there any conversion??

    #include<msp430f5438a.h>

    void main(void)
    {
    WDTCTL = WDTPW + WDTHOLD;

    P5SEL |= BIT6 + BIT7; // TXD AND RXD
    P5DIR |= BIT6 + BIT7;
    P1DIR |= BIT0;
    P1OUT &= ~0x00;

    UCA1CTL1 |= UCSWRST; // ENABLE RESET
    UCA1CTL1 |= UCSSEL_2; // CLOCK SELECTION SMCLK
    UCA1BR0 = 109; // 1MHZ = 115200
    UCA1BR1 = 0; // 1MHZ = 115200
    UCA1MCTL |= UCBRS_2 + UCBRF_0;
    UCA1CTL1 &= ~UCSWRST; // RELEASE RESET

    UCA1IE = UCTXIE;
    __bis_SR_register(LPM0_bits + GIE); // Enter LPM4, Enable interrupts
    }

    #pragma vector=USCI_A1_VECTOR
    __interrupt void USCI_A1_ISR(void)
    {
    P1OUT ^= BIT0;

    // while(!(UCA1IFG&UCTXIFG))
    UCA1TXBUF = '9'; // TX -> RXed character

    }

    thank you...
  • This code unreadable because the comments are lying.

    To send multiple digits, you have to send multiple bytes (i.e., in the interrupt handler, send the n-th byte of the string, and increment n, and do not forget to do the correct thing when you reach the end of the string).
  • hi i am attaching the code again.... the transmitted data to pc is not as i expected.. it is randomly varying.. may i know the reason??

    #include<msp430f5438a.h>
    #include<stdio.h>

    void main(void)
    {
    WDTCTL = WDTPW + WDTHOLD;

    P5SEL |= BIT6 + BIT7; // TXD AND RXD
    P5DIR |= BIT6 + BIT7;
    P1DIR |= BIT0;
    P1OUT &= ~0x00;

    UCA1CTL1 |= UCSWRST; // ENABLE RESET
    UCA1CTL1 |= UCSSEL_2; // CLOCK SELECTION SMCLK
    UCA1BR0 = 109; // 1MHZ = 115200
    UCA1BR1 = 0; // 1MHZ = 115200
    UCA1MCTL |= UCBRS_2 + UCBRF_0;
    UCA1CTL1 &= ~UCSWRST; // RELEASE RESET

    UCA1IE = UCTXIE;

    __bis_SR_register(LPM0_bits + GIE); // Enter LPM4, Enable interrupts

    }

    #pragma vector=USCI_A1_VECTOR
    __interrupt void USCI_A1_ISR(void)
    {
    P1OUT ^= BIT0;
    UCA1TXBUF = '2'; // TX -> RXed character
    }
  • This is exactly the same code!

    Possibly the clock is still off. Use a crystal.
  • In above program, sometime i am getting exactly '2' sometimes '&' and sometimes i am getting anything on Tera Term window..... May i know the reason??
  • With the start and stop bits, '2' is transmitted as 0010011001. Sending it continuously results in 001001100100100110010010011001…

    '&' corresponds to 0011001001. Apparently, the receiver started at the wrong place in the stream. This can happen when you start the sender before the receiver.
  • thank you for your explanation......
    Ya i understood clearly is there any solution to notify the transmitter to wait until the receiver complete the current process??
  • You could wait until the PC sends a byte to the MSP that tells it to start (or stop).
  • Dear Clemens Ladisch,
    Ya now i understood the problem, i am not checking the transmit buffer. Whether it is free or not...... I tried like this while
    (!(UCA1IFG&UCTXIFG))
    UCA1TXBUF = 'A';
    In this case it is not transmitting the data.....................................
    Is there any other solution??
    Thanks in advance
  • I am not transmitting from PC. I am doing one way communication MSP tx pin to PC rx pin....... I think my controller is continuosly transmitting without proper package..... is it correct??
  • If you insert pauses between the bytes, it's more likely that the PC finds the correct start bit.

**Attention** This is a public forum