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.

Software SPI Code problem

Other Parts Discussed in Thread: MSP430FG439

Hello Everyone,

I am trying to communicate 2 MSP430FG439 through SPI.one of my MSP(MASTER) is continuosly sending data on UART . As you can use only one peripheral either SPI or UART in MSP430FG439. i tried to make software SPI code for the master. In slave i m using inbuilt SPI.each MSP is connected to LCD

Slave is able to read the data send by Master.

But my master is not able to read the data send by Slave

Here is my code for Master:

int main(void)
{
unsigned char b;
long int j;
    WDTCTL = WDTPW | WDTHOLD; 
    SCFI0 |= FN_4;                          // x2 DCO frequency, 8MHz nominal
                                            // DCO
    SCFQCTL = 91;                           // 32768 x 2 x (91 + 1) = 6.03 MHz
    FLL_CTL0 = DCOPLUS + XCAP10PF;          // DCO+ set so freq = xtal x D x
                                            //(N + 1)
    // Loop until 32kHz crystal stabilizes
    do
    {
      IFG1 &= ~OFIFG;                       // Clear oscillator fault flag
      for (j = 50000; j; j--);              // Delay
    }
    while (IFG1 & OFIFG);                   // Test osc fault flag
    set_LCD();
P1DIR |=MOSI + CLK;    
P1DIR &= ~( MISO);                     // Set P1.0 to 
b=7;
for(;;)
{
 c=spiWr(b);
  display_number(c,3,0);
  MSDELAY(1000);
  MSDELAY(1000);
}
 
}

unsigned char spiWr(unsigned char c)
{
		/* software SPI, send MSB first */
//	static unsigned char i;
	 char ret;
    unsigned char mask;
    
                                        //SPI Mode 0. CS active low. Clock idle 0. Clock rising edge.
    P1OUT &= ~CLK;                       //CLK = 0;
    
                                       //Enable SPI communication. The SPI Enable signal must be pulsed low for each byte sent!
    P1OUT &= ~SS;                     // = 0;
    
    //Ensure a minimum delay of 500ns between falling edge of SPI Enable signal
    //and rising edge of SPI Clock!
    MSDELAY(1);
    mask = 0x80;                //Initialize to write and read bit 7
    ret = 0;                    //Initialize read byte with 0

	do  {
       // MOSI = 0;                //Clock out current bit onto SPI Out line
        if (c & mask) 
	P1OUT |= MOSI;	                        //MOSI = 1;
		else
	P1OUT &= MOSI;		              //MOSI = 0;
	MSDELAY(1);
        P1OUT |= CLK;                         //CLK = 1;                //Set SPI Clock line
        if (MISO) 
      ret |= mask;                                    //Read current bit fromSPI In line
       MSDELAY(1);                    
       MSDELAY(1);
       P1OUT &= ~CLK;	                      //CLK = 0;                //Set SPI Clock line
       mask = mask >> 1;           //Shift mask so that next bit is written and read from SPI lines
       MSDELAY(1);                      
    } while (mask != 0);
      P1OUT |= SS;	
     return ret;
                                       // = 1;
}

Here is my code for slave:

void main(void)
{   
    unsigned char c=0,i=0;
    long int j;
    WDTCTL = WDTPW | WDTHOLD; 
    SCFI0 |= FN_4;                          // x2 DCO frequency, 8MHz nominal
                                            // DCO
    SCFQCTL = 91;                           // 32768 x 2 x (91 + 1) = 6.03 MHz
    FLL_CTL0 = DCOPLUS + XCAP10PF;          // DCO+ set so freq = xtal x D x
                                            //(N + 1)
    // Loop until 32kHz crystal stabilizes
    do
    {
      IFG1 &= ~OFIFG;                       // Clear oscillator fault flag
      for (j = 50000; j; j--);              // Delay
    }
    while (IFG1 & OFIFG);                   // Test osc fault flag
   set_LCD();
     P3SEL |= 0x0E;                            // P3.1-3 SPI option select
     P3DIR |= 0x01;                            // P3.0 output direction
    ME1 |= USPIE0;                            // Enable USART0 SPI mode
    UCTL0 |= CHAR + SYNC;                // 8-bit SPI Master **SWRST**
    UTCTL0 |= CKPH  + SSEL0+STC;            // SMCLK, 3-pin mode
    UCTL0 &= ~SWRST;                          // Initalize USART state machine
 

  while (1)
  {
    volatile unsigned int i;
    for (i = 0xFFFF; i > 0; i--);           // Delay
     TXBUF0=5;
    while (!(IFG1 & URXIFG0));              // USART0 TX buffer ready?
   
    display_number(RXBUF0,3,0);
  }

}




Data from master to slave is going properly. it is displaying 7 on LCD of slave. but when
I am trying to send digit 5 from slave to master but it is always showing me 255.
plz help me to improve this code.


**Attention** This is a public forum