該協(xié)議基于在同一塊MSP430上用串口1發(fā)射,串口0接收,使用兩塊無(wú)線收發(fā)的NRF401模塊。
初始化串口0,用于接收
void init_UART0(void)
{
UCTL0 &= ~SWRST;
UCTL0 |=CHAR;//0X10;
UBR00 = 0x03;
UBR10 = 0x00;
UMCTL0 = 0x4A;
UTCTL0 = SSEL0;//0X10;
ME1 |= UTXE0+URXE0 ; //enable the UART0
IE1 |=URXIE0;
P3SEL|=BIT4+BIT5;
P3DIR|=BIT4;
Clinet_Receive;
}
初始化串口1,用于發(fā)送
void init_UART1()
{
UCTL1 &= ~SWRST;
UCTL1 |=CHAR;//0X10;
UBR01 = 0x03;
UBR11 = 0x00;
UMCTL1 = 0x4A;
UTCTL1 = SSEL0; //
ME2 |= URXE1+UTXE1 ; //enable the UART1
IE2 |=URXIE1;
P3SEL|=BIT6+BIT7;
P3DIR|=BIT6+BIT7;
Host_Transmit;
}
初始化NRF401發(fā)射模塊
void init_NRF401_TR(void)
{
Host_PowerUp; //上電到發(fā)射狀態(tài)避免輻射和干擾
NRF401_CS;
delayms(5);
Host_Transmit; //設(shè)置串口無(wú)線模塊為發(fā)射模式
delayms(1); //延時(shí)4毫秒}
初始化NRF401接收模塊
void init_NRF401_RE(void)
{
Host_PowerUp; //上電到發(fā)射狀態(tài)避免輻射和干擾
NRF401_CS;
Host_Receive; //設(shè)置串口無(wú)線模塊為發(fā)射模式
delayms(5); //延時(shí)4毫秒}
發(fā)射模塊發(fā)送數(shù)據(jù)
__monitor void uart_H2C(unsigned char id,unsigned char cmd) //Host to Host transmit mode{
unsigned int crc=0;
unsigned char uart_Host_Tbuf[UART_BUF_SIZE]; //發(fā)送緩沖區(qū)
pro[0]=id;
pro[1]=cmd;
crc=cal_crc(pro,2); //計(jì)算校驗(yàn)碼
uart_Host_Tbuf[0]=0x11; //先導(dǎo)字段,容易丟失
uart_Host_Tbuf[1]=0x55; //抗干擾字節(jié)
uart_Host_Tbuf[2]=0xAA; //幀開始字節(jié)
uart_Host_Tbuf[3]=id;
uart_Host_Tbuf[4]=cmd;
uart_Host_Tbuf[5]=(char)(crc/256); //循環(huán)冗余校驗(yàn)高8位
uart_Host_Tbuf[6]=(char)(crc%256); //循環(huán)冗余校驗(yàn)低8位
uart_Host_Tbuf[7]=0x77; //結(jié)束標(biāo)志
uart_Host_Tnum=8; //發(fā)送幀長(zhǎng)度,3個(gè)先導(dǎo)字節(jié)+2data+2個(gè)校驗(yàn)字節(jié)+1個(gè)結(jié)束字
Host_Transmit;
delayms(5);
uart_Puts(uart_Host_Tbuf,uart_Host_Tnum,1); //發(fā)送數(shù)據(jù)}
通過(guò)實(shí)驗(yàn),發(fā)現(xiàn)在發(fā)送是延時(shí)片刻數(shù)據(jù)接收誤碼率低
__monitor void uart_Puts(unsigned char *str,unsigned char length,unsigned char uart_id)
//輸出多數(shù)據(jù)函數(shù)
{
while (length!=0)
{
if(uart_id==0)
uart0_putchar(*str++ ); //通過(guò)串口發(fā)送數(shù)據(jù)
else
{
uart1_putchar(*str++ ); //通過(guò)串口發(fā)送數(shù)據(jù)
delay(255);
}
length--;
}
}
void uart1_putchar(char c) //串口1發(fā)送一個(gè)字符{
TXBUF1 = c;
while((UTCTL1&0x01)==0);
}
接收通過(guò)中斷處理:
#pragma vector = UART0RX_VECTOR //下位機(jī)先進(jìn)行數(shù)據(jù)接收__interrupt void Uart0rx_IRS(void)
{
int j=0;
_DINT();
if(flag0==0)
{
if(RXBUF0==0xFF)
{
flag0=1;
}
}
if(flag0==1)
{
if(RXBUF0==DeviceID) //設(shè)備碼驗(yàn)證
{
flag1=1;
uart_Client_RPos=0;
}
}
if(flag0==1&&flag1==1)
{
uart_Client_Rbuf[uart_Client_RPos]=RXBUF0;
uart_Client_Rnum++; //接收數(shù)據(jù)計(jì)數(shù)器加1
uart_Client_RPos++; //移動(dòng)接收緩沖區(qū)指針
if(uart_Client_Rbuf[uart_Client_RPos-1]==V) //幀結(jié)束字符
{
flag0=0;
flag1=0; //幀起始標(biāo)志清除
for(;j //轉(zhuǎn)移接收到的數(shù)據(jù)
printf("UART0:%s",Data);
Clinet_Transmit; //設(shè)置為發(fā)送模式
delay(3000); //延時(shí)3m秒
}//for
} //if
_EINT();
}//__interrupt
評(píng)論