252177861 发表于 2009-2-21 20:38:19

求助 nrf2401程序的问题

请各位帮忙看一下下面的代码有什么问题   在调试NRF2410的时候(就是淘宝超值区的那个模块),接受不到一个数据包,单片机用得是C8051f020
各IO口都已经配置好 该推挽的推挽了 该上拉的也上啦了

#include "nrf2401.h"

code uchar CofigBuf=
      {
      0x00,               //接收频道二有效数据长度
         
      40,             //接收频道一有效数据长度
         
      0x00,0x00,0x00,0x00,0x00,   //接收频道二地址
         
      0x00,0xcd,0xef,0x12,0xaa,   //接收频道一地址
         
      0x83,               //32位地址,16位CRC,使能CRC
                        //bit7~2:ADDR_W,最大40位
                                    
                        //bit1:CRC_L
                        //Logic 0: 8 bit CRC   
                        //Logic 1: 16 bit CRC
                                    
                                    
                        //BIT0:CRC_EN
                        //Logic 0: On-chip CRC generation/checking disabled
                        //Logic 1: On-chip CRC generation/checking enabled
                                    
                                    
      0x4f,               //ShockBurst模式,250 kbps,16M晶振,0dBm最大功率
         
                        //Bit 15:RX2_EN
                        //Logic 0: One channel receive
                        //Logic 1: Two channels receive
                                    
                        //Bit 14:
                        //Communication Mode:
                        //Logic 0: nRF2401 operates in direct mode.
                        //Logic 1: nRF2401 operates in ShockBurst. mode

                        //Bit 13:
                        //RF Data Rate:
                        //Logic 0: 250 kbps
                        //Logic 1: 1 Mbps
                                    
                        //Bit 12-10:Selects the nRF2401 crystal frequency to be used:
                        //D12   D11   D10   Crystal Frequency
                        //0       0   0               4
                        //0   0   1               8
                        //0   1   0         12
                        //0   1   1         16
                        //1   0   0         20
                                    
                        //Bit 9-8:RF_PWR: Sets nRF2401 RF output power in transmit mode:
                        //D9D8P
                        //0   0         -20
                        //0   1         -10
                        //1   0         -5
                        //1         1         0
                                    
                                    
                                    
      0x50            //2400+4*1=2404MHZ=2.404G,发送模式
         
                        //Bit 7 – 1:RF_CH#: Sets the frequency channel the nRF2401 operates on.
                        //Channelrf = 2400MHZ + RF_CH# * 1.0MHZ
                                    
                        //Bit 0:Set active mode:
                        //Logic 0: transmit mode
                        //Logic 1: receive mode
      };
         
         
uchar AddrCofig={0xcd, 0xef, 0x12, 0xaa};                //地址


/*****************************************************************************************
//函数名void NRF2410_IO_Init(void)
//输入:无
//输出:无
//功能描述:初始化IO程序
/*****************************************************************************************/
void NRF2410_IO_Init(void)
{
    POW_UP = 1;
    Delay1ms(4);
    CE = 0;
    CS = 0;
    CLK1 = 0;
}

/*****************************************************************************************
//函数名:void NRF2410_WriteByte(uchar dat)
//输入:发送的数据
//输出:无
//功能描述:串行发送一个字节高位在前
/*****************************************************************************************/
static
void NRF2410_WriteByte(uchar dat)
{
    uchar i;

    CLK1 = 0;
    _nop_();
    _nop_();
    for(i=0; i<8; i++)
    {
      if(dat & 0x80)
      {
            DATA = 1;
      }
      else
      {
            DATA = 0;
      }
      CLK1 = 1;
      _nop_();
      _nop_();
      dat <<= 1;
      CLK1 = 0;
      _nop_();
      _nop_();
    }
}


/*****************************************************************************************
//函数名:uchar NRF2401_ReadByte(void)
//输入:无
//输出:读出的数据
//功能描述:SPI读出一个字节
/*****************************************************************************************/
static
uchar NRF2401_ReadByte(void)
{
    uchar i,temp= 0;
    DATA = 1;   
    CLK1 = 0;
    _nop_();
    _nop_();
    for(i=0; i<8; i++)
    {
      temp <<= 1;
      CLK1 = 1;
      _nop_();
      _nop_();
      if(DATA)++temp;
      CLK1 = 0;
      _nop_();
      _nop_();
    }
    return temp;
}

