微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > nrf24l01不能接收数据啊!

nrf24l01不能接收数据啊!

时间:10-02 整理:3721RD 点击:

手上有两个nrf,可以发送数据就是不能接收,大家说说是什么问题啊?如果有完整的收发程序,麻烦发上来!谢谢大家!请大家帮帮忙

发送函数:
#include <reg52.h>
#include <intrins.h>
sbit         MISO        =P1^5;
sbit         MOSI        =P1^4;
sbit        SCK            =P1^3;
sbit        CE            =P1^1;
sbit        CSN                =P1^2;
sbit        IRQ                =P1^6;
sbit    LED2    =P0^5;
sbit    LED1    =P0^4;
sbit    LED3    =P0^6;
sbit    KEY1    =P3^0;
sbit    KEY2    =P3^1;

#define TX_ADR_WIDTH    5   // 5 bytes TX address width
#define RX_ADR_WIDTH    5   // 5 bytes RX address width
#define TX_PLOAD_WIDTH  20  // 20 bytes TX payload
#define RX_PLOAD_WIDTH  20  // 20 bytes TX payload
typedef unsigned char BYTE;
typedef unsigned char uchar;
//****************************************************************//
// SPI(nRF24L01) commands
#define READ_REG        0x00  // Define read command to register
#define WRITE_REG       0x20  // Define write command to register
#define RD_RX_PLOAD     0x61  // Define RX payload register address
#define WR_TX_PLOAD     0xA0  // Define TX payload register address
#define FLUSH_TX        0xE1  // Define flush TX register command
#define FLUSH_RX        0xE2  // Define flush RX register command
#define REUSE_TX_PL     0xE3  // Define reuse TX payload register command
#define NOP             0xFF  // Define No Operation, might be used to read status register

//***************************************************//
// SPI(nRF24L01) registers(addresses)
#define CONFIG          0x00  // 'Config' register address
#define EN_AA           0x01  // 'Enable Auto Acknowledgment' register address
#define EN_RXADDR       0x02  // 'Enabled RX addresses' register address
#define SETUP_AW        0x03  // 'Setup address width' register address
#define SETUP_RETR      0x04  // 'Setup Auto. Retrans' register address
#define RF_CH           0x05  // 'RF channel' register address
#define RF_SETUP        0x06  // 'RF setup' register address
#define STATUS          0x07  // 'Status' register address
#define OBSERVE_TX      0x08  // 'Observe TX' register address
#define CD              0x09  // 'Carrier Detect' register address
#define RX_ADDR_P0      0x0A  // 'RX address pipe0' register address
#define RX_ADDR_P1      0x0B  // 'RX address pipe1' register address
#define RX_ADDR_P2      0x0C  // 'RX address pipe2' register address
#define RX_ADDR_P3      0x0D  // 'RX address pipe3' register address
#define RX_ADDR_P4      0x0E  // 'RX address pipe4' register address
#define RX_ADDR_P5      0x0F  // 'RX address pipe5' register address
#define TX_ADDR         0x10  // 'TX address' register address
#define RX_PW_P0        0x11  // 'RX payload width, pipe0' register address
#define RX_PW_P1        0x12  // 'RX payload width, pipe1' register address
#define RX_PW_P2        0x13  // 'RX payload width, pipe2' register address
#define RX_PW_P3        0x14  // 'RX payload width, pipe3' register address
#define RX_PW_P4        0x15  // 'RX payload width, pipe4' register address
#define RX_PW_P5        0x16  // 'RX payload width, pipe5' register address
#define FIFO_STATUS     0x17  // 'FIFO Status Register' register address
//***************************************************************//
//                   FUNCTION's PROTOTYPES  //
/****************************************************************/
// void SPI_Init(BYTE Mode);     // Init HW or SW SPI
BYTE SPI_RW(BYTE byte);                                // Single SPI read/write
BYTE SPI_Read(BYTE reg);                               // Read one byte from nRF24L01
BYTE SPI_RW_Reg(BYTE reg, BYTE byte);                  // Write one byte to register 'reg'
BYTE SPI_Write_Buf(BYTE reg, BYTE *pBuf, BYTE bytes);  // Writes multiply bytes to one register
BYTE SPI_Read_Buf(BYTE reg, BYTE *pBuf, BYTE bytes);   // Read multiply bytes from one register
//*****************************************************************/
void inerDelay_us(unsigned char n);
void init_io(void) ;
void SetRX_Mode(void);
unsigned char nRF24L01_RxPacket(unsigned char* rx_buf);
void nRF24L01_TxPacket(unsigned char * tx_buf);

