OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 6814|回复: 1

STMF401 无线传输收不到数据?

[复制链接]

10

主题

16

帖子

0

精华

初级会员

Rank: 2

积分
136
金钱
136
注册时间
2014-9-5
在线时间
28 小时
发表于 2014-10-5 20:29:59 | 显示全部楼层 |阅读模式
5金钱

 


#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("\nNRF24L01?è??????????...\r\n");
}

//·???????
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("\nNRF24L01?è??·???????...\r\n");
}


static char  ReceiveDataFormNRF()
{
 printf("Received: %s\r\n", 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("\n??????NRF24L01????...\r\n");return 1 ;}        //MCU ??NRF ????????
   else        {printf("\n??????NRF24L01???í...\r\n");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("\r\n  This is NRF24L01+ Comunication CopyRights 2014 \r\n");
 
 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("\r\n Don't Receive Any Data...\r\n");
   }
   else
   {
    printf(" Receive Data: %s \r\n", rx_buf);
   }
   
  }
  
  */
  while(1)
  {
   //SPI_IRQ();
   if(NRF24L01_RxPacket(rx_buf)==0)
   {
    printf(" Receive Data: %s \r\n", rx_buf);
   }
   else
   {
    printf("\r\n No Receive Any Data...\r\n");
   }
  }
  
  
 }
}





请各位帮帮忙

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165309
金钱
165309
注册时间
2010-12-1
在线时间
2108 小时
发表于 2014-10-5 23:10:24 | 显示全部楼层
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2024-11-22 23:25

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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