“(SKU:RB-03T006)NRF24L01無線數(shù)傳模塊”的版本間的差異

來自ALSROBOT WiKi
跳轉(zhuǎn)至: 導(dǎo)航、 搜索
?應(yīng)用例程1
第50行: 第50行:
 
發(fā)送端代碼<br/>
 
發(fā)送端代碼<br/>
 
<pre style='color:blue'>
 
<pre style='color:blue'>
 +
/*********************************************************************
 +
**  Device:  nRF24L01+                                              **
 +
**  File:  EF_nRF24L01_TX.c                                        **
 +
**                                                                  **
 +
**                                                                  **
 +
**  Copyright (C) 2011 ElecFraks.                                  **
 +
**  This example code is in the public domain.                      **
 +
**                                                                  **
 +
**  Description:                                                    **
 +
**  This file is a sample code for your reference.                  **
 +
**  It's the v1.0 nRF24L01+ Hardware SPI by arduino                **
 +
**  Created by ElecFreaks. Robi.W,11 June 2011                      **
 +
**                                                                  **
 +
**  http://www.elecfreaks.com                                      **
 +
**                                                                  **
 +
**  SPI-compatible                                                **
 +
**  CS - to digital pin 8                                          **
 +
**  CSN - to digital pin 9  (SS pin)                              **
 +
**  MOSI - to digital pin 11 (MOSI pin)                            **
 +
**  MISO - to digital pin 12 (MISO pin)                            **
 +
**  CLK - to digital pin 13 (SCK pin)                              **
 +
*********************************************************************/
 +
 
#include <SPI.h>
 
#include <SPI.h>
?
#include <Mirf.h>
+
#include "API.h"
?
#include <nRF24L01.h>
+
#include "nRF24L01.h"
?
#include <MirfHardwareSpiDriver.h>
+
 
?
void setup()
+
//***************************************************
 +
#define TX_ADR_WIDTH    5  // 5 unsigned chars TX(RX) address width
 +
#define TX_PLOAD_WIDTH  32  // 32 unsigned chars TX payload
 +
 
 +
unsigned char TX_ADDRESS[TX_ADR_WIDTH]  =
 
{
 
{
?
Serial.begin(9600);
+
  0x34,0x43,0x10,0x10,0x01
?
Mirf.spi = &MirfHardwareSpi;
+
}; // Define a static TX address
?
Mirf.init();
+
 
?
Mirf.setRADDR((byte *)"clie1");  
+
unsigned char rx_buf[TX_PLOAD_WIDTH] = {0}; // initialize value
?
Mirf.payload = 5;  
+
unsigned char tx_buf[TX_PLOAD_WIDTH] = {0};
?
Mirf.channel = 3;  
+
//***************************************************
?
Mirf.config();
+
void setup()
 +
{
 +
  Serial.begin(9600);
 +
  pinMode(CE,  OUTPUT);
 +
  pinMode(CSN, OUTPUT);
 +
  pinMode(IRQ, INPUT);
 +
  SPI.begin();
 +
  delay(50);
 +
  init_io();                        // Initialize IO port
 +
  unsigned char sstatus=SPI_Read(STATUS);
 +
  Serial.println("*******************TX_Mode Start****************************");
 +
  Serial.print("status = ");  
 +
  Serial.println(sstatus,HEX);     // There is read the mode’s status register, the default value should be ‘E’
 +
  TX_Mode();                       // set TX mode
 
}
 
}
?
void loop(){
+
 
?
unsigned long time = millis();
+
void loop()  
?
Mirf.setTADDR((byte *)"serv1"); // 標(biāo)記對方的地址
+
?
Sends("hello"); //發(fā)送了一個5字節(jié)的hello
+
?
delay(1000);
+
?
}
+
?
void Sends(char *str)
+
 
