8 years, 2 months ago.

Loop Not Working

I had a problem recently getting the nRF24L01 to communicate with another nRF24L01, I discovered the board I am using has a new version and the CE pin had changed which is why mine wasn't working.

I have now run into a different problem. I have achieved communication between the boards. I am using one as a recevier and one as a sender.

My code runs that I type on the PC and this then sends a message from the sender to the receiver. This works on first boot and the recevier gets the message. However it doesn't work after this, unless I reset the sender and try again.

Any idea what would be causing this? My code for both is below.

Sender

#include "mbed.h"
#include "nRF24L01P_PTX.h"
#include "nRF24L01P_PRX.h"
 
int main()
{
   Serial pc(USBTX, USBRX);
   nRF24L01P Device(PTD6, PTD7, PTD5, PTD4);
 
   nRF24L01P_PTX PTX(Device, PTB20, PTC18);

   #define TRANSFER_SIZE   4
    char txData[TRANSFER_SIZE];
    int txDataCnt = 0;
 
   PTX.Initialize();
   PTX.SetChannel(125);
   PTX.SetDataRate(2000);
   PTX.SetDestinationAddress(0x3631337830);
   PTX.PowerUp();

   while (1)
   {  
   
    if(pc.readable()){
      
       txData[txDataCnt++] = pc.getc();
       
        if ( txDataCnt >= sizeof( txData ) ) {  
            char c = 'a';
              if(PTX.IsReadyTransmit())
            {
                    printf("Transmit\r\n");
                    int r = PTX.TransmitPacket(&c, 1);
                    printf("Returned: %d \r\n", r);
             }
            txDataCnt = 0;
             wait(1);
        }
      }
   }
}

Receiver

#include "mbed.h"
#include "nRF24L01P_PTX.h"
#include "nRF24L01P_PRX.h"
 
int main()
{    
   nRF24L01P Receiver(PTD6, PTD7, PTD5, PTD4);
   nRF24L01P_PRX PRX(Receiver, PTB20, PTC18);
   


   PRX.Initialize();
   PRX.SetChannel(125);
   PRX.SetAddress(0x3631337830);
   PRX.SetPayloadSize(1);
   PRX.PowerUp();
   PRX.StartReceive();

   while (1)
   {
      printf("Start of While Loop \r\n");
      if (PRX.IsPacketReady())
      {
         printf("Packet Ready\r\n");
         char d;
         int r = PRX.ReadPacket(&d);
         printf("Read %d %c\r\n", r, d);
      }
      printf("Exited if Statement \r\n");
      wait(1);
   }
}

Question relating to:

I have the same problem. Have you resolved that issue?

I checked out and the library and it seems that it is hanging on the while statement that checks the getStatusRegister.

I'll see what it does and post the answer if the problem is solved.

posted by Junshin Park 11 May 2016

1 Answer

8 years, 2 months ago.

Try adding some debug statements to see where it is stopping (as in your receive code). For example I would be interested in knowing the result of if(PTX.IsReadyTransmit()).