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.

MSP430F133: Receiving Strings via bluetooth, storage of strings and transmission.

Part Number: MSP430F133

Hi, I am using code composer for the MSP430fr133, I have successfully sent a Bluetooth signal from an external application to the RX pin of the MSP430 via the HC-05 Bluetooth module, I have set up an if statement as part of my interrupt to light an LED when this data is received and stored. The string is currently being stored in the RXBuffer on the MSP which is then transmitted by another function within the code to transmit the data stored within the RXBuffer via the TXBuffer which will transmit the data out of the MSP430fr133 via the TX pin. However my issue is the data which is sent to the MSP is not the same as the data being transmitted out (I have checked all baud rates which match). It seems like data is being distorted in the buffer. Any help in this area would be greatly appreciated and I can provide my code if necessary.

  • I think you need to provide the code.

    What are you getting back? garbage? or does it seem to make sense in some way?

  • #include <msp430.h>
    #include "hal_LCD.h"
    #define BUFFER_SIZE 128
    char rxBuffer[BUFFER_SIZE];
    unsigned int rxIndex = 0;
    void initGPIO(void) {
        P4DIR |= BIT0; // Set P4.0 as an output direction to drive an LED
        P4OUT &= ~BIT0; // Initially turn off the LED
    }
    void initUART(void) {
        PM5CTL0 &= ~LOCKLPM5; // Disable the GPIO high-impedance mode
        // Configure pins for UART functionality
        P1SEL0 |= BIT0 | BIT1; // Adjust for your MSP430 variant
        UCA0CTLW0 |= UCSWRST; // Put eUSCI in reset state
        UCA0CTLW0 |= UCSSEL__SMCLK; // Use SMCLK as clock source
        // Configure baud rate to 9600
        UCA0BR0 = 6;
        UCA0BR1 = 0;
        UCA0MCTLW = UCOS16 | UCBRF_8 | 0x20;
        UCA0CTLW0 &= ~UCSWRST; // Initialize eUSCI
        UCA0IE |= UCRXIE; // Enable USCI_A0 RX interrupt
    }
    void flashLED(void) {
        P4OUT |= BIT0; // Turn on the LED
        __delay_cycles(1000000); // Wait for a short period
        P4OUT &= ~BIT0; // Turn off the LED
    }
    // Function to transmit data
    void transmitString(char *string) {
        unsigned int i = 0;
        while (string[i] != '\0') { // Transmit until null terminator
            while (!(UCA0IFG & UCTXIFG)); // Wait for TX buffer to be ready
            UCA0TXBUF = string[i++];
        }
    }
    void initSystem(void) {
        WDTCTL = WDTPW | WDTHOLD;
        PM5CTL0 &= ~LOCKLPM5;
        initUART();
        Init_LCD();
    }
    void LCDDisplayScrollText() {
        char *testMsg = "DATA RECEIVED";
        displayScrollText(testMsg);
    }
    #pragma vector=USCI_A0_VECTOR
    __interrupt void USCI_A0_ISR(void) {
        switch (__even_in_range(UCA0IV, USCI_UART_UCTXCPTIFG)) {
            case USCI_NONE: break;
            case USCI_UART_UCRXIFG:
                char receivedChar = UCA0RXBUF;
                if (receivedChar == '\n') {              
                    flashLED();
                    LCDDisplayScrollText();
                    transmitString(rxBuffer);        
                    rxIndex = 0;
                    __delay_cycles(2000000); // Wait for a short period
                } else if (rxIndex < BUFFER_SIZE - 1) {
                    rxBuffer[rxIndex++] = receivedChar;
                }
                break;
            default: break;
        }
    }
    int main(void) {
        WDTCTL = WDTPW | WDTHOLD;
        initSystem();
        initGPIO();
        __bis_SR_register(GIE);
        while(1) {
        }
    }
  • Please use insert code facility.

    You don't nul terminate RXBuffer before sending it to transmit. I tend to add a nul after every received character, so that it is always nul terminated.

    It is also bad form to wait so long in an interrupt.

  • Will adding a null after every received character still allow the string to be transmitted as one? 

  • The idea is that you treat every character as the end of the string and add a nul. When the next character comes in, you overwrite that nul with the new character and add another one.

    In your case, you could also add the nul when you get the '\n'.

  •  char receivedChar = UCA0RXBUF;
                if (receivedChar == '\n') {
                    rxBuffer[rxIndex] = '\0'; // Null-terminate the string
                    flashLED(); // Visual indication of data reception
                    LCDDisplayScrollText(); // Display "DATA RECEIVED" on LCD
                    transmitString(rxBuffer); // Transmit the received string back
                    rxIndex = 0; // Reset index for next string

    Is this an appropriate addition of the null terminator?

  • Yup. Note you will not have a '\n' in your string.

  • This issue unfortunately still persist, the '\n' comes as part of the string which is sent via bluetooth so everything before this should be stored, however the data being transmitted is still incorrect.

  • The text coming out is cohert for example if 'Hi' is sent xi or RJ, however it can sometimes be much further off.

  • #include <msp430.h>
    
    #define BUFFER_SIZE 256
    
    char RXData[BUFFER_SIZE];
    int rxIndex = 0;
    
    void Init_Clock(void) {
        __bis_SR_register(SCG0);
        CSCTL3 |= SELREF__REFOCLK;
        CSCTL0 = 0;
        CSCTL1 &= ~(DCORSEL_7);
        CSCTL1 |= DCORSEL_3;
        CSCTL2 = FLLD_0 + 243;
        __delay_cycles(3);
        __bic_SR_register(SCG0);
        while (CSCTL7 & (FLLUNLOCK0 | FLLUNLOCK1));
        CSCTL4 = SELMS__DCOCLKDIV | SELA__REFOCLK;
    }
    
    void Init_UART(void) {
        P1SEL0 |= BIT0 | BIT1;
    
        UCA0CTLW0 |= UCSWRST;
        UCA0CTLW0 |= UCSSEL__SMCLK;
    
        UCA0BR0 = 52;
        UCA0BR1 = 0;
        UCA0MCTLW = 0x4900 | UCOS16 | UCBRF_1;
        UCA0CTLW0 &= ~UCSWRST;
        UCA0IE |= UCRXIE;
    }
    
    void transmitString(void) {
        int i;
        for (i = 0; i < rxIndex; i++) {
            while (!(UCA0IFG & UCTXIFG));
            UCA0TXBUF = RXData[i];
        }
        rxIndex = 0;
        RXData[rxIndex] = '\0';
    }
    
    void main(void) {
        WDTCTL = WDTPW | WDTHOLD;
        Init_Clock();
        PM5CTL0 &= ~LOCKLPM5;
        Init_UART();
    
        __bis_SR_register(GIE);
    
        while (1) {
            __no_operation(); // Placeholder for background tasks.
        }
    }
    
    #pragma vector=USCI_A0_VECTOR
    __interrupt void USCI_A0_ISR(void) {
        switch (__even_in_range(UCA0IV, USCI_UART_UCTXCPTIFG)) {
            case USCI_NONE: break;
            case USCI_UART_UCRXIFG:
                if (rxIndex < BUFFER_SIZE - 1) {
                    char receivedChar = UCA0RXBUF;
                    if (UCA0STATW & UCRXERR) {
                        UCA0STATW &= ~(UCFE | UCOE | UCPE | UCBRK); // Clear errors
                        rxIndex = 0; // Reset the buffer
                        RXData[rxIndex] = '\0';
                    } else {
                        RXData[rxIndex++] = receivedChar;
                        RXData[rxIndex] = '\0';
    
                        if (receivedChar == '\n') {
                            transmitString(); // This assumes transmission is correct
                            rxIndex = 0;
                            RXData[rxIndex] = '\0';
                        }
                    }
                } else {
                    transmitString(); // Buffer full, transmit what we have
                    rxIndex = 0;
                    RXData[rxIndex] = '\0'; // Reset the buffer
                }
                break;
            case USCI_UART_UCTXIFG: break;
            case USCI_UART_UCSTTIFG: break;
            case USCI_UART_UCTXCPTIFG: break;
            default: break;
        }
    }

    I have trialled new UART configuration and implementation of a clock function, which has improved the transmission of data, however the incorrect output data issue still remains. Any help would be greatly appreciated.  

  • Time to put a debugger with a break in the transmit function and a  scope on the serial line to see if what you are sending is what comes out.

    Maybe your receiver needs more time between bytes.

  •  I have uploaded an image of the buffer when the message HI was sent, as you can see some unneeded data is also being sent.

  • Hi, 

    Have you tried using a logic analyzer to observe Rx on the hardware?

    Regards,

    Helic

  • Since we don't know what was in the buffer *before* "Hi" was sent, we can't tell much from this. You should make sure to fill the buffer with something like 0xDEADBEEF to make sure that what you are seeing is stuff your program put into the buffer as opposed to what was junk in the buffer to start with.

    It might be time to put a scope on the serial line to see if the module is sending anything else.

    You might also try the basic Echo program from the examples.

    Here is a good tutorial on Serial communication:

    http://www.simplyembedded.org/tutorials/msp430-uart/

**Attention** This is a public forum