#include <msp430x14x.h>
本文引用地址:
http://m.butianyuan.cn/article/201611/320058.htmunsigned char RecBuf[255]; //接收數(shù)據(jù)緩存
unsigned char *SDat; //指向要發(fā)送的數(shù)據(jù)
unsigned char Rindex; //接收數(shù)據(jù)計(jì)數(shù)
unsigned char Tindex; //發(fā)送數(shù)據(jù)計(jì)數(shù)
unsigned char SendLen; //發(fā)送數(shù)據(jù)長度
void ConfigUart0()
{
P3SEL |= 0x30; // 選擇P3.4和P3.5做UART通信端口
ME1 |= UTXE0 + URXE0; // 使能USART0的發(fā)送和接受
UCTL0 |= CHAR; // 選擇8位字符
UTCTL0 |= SSEL1; // UCLK = SMCLK
UBR00 = 0xD0; // 波特率9600
UBR10 = 0x00; //
UMCTL0 = 0x40; // Modulation
UCTL0 &= ~SWRST; // 初始化UART狀態(tài)機(jī)
IE1 |= URXIE0 + UTXIE0; // 使能USART0的收發(fā)中斷
}
void main(void)
{
unsigned int i;
WDTCTL = WDTPW + WDTHOLD; // 關(guān)閉看門狗
BCSCTL1 &= ~XT2OFF; //使TX2有效,TX2上電默認(rèn)是關(guān)閉的
do
{
IFG1 &= ~OFIFG; //清除振蕩器失效標(biāo)志
for(i=0xff;i>0;i--); //延時,待穩(wěn)定
}
while((IFG1 & OFIFG)!=0); //若振蕩器失效標(biāo)志有效
BCSCTL2 |= SELM1; //使MCLK=XT2
BCSCTL2 |= SELS+DIVS1; //SMCLK=XT2,4分頻,即2MHZ
BoardConfig(0xb8); // 關(guān)閉數(shù)碼管、流水燈和電平轉(zhuǎn)換
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; // 設(shè)置中斷標(biāo)志,進(jìn)入發(fā)送中斷程序
}
}
}
#pragma vector = UART0TX_VECTOR
__interrupt void UART0_TXISR(void)
{
if(Tindex < SendLen)
{
TXBUF0 = *(SDat+Tindex);
Tindex++;
}
else
{
Tindex=0;
//LPM1;
}
}
評論