{
 
{
?
int lens;
+
  int k = 0;
?
lens=strlen(str);
+
  for(;;)
?
char msg[lens];
+
  {
?
int i;
+
    for(int i=0; i<32; i++)
?
for (i=0;i<lens;i++)
+
        tx_buf[i] = k++;       
 +
    unsigned char sstatus = SPI_Read(STATUS);                  // read register STATUS's value
 +
    if(sstatus&TX_DS)                                          // if receive data ready (TX_DS) interrupt
 +
    {
 +
      SPI_RW_Reg(FLUSH_TX,0);                                 
 +
      SPI_Write_Buf(WR_TX_PLOAD,tx_buf,TX_PLOAD_WIDTH);      // write playload to TX_FIFO
 +
    }
 +
    if(sstatus&MAX_RT)                                        // if receive data ready (MAX_RT) interrupt, this is retransmit than  SETUP_RETR                         
 +
    {
 +
      SPI_RW_Reg(FLUSH_TX,0);
 +
      SPI_Write_Buf(WR_TX_PLOAD,tx_buf,TX_PLOAD_WIDTH);      // disable standy-mode
 +
    }
 +
    SPI_RW_Reg(WRITE_REG+STATUS,sstatus);                    // clear RX_DR or TX_DS or MAX_RT interrupt flag
 +
    delay(1000);
 +
  }
 +
}
 +
 
 +
//**************************************************
 +
// Function: init_io();
 +
// Description:
 +
// flash led one time,chip enable(ready to TX or RX Mode),
 +
// Spi disable,Spi clock line init high
 +
//**************************************************
 +
void init_io(void)
 
{
 
{
?
msg[i]= int(str[i]);
+
  digitalWrite(IRQ, 0);
 +
  digitalWrite(CE, 0); // chip enable
 +
  digitalWrite(CSN, 1);                // Spi disable
 
}
 
}
?
Mirf.send((byte *)&msg);
 
?
while(Mirf.isSending()){}
 
  
?
}</pre>
+
/************************************************************************
 +
**  * Function: SPI_RW();
 +
*
 +
* Description:
 +
* Writes one unsigned char to nRF24L01, and return the unsigned char read
 +
* from nRF24L01 during write, according to SPI protocol
 +
************************************************************************/
 +
unsigned char SPI_RW(unsigned char Byte)
 +
{
 +
  return SPI.transfer(Byte);
 +
}
 +
 
 +
/**************************************************/
 +
 
 +
/**************************************************
 +
* Function: SPI_RW_Reg();
 +
*
 +
* Description:
 +
* Writes value 'value' to register 'reg'
 +
/**************************************************/
 +
unsigned char SPI_RW_Reg(unsigned char reg, unsigned char value)
 +
{
 +
  unsigned char status;
 +
 
 +
  digitalWrite(CSN, 0);                  // CSN low, init SPI transaction
 +
  SPI_RW(reg);                            // select register
 +
  SPI_RW(value);                          // ..and write value to it..
 +
  digitalWrite(CSN, 1);                  // CSN high again
 +
 
 +
  return(status);                  // return nRF24L01 status unsigned char
 +
}
 +
/**************************************************/
 +
 
 +
/**************************************************
 +
* Function: SPI_Read();
 +
*
 +
* Description:
 +
* Read one unsigned char from nRF24L01 register, 'reg'
 +
/**************************************************/
 +
unsigned char SPI_Read(unsigned char reg)
 +
{
 +
  unsigned char reg_val;
 +
 
 +
  digitalWrite(CSN, 0);                // CSN low, initialize SPI communication...
 +
  SPI_RW(reg);                        // Select register to read from..
 +
  reg_val = SPI_RW(0);                // ..then read register value
 +
  digitalWrite(CSN, 1);                // CSN high, terminate SPI communication
 +
 
 +
  return(reg_val);                    // return register value
 +
}
 +
/**************************************************/
 +
 
 +
/**************************************************
 +
* Function: SPI_Read_Buf();
 +
*
 +
* Description:
 +
* Reads 'unsigned chars' #of unsigned chars from register 'reg'
 +
* Typically used to read RX payload, Rx/Tx address
 +
/**************************************************/
 +
unsigned char SPI_Read_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes)
 +
{
 +
  unsigned char sstatus,i;
 +
 
 +
  digitalWrite(CSN, 0);                  // Set CSN low, init SPI tranaction
 +
  sstatus = SPI_RW(reg);          // Select register to write to and read status unsigned char
 +
 
 +
  for(i=0;i<bytes;i++)
 +
  {
 +
    pBuf[i] = SPI_RW(0);    // Perform SPI_RW to read unsigned char from nRF24L01
 +
  }
 +
 
 +
  digitalWrite(CSN, 1);                  // Set CSN high again
 +
 
 +
  return(sstatus);                  // return nRF24L01 status unsigned char
 +
}
 +
/**************************************************/
 +
 
 +
/**************************************************
 +
* Function: SPI_Write_Buf();
 +
*
 +
* Description:
 +
* Writes contents of buffer '*pBuf' to nRF24L01
 +
* Typically used to write TX payload, Rx/Tx address
 +
/**************************************************/
 +
unsigned char SPI_Write_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes)
 +
{
 +
  unsigned char sstatus,i;
 +
 
 +
  digitalWrite(CSN, 0);                  // Set CSN low, init SPI tranaction
 +
  sstatus = SPI_RW(reg);            // Select register to write to and read status unsigned char
 +
  for(i=0;i<bytes; i++)            // then write all unsigned char in buffer(*pBuf)
 +
  {
 +
    SPI_RW(*pBuf++);
 +
  }
 +
  digitalWrite(CSN, 1);                  // Set CSN high again
 +
  return(sstatus);                  // return nRF24L01 status unsigned char
 +
}
 +
/**************************************************/
 +
 
 +
