nRF24L01+ with dynamically changing payload

Hi Guys,

I am having trouble changing the payload length. If I just use static payload length then everything works fine. Below is the code for static length payload:

void NRF24L01_INIT(void)
{
int PAYLOAD_LEN = 20;
SPI_Write_Byte(EN_AA, 0x01); //Enable auto-acknowledgment for data pipe 0
SPI_Write_Byte(EN_RXADDR, 0x01);  //Enable data pipe 0
SPI_Write_Byte(SETUP_AW, 0x03);  //Set address width to 5 bytes
SPI_Write_Byte(RF_CH, 0x69); //Set channel frequency to 2.505GHz
SPI_Write_Byte(RF_SETUP, 0x26); //Set data rate to 250kbps and 0dB gain
nRF_Set_Addr_RX(N1_address, NRF_ADDR_LEN); //Set the 5-bytes receiver address as 0x11 0x12 0x13 0x14 0x15
nRF_Set_Addr_TX(BS_address, NRF_ADDR_LEN); //Set the 5-bytes transmitter address as 0x11 0x12 0x13 0x14 0x15
SPI_Write_Byte(RX_PW_P0, PAYLOAD_LEN); //Set the payload width as 20-bytes
}
void SPI_Write_Byte(unsigned char reg, unsigned char data)
{
 _delay_us(10);
 PORTB &= ~_BV(CSN); //CSN low
 _delay_us(10);
 SPI_Tranceiver(W_REGISTER + reg);
 _delay_us(10);
 SPI_Tranceiver(data);
 _delay_us(10);
 PORTB |= _BV(CSN); //CSN high
}

Now I wanted to have dynamic payload option. so I changed the code like this:

void NRF24L01_INIT(void)
{
int PAYLOAD_LEN = 20;
SPI_Write_Byte(EN_AA, 0x01); //Enable auto-acknowledgment for data pipe 0
SPI_Write_Byte(EN_RXADDR, 0x01);  //Enable data pipe 0
SPI_Write_Byte(SETUP_AW, 0x03);  //Set address width to 5 bytes
SPI_Write_Byte(RF_CH, 0x69); //Set channel frequency to 2.505GHz
SPI_Write_Byte(RF_SETUP, 0x26); //Set data rate to 250kbps and 0dB gain
nRF_Set_Addr_RX(N1_address, NRF_ADDR_LEN); //Set the 5-bytes receiver address as 0x11 0x12 0x13 0x14 0x15
nRF_Set_Addr_TX(BS_address, NRF_ADDR_LEN); //Set the 5-bytes transmitter address as 0x11 0x12 0x13 0x14 0x15

//toggle_features();
SPI_Write_Byte(FEATURE, 0x1D);
SPI_Write_Byte(EN_DPL, 0x02 );
// Enable dynamic payload on pipe 0
SPI_Write_Byte(DYNPD,  0x1C);
SPI_Write_Byte(DPL_P0,  0x00);
SPI_Write_Byte(RX_PW_P0, PAYLOAD_LEN); //Set the payload width as 20-bytes
}

Now anywhere on the code I can just call

int PAYLOAD_LEN = 10
SPI_Write_Byte(RX_PW_P0, PAYLOAD_LEN);

and change the payload length.

But it is not working. Datasheet says:

In order to enable DPL the EN_DPL bit in the FEATURE register must be set. In RX mode the DYNPD register
has to be set. A PTX that transmits to a PRX with DPL enabled must have the DPL_P0 bit in DYNPD
set.

I set everything but not transmitting. Do anybody has an idea?

Is there a reason why you are not using a library to handle the low level details of the nRF24? The examples in this Simple nRF24L01+ Tutorial use TMRh20's RF24 library.

Even if you don't want to use the library it is well documented and studying its code may help with your problem.

...R

Hi thank you for the reply. Yes I looked at that tutorial, you really did great job on that. I loved it. Just a small question. Should i enabledynamicpayload() for both receiver and sender or only sender or only receiver?

Node1 is transmitting always 20 bytes to Node2 but Node2 is trasmitting dynamic payload size to Node1.

diwasbhattarai:
Should i enabledynamicpayload() for both receiver and sender or only sender or only receiver?

Both.

I believe if you are using the ackPayload feature the dynamic payloads are set automatically.

...R