First notice a few questions:
1,SPI Communication, the timing (phase, polarity) of both sides must be the same (see SPI four timing settings)
2, the host sets the clock, the slave does not need to set the clock.
3, clock on the pin, when there is data transmission, only the clock, no data transmission, there is no clock
Test the following program on the msp430f149 successful experiment, for two board communication
Host:
(SPI. C
#include <msp430x14x.h>
#define Uspi3on P3sel |= 0X0E
#define Uspi4on P3sel |= 0X0F
Char txflag=1;
Char rxflag=0;
void Clkinit ()
{
char i;
bcsctl1&=~xt2off;
ifg1&=~ofifg;
while ((IFG1&OFIFG)!=0)
{
for (i=0;i<0xff;i++)
ifg1&=~ofifg;
}
BCSCTL2 |= Selm_2+sels+divs_3;
}
Char spimasterinit (Long Baud,char databits,char Mode,char clkmode)
{
long int brclk;
U0ctl |= Swrst;
U0ctl |= sync+mm;
U0tctl |=ssel0;
U0br0 =0x02;
U0BR1 = 0;
The baud rate can be set here, so simply omit it for testing.
/*if (baud<=16364)
{
U0tctl |= SSEL0;
BRCLK = 32768;
}
Else
{
U0tctl |=ssel1;
brclk=1000000;
}
if (baud<300| | BAUD>115200)
{
return 0;
}
int fen = Brclk/baud;
if (fen<2) return (0);
Else
{
U0br0 = fen/256;
U0BR1 = fen% 256;
}*/
Switch (databits)
{
Case 7:case ' 7 ': U0ctl &=~CHAR;break;
Case 8:case ' 8 ': U0ctl |=char;break;
Default:return 0;
}
Switch (mode)
{
Case 3:case ' 3 ': U0tctl |=STC; Uspi3on;break;
Case 4:case ' 4 ': U0tctl &=~STC; Uspi4on;break;
Default:return 0;
}
Switch (Clkmode)
{
Case 0:case ' 0 ': U0tctl &=~CKPH; U0tctl &=~CKPL;break;
Case 1:case ' 1 ': U0tctl &=~CKPH; U0tctl |= Ckpl;break;
Case 2:case ' 2 ': U0tctl |= ckph; U0tctl &=~CKPL;break;
Case 3:case ' 3 ': U0tctl |= ckph; U0tctl |= Ckpl;break;
Default:return 0;
}
ME1 |= USPIE0;
UCTL0 &= ~swrst;
IE1 |= urxie0+utxie0;
return (1);
}
void SPILPM ()
{
if (u0tctl&ssel0) LPM3;
else LPM0;
}
void Spiwritedat (char c)
{
while (txflag==0);
txflag=0;
U0txbuf=c;
}
Char Spireaddat ()
{
while (rxflag==0);
rxflag=0;
return (U0RXBUF);
}
Char Spiwritedata (char c)
{
Spiwritedat (c);
return Spireaddat ();
}
#pragma vector=usart0rx_vector
__interrupt void Uartrx ()
{
rxflag=1;
}
#pragma vector=usart0tx_vector
__interrupt void Uarttx ()
{
txflag=1;
}
(MAIN.C)
#include "msp430x14x.h"
#include "SPi.h"
void Main ()
{
Char f,d=0x02;
Char e=0x02;
Wdtctl=wdtpw+wdthold;
Clkinit ();
Spimasterinit (16362,8,3,0);
_eint ();
while (1)
{
Spiwritedat (d);
F=spireaddat ();
if (f==e)
{
d++;
e++;
}
}
}
Slave program: Basically the same, there is no clock setting, and set into slave mode.
SPI Four timing:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6C/7A/wKioL1VKuKbAJX_tAAMX8k5lHSs859.jpg "title=" Spi.png "alt=" Wkiol1vkukbajx_taamx8k5lhss859.jpg "/>
This article from "My Blog" blog, declined reprint!
SPI settings for msp430f149