/**************************************************
 +
* Function: TX_Mode();
 +
*
 +
* 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 unsigned chars) 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 TX_Mode(void)
 +
{
 +
  digitalWrite(CE, 0);
 +
 
 +
  SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH);    // Writes TX_Address to nRF24L01
 +
  SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // RX_Addr0 same as TX_Adr for Auto.Ack
 +
 
 +
  SPI_RW_Reg(WRITE_REG + EN_AA, 0x01);      // Enable Auto.Ack:Pipe0
 +
  SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01);  // Enable Pipe0
 +
  SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x1a); // 500us + 86us, 10 retrans...
 +
  SPI_RW_Reg(WRITE_REG + RF_CH, 40);        // Select RF channel 40
 +
  SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07);  // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
 +
  SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e);    // Set PWR_UP bit, enable CRC(2 unsigned chars) & Prim:TX. MAX_RT & TX_DS enabled..
 +
  SPI_Write_Buf(WR_TX_PLOAD,tx_buf,TX_PLOAD_WIDTH);
 +
 
 +
  digitalWrite(CE, 1);
 +
}
 +
</pre>
 
<br/>
 
<br/>
 
接收端代碼:<br/>
 
接收端代碼:<br/>
?
<pre style='color:blue'>#include <SPI.h>
+
<pre style='color:blue'>
?
#include <Mirf.h>
+
/*********************************************************************
?
#include <nRF24L01.h>
+
**  Device:  nRF24L01+                                              **
?
#include <MirfHardwareSpiDriver.h>
+
**  File:  EF_nRF24L01_RX.c                                        **
?
void setup(){
+
**                                                                  **
?
Serial.begin(9600);
+
**                                                                  **
?
Mirf.spi = &MirfHardwareSpi;
+
**  Copyright (C) 2011 ElecFraks.                                  **
?
Mirf.init();
+
**  This example code is in the public domain.                      **
?
Mirf.setRADDR((byte *)"serv1"); // 本地名
+
**                                                                  **
?
Mirf.payload = 5; // 長度
+
**  Description:                                                    **
?
Mirf.channel = 3; // 信道
+
**  This file is a sample code for your reference.                  **
?
Mirf.config();
+
**  It's the v1.0 nRF24L01+ Hardware SPI by arduino                **
 +
**  Created by ElecFreaks. Robi.W,11 June 2011                      **
 +
**                                                                  **
 +
**  http://www.elecfreaks.com                                      **
 +
**                                                                  **
 +
**  SPI-compatible                                                **
 +
**  CS - to digital pin 8                                          **
 +
**  CSN - to digital pin 9  (SS pin)                              **
 +
**  MOSI - to digital pin 11 (MOSI pin)                            **
 +
**  MISO - to digital pin 12 (MISO pin)                            **
 +
**  CLK - to digital pin 13 (SCK pin)                              **
 +
*********************************************************************/
 +
 
 +
 
 +
#include <SPI.h>
 +
#include "API.h"
 +
#include "nRF24L01.h"
 +
 
 +
//***************************************************
 +
#define TX_ADR_WIDTH    5  // 5 unsigned chars TX(RX) address width
 +
#define TX_PLOAD_WIDTH  32  // 32 unsigned chars TX payload
 +
 
 +
unsigned char TX_ADDRESS[TX_ADR_WIDTH]  =
 +
{
 +
  0x34,0x43,0x10,0x10,0x01
 +
}; // Define a static TX address
 +
 
 +
unsigned char rx_buf[TX_PLOAD_WIDTH] = {0}; // initialize value
 +
unsigned char tx_buf[TX_PLOAD_WIDTH] = {0};
 +
//***************************************************
 +
void setup()  
 +
{
 +
  Serial.begin(9600);
 +
  pinMode(CE,  OUTPUT);
 +
  pinMode(CSN, OUTPUT);
 +
  pinMode(IRQ, INPUT);
 +
  SPI.begin();
 +
  delay(50);
 +
  init_io();                       // Initialize IO port
 +
  unsigned char sstatus=SPI_Read(STATUS);
 +
  Serial.println("*******************RX_Mode Start****************************");
 +
  Serial.print("status = ");  
 +
  Serial.println(sstatus,HEX);    // There is read the mode’s status register, the default value should be ‘E’
 +
  RX_Mode();                       // set RX mode
 
}
 
}
?
void loop()
+
 
 +
void loop()  
 
