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
/***设置数码管显示****/
#define L1_OFF P4OUT|=BIT0 //关L1
#define L1_NO P4OUT&=~BIT0 //点亮L1
#define L2_OFF P4OUT|=BIT1 //关L2
#define L2_NO P4OUT&=~BIT1 //点亮L2
#define L3_OFF P4OUT|=BIT2 //关L3
#define L3_NO P4OUT&=~BIT2 //点亮L3
uchar const Segment1[]={0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f}; //不带小数点编码
uchar const Segment2[]={0x40, 0x79, 0x24, 0x30, 0x19, 0x12, 0x02, 0x78, 0x00, 0x10}; //带小数点编码
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)); // 发送完成
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; //关狗
P4DIR = 0XFF; //P4设置为输出,位码控制
P4SEL = 0;
P5DIR = 0XFF; //P5设置为输出,断码控制
P5SEL = 0;
P4OUT = 0XFF; //关闭数码管,共阳极数码管
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; //个位
Display(TempNum1,TempNum2,TempNum3); //显示转换值
}
}
#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;
}