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.

SPI interface between MSP430G2553 and Arduino Uno R3.

Other Parts Discussed in Thread: MSP430G2553

Hi all,

I am interfacing SPI between MSP430G2553 and Arduino Uno R3, MSP430 acts as master and arduino is slave. I am trying to send data of 0xAA to arduino and printing on serial monitor but I see that data received is always 0x00. Below is the MSP430 code. Please let know whats wrong with my code.

#include <msp430.h>

/*
* main.c
*/

/* Function Declarations done here */

void config_pins(void);
void config_SPI();

/* Function Declaration ends here */

/* Global Variables are declared here */

unsigned int rx_data = 0x00;
unsigned int tx_data = 0x5A;

/* Global Variables declaration ends here */

int main(void) {


WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer

unsigned int i=0;


config_pins();

config_SPI();

/* loop Infinitely here */

while(1)
{

P1OUT &= (~BIT5);                            // Select Device

while (!(IFG2 & UCA0TXIFG));        // USCI_A0 TX buffer ready?

UCA0TXBUF = tx_data;                  // Transmit data

while(!(IFG2 & UCA0RXIFG));       // Wait till the data is received.

rx_data = UCA0RXBUF;

if(rx_data == 0x55)
{
P1OUT |= BIT0;
while(i<65536)
{
i++;
}
i=0;
P1OUT |= (~BIT0);

P1OUT |= BIT5;

}


return 0;
}

void config_pins()
{
         /* P1.5 is used as slave select and LED on P1.0 is toggled on receiving
              the data. 
           */
       

        P1OUT = 0x20 | 0xFF;
        P1DIR = 0x20 | 0xFF;


       P1DIR = 0x01 | 0xFF;
       P1OUT = 0x01 | 0xFF;


       P1SEL = 0x16;
       P1SEL2 = 0x16;


       P1OUT = 0xFE & 0xFF;


}
void config_SPI()
{
/* Configure SPI
* 1. Data Captured on rising edge and sampled at falling edge.
* 2. Polarity - Inactive state is high
* 3. 8 Bit Data Length
* 4. Master Mode
* 5. 3 Pin SPI mode
* 6. Synchronous Mode.
*/
 UCA0CTL1 = UCSWRST; //11000001
UCA0CTL0 = 0x69; //01101001
UCA0BR0 = 0x0;
UCA0BR1 = 0x2;
UCA0MCTL = 0x0;
/* Configure SPI
* 1. SPI Listen is set to false.
*/
UCA0STAT = BIT7;

/* Configure SPI
* 1. Enable SPI for operation.
*/

UCA0CTL1 = ~UCSWRST;


}

  • Did you already check if the slave works correct? Furthermore did you check if the MSP sends and receives the data as expected without the slave? By connecting TX and RX, for example.

    What is your intention of this operation?

    P1OUT = 0x20 | 0xFF;

    The 0x20 is don't care in this case. What did you plan to do?

    I think you are handling the bitwise operations wrong, maybe you have a wrong idea on how to use them. Here is another example:

    UCA0CTL1 = ~UCSWRST;

    This overrides all bits of the register - I guess you meant

    UCA0CTL1 &= ~UCSWRST;

    Same for some other operations.

    Dennis

  • Hi Dennis,

    Thanks for correcting my mistakes there. Even after correcting those mistakes, no success.

    One more doubt here, when you say connecting Tx and Rx, do you mean to short MISO and MOSI of MSP430 and check for data transmission?
    What about the clock in this case ? Will it not cause any problem in this regard. May be my doubt seems immatured but thought of asking once.

    One more point I would like to make is UNO's operating voltage is between 1.8 to 5.5 V (ATMEGA328P) and MSP's maximum operating voltage is 3.3 V. Can voltage levels mismatch cause this problem.?

    Thanks and regards,
    Shivakumar V W
  • Hi Shivakumar!

    shivakumar wantamutte said:
    when you say connecting Tx and Rx, do you mean to short MISO and MOSI of MSP430 and check for data transmission?

    Yes, exactly. If you then send something, you should receive the same. If that works, you at least know that your configuration of the USCI works in general.

    shivakumar wantamutte said:
    What about the clock in this case ? Will it not cause any problem in this regard.

    No, don't worry about the clock! There is always only one clock signal for the receiver and the transmitter. Now the MSP is receiver and transmitter, so it uses it's own clock, no problem at all.

    shivakumar wantamutte said:
    One more point I would like to make is UNO's operating voltage is between 1.8 to 5.5 V (ATMEGA328P) and MSP's maximum operating voltage is 3.3 V. Can voltage levels mismatch cause this problem.?

    Yes, this is possible. If the input voltage for one device is below it's minimum threshold for detecting a high-signal, it will not recognize it, of course. Furthermore a mismatch larger than a typical diode's forward voltage can lead to excessive current flows between both. You would have to limit the current or directly use a level translator. What are your used voltages?

    Dennis

  • Hi Dennis,

    I've done changes to the code and looks SPI now works on MSP430 on shorting MOSI and MISO lines. Below is the code.
    Thanks for this suggestion. Also, Can I try SPI communication between USCI_A and USCI_B on a same chip?



    #include <msp430.h>

    /*
    * main.c
    */

    /* Function Declarations done here */
    void config_clock();
    void config_pins(void);
    void config_SPI();

    /* Function Declaration ends here */



    /* Global Variables are declared here */

    unsigned int rx_data = 0x00;
    unsigned int tx_data = 0x5A;

    /* Global Variables declaration ends here */



    int main(void) {
    WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
    unsigned int i=0;

    config_clock();
    config_pins();

    config_SPI();

    /* loop Infinitely here*/

    while(1)
    {
    /* P1OUT &= (~BIT5); */ // Select Device
    while (!(IFG2 & UCA0TXIFG)); // USCI_A0 TX buffer ready?
    UCA0TXBUF = tx_data; // Transmit data
    while(!(IFG2 & UCA0RXIFG)); // Wait till the data is received.
    rx_data = UCA0RXBUF;

    if(rx_data == 0x5A)
    {
    P1OUT |= BIT0;
    while(i<1000)
    {
    i++;
    }
    i=0;

    P1OUT = 0xFF & 0xFE;

    while(i<1000)
    {
    i++;
    }
    i=0;
    /* P1OUT |= BIT5;*/
    }
    }

    return 0;
    }

    void config_pins()
    {
    /* P1.0 is toggled on receiving
    the data.
    */
    /* P1DIR = 0x20 & 0xFF;
    P1OUT = 0x20 & 0xFF;*/
    P1DIR = 0x01 & 0xFF;
    P1OUT = 0x01 & 0xFF;
    P1SEL = 0x16;
    P1SEL2 = 0x16;
    P1OUT = 0xFE & 0xFF;

    /* Port B Configurations below */
    /*
    P1DIR = 0x10 & 0xFF; // 00010000
    P1OUT = 0x10 & 0xFF;
    P1DIR =0x01 & 0xFF;
    P1OUT = 0x01 & 0xFF;
    P1SEL = 0xE0; // 11100000
    P1SEL2 = 0xE0;*/
    }

    void config_clock(void)
    {
    BCSCTL1=CALBC1_1MHZ; //Setting clock of 1 MHz.
    DCOCTL=CALDCO_1MHZ;

    }

    void config_SPI()
    {
    /* Configure SPI
    * 1. Data Captured on rising edge and sampled at falling edge.
    * 2. Polarity - Inactive state is Low
    * 3. 8 Bit Data Length
    * 4. Master Mode
    * 5. 3 Pin SPI mode
    * 6. Synchronous Mode.
    */
    UCA0CTL1 = 0xC1;
    UCA0CTL0 = 0x09;
    UCA0BR0 = 0x34;
    UCA0BR1 = 0x00;
    //UCB0MCTL = 0x0;
    /* Configure SPI
    * 1. SPI Listen is set to false.
    */
    UCA0STAT |= BIT7;

    /* Configure SPI
    * 1. Enable SPI for operation.
    */

    UCA0CTL1 = UCA0CTL1 & 0xFE;


    }
  • shivakumar wantamutte said:
    Also, Can I try SPI communication between USCI_A and USCI_B on a same chip?

    Yes, of course you can do that.

**Attention** This is a public forum