1。主机发送读取
/******************************************************************
**
** File : SPI.c | Master Send |
** Version : 1.0
** Description : SPI Master Send and Receive
** Author : LightWu
** Date : 2013-4-16
**
*******************************************************************/
#include "MSP430x24x.h"
unsigned char Data1;
void Delay(void)
{
int m;
for(m=5000;m>0;m--);
}
void SpiInit(void)
{
P3SEL |= 0x0E; // P3.3,2,1 USCI_B0 option select,注意管脚配置
P3DIR |= 0x01; // P3.0 output direction
UCB0CTL0 |= UCCKPH + UCMSB + UCMST + UCSYNC; // 3-pin, 8-bit SPI mstr, MSB 1st
UCB0CTL1 |= UCSSEL_2; // SMCLK
UCB0BR0 = 0x02;
UCB0BR1 = 0;
UCB0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
IE2 |= UCB0RXIE; // 打开接收中断
}
void main(void)
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD; //关狗
SpiInit();
while(1)
{
//Data1 = TLC549Read();
//注意需要加延时
Delay();
P3OUT &= ~0x01; // Enable TLC549, /CS reset
UCB0TXBUF = 0x55; // Transmit first character
_BIS_SR(LPM0_bits + GIE); // CPU off, enable interrupts
}
}
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCIA0RX_ISR (void)
{
Data1 = UCB0RXBUF; // data = 00|DATA
P3OUT |= 0x01; // Disable TLC549, /CS set
__bic_SR_register_on_exit(LPM0_bits); // Exit LPM0
}
2.从机接收发送
/******************************************************************
**
** File : SPI.c | Slave Receive and Send |
** Version : 1.0
** Description : SPI Slave Receive and Send
** Author : LightWu
** Date : 2013-4-16
**
*******************************************************************/
#include <msp430.h>
int main(void)
{
volatile unsigned int i;
WDTCTL = WDTPW+WDTHOLD; // Stop watchdog timer
// it is not yet in SPI mode
P3SEL |= 0x0E; // P3.3,2,1 option select
UCB0CTL1 = UCSWRST; // **Put state machine in reset**
UCB0CTL0 |= UCCKPH + UCSYNC+UCMSB; //3-pin, 8-bit SPI master
UCB0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
IE2 |= UCB0RXIE; // Enable USCI_B0 RX interrupt
_BIS_SR(LPM3_bits + GIE); // Enter LPM4, enable interrupts
}
// Echo character
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCIB0RX_ISR (void)
{
while (!(IFG2 & UCB0TXIFG)); // USCI_B0 TX buffer ready?
UCB0TXBUF = UCB0RXBUF;
}