10 years, 4 months ago.

Sample code?

Do you have a working sample to test this library?

Question relating to:

nRF24L01 driver nRF24L01

1 Answer

10 years, 4 months ago.

A simple test that transmits a test payload (single character 'a') from one device (PTX) to another device (PRX) connected to the same SPI bus. The signals are set up for a FRDMKL25z, you might need to chamge them around.

#include "mbed.h"
#include "nRF24L01P_PTX.h"
#include "nRF24L01P_PRX.h"

int main()
{
   nRF24L01P Device(PTD2, PTD3, PTD1, PTD5);

   nRF24L01P_PTX PTX(Device, PTA13, PTD0);

   PTX.Initialize();
   PTX.SetDataRate(2000);
   PTX.PowerUp();


   nRF24L01P Receiver(PTD2, PTD3, PTD1, PTC7);
   nRF24L01P_PRX PRX(Receiver, PTA12, PTD4);

   printf("Initialize\r\n");
   PRX.Initialize();
   printf("SetDataRate\r\n");
   PRX.SetDataRate(2000);
   printf("SetPayloadSize\r\n");
   PRX.SetPayloadSize(1);
   printf("PowerUp\r\n");
   PRX.PowerUp();
   printf("StartReceive\r\n");
   PRX.StartReceive();
   printf("Loop\r\n");
   
   while (1)
   {
      char c = 'a';
      printf("Transmit\r\n");
      int r = PTX.TransmitPacket(&c, 1);
      printf("%d\r\n", r);
      
      if (PRX.IsPacketReady())
      {
         char d;
         int r = PRX.ReadPacket(&d);
         printf("Read %d %c\r\n", r, d);
      }
      
      wait_us(1000);
   }
}

Accepted Answer

hi Ian. you used two nrf24l01+ module in your board? One Of Them For Receiver And On of Them for Transmiter?

posted by hamed ghobadi 19 Aug 2014

Yes, for the test I used two modules on one mbed. It is very simple to run it on two mbed's, just split the code into two files, one containing the PTX stuff and the other containing the PRX stuff. My test board was a FRDM-KL25Z, you'll need to change the pin assignments for other hardware.

posted by Ian McCulloch 19 Aug 2014

tanks Ian. who can i read mac address from nrf24l01? the nrf24l01, It is possible?has It a unique address?

posted by hamed ghobadi 20 Aug 2014

The nRF24L01+ doesn't have a unique address built into it. See the datasheet at http://www.nordicsemi.com/eng/Products/2.4GHz-RF/nRF24L01P. You can set the channel and address to anything you like. The code sample uses the default channel and address.

You can change the channel in the PRX and PTX classes with the SetChannel() function. This should be set the same for both the PTX and the PRX.

You can set the receiver address in the PRX::SetAddress() function. The PTX should use the same address for the destination, using PTX::SetDestinationAddress().

The nRF24L01+ also allows for different 'pipes'. The nRF25L01 class exposes the basic functionality, but the PTX and PRX classes don't yet make use of it.

posted by Ian McCulloch 20 Aug 2014

Sir, I am really stuck at using NRF with KL25Z as transmitter. Please provide me with a simple code for transmitter to transmit '111'.

posted by sarthak kelapure 09 Feb 2017