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.

MSP430F5529 UART comunication

Expert 1385 points

Other Parts Discussed in Thread: MSP430F5232, AM3352

I use the Ti MSP430F5232 's  two UART to communicate, it's OK,

But I use the 430 to communicate with AM3352, the 430 can't recieve the infomation from AM3352; when 430 send  message to 3352,the 3352 recieve messy code

On both sides of the communication parameters are as follows

                 baudrate         Parity bit                stop bits

AM3352: 115200            1                                no

MSP430:   115200            1                               no

my code as follow:

#include <msp430.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define RBUF_SIZE  256
/*Set RTS*/
#define RTS_SWITCH_TIME 10000            //RTS switch time;
#define RTS_OUT         (P4DIR |=BIT3)        //set the RTS out;
#define RTS_UP            (P4OUT |=BIT3 )        //set 485 at send Mode
#define RTS_DOWN         (P4OUT &=~BIT3 )    //set 485 at recv mode
unsigned char  read_cur_flag = 0 ;
/*RTS end*/
unsigned char *uca0_recv_buf;        //uca0_recv buf for uart0
unsigned int byte_count0 = 0;    //count for uart0
unsigned char *uca1_recv_buf;        //uca0_recv buf for uart1
unsigned int byte_count1 = 0;    //count for uart1

void UCA0_CFG(void)
{
    P3SEL |= BIT3+BIT4;        //configure GPIO as UART mode;
    //UCA0CTL0 |=BIT5;
    UCA0CTL1 |= UCSWRST;      // **Put state machine in reset**

    UCA0CTL1 |= UCSSEL_2;                     // SMCLK

     UCA0BR0 = 9;                              // 1MHz 115200 (see User's Guide)
     UCA0BR1 = 0;

     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(GIE);       // interrupts enabled

}
void UCA1_CFG(void)
{
    P4SEL |=BIT5+BIT4;        //configure GPIO as UART mode;

    UCA1CTL1 |= UCSWRST;      // **Put state machine in reset**

    UCA1CTL1 |= UCSSEL_2;                     // SMCLK

    UCA1BR0 = 9;                              // 1MHz 115200 (see User's Guide)
    UCA1BR1 = 0;

    UCA1MCTL |= UCBRS_1 + UCBRF_0;            // Modulation UCBRSx=1, UCBRFx=0
    UCA1CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**

    UCA1IE |= UCRXIE;                         // Enable USCI_A0 RX interrupt

    __bis_SR_register(GIE);       // interrupts enabled

    RTS_OUT;    //configre the RTS for 485 communication;
    RTS_UP;
    sleep(1000);
}
/*
 *led_conf
 */