extern uchar const TX_ADDRESS[TX_ADR_WIDTH];//TX address
extern uchar const RX_ADDRESS[RX_ADR_WIDTH];//;RX address

uchar const TX_ADDRESS[TX_ADR_WIDTH]  = {0x34,0x43,0x10,0x10,0x01}; // Define a static TX address
uchar const RX_ADDRESS[RX_ADR_WIDTH]  = {0x34,0x43,0x10,0x10,0x01}; // Define a static RX address

uchar         bdata sta;
sbit        RX_DR        =sta^6;
sbit        TX_DS        =sta^5;
sbit        MAX_RT        =sta^4;
void inerDelay_us(unsigned char n)
{
        for(;n>0;n--)
                _nop_();
}
void init_io(void)
{
    inerDelay_us(100);
        CE=0;    // chip enable
        CSN=1;   // Spi  disable
        SCK=0;   //
        SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH);    // 写本地地址       
        SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS, RX_ADR_WIDTH); // 写接收端地址
        SPI_RW_Reg(WRITE_REG + EN_AA, 0x01);      //  频道0自动        ACK应答允许       
        SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01);  //  允许接收地址只有频道0,如果需要多频道可以参考Page21  
        SPI_RW_Reg(WRITE_REG + RF_CH, 0);        //   设置信道工作为2.4GHZ,收发必须一致
        SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //设置接收数据长度,本次设置为32字节
        SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07);                   //设置发射速率为1MHZ,发射功率为最大值0dB发送
}
/**************************************************
Function: SPI_RW();
  Description:
  Writes one byte to nRF24L01, and return the byte read
  from nRF24L01 during write, according to SPI protocol
/**************************************************/
uchar SPI_RW(uchar byte)
{
        uchar bit_ctr;
           for(bit_ctr=0;bit_ctr<8;bit_ctr++) // output 8-bit
           {
                MOSI = (byte & 0x80);         // output 'byte', MSB to MOSI
                byte = (byte << 1);           // shift next bit into MSB..
                SCK = 1;                      // Set SCK high..
                byte |= MISO;                         // capture current MISO bit
                SCK = 0;                              // ..then set SCK low again
           }
    return(byte);                             // return read byte
}
/**************************************************/
/**************************************************
Function: SPI_RW_Reg();
  Description:
  Writes value 'value' to register 'reg'
/**************************************************/
uchar SPI_RW_Reg(BYTE reg, BYTE value)
{
        uchar status;
       
        CSN = 0;                   // CSN low, init SPI transaction
        status = SPI_RW(reg);      // select register
        SPI_RW(value);             // ..and write value to it..
        CSN = 1;                   // CSN high again
       
        return(status);            // return nRF24L01 status byte
}
/**************************************************/
/**************************************************
Function: SPI_Read();
  Description:
  Read one byte from nRF24L01 register, 'reg'
/**************************************************/
BYTE SPI_Read(BYTE reg)
{
        BYTE reg_val;
       
        CSN = 0;                // CSN low, initialize SPI communication...
        SPI_RW(reg);            // Select register to read from..
        reg_val = SPI_RW(0);    // ..then read registervalue
        CSN = 1;                // CSN high, terminate SPI communication
       
        return(reg_val);        // return register value
}
/**************************************************/
/**************************************************
Function: SPI_Read_Buf();
  Description:
  
/**************************************************/
uchar SPI_Read_Buf(BYTE reg, BYTE *pBuf, BYTE bytes)
{
        uchar status,byte_ctr;
       
        CSN = 0;                                    // Set CSN low, init SPI tranaction
        status = SPI_RW(reg);                       // Select register to write to and read status byte
       
        for(byte_ctr=0;byte_ctr<bytes;byte_ctr++)
                pBuf[byte_ctr] = SPI_RW(0);    //
       
        CSN = 1;                           
       
        return(status);                    // return nRF24L01 status byte
}
/**************************************************/
/**************************************************
Function: SPI_Write_Buf();
  Description:
  Writes contents of buffer '*pBuf' to nRF24L01
  Typically used to write TX payload, Rx/Tx address
/**************************************************/
uchar SPI_Write_Buf(BYTE reg, BYTE *pBuf, BYTE bytes)
{
        uchar status,byte_ctr;
       
        CSN = 0;                  
        status = SPI_RW(reg);   
        for(byte_ctr=0; byte_ctr<bytes; byte_ctr++)
                SPI_RW(*pBuf++);
        CSN = 1;
        return(status);
}
/**************************************************/
/**************************************************
Function: RX_Mode();
  Description:
  
/**************************************************/
void SetRX_Mode(void)
{
        CE=0;
        SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f);     // IRQ收发完成中断响应,16位CRC ,主接收
       
        CE = 1;
        inerDelay_us(130);
}
/**************************************************/
unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
{
    unsigned char revale=0;
        sta=SPI_Read(STATUS);        // 读取状态寄存其来判断数据接收状况
        if(RX_DR)                                // 判断是否接收到数据
        {
        LED3=0;
            CE = 0;                         //SPI使能
                SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer
                revale =1;                        //读取数据完成标志
        }
        SPI_RW_Reg(WRITE_REG+STATUS,sta);   //接收到数据后RX_DR,TX_DS,MAX_PT都置高为1,通过写1来清楚中断标志
        return revale;
}
/**************************************************
Function: nRF24L01_TxPacket();
  Description:
  This function initializes one nRF24L01 device to
  TX mode, set TX address, set RX address for auto.ack,
  fill TX payload, select RF channel, datarate & TX pwr.
  PWR_UP is set, CRC(2 bytes) is enabled, & PRIM:TX.
  
        ToDo: One high pulse(>10us) on CE will now send this
        packet and expext an acknowledgment from the RX device.
/**************************************************/
void nRF24L01_TxPacket(unsigned char * tx_buf)
{
       
   CE=0;                        //StandBy I模式       
        SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // 装载接收端地址
        SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH);                          // 装载数据       
        SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e);                    // IRQ收发完成中断响应,16位CRC,主发送
        CE=1;                 //置高CE,激发数据发送
        LED1=0;
        LED2=0;
        inerDelay_us(10);
       
}
void Delay(unsigned int s)
{
        unsigned int i;
        for(i=0; i<s; i++);
        for(i=0; i<s; i++);
}
void main(void)
{
        unsigned char a;
        unsigned char leng =0;
        unsigned char tf =0;
        unsigned char TxBuf[20]={0};         //
        unsigned char RxBuf[20]={0};       
    init_io() ;
        while(1)
        {
       
                     TxBuf[1] = 0x01;
                        TxBuf[2] = 0x02;
                        nRF24L01_TxPacket(TxBuf);        // Transmit Tx buffer data
                        TxBuf[1] = 0x00;
                        TxBuf[2] = 0x00;
                       
                        Delay(1000);
                        LED1 = 1;
                        LED2 = 1;
                        Delay(100000);
        }
       
}