{
 
{
?
byte data[Mirf.payload];
+
  for(;;)
?
if(!Mirf.isSending() && Mirf.dataReady())
+
  {
 +
    unsigned char status = SPI_Read(STATUS);                        // read register STATUS's value
 +
    if(status&RX_DR)                                                // if receive data ready (TX_DS) interrupt
 +
    {
 +
      SPI_Read_Buf(RD_RX_PLOAD, rx_buf, TX_PLOAD_WIDTH);            // read playload to rx_buf
 +
      SPI_RW_Reg(FLUSH_RX,0);                                        // clear RX_FIFO
 +
      for(int i=0; i<32; i++)
 +
      {
 +
          Serial.print(" ");
 +
          Serial.print(rx_buf[i],HEX);                              // print rx_buf
 +
      }
 +
      Serial.println(" ");
 +
    }
 +
    SPI_RW_Reg(WRITE_REG+STATUS,status);                            // clear RX_DR or TX_DS or MAX_RT interrupt flag
 +
    delay(1000);
 +
  }
 +
}
 +
 
 +
//**************************************************
 +
// Function: init_io();
 +
// Description:
 +
// flash led one time,chip enable(ready to TX or RX Mode),
 +
// Spi disable,Spi clock line init high
 +
//**************************************************
 +
void init_io(void)
 
{
 
{
?
Mirf.getData(data);
+
  digitalWrite(IRQ, 0);
?
int i;
+
  digitalWrite(CE, 0); // chip enable
?
String Temp;
+
  digitalWrite(CSN, 1);                 // Spi disable
?
for (i = 0; i < Mirf.payload; i++) //把收到的信息拼起來,到一個串里面
+
}
 +
 
 +
/************************************************************************
 +
**  * Function: SPI_RW();
 +
*
 +
* Description:
 +
* Writes one unsigned char to nRF24L01, and return the unsigned char read
 +
* from nRF24L01 during write, according to SPI protocol
 +
************************************************************************/
 +
unsigned char SPI_RW(unsigned char Byte)
 
{
 
{
?
Temp += char(data[i]);
+
  return SPI.transfer(Byte);
 
}
 
}
?
Serial.print("Get:");
+
 
?
Serial.print(Mirf.payload);
+
/**************************************************/
?
Serial.print(" ");
+
 
?
Serial.println(Temp);
+
/**************************************************
?
Mirf.setTADDR((byte *)"clie1");
+
* Function: SPI_RW_Reg();
?
Mirf.send(data);
+
*
 +
* Description:
 +
* Writes value 'value' to register 'reg'
 +
/**************************************************/
 +
unsigned char SPI_RW_Reg(unsigned char reg, unsigned char value)
 +
{
 +
  unsigned char status;
 +
 
 +
  digitalWrite(CSN, 0);                  // CSN low, init SPI transaction
 +
  SPI_RW(reg);                            // select register
 +
  SPI_RW(value);                          // ..and write value to it..
 +
  digitalWrite(CSN, 1);                  // CSN high again
 +
 
 +
  return(status);                  // return nRF24L01 status unsigned char
 +
}
 +
/**************************************************/
 +
 
 +
/**************************************************
 +
* Function: SPI_Read();
 +
*
 +
* Description:
 +
* Read one unsigned char from nRF24L01 register, 'reg'
 +
/**************************************************/
 +
unsigned char SPI_Read(unsigned char reg)
 +
{
 +
  unsigned char reg_val;
 +
 
 +
  digitalWrite(CSN, 0);                // CSN low, initialize SPI communication...
 +
  SPI_RW(reg);                        // Select register to read from..
 +
  reg_val = SPI_RW(0);                // ..then read register value
 +
  digitalWrite(CSN, 1);                // CSN high, terminate SPI communication
 +
 
 +
  return(reg_val);                    // return register value
 +
}
 +
/**************************************************/
 +
 
 +
/**************************************************
 +
* Function: SPI_Read_Buf();
 +
*
 +
* Description:
 +
* Reads 'unsigned chars' #of unsigned chars from register 'reg'
 +
* Typically used to read RX payload, Rx/Tx address
 +
/**************************************************/
 +
unsigned char SPI_Read_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes)
 +
{
 +
  unsigned char sstatus,i;
 +
 
 +
  digitalWrite(CSN, 0);                   // Set CSN low, init SPI tranaction
 +
  sstatus = SPI_RW(reg);           // Select register to write to and read status unsigned char
 +
 
 +
  for(i=0;i<bytes;i++)
 +
  {
 +
    pBuf[i] = SPI_RW(0);    // Perform SPI_RW to read unsigned char from nRF24L01
 +
  }
 +
 
 +
  digitalWrite(CSN, 1);                  // Set CSN high again
 +
 
 +
  return(sstatus);                  // return nRF24L01 status unsigned char
 +
}
 +
/**************************************************/
 +
 
 +
/**************************************************
 +
* Function: SPI_Write_Buf();
 +
*
 +
* Description:
 +
* Writes contents of buffer '*pBuf' to nRF24L01
 +
* Typically used to write TX payload, Rx/Tx address
 +
/**************************************************/
 +
unsigned char SPI_Write_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes)
 +
{
 +
  unsigned char sstatus,i;
 +
 
 +
  digitalWrite(CSN, 0);                  // Set CSN low, init SPI tranaction
 +
  sstatus = SPI_RW(reg);            // Select register to write to and read status unsigned char
 +
  for(i=0;i<bytes; i++)            // then write all unsigned char in buffer(*pBuf)
 +
  {
 +
    SPI_RW(*pBuf++);
 +
  }
 +
  digitalWrite(CSN, 1);                  // Set CSN high again
 +
  return(sstatus);                  // return nRF24L01 status unsigned char
 +
}
 +