void led_flashing(void)
{
    P1DIR |= BIT0;                            // Set P1.0 to output direction
    P1REN |=BIT0;                            //set pollup resistor;
    P4DIR |= BIT7;
    P4REN |=BIT7;
    int k = 30;
    while(k-- > 0)
    {
        P1OUT |=BIT0;
        P4OUT |=BIT7;
        int i=10050;
        while(i-- > 0) ;

        P1OUT &=~BIT0;
        P4OUT &=~BIT7;
        i=10050;
        while(i-- > 0) ;
    }
}
void uca0_SendByte(unsigned char ch)
{

     while (!(UCA0IFG&UCTXIFG));             // USCI_A0 TX buffer ready?
            UCA0TXBUF =ch; //UCA0RXBUF;                  // TX -> RXed character
}
void uca0_sendNByte(unsigned char*str, int len)
{
    int i = 0;
    for(i = 0 ;i < len ; i++)
        {
        uca0_SendByte(str[i]);
        }
}
void uca1_SendByte(unsigned char ch)
{
    unsigned char rc =read_cur_flag ;

    sleep (1);
    while(1)        //wait for read interrupt end;
        if((rc == read_cur_flag)&&(rc == 0))
            break ;

    RTS_DOWN; //set rts for 485 send mode;
    sleep(RTS_SWITCH_TIME);
     while (!(UCA1IFG&UCTXIFG));             // USCI_A1 TX buffer ready?
            UCA1TXBUF =ch; //UCA0RXBUF;                  // TX -> RXed character
    RTS_UP;
}
void uca1_sendNByte(unsigned char*str, int len)
{
    int i = 0;
    for(i = 0 ;i < len ; i++)
        {
        uca1_SendByte(str[i]);
        }
}
unsigned int uca0_recv(unsigned char *str)
{
    unsigned int i= 0;

    if(byte_count0==0) return 0;
    while(i <= byte_count0)
        {
        str[i] =uca0_recv_buf[i];
        i++;
        }
    memset(uca0_recv_buf,0,byte_count0*sizeof(char));
    byte_count0 = 0;

    return i-1;
}
unsigned int uca1_recv(unsigned char *str)
{
    unsigned int i= 0;

    if(byte_count1==0) return 0;
    while(i <= byte_count1)
        {
        str[i] =uca1_recv_buf[i];
        i++;
        }
    memset(uca1_recv_buf,0,byte_count1*sizeof(char));
    byte_count1 = 0;

    return i-1;
}
//recieve interrupt for uart0!
#pragma vector=USCI_A0_VECTOR
__interrupt  void USCI_A0_ISR(void)
{
    switch(__even_in_range(UCA0IV,4))
      {
      case 0:break;                             // Vector 0 - no interrupt
      case 2:                                   // Vector 2 - RXIFG
        if(byte_count0 < RBUF_SIZE && uca0_recv_buf !=NULL)
          uca0_recv_buf[byte_count0++] = UCA0RXBUF;
        break;
      case 4:break;                             // Vector 4 - TXIFG
      default: break;
      }
}
//recieve interrupt for uart1!
#pragma vector=USCI_A1_VECTOR
__interrupt  void USCI_A1_ISR(void)
{
    read_cur_flag = 1;
    switch(__even_in_range(UCA1IV,4))
      {
      case 0:break;                             // Vector 0 - no interrupt
      case 2:                                   // Vector 2 - RXIFG
        if(byte_count1 < RBUF_SIZE && uca1_recv_buf !=NULL)
            uca1_recv_buf[byte_count1++] = UCA1RXBUF;
        break;
      case 4:break;                             // Vector 4 - TXIFG
      default: break;
      }
    read_cur_flag = 0;
}

void sleep(int count)
{
    int i;

    while(count-- > 0)
    for(i = 0 ;i < 1000; i++) ;
}

#include <msp430.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "UART.H"
/*
 * main.c
 */
int main(void)
{
    unsigned int res,len;
    unsigned char buf[500]={0};
    unsigned char buf1[500]={0};
    unsigned char uca0_recv_tab[500]={0};
    unsigned char uca1_recv_tab[500]={0};

    WDTCTL = WDTPW + WDTHOLD;    // Stop watchdog timer

    uca0_recv_buf =buf;
    uca1_recv_buf =buf1;
    UCA0_CFG();
    UCA1_CFG();  //configure the UART for loop;

    while(1)
        {
        //uca0_recv(uca0_recv_tab);
        uca0_sendNByte("234567",6);
        //uca0_SendByte(0x55);
        //sleep(1);
        /*res =uca0_recv(uca0_recv_tab);
        if((len =strlen(uca0_recv_tab)) > 0)
                    {
                     uca0_sendNByte(uca0_recv_tab, len);
                    }
        sleep(1000);
        led_flashing();
        res =uca1_recv(uca1_recv_tab);
        sleep(1000);
        if((len =strlen(uca1_recv_tab)) > 0)
                   {
                  uca1_sendNByte(uca1_recv_tab, len);
                   }
        sleep(1000);*/
        }
    while (1) ;//led_flashing();
    return 0;
}

**Attention** This is a public forum