#include msp430x14x.h> unsigned char RecBuf[255]; //接收数据缓存 unsigned char *SDat; //指向要发送的数据 unsigned char Rindex; //接收数据计数 unsigned char Tindex; //发送数据计数 unsigned char SendLen; //发送数据长度 void ConfigUart0() { P3SEL |= 0x30; // 选择P3.4和P3.5做UART通信端口 ME1 |= UTXE0 + URXE0; // 使能USART0的发送和接受 UCTL0 |= CHAR; // 选择8位字符
UTCTL0 |= SSEL1; // UCLK = SMCLK UBR00 = 0xD0; // 波特率9600 UBR10 = 0x00; // UMCTL0 = 0x40; // Modulation UCTL0 &= ~SWRST; // 初始化UART状态机 IE1 |= URXIE0 + UTXIE0; // 使能USART0的收发中断 }void main(void) { unsigned int i;
WDTCTL = WDTPW + WDTHOLD; // 关闭看门狗 BCSCTL1 &= ~XT2OFF; //使TX2有效,TX2上电默认是关闭的 do { IFG1 &= ~OFIFG; //清除振荡器失效标志 for(i=0xff;i>0;i--); //延时,待稳定 } while((IFG1 & OFIFG)!=0); //若振荡器失效标志有效 BCSCTL2 |= SELM1; //使MCLK=XT2 BCSCTL2 |= SELS+divS1; //SMCLK=XT2,4分频,即2MHZ
BoardConfig(0xb8); // 关闭数码管、流水灯和电平转换 ConfigUart0(); //配置UART0 _EINT(); //打开全局中断
while(1) { LPM1; } } #pragma vector = UART0RX_VECTOR __interrupt void UART0_RXISR(void) { RecBuf[Rindex++] = RXBUF0; if(RecBuf[Rindex-1]==0xfe) //接收完毕 { SendLen = Rindex; Rindex=0; if(RecBuf[0]==0xfd) //帧头正确 { LPM1_EXIT; SDat = RecBuf; IFG1 |= UTXIFG0; // 设置中断标志,进入发送中断程序 } } }#pragma vector = UART0TX_VECTOR __interrupt void UART0_TXISR(void) { if(Tindex < SendLen) { TXBUF0 = *(SDat+Tindex); Tindex++; } else { Tindex=0; //LPM1; } } |