代码一:
//******************************************************************************
//Description:SPI slave talks to SPI master using 3-wire mode. Data is sent
//to the master starting at 0x00 and increments. Received data from the
//master is expected to start at 0xff and decrements with each transmission.
//******************************************************************************
#include <msp430x14x.h>
char MST_data = 0x00,SLV_data = 0XFF;
void main()
{
WDTCTL = WDTPW + WDTHOLD; P6DIR |= BIT0;
P6OUT |= BIT0;
P3SEL = 0X0E; //P3.1,2,3 SPI option select
P3DIR |= BIT1;
P3OUT |= BIT1; U0CTL |= SWRST + SYNC + MM +CHAR; //master spi 8-bit
U0TCTL |= CKPH + SSEL1 + STC;
U0TCTL &= ~CKPL; U0BR0 = 0X02; //SPICLK = SMCLK/2
U0BR1 = 0X00;
U0MCTL = 0X00; ME1 = USPIE0;
U0CTL &= ~SWRST;
IE1 |= URXIE0; //receive enable
_EINT(); while()
{
TXBUF0 = MST_data;
LPM0;
}
}
#pragma vector=USART0RX_VECTOR
__interrupt void SPI_RX()
{
P6OUT ^= BIT0;
while((IFG1 & UTXIFG0)==);
if(U0RXBUF == SLV_data)
{
SLV_data = SLV_data - ;
MST_data = MST_data + ;
U0TXBUF = MST_data;
}
else
TXBUF0 = MST_data;
}
代码二:
#include <msp430x14x.h>
char MST_data = 0x00,SLV_data = 0xff;
void main()
{
WDTCTL = WDTPW + WDTHOLD;
P3SEL = 0X0E;
UOCTL = CHAR + SYNC + SWRST;
U0TCTL = CKPL + SSEL1 + STC;
U0CLT &= ~CKPH; ME1 |= USPIE0;
U0CTL &= ~SWRST;
IE1 |= URXIE0;
_EINT();
while()
{
TXBUF0 = SLV_data;
LMP4;
}
}
#pragma vector=USART0RX_VECTOR
__interrupt void USART0_RX()
{
while((IFG1 & OFIFG)==);
if(U0RXBUF == MST_data)
{
SLV_data = SLV_data - ;
MST_data = MST_data + ;
TXBUF0 = SLV_data;
}
else
TXBUF0 = SLV_data;
}