#include <msp430x16x.h>
char MST_Data = 0x00, SLV_Data = 0xFF;
void main(void)
{
unsigned int i;
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P2OUT = 0x000; // Setup P1.0 for LED output
P2DIR |= 0x001;
P3SEL = 0x00E; // Setup P3 for SPI mode
P3OUT = 0x020; // Setup P3.4 for Scope trigger and
P3DIR |= 0x030; // P3.5 for slave initialization
U0CTL = CHAR + SYNC + MM + SWRST; // 8-bit, SPI, Master
U0TCTL = CKPL + SSEL1 + STC; // Polarity, SMCLK, 3-wire
U0BR0 = 0x002; // *** = SMCLK/2
U0BR1 = 0x000;
U0MCTL = 0x000;
ME1 = USPIE0; // Module enable
U0CTL &= ~SWRST; // SPI enable
IE1 |= URXIE0; // Recieve interrupt enable
_EINT(); // Enable interrupts
P3OUT &= ~0x020; // Toggle P3.5: slave reset
P3OUT |= 0x020;
i = 50000; // Delay
do (i--);
while (i != 0);
while (1)
{
TXBUF0 = MST_Data; // Transmit first character
LPM0; // CPU off SPI双机通信的时候,主机停在这里,不是应该接受到一个数据后进入终端吗??主机程序和从机程序用的都是例程!是不是调试的时候要有先后顺序
}
} // End Main
#pragma vector=USART0RX_VECTOR
__interrupt void SPI0_rx (void)
{
P3OUT ^= 0x010; // XOR P3.4 for scope trigger
while ((IFG1 & UTXIFG0) == 0); // USART0 TX buffer ready?
if (U0RXBUF == SLV_Data) // Test for correct character RX'd
{
SLV_Data = SLV_Data -1; // Decrement incoming data mask
MST_Data = MST_Data +1; // Increment out-going data
TXBUF0 = MST_Data;
P2OUT |= 0x001; // Pulse P1.0 indicating valid data
P2OUT &= ~0x001;
}
else
{
TXBUF0 = MST_Data;
P2OUT |= 0x001; // Set P1.0 indicating data error
}
}