接收函数:
#include <reg52.h>
#include <intrins.h>

//hard pin mapping
sbit         MISO        =P1^5;
sbit         MOSI        =P1^4;
sbit        SCK            =P1^3;
sbit        CE            =P1^1;
sbit        CSN                =P1^2;
sbit        IRQ                =P1^6;
sbit    LED3    =P0^6;
sbit    LED2    =P0^5;
sbit    LED1    =P0^4;
sbit    KEY1    =P3^0;
sbit    KEY2    =P3^1;

#define TX_ADR_WIDTH    5   // 5 bytes TX address width
#define RX_ADR_WIDTH    5   // 5 bytes RX address width
#define TX_PLOAD_WIDTH  20  // 20 bytes TX payload
#define RX_PLOAD_WIDTH  20  // 20 bytes TX payload
typedef unsigned char BYTE;
typedef unsigned char uchar;
//****************************************************************//
// SPI(nRF24L01) commands
#define READ_REG        0x00  // Define read command to register
#define WRITE_REG       0x20  // Define write command to register
#define RD_RX_PLOAD     0x61  // Define RX payload register address
#define WR_TX_PLOAD     0xA0  // Define TX payload register address
#define FLUSH_TX        0xE1  // Define flush TX register command
#define FLUSH_RX        0xE2  // Define flush RX register command
#define REUSE_TX_PL     0xE3  // Define reuse TX payload register command
#define NOP             0xFF  // Define No Operation, might be used to read status register

