用两块launchpad传SPI数据,一端发送一端接收,接收一端再用串口传给电脑显示。这是项目中一小段调试的程序。
发送程序
#include
unsigned char MST_Data, SLV_Data;
int main(void)
{
volatile unsigned int i;
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P1OUT = 0x00; // P1 setup for LED & reset output
P1DIR |= BIT0 + BIT5; //
P1SEL = BIT1 + BIT2 + BIT4;
P1SEL2 = BIT1 + BIT2 + BIT4;
UCA0CTL0 |= UCCKPL + UCMSB + UCMST + UCSYNC; // 3-pin, 8-bit SPI master
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 |= 0x02; // /2
UCA0BR1 = 0; //
UCA0MCTL = 0; // No modulation
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
IE2 |= UCA0RXIE; // Enable USCI0 RX interrupt
P1OUT &= ~BIT5; // Now with SPI signals initialized,
P1OUT |= BIT5; // reset slave
__delay_cycles(75); // Wait for slave to initialize
MST_Data = 0x01; // Initialize data values
while(1)
{
UCA0TXBUF = MST_Data; // Transmit first character
MST_Data++;
__delay_cycles(1000);
}
//__bis_SR_register(LPM0_bits + GIE); // CPU off, enable interrupts
}
复制代码接收程序
#include <msp430.h>
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
while (!(P1IN & BIT5)); // If clock sig from mstr stays low,
// it is not yet in SPI mode
// P1DIR |= BIT6;
// P1OUT |= BIT6;
BCSCTL1 = CALBC1_16MHZ; // Set DCO
DCOCTL = CALDCO_16MHZ;
P1SEL = BIT1 + BIT2 + BIT5 + BIT6 + BIT7; // P1.1 = RXD, P1.2=TXD
P1SEL2 = BIT1 + BIT2 + BIT5 + BIT6 + BIT7;
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 0x82; // 16MHz 9600 UCA0BRX=1666=0x0682
UCA0BR1 = 0x06; // 16MHz 9600
UCA0MCTL = UCBRS2 + UCBRS1; // Modulation UCBRSx = 6
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt
UCB0CTL1 = UCSWRST; // **Put state machine in reset**
UCB0CTL0 |= UCCKPL + UCMSB + UCSYNC; // 3-pin, 8-bit SPI master
UCB0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
IE2 |= UCB0RXIE; // Enable USCI0 RX interrupt
__bis_SR_register(LPM4_bits + GIE); // Enter LPM4, enable interrupts
}
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = UCB0RXBUF; // TX -> RXed character
}
测试通过可以。
本文来自论坛,点击查看完整帖子内容。