/**************************************************/
 +
 
 +
void RX_Mode(void)
 +
{
 +
  digitalWrite(CE, 0);
 +
 
 +
  SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // Use the same address on the RX device as the TX device
 +
  SPI_RW_Reg(WRITE_REG + EN_AA, 0x01);      // Enable Auto.Ack:Pipe0
 +
  SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0
 +
  SPI_RW_Reg(WRITE_REG + RF_CH, 40);        // Select RF channel 40
 +
  SPI_RW_Reg(WRITE_REG + RX_PW_P0, TX_PLOAD_WIDTH); // Select same RX payload width as TX Payload width
 +
  SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07);  // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
 +
  SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f);    // Set PWR_UP bit, enable CRC(2 unsigned chars) & Prim:RX. RX_DR enabled..
 +
  digitalWrite(CE, 1);                            // Set CE pin high to enable RX device
 +
  //  This device is now ready to receive one packet of 16 unsigned chars payload from a TX device sending to address
 +
  //  '3443101001', with auto acknowledgment, retransmit count of 10, RF channel 40 and datarate = 2Mbps.
 
}
 
}
?
}</pre>
+
</pre>
  
?
==應(yīng)用例程2==
 
?
[[文件:hohn10.jpg|500px|有框|居中]]
 
?
[[文件:hohn11.jpg|500px|有框|居中]]
 
 
==產(chǎn)品相關(guān)推薦==
 
==產(chǎn)品相關(guān)推薦==
 +