//***************************************************//
// SPI(nRF24L01) registers(addresses)
#define CONFIG          0x00  // 'Config' register address
#define EN_AA           0x01  // 'Enable Auto Acknowledgment' register address
#define EN_RXADDR       0x02  // 'Enabled RX addresses' register address
#define SETUP_AW        0x03  // 'Setup address width' register address
#define SETUP_RETR      0x04  // 'Setup Auto. Retrans' register address
#define RF_CH           0x05  // 'RF channel' register address
#define RF_SETUP        0x06  // 'RF setup' register address
#define STATUS          0x07  // 'Status' register address
#define OBSERVE_TX      0x08  // 'Observe TX' register address
#define CD              0x09  // 'Carrier Detect' register address
#define RX_ADDR_P0      0x0A  // 'RX address pipe0' register address
#define RX_ADDR_P1      0x0B  // 'RX address pipe1' register address
#define RX_ADDR_P2      0x0C  // 'RX address pipe2' register address
#define RX_ADDR_P3      0x0D  // 'RX address pipe3' register address
#define RX_ADDR_P4      0x0E  // 'RX address pipe4' register address
#define RX_ADDR_P5      0x0F  // 'RX address pipe5' register address
#define TX_ADDR         0x10  // 'TX address' register address
#define RX_PW_P0        0x11  // 'RX payload width, pipe0' register address
#define RX_PW_P1        0x12  // 'RX payload width, pipe1' register address
#define RX_PW_P2        0x13  // 'RX payload width, pipe2' register address
#define RX_PW_P3        0x14  // 'RX payload width, pipe3' register address
#define RX_PW_P4        0x15  // 'RX payload width, pipe4' register address
#define RX_PW_P5        0x16  // 'RX payload width, pipe5' register address
#define FIFO_STATUS     0x17  // 'FIFO Status Register' register address
//***************************************************************//
//                   FUNCTION's PROTOTYPES  //
/****************************************************************/
// void SPI_Init(BYTE Mode);     // Init HW or SW SPI
BYTE SPI_RW(BYTE byte);                                // Single SPI read/write
BYTE SPI_Read(BYTE reg);                               // Read one byte from nRF24L01
BYTE SPI_RW_Reg(BYTE reg, BYTE byte);                  // Write one byte to register 'reg'
BYTE SPI_Write_Buf(BYTE reg, BYTE *pBuf, BYTE bytes);  // Writes multiply bytes to one register
BYTE SPI_Read_Buf(BYTE reg, BYTE *pBuf, BYTE bytes);   // Read multiply bytes from one register
//*****************************************************************/
void inerDelay_us(unsigned char n);
void init_io(void) ;
void SetRX_Mode(void);
unsigned char nRF24L01_RxPacket(unsigned char* rx_buf);
void nRF24L01_TxPacket(unsigned char * tx_buf);