/*****************************************************************************************
//函数名:void NRF2401_SetTxMode(void)
//输入:无
//输出:无
//功能描述:转为发送模式
/*****************************************************************************************/
void NRF2401_SetTxMode(void)
{
      uchar ch;
      CS = 1;
      Delay1us(10);
      ch = FRESELE;
      NRF2410_WriteByte(ch);
      CS = 0;
      Delay1us(200);
}

/*****************************************************************************************
//函数名:NRF2401_SetRxMode()
//输入:无
//输出:无
//功能描述:转为接收模式
/*****************************************************************************************/
void NRF2401_SetRxMode(void)
{
      uchar ch;
      CS = 1;
      Delay1us(10);
      ch = FRESELE;
      NRF2410_WriteByte(ch | 0x01);
      CS = 0;
      Delay1us(200);            //200us
}

/*****************************************************************************************
//函数名:NRF2401_Init()
//输入:无
//输出:无
//功能描述:Nrf2401_Init初始化,配置成32位地址。
/*****************************************************************************************/
void NRF2401_Init(void)
{
    uchar i;
    CE = 0;
    CS = 1;
    Delay1us(10);
       for(i=0; i<15; i++)
       {
          NRF2410_WriteByte(CofigBuf);
       }
    CS = 0;   
    Delay1ms(1);
}

/*****************************************************************************************
//函数名:void NRF2401_TX_Data(uchar *TxBuf)
//输入:uchar *TxBuf   5字节缓冲
//输出:无
//功能描述:发送缓冲区的数据。
/*****************************************************************************************/
void NRF2401_TX_Data(uchar *TxBuf)
{
    uchar i;   
    NRF2401_SetTxMode();   
    CE = 1;   
    Delay1us(100);   
       for(i=0; i<4; i++)
       {
         NRF2410_WriteByte(AddrCofig); //发送地址
       }
       for(i=0; i<5; i++)
       {
         NRF2410_WriteByte(TxBuf);   //发送五位数据
       }
    CE = 0;
    Delay1ms(1);
}

/*****************************************************************************************
//函数名:uchar NRF2401_RX_Data(uchar *RxBuf)
//输入:uchar *RxBuf    5字节接收缓冲
//输出:0 :超时错误1 :正常返回
//功能描述:接收数据 保存在*RxBuf接收缓冲区内
/*****************************************************************************************/
uchar NRF2401_RX_Data(uchar *RxBuf)
{
    uchar i;
    uint time=0;
    NRF2401_SetRxMode();
    CE = 1;   
    while(DR1 == 0)
    {
      ++time;
      if(DR1==1)break ;
      if (time > 200)
      {
            CE = 0;
            return 0;
      }                  
    }
    i = 0;
    while(DR1)
    {
      RxBuf = NRF2401_ReadByte();
      ++i;
      if (i == 5)
      break;
    }
    while(DR1)
    {
      NRF2401_ReadByte();
    }
    CE = 0;
    return 1;
}

252177861 发表于 2009-2-22 01:39:30

大哥们帮忙看会啊

155107149 发表于 2009-2-22 13:05:46

帮顶。。。。

252177861 发表于 2009-2-22 16:15:55

自己顶   都已经淹没了~~~~

252177861 发表于 2009-2-22 16:18:07

问题还没解决的啊    期待哪位好心人 或者调试过2401的看看   帮忙想想问题出哪了   现在也不晓得模块工作没的

252177861 发表于 2009-2-22 18:08:27

有谁能发个能用的代码让我测试一下吗?

zajia 发表于 2009-2-22 20:11:12

ICC的,肯定能用,是“avenbbs ”提供的。


这个链接里的NRF2401子程序就是参考后,改的。
http://www.ouravr.com/bbs/bbs_content.jsp?bbs_sn=3213867&bbs_page_no=1&search_mode=3&search_text=zajia&bbs_id=9999

点击此处下载 ourdev_421480.rar(文件大小:37K) (原文件名:ourdev_556789_NRF2401_icc719.rar)

252177861 发表于 2009-2-25 19:49:26

谢谢楼上的!

szy494468597 发表于 2012-3-7 10:41:49

mark

icecreamja123 发表于 2013-8-26 18:37:49

谢谢楼上的
页: [1]
查看完整版本: 求助 nrf2401程序的问题