51/AVR单片机技术驿站!  <在线翻译>     便利工具    特色网址   无弹窗、无插件的绿色站点...  英才招聘   学历查询  喜欢>>收藏我站 

当前位置:首页 > 单片机技术文章 > MSP430单片机 > 详细内容
基于MSP430的无线传输协议
发布时间:2009/7/28  阅读次数:884  字体大小: 【】 【】【
该协议基于在同一块MSP430上用串口1发射,串口1接收,使用两块无线收发的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<uart_Client_RPos-1;j++) Data[j]=uart_Client_Rbuf[j];      //转移接收到的数据
                          printf("UART0:%s\n",Data);
               
                         
                          Clinet_Transmit;                             //设置为发送模式
                          delay(3000);                                    //延时3m秒
                    
                          }//for
     } //if
      _EINT();

}//__interrupt
我要评论
  • 匿名发表
  • [添加到收藏夹]
  • 发表评论:(匿名发表无需登录,已登录用户可直接发表。) 登录状态:未登录
最新评论
所有评论[0]
    暂无已审核评论!

网站导航 管理登陆 ┊ 免责声明 问题反馈  友链说明
本站部分内容来自网络共享资源,如有冒犯您的权利请来信告之删除或纠正!
不得对本站进行复制、盗链或镜像,转载内容须获得同意或授权;欢迎友情链接、站务合作!

    我要报警 Alexa
 mcusy_cn#126.com (请把#改成@) 交流:522422171
本站学习交流群:138..158(高级群1-)、77930286(高级群2)、61804809(群3)
Copyright© MCUSY All Rights Reserved
本站网警备案号: WZ36040002485
  ICP备案证书号:粤ICP备09034963号