这里还有一个G2553的程序,可是我要用F149的。
/******************************************************************************
Project: TN9 HUB (MSP430G2xx3)
MCU:msp430g2553
MCLK:8Mhz
SMCLK:1Mhz
Ver 0.01
Tony Kuai 2013/4/10
Built with IAR C/C++ Compiler for MSP430 Ver 5.40.2
******************************************************************************/
#include "msp430g2553.h" // MSP430 device being used.
#define pLED_TN9 BIT7
#define pLED_232 BIT6
#define pIRT_Data BIT1
#define AlphaBit 256
unsigned char SPI_RX_BUF[5]={0}; //SPI receive Buff
unsigned char SPI_BitCnt=8;
unsigned char SPI_FrameCnt=40; //Defualt is 5*8=40 Bytes
unsigned char SPI_BUF_RX_POINTER=0;
unsigned char RX_CDATA=0; //0:recieving 1: transmiting data
unsigned char fIRT_DataReady;
int main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
if (CALBC1_8MHZ==0xFF) // If calibration constant erased
{
while(1); // do not load, trap CPU!!
}
//MCU main clock setting
DCOCTL = 0; // Select lowest DCOx and MODx settings
BCSCTL1 = CALBC1_8MHZ; // Set range
DCOCTL = CALDCO_8MHZ; // Set DCO step + modulation */
BCSCTL2 |= DIVS0|DIVS1; //Sclk=1MHZ
BCSCTL3 |= LFXT1S_2; // LFXT1 = VLO(开12K内部振荡器)
IFG1 &= ~OFIFG; // Clear OSCFault flag
//UART setting, 9600BPs,
P1SEL |= BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
P1SEL2 |= BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 104; // 1MHz 9600
UCA0BR1 = 0; // 1MHz 9600
UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt
//Port P1 and P2 setting
P1DIR = 0xFF; // 1111 1101
P1OUT = 0xFF; // 1111 1111
P1SEL = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD, P1.5=UCB0CLK ,P1.6=UCB0SOMI ,P1.7=UCB0SIMO
P1SEL2= BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD, P1.5=UCB0CLK ,P1.6=UCB0SOMI ,P1.7=UCB0SIMO
// P2REN |= 0x3c; // 1111 1100 内部上拉电阻(如果外部有上拉电阻,低电平会下不来,细看手册)
P2DIR = 0xFC; // 1111 1100 -->IRT clock and IRT data seting as input
P2OUT = 0x7F; // 1111 1111
P2SEL = 0x00; //
P2SEL2= 0x00; //
P2IE = 0x01; // 0000 0001 使能外部中断-->IRT clock
P2IES = 0x01; // 0000 0001 由高到低变化
P2IFG = 0x00; // 初始化中断数值
//Timer0_A3 setting
CCTL0 = CCIE; // CCR0 interrupt enabled
CCR0 = 5000;//5000us中断一次
TACTL = TASSEL_2 + MC_1; // SMCLK, upmode
while(1)
{
if (fIRT_DataReady==0x55)//wait for IRT data ready
{
fIRT_DataReady=0; //clear IRT data ready flag
//Send data to PC(RS232)
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF=SPI_RX_BUF[0]; // TX -> RXed character
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF=SPI_RX_BUF[1]; // TX -> RXed character
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF=SPI_RX_BUF[2]; // TX -> RXed character
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF=SPI_RX_BUF[3]; // TX -> RXed character
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF=SPI_RX_BUF[4]; // TX -> RXed character
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
}
}
}
/*****************************************************************************
函数功能:P2口中断处理
应用范围:P2.0 IRT clock
入口参数:无
出口参数:无
****************************************************************************/
#pragma vector=PORT2_VECTOR
__interrupt void port2_ISR (void)
{
CCR0 = 5000;//reset Timer A0
if(P2IN&pIRT_Data)
{
RX_CDATA|=0x01;
}
else
{
RX_CDATA&=~0x01;
}
SPI_BitCnt--;
SPI_FrameCnt--;
if(SPI_BitCnt==0)
{
SPI_RX_BUF[SPI_BUF_RX_POINTER]=RX_CDATA;
SPI_BUF_RX_POINTER++;
if (SPI_BUF_RX_POINTER==5)
{
_NOP();
}
SPI_BitCnt=8;
}
else
{
RX_CDATA=RX_CDATA<<1;
}
}
/*****************************************************************************
函数功能:Timer A0中断处理
应用范围:用于各控制的时间记数,5ms一次
入口参数:无
出口参数:无
****************************************************************************/
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A (void)
{
SPI_BitCnt=8;
if(SPI_BUF_RX_POINTER==5)
{
if((SPI_RX_BUF[4]==0x0d)&&(SPI_RX_BUF[3]==(SPI_RX_BUF[0]+SPI_RX_BUF[1]+SPI_RX_BUF[2])%AlphaBit))
{
fIRT_DataReady=0x55; //set data ready flag
}
}
SPI_BUF_RX_POINTER=0;
SPI_FrameCnt=0x28;
}