该协议基于在同一块MSP430上用串口1发射,串口0接收,使用两块无线收发的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,用于发送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发射模块void init_NRF401_TR(void){ Host_PowerUp; //上电到发射状态避免辐射和干扰 NRF401_CS; delayms(5); Host_Transmit; //设置串口无线模块为发射模式 delayms(1); //延时4毫秒}初始化NRF401接收模块void init_NRF401_RE(void){ Host_PowerUp; //上电到发射状态避免辐射和干扰 NRF401_CS; Host_Receive; //设置串口无线模块为发射模式 delayms(5); //延时4毫秒}发射模块发送数据__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]; //发送缓冲区 pro[0]=id; pro[1]=cmd; crc=cal_crc(pro,2); //计算校验码 uart_Host_Tbuf[0]=0x11; //先导字段,容易丢失 uart_Host_Tbuf[1]=0x55; //抗干扰字节 uart_Host_Tbuf[2]=0xAA; //帧开始字节 uart_Host_Tbuf[3]=id; uart_Host_Tbuf[4]=cmd; uart_Host_Tbuf[5]=(char)(crc/256); //循环冗余校验高8位 uart_Host_Tbuf[6]=(char)(crc%256); //循环冗余校验低8位 uart_Host_Tbuf[7]=0x77; //结束标志 uart_Host_Tnum=8; //发送帧长度,3个先导字节+2data+2个校验字节+1个结束字 Host_Transmit; delayms(5); uart_Puts(uart_Host_Tbuf,uart_Host_Tnum,1); //发送数据}通过实验,发现在发送是延时片刻数据接收误码率低__monitor void uart_Puts(unsigned char *str,unsigned char length,unsigned char uart_id)//输出多数据函数{ while (length!=0) { if(uart_id==0) uart0_putchar(*str++ ); //通过串口发送数据 else { uart1_putchar(*str++ ); //通过串口发送数据 delay(255); } length--; } }void uart1_putchar(char c) //串口1发送一个字符{TXBUF1 = c;while((UTCTL1&0x01)==0);}接收通过中断处理:#pragma vector = UART0RX_VECTOR //下位机先进行数据接收__interrupt void Uart0rx_IRS(void){ int j=0; _DINT(); if(flag0==0) { if(RXBUF0==0xFF) { flag0=1; } } if(flag0==1) { if(RXBUF0==DeviceID) //设备码验证 { flag1=1; uart_Client_RPos=0; } } if(flag0==1&&flag1==1) { uart_Client_Rbuf[uart_Client_RPos]=RXBUF0; uart_Client_Rnum++; //接收数据计数器加1 uart_Client_RPos++; //移动接收缓冲区指针 if(uart_Client_Rbuf[uart_Client_RPos-1]=='V') //帧结束字符 { flag0=0; flag1=0; //帧起始标志清除 for(;j //转移接收到的数据 printf("UART0:%sn",Data); Clinet_Transmit; //设置为发送模式 delay(3000); //延时3m秒 }//for } //if _EINT();}//__interrupt