搜索
bottom↓
回复: 0

麻烦大家帮我看看nrf905的收发问题

[复制链接]

出0入0汤圆

发表于 2010-8-13 08:55:00 | 显示全部楼层 |阅读模式
麻烦大家帮我看看以下程序怎么发送不了数据
发送部分
#include <reg52.h>
#include <ABSACC.h>
#include <intrins.h>
#include <stdio.h>
#include <io_def.h>

#define uint unsigned int
#define uchar unsigned char
#define BYTE_BIT0        0x01
#define BYTE_BIT1        0x02
#define BYTE_BIT2        0x04
#define BYTE_BIT3        0x08
#define BYTE_BIT4        0x10
#define BYTE_BIT5        0x20
#define BYTE_BIT6        0x40
#define BYTE_BIT7        0x80
////////////SPI指令
#define WC                0x00
#define RC                0x10
#define WTP                0x20
#define RTP                0x21
#define WTA                0x22
#define RTA                0x23
#define RRP                0x24
bdata unsigned  char DATA_BUF;
#define DATA7        ((DATA_BUF&BYTE_BIT7) != 0)
#define DATA0   ((DATA_BUF&BYTE_BIT0) != 0)

unsigned char bflag;//指示是否需要发送无线

#define TxRxBuf_Len 4
unsigned char TxRxBuf[TxRxBuf_Len];


void Delaynop(void);                                                                                                                                                                                                                    

//RF寄存器配置//
unsigned char idata RFConf[11]=
{
  0x00,                             //配置命令//
  0x4c,                             //CH_NO,配置频段在423MHZ
  0x0C,                             //输出功率为10db,不重发,节电为正常模式
  0x44,                             //地址宽度设置,为4字节
  0x04,0x04,                        //接收发送有效数据长度为32字节
  0xCC,0xCC,0xCC,0xCC,              //接收地址
  0x58,                              //CRC充许,8位CRC校验,外部时钟信号不使能,16M晶振
};

code TxAddress[4]={0xcc,0xcc,0xcc,0xcc};

///////////延时/////////////////
static void Delay(uchar n)
{
        uint i;
        while(n--)
        for(i=0;i<80;i++);
}

void Delaynop(void)
{
    unsigned char i;
        for(i=0;i<10;i++);
}



void SpiWrite(unsigned char send)
{
        unsigned char i;
        DATA_BUF=send;
        for (i=0;i<8;i++)
        {
                if (DATA7)        //总是发送最高位
                {
                        MOSI=1;
                }
                else
                {
                        MOSI=0;
                }
                SCK=1;
                Delaynop();
                DATA_BUF=DATA_BUF<<1;
                SCK=0;
                Delaynop();
        }
}

////////////////初始化nRF905///////////////////
void nRF905Init(void)
{
    CSN=1;                                                // Spi         disable
        SCK=0;                                                // Spi clock line init low
        DR=0;                                                // Init DR for input
        AM=0;                                                // Init AM for input
        CD=0;                                                // Init CD for input
        PWR=1;                                        // nRF905 power on
        TRX_CE=0;                                        // Set nRF905 in standby mode
        TXEN=0;                                        // set radio in Rx mode
}

////////初始化寄存器
void Config905(void)
{
        uchar i;
        CSN=0;                                                // Spi enable for write a spi command
        //SpiWrite(WC);                                // Write config command写放配置命令
        for (i=0;i<11;i++)        // Write configration words  写放配置字
        {
           SpiWrite(RFConf);
        }
        CSN=1;                                        // Disable Spi
}

/////////////发送数据
void TxPacket(unsigned char *Txbuf)
{
        uchar i;
        //Config905();
        CSN=0;
        SpiWrite(WTP);                                // Write payload command
        for (i=0;i<4;i++)
        {
                SpiWrite(Txbuf);                // Write 32 bytes Tx data
        }// Spi enable for write a spi command
        CSN=1;
        Delay(1);                                                // Spi disable
        CSN=0;                                                // Spi enable for write a spi command
        SpiWrite(WTA);                                // Write address command
        for (i=0;i<4;i++)                        // Write 4 bytes address
        {
                SpiWrite(TxAddress);
        }
        CSN=1;                                                // Spi disable
        TRX_CE=1;                                        // Set TRX_CE high,start Tx data transmission
        Delay(5);                                        // while (DR!=1);
        TRX_CE=0;                                        // Set TRX_CE low
}
////////////////////////////////////////////////////
void SetTxMode(void)
{
    PWR=1;
        TRX_CE=1;
        TXEN=1;
        Delay(2);                                         // delay for mode change(>=650us)
}


unsigned char CheckCD(void)                //Pin->检查是否已存在 同频率载波
{
        if (CD==1)
        {
                return 1;
        }
        else
        {
                return 0;
        }
}
void TX(unsigned char *TxRxBuf)
{
     SetTxMode();// Set nRF905 in Tx mode
         TxPacket(TxRxBuf);// Send data by nRF905
     CheckCD();        // 返回CD的当前电平
                               
}

void main()
{
   
   nRF905Init();
    Config905();
        while (1)
        {
            
      

              if(KEY1==0)
                          {       
                          Delay(1);
                          LED1 =0;
                          
                           bflag =1;
                           TxRxBuf[0]=0x29;
                           }
       
               
                if(KEY2==0)
                        {
                  Delay(1);                       
                          LED2 =0;
                          
                      bflag =1;
                          TxRxBuf[0]=0x30;
                         }
               
       
                Delay(1000);
                LED2 =1;
                LED1 =1;
       

                if(bflag==1)
                {
                        bflag =0;
                   
                        TX(TxRxBuf);
                    Delay(1000);
                        TX(TxRxBuf);  
                           
                }
          }
}