論壇地址:[http://www.makerspace.cn/portal.php 奧松機器人技術(shù)論壇]<br/>
 
購買地址:[http://www.gharee.com/goods-54.html NRF24L01無線數(shù)傳模塊]
 
購買地址:[http://www.gharee.com/goods-54.html NRF24L01無線數(shù)傳模塊]

2015年7月2日 (四) 16:58的版本

RB-03T006.jpg

目錄

產(chǎn)品概述

NRF24L01是一款新型單片射頻收發(fā)器件,工作于2.4 GHz~2.5 GHz ISM頻段。內(nèi)置頻率合成器、功率放大器、晶體振蕩器、調(diào)制器等功能模塊,并融合了增強型ShockBurst技術(shù),其中輸出功率和通信頻道可通過程序進 行配置。NRF24L01功耗低,在以-6 dBm的功率發(fā)射時,工作電流也只有9 mA;接收時,工作電流只有12.3 mA,多種低功率工作模式(掉電模式和空閑模式)使節(jié)能設(shè)計更方便。

規(guī)格參數(shù)

  1. 2Mbit/s速率下接收時的峰值電流12.5mA
  2. 在2Mbit/s速率下@0dBm輸出時的峰值電流11mA
  3. 掉電模式下的功耗400nA
  4. 待機模式下的功耗32uA
  5. 130us 的快速切換和喚醒時間
  6. 具有片內(nèi)穩(wěn)壓器oltage regulators
  7. 可在1.9 to 3.6V低電壓工作
  8. MultiCeiverMT硬件提供同時6個接收機的功能,2Mbit/s 使得高質(zhì)量的VoIP成為可能

使用方法

引腳說明

Hohn7.jpg

應(yīng)用例程

1.庫文件下載
mirf 庫下載地址
2.連接Arduino和NRF2401模塊

NRF2401 Arduino
VCC 3V3
GND GND
CSN D7
CE D8
MOSI D11
MISO D12
SCK D13


3.代碼下載
發(fā)送端代碼

/*********************************************************************
**  Device:  nRF24L01+                                              **
**  File:   EF_nRF24L01_TX.c                                        **
**                                                                  **
**                                                                  **
**  Copyright (C) 2011 ElecFraks.                                   **
**  This example code is in the public domain.                      **
**                                                                  **
**  Description:                                                    **
**  This file is a sample code for your reference.                  **
**  It's the v1.0 nRF24L01+ Hardware SPI by arduino                 **
**  Created by ElecFreaks. Robi.W,11 June 2011                      **
**                                                                  **
**  http://www.elecfreaks.com                                       **
**                                                                  **
**   SPI-compatible                                                 **
**   CS - to digital pin 8                                          **
**   CSN - to digital pin 9  (SS pin)                               **
**   MOSI - to digital pin 11 (MOSI pin)                            **
**   MISO - to digital pin 12 (MISO pin)                            **
**   CLK - to digital pin 13 (SCK pin)                              **
*********************************************************************/

#include <SPI.h>
#include "API.h"
#include "nRF24L01.h"

//***************************************************
#define TX_ADR_WIDTH    5   // 5 unsigned chars TX(RX) address width
#define TX_PLOAD_WIDTH  32  // 32 unsigned chars TX payload

unsigned char TX_ADDRESS[TX_ADR_WIDTH]  = 
{
  0x34,0x43,0x10,0x10,0x01
}; // Define a static TX address

unsigned char rx_buf[TX_PLOAD_WIDTH] = {0}; // initialize value
unsigned char tx_buf[TX_PLOAD_WIDTH] = {0};
//***************************************************
void setup() 
{
  Serial.begin(9600);
  pinMode(CE,  OUTPUT);
  pinMode(CSN, OUTPUT);
  pinMode(IRQ, INPUT);
  SPI.begin();
  delay(50);
  init_io();                        // Initialize IO port
  unsigned char sstatus=SPI_Read(STATUS);
  Serial.println("*******************TX_Mode Start****************************");
  Serial.print("status = ");    
  Serial.println(sstatus,HEX);     // There is read the mode’s status register, the default value should be ‘E’
  TX_Mode();                       // set TX mode
}

void loop() 
{
  int k = 0;
  for(;;)
  {
    for(int i=0; i<32; i++)
        tx_buf[i] = k++;        
    unsigned char sstatus = SPI_Read(STATUS);                   // read register STATUS's value
    if(sstatus&TX_DS)                                           // if receive data ready (TX_DS) interrupt
    {
      SPI_RW_Reg(FLUSH_TX,0);                                  
      SPI_Write_Buf(WR_TX_PLOAD,tx_buf,TX_PLOAD_WIDTH);       // write playload to TX_FIFO
    }
    if(sstatus&MAX_RT)                                         // if receive data ready (MAX_RT) interrupt, this is retransmit than  SETUP_RETR                          
    {
      SPI_RW_Reg(FLUSH_TX,0);
      SPI_Write_Buf(WR_TX_PLOAD,tx_buf,TX_PLOAD_WIDTH);      // disable standy-mode
    }
    SPI_RW_Reg(WRITE_REG+STATUS,sstatus);                     // clear RX_DR or TX_DS or MAX_RT interrupt flag
    delay(1000);
  }
}

//**************************************************
// Function: init_io();
// Description:
// flash led one time,chip enable(ready to TX or RX Mode),
// Spi disable,Spi clock line init high
//**************************************************
void init_io(void)
{
  digitalWrite(IRQ, 0);
  digitalWrite(CE, 0);			// chip enable
  digitalWrite(CSN, 1);                 // Spi disable	
}

/************************************************************************
**   * Function: SPI_RW();
 * 
 * Description:
 * Writes one unsigned char to nRF24L01, and return the unsigned char read
 * from nRF24L01 during write, according to SPI protocol
************************************************************************/
unsigned char SPI_RW(unsigned char Byte)
{
  return SPI.transfer(Byte);
}

/**************************************************/

/**************************************************
 * Function: SPI_RW_Reg();
 * 
 * Description:
 * Writes value 'value' to register 'reg'
/**************************************************/
unsigned char SPI_RW_Reg(unsigned char reg, unsigned char value)
{
  unsigned char status;

  digitalWrite(CSN, 0);                   // CSN low, init SPI transaction
  SPI_RW(reg);                            // select register
  SPI_RW(value);                          // ..and write value to it..
  digitalWrite(CSN, 1);                   // CSN high again

  return(status);                   // return nRF24L01 status unsigned char
}
/**************************************************/

/**************************************************
 * Function: SPI_Read();
 * 
 * Description:
 * Read one unsigned char from nRF24L01 register, 'reg'
/**************************************************/
unsigned char SPI_Read(unsigned char reg)
{
  unsigned char reg_val;

  digitalWrite(CSN, 0);                // CSN low, initialize SPI communication...
  SPI_RW(reg);                         // Select register to read from..
  reg_val = SPI_RW(0);                 // ..then read register value
  digitalWrite(CSN, 1);                // CSN high, terminate SPI communication

  return(reg_val);                     // return register value
}
/**************************************************/

/**************************************************
 * Function: SPI_Read_Buf();
 * 
 * Description:
 * Reads 'unsigned chars' #of unsigned chars from register 'reg'
 * Typically used to read RX payload, Rx/Tx address
/**************************************************/
unsigned char SPI_Read_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes)
{
  unsigned char sstatus,i;

  digitalWrite(CSN, 0);                   // Set CSN low, init SPI tranaction
  sstatus = SPI_RW(reg);       	    // Select register to write to and read status unsigned char

  for(i=0;i<bytes;i++)
  {
    pBuf[i] = SPI_RW(0);    // Perform SPI_RW to read unsigned char from nRF24L01
  }

  digitalWrite(CSN, 1);                   // Set CSN high again

  return(sstatus);                  // return nRF24L01 status unsigned char
}
/**************************************************/

/**************************************************
 * Function: SPI_Write_Buf();
 * 
 * Description:
 * Writes contents of buffer '*pBuf' to nRF24L01
 * Typically used to write TX payload, Rx/Tx address
/**************************************************/
unsigned char SPI_Write_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes)
{
  unsigned char sstatus,i;

  digitalWrite(CSN, 0);                   // Set CSN low, init SPI tranaction
  sstatus = SPI_RW(reg);             // Select register to write to and read status unsigned char
  for(i=0;i<bytes; i++)             // then write all unsigned char in buffer(*pBuf)
  {
    SPI_RW(*pBuf++);
  }
  digitalWrite(CSN, 1);                   // Set CSN high again
  return(sstatus);                  // return nRF24L01 status unsigned char
}
/**************************************************/

/**************************************************
 * Function: TX_Mode();
 * 
 * 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 unsigned chars) 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 TX_Mode(void)
{
  digitalWrite(CE, 0);

  SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH);    // Writes TX_Address to nRF24L01
  SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // RX_Addr0 same as TX_Adr for Auto.Ack

  SPI_RW_Reg(WRITE_REG + EN_AA, 0x01);      // Enable Auto.Ack:Pipe0
  SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01);  // Enable Pipe0
  SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x1a); // 500us + 86us, 10 retrans...
  SPI_RW_Reg(WRITE_REG + RF_CH, 40);        // Select RF channel 40
  SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07);   // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
  SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e);     // Set PWR_UP bit, enable CRC(2 unsigned chars) & Prim:TX. MAX_RT & TX_DS enabled..
  SPI_Write_Buf(WR_TX_PLOAD,tx_buf,TX_PLOAD_WIDTH);

  digitalWrite(CE, 1);
}


接收端代碼:

/*********************************************************************
**  Device:  nRF24L01+                                              **
**  File:   EF_nRF24L01_RX.c                                        **
**                                                                  **
**                                                                  **
**  Copyright (C) 2011 ElecFraks.                                   **
**  This example code is in the public domain.                      **
**                                                                  **
**  Description:                                                    **
**  This file is a sample code for your reference.                  **
**  It's the v1.0 nRF24L01+ Hardware SPI by arduino                 **
**  Created by ElecFreaks. Robi.W,11 June 2011                      **
**                                                                  **
**  http://www.elecfreaks.com                                       **
**                                                                  **
**   SPI-compatible                                                 **
**   CS - to digital pin 8                                          **
**   CSN - to digital pin 9  (SS pin)                               **
**   MOSI - to digital pin 11 (MOSI pin)                            **
**   MISO - to digital pin 12 (MISO pin)                            **
**   CLK - to digital pin 13 (SCK pin)                              **
*********************************************************************/


#include <SPI.h>
#include "API.h"
#include "nRF24L01.h"

//***************************************************
#define TX_ADR_WIDTH    5   // 5 unsigned chars TX(RX) address width
#define TX_PLOAD_WIDTH  32  // 32 unsigned chars TX payload

unsigned char TX_ADDRESS[TX_ADR_WIDTH]  = 
{
  0x34,0x43,0x10,0x10,0x01
}; // Define a static TX address

unsigned char rx_buf[TX_PLOAD_WIDTH] = {0}; // initialize value
unsigned char tx_buf[TX_PLOAD_WIDTH] = {0};
//***************************************************
void setup() 
{
  Serial.begin(9600);
  pinMode(CE,  OUTPUT);
  pinMode(CSN, OUTPUT);
  pinMode(IRQ, INPUT);
  SPI.begin();
  delay(50);
  init_io();                        // Initialize IO port
  unsigned char sstatus=SPI_Read(STATUS);
  Serial.println("*******************RX_Mode Start****************************");
  Serial.print("status = ");    
  Serial.println(sstatus,HEX);     // There is read the mode’s status register, the default value should be ‘E’
  RX_Mode();                        // set RX mode
}

void loop() 
{
  for(;;)
  {
    unsigned char status = SPI_Read(STATUS);                         // read register STATUS's value
    if(status&RX_DR)                                                 // if receive data ready (TX_DS) interrupt
    {
      SPI_Read_Buf(RD_RX_PLOAD, rx_buf, TX_PLOAD_WIDTH);             // read playload to rx_buf
      SPI_RW_Reg(FLUSH_RX,0);                                        // clear RX_FIFO
      for(int i=0; i<32; i++)
      {
          Serial.print(" ");
          Serial.print(rx_buf[i],HEX);                              // print rx_buf
      }
      Serial.println(" ");
    }
    SPI_RW_Reg(WRITE_REG+STATUS,status);                             // clear RX_DR or TX_DS or MAX_RT interrupt flag
    delay(1000);
  }
}

//**************************************************
// Function: init_io();
// Description:
// flash led one time,chip enable(ready to TX or RX Mode),
// Spi disable,Spi clock line init high
//**************************************************
void init_io(void)
{
  digitalWrite(IRQ, 0);
  digitalWrite(CE, 0);			// chip enable
  digitalWrite(CSN, 1);                 // Spi disable	
}

/************************************************************************
**   * Function: SPI_RW();
 * 
 * Description:
 * Writes one unsigned char to nRF24L01, and return the unsigned char read
 * from nRF24L01 during write, according to SPI protocol
************************************************************************/
unsigned char SPI_RW(unsigned char Byte)
{
  return SPI.transfer(Byte);
}

/**************************************************/

/**************************************************
 * Function: SPI_RW_Reg();
 * 
 * Description:
 * Writes value 'value' to register 'reg'
/**************************************************/
unsigned char SPI_RW_Reg(unsigned char reg, unsigned char value)
{
  unsigned char status;

  digitalWrite(CSN, 0);                   // CSN low, init SPI transaction
  SPI_RW(reg);                            // select register
  SPI_RW(value);                          // ..and write value to it..
  digitalWrite(CSN, 1);                   // CSN high again

  return(status);                   // return nRF24L01 status unsigned char
}
/**************************************************/

/**************************************************
 * Function: SPI_Read();
 * 
 * Description:
 * Read one unsigned char from nRF24L01 register, 'reg'
/**************************************************/
unsigned char SPI_Read(unsigned char reg)
{
  unsigned char reg_val;

  digitalWrite(CSN, 0);                // CSN low, initialize SPI communication...
  SPI_RW(reg);                         // Select register to read from..
  reg_val = SPI_RW(0);                 // ..then read register value
  digitalWrite(CSN, 1);                // CSN high, terminate SPI communication

  return(reg_val);                     // return register value
}
/**************************************************/

/**************************************************
 * Function: SPI_Read_Buf();
 * 
 * Description:
 * Reads 'unsigned chars' #of unsigned chars from register 'reg'
 * Typically used to read RX payload, Rx/Tx address
/**************************************************/
unsigned char SPI_Read_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes)
{
  unsigned char sstatus,i;

  digitalWrite(CSN, 0);                   // Set CSN low, init SPI tranaction
  sstatus = SPI_RW(reg);       	    // Select register to write to and read status unsigned char

  for(i=0;i<bytes;i++)
  {
    pBuf[i] = SPI_RW(0);    // Perform SPI_RW to read unsigned char from nRF24L01
  }

  digitalWrite(CSN, 1);                   // Set CSN high again

  return(sstatus);                  // return nRF24L01 status unsigned char
}
/**************************************************/

