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.
主机程序
#include <msp430.h>
unsigned char MST_Data, SLV_Data;
//主机,主动发送数据;并接收通过串口上传
void Clk_init()
{
BCSCTL1 = CALBC1_16MHZ; // Set DCO
DCOCTL = CALDCO_16MHZ;
}
void IO_init() //端口初始化
{
P1SEL = BIT1 + BIT2 + BIT5 + BIT6 + BIT7; // P1.1 = RXD, P1.2=TXD
P1SEL2 = BIT1 + BIT2 + BIT5 + BIT6 + BIT7;
}
void Serial_init() //串口初始化
{
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
}
void SPI_init() //SPI接口初始化
{
//UCB0CTL1 = UCSWRST; // **Put state machine in reset**
UCB0CTL0 |= UCCKPL + UCMSB + UCMST + UCSYNC; // 3-pin, 8-bit SPI master
UCB0CTL1 |= UCSSEL_2; // SMCLK
UCB0BR0 |= 0x02; // /2
UCB0BR1 = 0; //
UCA0MCTL = 0; // No modulation 波特率调整
UCB0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
IE2 |= UCB0RXIE+UCB0TXIE; // Enable USCI0 RX interrupt
}
void SPI_SendData()
{
UCB0TXBUF = MST_Data; // Transmit first character
MST_Data++;
__delay_cycles(1000000);
}
void Init_all()
{
Clk_init();
IO_init();
Serial_init();
SPI_init();
}
int main(void)
{
volatile unsigned int i;
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
Init_all();
__delay_cycles(75); // Wait for slave to initialize
MST_Data = 0x01; // Initialize data values
while(1)
{
SPI_SendData(); //SPI send data
}
}
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = UCB0RXBUF; // TX -> RXed character
}
从机程序
#include <msp430.h>
//从机,接收SPI数据返回回去,并通过串口发给电脑。
unsigned char temp = 0x00;
void Clk_init()
{
BCSCTL1 = CALBC1_16MHZ; // Set DCO
DCOCTL = CALDCO_16MHZ;
}
void IO_init()
{
P1SEL = BIT1 + BIT2 + BIT5 + BIT6 + BIT7; // P1.1 = RXD, P1.2=TXD
P1SEL2 = BIT1 + BIT2 + BIT5 + BIT6 + BIT7;
}
void Serial_init()
{
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
}
void SPI_init()
{
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
}
void Init_all()
{
Clk_init();
IO_init();
Serial_init();
SPI_init();
}
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
Init_all();
__bis_SR_register(LPM4_bits + GIE); // Enter LPM4, enable interrupts
}
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
temp = UCB0RXBUF;
while (!(IFG2&UCB0TXIFG)); // USCI_A0 TX buffer ready?
UCB0TXBUF = temp; // TX -> RXed character
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = temp; // TX -> RXed character
}