STMF401 无线传输收不到数据?

2019-07-21 04:43发布

 
#include "NRF24L01.h"
#include "SPI.h"
#include "USART.h"
#include "stdio.h"  
uint8_t NRF24L01_TXDATA[TX_PLOAD_WIDTH];//nrf24l01????????????
uint8_t NRF24L01_RXDATA[RX_PLOAD_WIDTH];//nrf24l01?è??·?????????
//????????????·??????·???????©?à??·????÷?????????ò·?????????????????
u8  TX_ADDRESS[TX_ADR_WIDTH]= {0x34,0xc3,0x10,0x10,0x11}; //±??????·
u8  RX_ADDRESS[RX_ADR_WIDTH]= {0x34,0xc3,0x10,0x10,0x11}; //???????·       //???????÷
uint8_t NRF_Write_Reg(uint8_t reg, uint8_t value)
{
    uint8_t status;
    SPI_CSN_L();      
    status = SPI_RW(reg); 
    SPI_RW(value);    /* ?????? */
    SPI_CSN_H();       /* ???????÷?? */
    return  status;
}
//???????÷
uint8_t NRF_Read_Reg(uint8_t reg)
{
    uint8_t reg_val;
    SPI_CSN_L();     
    SPI_RW(reg);    
    reg_val = SPI_RW(0);   /* ???????????÷·??????? */
    SPI_CSN_H(); 
 
    return  reg_val;
}
//????????
uint8_t NRF_Write_Buf(uint8_t reg, uint8_t *pBuf, uint8_t uchars)
{
    uint8_t i;
    uint8_t status;
    SPI_CSN_L();            /* ???¨?÷?? */
    status = SPI_RW(reg); /* ???????÷???· */
    for(i=0; i<uchars; i++)
    {
        SPI_RW(pBuf);  /* ?????? */
    }
    SPI_CSN_H();      /* ???????÷?? */
    return  status; 
}
//????????
uint8_t NRF_Read_Buf(uint8_t reg, uint8_t *pBuf, uint8_t uchars)
{
    uint8_t i;
    uint8_t status;
    SPI_CSN_L();      /* ???¨?÷?? */
    status = SPI_RW(reg); /* ???????÷???· */
    for(i=0; i<uchars; i++)
    {
        pBuf = SPI_RW(0); /* ????·??????? */  
    }
    SPI_CSN_H();      /* ???????÷?? */
    return  status;
}
//??????°ü
void NRF_TxPacket(uint8_t * tx_buf, uint8_t len)

    SPI_CE_L();   //StandBy I???? 
    NRF_Write_Buf(WR_TX_PLOAD, tx_buf, len);     // ×°?????? 
    SPI_CE_H();   //????CE???¤·?????·???
  
}
// ??????°ü
uint8_t NRF_RxPacket(uint8_t * rx_buf, uint8_t len)
{
 u8 sta;               
        //SPI2_SetSpeed(SPI_SPEED_4); //spi??????9Mhz?¨24L01??×??óSPI?±????10Mhz?©  
 sta=NRF_Read_Reg(NRFRegSTATUS);  //????×????????÷????     
 NRF_Write_Reg(NRF_WRITE_REG+NRFRegSTATUS,sta); //????TX_DS?òMAX_RT????±ê??
 if(sta&RX_OK)//??????????
 {
  NRF_Write_Buf(RD_RX_PLOAD,rx_buf,len);//????????
  NRF_Write_Reg(FLUSH_RX,0xff);//????RX FIFO?????÷
  return 0;
 }   
 return 1;//??????????????
 
 
}
//??????
char NRF24L01_Init(void)
{
   SPI1_Init();
   return NRF24L01_Check();
}
//????????
void SetRX_Mode(void)
{
    SPI_CE_L();
   NRF_Write_Reg(FLUSH_RX,0xff);//????TX FIFO?????÷   
   NRF_Write_Buf(NRF_WRITE_REG+RX_ADDR_P0,(uint8_t*)RX_ADDRESS,RX_ADR_WIDTH);//??RX???????·
    NRF_Write_Reg(NRF_WRITE_REG+EN_AA,0x01);    //?????¨??0??×???????   
   NRF_Write_Reg(NRF_WRITE_REG+EN_RXADDR,0x01);//?????¨??0?????????·   
   NRF_Write_Reg(NRF_WRITE_REG+RF_CH,40);      //?è??RF?¨??????   
   NRF_Write_Reg(NRF_WRITE_REG+RX_PW_P0,RX_PLOAD_WIDTH);//?????¨??0?????§?????í??     
   NRF_Write_Reg(NRF_WRITE_REG+RF_SETUP,0x0f);//?è??TX·???????,0db????,2Mbps,?????ù????????  
   NRF_Write_Reg(NRF_WRITE_REG+CONFIG, 0x0f);//?????ù±??¤×÷??????????WR_UP,EN_CRC,16BIT_CRC,????????
    SPI_CE_H();
    printf(" NRF24L01?è??????????... ");
} //·???????
void SetTX_Mode(void)
{
    SPI_CE_L();
    NRF_Write_Reg(FLUSH_TX,0xff);          //????TX FIFO?????÷   
    NRF_Write_Buf(NRF_WRITE_REG+TX_ADDR,(u8*)TX_ADDRESS,TX_ADR_WIDTH);  //??TX???????·
   NRF_Write_Buf(NRF_WRITE_REG+RX_ADDR_P0,(u8*)RX_ADDRESS,RX_ADR_WIDTH);  //?è??TX???????·,?÷??????????ACK  
   NRF_Write_Reg(NRF_WRITE_REG+EN_AA,0x01);     //?????¨??0??×???????   
   NRF_Write_Reg(NRF_WRITE_REG+EN_RXADDR,0x01); //?????¨??0?????????· 
   NRF_Write_Reg(NRF_WRITE_REG+SETUP_RETR,0x1a);//?è??×?????·??????±??:500us + 86us;×??ó×?????·?????:10??
   NRF_Write_Reg(NRF_WRITE_REG+RF_CH,40);       //?è??RF?¨????40
   NRF_Write_Reg(NRF_WRITE_REG+RF_SETUP,0x0f);  //?è??TX·???????,0db????,2Mbps,?????ù????????  
   NRF_Write_Reg(NRF_WRITE_REG+CONFIG,0x0e);    //?????ù±??¤×÷??????????WR_UP,EN_CRC,16BIT_CRC,????????,?????ù??????
    SPI_CE_H();
   printf(" NRF24L01?è??·???????... ");
}
static char  ReceiveDataFormNRF()
{
 printf("Received: %s ", NRF24L01_RXDATA); 
} //?é??????
void Nrf_Irq(void)
{
    uint8_t sta = NRF_Read_Reg(NRF_READ_REG + NRFRegSTATUS);
    if(sta & (1<<RX_DR))//????????
    {
        NRF_Read_Buf(RD_RX_PLOAD,NRF24L01_RXDATA, RX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer
        ReceiveDataFormNRF();      //×???×?????
    }
    NRF_Write_Reg(0x27, sta);//????nrf??????±ê????
}
//????????
u8 NRF24L01_RxPacket(u8 *rxbuf)
{
 u8 sta;               
        //SPI2_SetSpeed(SPI_SPEED_4); //spi??????9Mhz?¨24L01??×??óSPI?±????10Mhz?©  
 sta=NRF_Read_Reg(NRFRegSTATUS);  //????×????????÷????     
 NRF_Write_Reg(NRF_WRITE_REG+NRFRegSTATUS,sta); //????TX_DS?òMAX_RT????±ê??
 if(sta&RX_OK)//??????????
 {
  NRF_Write_Buf(RD_RX_PLOAD,rxbuf,RX_PLOAD_WIDTH);//????????
  NRF_Write_Reg(FLUSH_RX,0xff);//????RX FIFO?????÷
  return 0;
 }   
 return 1;//??????????????
}     //????SPI??????·?????NRF??????·?????
u8 NRF24L01_Check(void)
{
   u8 buf[5]={0xC2,0xC2,0xC2,0xC2,0xC2};
   u8 buf1[5];
   u8 i=0;
   /*????5 ??×????????·.  */
   NRF_Write_Buf(NRF_WRITE_REG+TX_ADDR,buf,5);
   /*?????????????· */
   NRF_Read_Buf(TX_ADDR,buf1,5);
    /*±???*/
   for (i=0;i<5;i++)
   {
      if (buf1!=0xC2)
      break;
   }
 
   if (i==5)   {printf(" ??????NRF24L01????... ");return 1 ;}        //MCU ??NRF ????????
   else        {printf(" ??????NRF24L01???í... ");return 0 ;}        //MCU??NRF??????????   
}



#include "NRF24L01.h"
#include "SPI.h"
#include "USART.h"
#include "stdint.h"  
void SoftDelay(int xms)
{
 int i, j;
 for(i = 0; i < xms; ++ i)
  for(j = 0; j < xms; ++ j)
   { ; }
}

int main(void)
{
 int mode = 1;       // ????????
 uint8_t tx_buf[32] = {'A','B','C', 'D','E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N'};  // ·????????? 
 uint8_t rx_buf[32] = {'B'};  // ??????????
 
 USART1_Init(9600);
 
 printf("   This is NRF24L01+ Comunication CopyRights 2014 ");
 
 NRF24L01_Init();              //NRF24L01??????
 
 
 
 /* ·??????? */
 if(mode == 0)
 {
   SetTX_Mode();               
   while(1)
   {
    NRF_TxPacket(tx_buf, 32);
   }
  
 }
 else /* ???????? */
 {
  SetRX_Mode(); 
   
  /*
  while(1)
  {
   if(NRF_RxPacket(rx_buf, 32) == 1)
   {
    printf(" Don't Receive Any Data... ");
   }
   else
   {
    printf(" Receive Data: %s ", rx_buf);
   }
   
  }
  
  */
  while(1)
  {
   //SPI_IRQ();
   if(NRF24L01_RxPacket(rx_buf)==0)
   {
    printf(" Receive Data: %s ", rx_buf);
   }
   else
   {
    printf(" No Receive Any Data... ");
   }
  }
  
  
 }
}



请各位帮帮忙
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。