/**************************************************
 * Function: SPI_Write_Buf();
 * 
 * Description:
 * Writes contents of buffer '*pBuf' to nRF24L01
 * Typically used to write TX payload, Rx/Tx address
/**************************************************/
unsigned char SPI_Write_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes)
{
  unsigned char sstatus,i;

  digitalWrite(CSN, 0);                   // Set CSN low, init SPI tranaction
  sstatus = SPI_RW(reg);             // Select register to write to and read status unsigned char
  for(i=0;i<bytes; i++)             // then write all unsigned char in buffer(*pBuf)
  {
    SPI_RW(*pBuf++);
  }
  digitalWrite(CSN, 1);                   // Set CSN high again
  return(sstatus);                  // return nRF24L01 status unsigned char
}
/**************************************************/

void RX_Mode(void)
{
  digitalWrite(CE, 0);
  
  SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // Use the same address on the RX device as the TX device
  SPI_RW_Reg(WRITE_REG + EN_AA, 0x01);      // Enable Auto.Ack:Pipe0
  SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01);  // Enable Pipe0
  SPI_RW_Reg(WRITE_REG + RF_CH, 40);        // Select RF channel 40
  SPI_RW_Reg(WRITE_REG + RX_PW_P0, TX_PLOAD_WIDTH); // Select same RX payload width as TX Payload width
  SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07);   // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
  SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f);     // Set PWR_UP bit, enable CRC(2 unsigned chars) & Prim:RX. RX_DR enabled..
  digitalWrite(CE, 1);                             // Set CE pin high to enable RX device
  //  This device is now ready to receive one packet of 16 unsigned chars payload from a TX device sending to address
  //  '3443101001', with auto acknowledgment, retransmit count of 10, RF channel 40 and datarate = 2Mbps.
}

產(chǎn)品相關(guān)推薦

論壇地址:奧松機器人技術(shù)論壇
購買地址:NRF24L01無線數(shù)傳模塊