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.

CCS/MSP430G2553: unable to read all charaters in Serial Communication

Part Number: MSP430G2553

Tool/software: Code Composer Studio

Hi Everybody,

I'm trying to create communication between pc and MSP but i'm only getting the last character

The baudrate both pc and msp is 9600

Here's the code for the serial communication:

void InitSerialCommunication()
{
	 P1SEL |= RXD + TXD ; // P1.1 = RXD, P1.2=TXD
	 P1SEL2 |= RXD + TXD ; // P1.1 = RXD, P1.2=TXD

	 UCA0CTL1 |= UCSSEL_2; // SMCLK
	 UCA0BR0 =  0x68; // 1MHz 9600
	 UCA0BR1 = 0x00; // 1MHz 9600
	 UCA0MCTL = UCBRS2 + UCBRS0; // Modulation UCBRSx = 5
	 UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
}

int HasRequest()
{
	return (IFG2 & UCA0RXIFG);
}

void Transmit(char* data, int size)
{
	while(size > 0)
	{
		UCA0TXBUF = *data++;
		size--;
		__delay_cycles(DELAY_CHAR);//required to get all characters (if more than 10 characters or clock has changed, consider increase delay time)
	}

}

void Recieve(char* data)
{
	if(!HasRequest())
		return;

	*data = UCA0RXBUF;
	__delay_cycles(DELAY_CHAR);
}
DELAY_CHAR = 1000

and here's the main code (i'm using lcd to see the result )
#include <msp430g2553.h>

#include "LCD.h"
#include "SerialCommunication.h"
/*
 * main.c
 */

#define SDA_BIT BIT7
#define SCL_BIT BIT6
#define LCD_ADDRESS 0x27
#define WAIT_TIME 255
char key;
#define SIZE_M 5
char R[SIZE_M];
char counter = 0;
void main(void) {
    WDTCTL = WDTPW | WDTHOLD;	// Stop watchdog timer
    DCOCTL = 0; // Select lowest DCOx and MODx settings<
    BCSCTL1 = CALBC1_1MHZ; // Set DCO
    DCOCTL = CALDCO_1MHZ;

    InitLCD(SDA_BIT, SCL_BIT, LCD_ADDRESS);

    InitSerialCommunication();

    key = NONEKEY;

    StartLCD();

    __enable_interrupt();
    char address = 0x80;
	while(1)
    {

			Transmit("hiyou",5);
			//__delay_cycles(4500);
			while(HasRequest())
			{
				Recieve(&R[counter++]);
			}

			if(counter > 0)
			{
				WriteMsg(R,counter,address,counter);
				counter = 0;
			}

    }
}

Thank you all

  • Before putting next byte into TXBUF, you do not check that previous is fully sent out of UART. Such way you are overwriting all the bytes except last one. You shall either implement TX ISR or check for TXBUF empty flag.

  • As ilmars suggested, proper way for transmitting will be something like this.

    void pheonix_send_data_to_pc(unsigned char *sps, unsigned char length)
    {
    while (length)
    {
    while (!(UCA0IFG & UCTXIFG)); // add this line to ur code
    UCA0TXBUF = *sps;
    length--;
    sps++;
    }

    }


    one more thing is there any specific reason for using polling method for reception? u can use RX ISR .

**Attention** This is a public forum