接收部分
#include <reg52.h>
#include <ABSACC.h>
#include <intrins.h>
#include <stdio.h>
#include <io_def.h>

#define uint unsigned int                     //0 ~ 255
#define uchar unsigned char
/////////////////
#define BYTE_BIT0        0x01
#define BYTE_BIT1        0x02
#define BYTE_BIT2        0x04
#define BYTE_BIT3        0x08
#define BYTE_BIT4        0x10
#define BYTE_BIT5        0x20
#define BYTE_BIT6        0x40
#define BYTE_BIT7        0x80
////////////////
#define WC                0x00
#define RC                0x10
#define WTP                0x20
#define RTP                0x21
#define WTA                0x22
#define RTA                0x23
#define RRP                0x24

bdata unsigned  char DATA_BUF;
#define DATA7        ((DATA_BUF&BYTE_BIT7) != 0)
#define DATA0   ((DATA_BUF&BYTE_BIT0) != 0)

#define TxRxBuf_Len 4
unsigned char TxRxBuffer[TxRxBuf_Len];


//RF寄存器配置//
unsigned char idata RFConf[11]=
{
  0x00,                             //配置命令//
  0x4c,0x0c,0x44,0x04,0x04,0xcc,0xcc,0xcc,0xcc,0x58,                            //CRC充许,8位CRC校验,外部时钟信号不使能,16M晶振
};

///////////80us延时/////////////////
void Delay(uchar n)
{
        uint k;
        while(n--)
        for(k=0;k<80;k++);
}

void Delaynop(void)
{
    unsigned char i;
        for(i=0;i<10;i++);
}

///////////////
unsigned char SpiRead(void)
{
        unsigned char j;
        for (j=0;j<8;j++)
        {
        DATA_BUF=DATA_BUF<<1;
                SCK=1;
                Delaynop();
                if (MISO)        //读取最高位,保存至最末尾,通过左移位完成整个字节
                {
                        DATA_BUF|=BYTE_BIT0;
                }
                else
                {
                        DATA_BUF&=~BYTE_BIT0;
                }
                SCK=0;
                Delaynop();
         }
         return DATA_BUF;
}

void SpiWrite(unsigned char send)
{
        unsigned char i;
        DATA_BUF=send;
        for (i=0;i<8;i++)
        {
                if (DATA7)        //总是发送最高位
                {
                        MOSI=1;
                }
                else
                {
                        MOSI=0;
                }
                SCK=1;
                Delaynop();
                DATA_BUF=DATA_BUF<<1;
                SCK=0;
                Delaynop();
        }
}


////////////////初始化nRF905///////////////////
void nRF905Init(void)
{
    CSN=1;                                                // Spi         disable
        SCK=0;                                                // Spi clock line init low
        DR=0;                                                // Init DR for input
        AM=0;                                                // Init AM for input
        CD=0;                                                // Init CD for input
        PWR=1;                                        // nRF905 power on
        TRX_CE=0;                                        // Set nRF905 in standby mode
        TXEN=0;                                        // set radio in Rx mode
}

////////初始化寄存器
void Config905(void)
{
        uchar i;
        CSN=0;                                                // Spi enable for write a spi command
        //SpiWrite(WC);                                // Write config command写放配置命令
        for (i=0;i<11;i++)        // Write configration words  写放配置字
        {
           SpiWrite(RFConf);
        }
        CSN=1;                                        // Disable Spi
}


void SetRxMode(void)
{  
    PWR=1;
    TXEN=0;
        TRX_CE=1;
        Delay(1);                                         // delay for mode change(>=650us)
}

unsigned char CheckDR(void)                //检查是否有新数据传入 Data Ready
{
        if (DR=1&&TRX_CE==1 && TXEN==0)
        {
                return 1;
        }
        else
        {
                return 0;
        }
}

void RxPacket(unsigned char *TxRxBuffer)
{
        uchar i;
    Delay(1);
                                       
    Delay(100);
    TRX_CE=0;                                        // Set nRF905 in standby mode
        CSN=0;                                                // Spi enable for write a spi command
    Delay(1);
        SpiWrite(RRP);
                // Read payload command
        for (i = 0 ;i < 4 ;i++)
        {   
                TxRxBuffer=SpiRead();                // Read data and save to buffer
                  
        }
        CSN=1;
    Delay(10);
        TRX_CE=1;                                                       
}
////////////////
void RX(void)         
{
          SetRxMode();                        // Set nRF905 in Rx mode
                  if( CheckDR()==1)
                   {
                     Delay(10);
                       RxPacket(TxRxBuffer);
                    }
                        if(TxRxBuffer[0]==0x29)
          {
              LED1=0;
              LED2=1;
              Delay(10);
                  }
                        if(TxRxBuffer[0]==0x30)
                        {
                      LED1=1;
                          LED2=0;
                          Delay(10);
                        }
          Delay(10);                          // Recive data by nRF905
}


void main()
{

  nRF905Init();
  Config905();
  while(1)
   {

RX();

   }
}


麻烦大家帮忙看看了,搞了很久还是没进展,有没高手可以讨论下,还有有没类似调试成功的程序,万分感谢。。。
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-10 05:03

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表