extern uchar const TX_ADDRESS[TX_ADR_WIDTH];//TX address
extern uchar const RX_ADDRESS[RX_ADR_WIDTH];//;RX address
uchar const TX_ADDRESS[TX_ADR_WIDTH]  = {0x34,0x43,0x10,0x10,0x01}; // Define a static TX address
uchar const RX_ADDRESS[RX_ADR_WIDTH]  = {0x34,0x43,0x10,0x10,0x01}; // Define a static RX address

uchar         bdata sta;
sbit        RX_DR        =sta^6;
sbit        TX_DS        =sta^5;
sbit        MAX_RT        =sta^4;
void inerDelay_us(unsigned char n)
{
        for(;n>0;n--)
                _nop_();
}
void init_io(void)
{
     inerDelay_us(100);
  CE=0;    // chip enable
  CSN=1;   // Spi disable
  SCK=0;   // Spi clock line init high
SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH);    // 写本地地址
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS, RX_ADR_WIDTH); // 写接收端地址
SPI_RW_Reg(WRITE_REG + EN_AA, 0x01);      //  频道0自动 ACK应答允许
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01);  //  允许接收地址只有频道0,如果需要多频道可以参考Page21
SPI_RW_Reg(WRITE_REG + RF_CH, 0);        //   设置信道工作为2.4GHZ,收发必须一致
SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //设置接收数据长度,本次设置为32字节
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07);     //设置发射速率为1MHZ,发射功率为最大值0dB
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f);     // IRQ收发完成中断响应,16位CRC ,主接收
}
/**************************************************
Function: SPI_RW();
  Description:
  Writes one byte to nRF24L01, and return the byte read
  from nRF24L01 during write, according to SPI protocol
/**************************************************/
uchar SPI_RW(uchar byte)
{
        uchar bit_ctr;
           for(bit_ctr=0;bit_ctr<8;bit_ctr++) // output 8-bit
           {
                MOSI = (byte & 0x80);         // output 'byte', MSB to MOSI
                byte = (byte << 1);           // shift next bit into MSB..
                SCK = 1;                      // Set SCK high..
                byte |= MISO;                         // capture current MISO bit
                SCK = 0;                              // ..then set SCK low again
           }
    return(byte);                             // return read byte
}
/**************************************************/
/**************************************************
Function: SPI_RW_Reg();
  Description:
  Writes value 'value' to register 'reg'
/**************************************************/
uchar SPI_RW_Reg(BYTE reg, BYTE value)
{
        uchar status;
       
        CSN = 0;                   // CSN low, init SPI transaction
        status = SPI_RW(reg);      // select register
        SPI_RW(value);             // ..and write value to it..
        CSN = 1;                   // CSN high again
       
        return(status);            // return nRF24L01 status byte
}
/**************************************************/
/**************************************************
Function: SPI_Read();
  Description:
  Read one byte from nRF24L01 register, 'reg'
/**************************************************/
BYTE SPI_Read(BYTE reg)
{
        BYTE reg_val;
       
        CSN = 0;                // CSN low, initialize SPI communication...
        SPI_RW(reg);            // Select register to read from..
        reg_val = SPI_RW(0);    // ..then read registervalue
        CSN = 1;                // CSN high, terminate SPI communication
       
        return(reg_val);        // return register value
}
/**************************************************/
/**************************************************
Function: SPI_Read_Buf();
  Description:
  
/**************************************************/
uchar SPI_Read_Buf(BYTE reg, BYTE *pBuf, BYTE bytes)
{
        uchar status,byte_ctr;
       
        CSN = 0;                                    // Set CSN low, init SPI tranaction
        status = SPI_RW(reg);                       // Select register to write to and read status byte
       
        for(byte_ctr=0;byte_ctr<bytes;byte_ctr++)
                pBuf[byte_ctr] = SPI_RW(0);    //
       
        CSN = 1;                           
       
        return(status);                    // return nRF24L01 status byte
}
/**************************************************/
/**************************************************
Function: SPI_Write_Buf();
  Description:
  Writes contents of buffer '*pBuf' to nRF24L01
  Typically used to write TX payload, Rx/Tx address
/**************************************************/
uchar SPI_Write_Buf(BYTE reg, BYTE *pBuf, BYTE bytes)
{
        uchar status,byte_ctr;
       
        CSN = 0;                  
        status = SPI_RW(reg);   
        for(byte_ctr=0; byte_ctr<bytes; byte_ctr++) //
                SPI_RW(*pBuf++);
        CSN = 1;                 // Set CSN high again
        return(status);          //
}
/**************************************************/
/**************************************************
Function: RX_Mode();
  Description:
  
/**************************************************/
void SetRX_Mode(void)
{
        CE=0;
        inerDelay_us(130);
        SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f);     // IRQ收发完成中断响应,16位CRC ,主接收
        CE = 1;
        inerDelay_us(130);
}
/**************************************************/
unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
{
    unsigned char revale=0;
        //SetRX_Mode();
       
        sta=SPI_Read(STATUS);        // read register STATUS's value
        LED3=0;
        if(RX_DR)                                // if receive data ready (RX_DR) interrupt
        {
            CE = 0;
                SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer
                revale =1;//we have receive data
                LED1=0;
                LED2=0;                                                                        
                       
        }
        SPI_RW_Reg(WRITE_REG+STATUS,sta);// clear RX_DR or TX_DS or MAX_RT interrupt flag
       
        return revale;
}
/**************************************************
Function: nRF24L01_TxPacket();
  Description:
  This function initializes one nRF24L01 device to
  TX mode, set TX address, set RX address for auto.ack,
  fill TX payload, select RF channel, datarate & TX pwr.
  PWR_UP is set, CRC(2 bytes) is enabled, & PRIM:TX.
  
        ToDo: One high pulse(>10us) on CE will now send this
        packet and expext an acknowledgment from the RX device.
/**************************************************/
void nRF24L01_TxPacket(unsigned char * tx_buf)
{
         CE=0;   //StandBy I模式
        SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // 装载接收端地址
        SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH);     // 装载数据
        SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e);      // IRQ收发完成中断响应,16位CRC,主发送
        CE=1;   //置高CE,激发数据发送
        inerDelay_us(10);
       
}
void Delay(unsigned int s)
{
        unsigned int i;
        for(i=0; i<s; i++);
        for(i=0; i<s; i++);
}
void initcom ()
{
    TMOD=0x20;
        PCON=0x00;
        SCON=0x50;
        TH1=0xFd;//波特率9600
        TL1=0xFd;
        TR1=1;
}
void comm (int len)
{  
     SBUF=len;
     while(!TI);
     TI=0;
}
void main(void)
{
       
        unsigned char a,b;
        unsigned char leng =0;
        unsigned char tf =0;
        unsigned char TxBuf[20]={0};         //
        unsigned char RxBuf[20]={0};       
    init_io() ;
        initcom ();
        while(1)
        {
              SetRX_Mode();
                   if(nRF24L01_RxPacket(RxBuf))
                {
                        comm(RxBuf[1]);
                        comm(RxBuf[2]);
                        Delay(1000);
                        LED1=1;
                        LED2=1;
                }               
                RxBuf[1] = 0x00;
                RxBuf[2] = 0x00;
                        a=SPI_Read();
                comm(a);              //串口一直显示STATUS是0x0f
        }
       
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top