MSP430_SPI_Master_Read_Write
/******************************************************************
**
** File : SPI.c | Master Send Receive Interrupt |
** Version : 1.0
** Description: SPI interface TLC549
** Author : LightWu
** Date : 2013-4-16
**
*******************************************************************/
#include "MSP430x24x.h"
#define uint unsigned int
#define uchar unsigned char
/***設(shè)置數(shù)碼管顯示****/
#define L1_OFF P4OUT|=BIT0 //關(guān)L1
#define L1_NO P4OUT&=~BIT0 //點(diǎn)亮L1
#define L2_OFF P4OUT|=BIT1 //關(guān)L2
#define L2_NO P4OUT&=~BIT1 //點(diǎn)亮L2
#define L3_OFF P4OUT|=BIT2 //關(guān)L3
#define L3_NO P4OUT&=~BIT2 //點(diǎn)亮L3
uchar const Segment1[]={0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f}; //不帶小數(shù)點(diǎn)編碼
uchar const Segment2[]={0x40, 0x79, 0x24, 0x30, 0x19, 0x12, 0x02, 0x78, 0x00, 0x10}; //帶小數(shù)點(diǎn)編碼
uchar AdcFlag = 0;
uchar TempNum1;
uchar TempNum2;
uchar TempNum3;
unsigned char Data1;
void Display( uchar num1, uchar num2, uchar num3 );
void Delay(void)
{
uint m;
for(m=1000;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; // 打開接收中斷
}
unsigned char TLC549Read(void)
{
unsigned char Data;
P3OUT &= ~0x01; // Enable TLC549, /CS reset
UCB0TXBUF = 0x55; // Dummy write to start SPI
while (!(IFG2 & UCB0TXIFG)); // 發(fā)送完成
while (!(IFG2 & UCB0RXIFG)); // USCI_B0 RX buffer ready?
Data = UCB0RXBUF; // data = 00|DATA
P3OUT |= 0x01; // Disable TLC549, /CS set
return(Data);
}
void main(void)
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD; //關(guān)狗
P4DIR = 0XFF; //P4設(shè)置為輸出,位碼控制
P4SEL = 0;
P5DIR = 0XFF; //P5設(shè)置為輸出,斷碼控制
P5SEL = 0;
P4OUT = 0XFF; //關(guān)閉數(shù)碼管,共陽(yáng)極數(shù)碼管
SpiInit();
while(1)
{
//Data1 = TLC549Read();
P3OUT &= ~0x01; // Enable TLC549, /CS reset
UCB0TXBUF = 0x55; // Transmit first character
_BIS_SR(LPM0_bits + GIE); // CPU off, enable interrupts
TempNum1 = Data1/100; //百位
TempNum2 = Data1/10%10; //十位
TempNum3 = Data1%10; //個(gè)位
Display(TempNum1,TempNum2,TempNum3); //顯示轉(zhuǎn)換值
}
}
#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
}
void Display( uchar num1, uchar num2, uchar num3 )
{
P5OUT = Segment1[ num1 ];//
L1_NO;
Delay();
L1_OFF;
P5OUT = Segment1[ num2 ];//
L2_NO;
Delay();
L2_OFF;
P5OUT = Segment1[ num3 ];//
L3_NO;
Delay();
L3_OFF;
}