This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

PIC18 NRF24 communication failing

Hello, I have project to do, but I have too less knowledge about NRF's, I've tried almost all information sources and libraries, but no results. I have to make, that one PIC sends 5 bytes of data, and other do something with that data. But at first I'm playing only with switches, if HIGH led on other PIC High too. There's a codes.

TX:

#include "mcc_generated_files/mcc.h"
#define _XTAL_FREQ 16000000
unsigned char data;

void pulse(void);
void configure_transmitter(void);
void siuntimas(void);
void transmit_data(void);
#define nop() _asm nop _endasm
void main(void) {
    SYSTEM_Initialize();
    SPI1_Initialize();
    int i = 1;
    while(i < 10){
        LED1 = HIGH;
        LED2 = HIGH;
         __delay_ms(10);
         __delay_ms(10);
         LED1 = LOW;
         LED2 = LOW;
         __delay_ms(10);
        __delay_ms(10);
        i++;
    }
__delay_ms(10);
configure_transmitter();
//LED1 = HIGH;
__delay_ms(10);
while (1) {
//============================
    if(SW == 1){
        data = 1;
        LED1 = HIGH;
        __delay_ms(10);
    }
    else if(SW == 0)
    {
        data = 1;
        LED1 = LOW;
        __delay_ms(10);
    }
    __delay_ms(10);
    __delay_ms(10);
    //transmit_data();
    __delay_ms(10);
    siuntimas();
}
}
void configure_transmitter(void)
{
    SPI1_Initialize();
    CE = 0;
    CSN = 0;

 // PTX, CRC enabled, mask a couple of ints 
    SPI1_Exchange8bit(0x20);
    SPI1_Exchange8bit(0x38);
    pulse();

 //auto retransmit off
    SPI1_Exchange8bit(0x24); 
    SPI1_Exchange8bit(0x00); 
    pulse();

 //address width = 5
    SPI1_Exchange8bit(0x23);
    SPI1_Exchange8bit(0x03); 
    pulse();

 //data rate = 1MB
    SPI1_Exchange8bit(0x26);
    SPI1_Exchange8bit(0x07);
    pulse();

 //set channel 2, this is default but we did it anyway...
    SPI1_Exchange8bit(0x25);
    SPI1_Exchange8bit(0x02);
    pulse();

 //set address E7E7E7E7E7, also default...
    SPI1_Exchange8bit(0x30); 
    for (int j = 0; j < 5; j++)
    {
        SPI1_Exchange8bit(0xE7);
    } 
    pulse();

    //disable auto-ack, RX mode
    //shouldn't have to do this, but it won't TX if you don't
    SPI1_Exchange8bit(0x21);
    SPI1_Exchange8bit(0x00);
    CSN = 1;
}
    void pulse(void)
{
    CSN = 1; 
    __delay_us(10);
	CSN = 0;
}
void siuntimas()
{
    transmit_data();
    //CSN = 0;
    //SPI1_Exchange8bit(data);
    //CSN = 1;
    //pulse();

}
void transmit_data(void)
{  

    CSN = 0;

	//clear previous ints
  	SPI1_Exchange8bit(0x27);
SPI1_Exchange8bit(0x7E);
pulse();

//PWR_UP = 1
SPI1_Exchange8bit(0x20);
SPI1_Exchange8bit(0x3A);
pulse();

//clear TX fifo
//the data sheet says that this is supposed to come up 0 after POR, but that doesn't seem to be the case
SPI1_Exchange8bit(0xE1);
pulse();

//4 byte payload
SPI1_Exchange8bit(0xA0);
SPI1_Exchange8bit(0x34);
SPI1_Exchange8bit(0x33);
SPI1_Exchange8bit(0x32);
SPI1_Exchange8bit(0x31);
SPI1_Exchange8bit(data);
CSN = 1;



//Pulse CE to start transmission
CE = 1;
__delay_ms(10);
CE = 0;

int j = 1;
while(j < 10){
    LED2 = HIGH;
    __delay_ms(10);
    __delay_ms(10);
    LED2 = LOW;
    __delay_ms(10);
    __delay_ms(10);
    j++;
}
}

And RX:

#include "mcc_generated_files/mcc.h"
//============================================================
//============================================================
#define _XTAL_FREQ 16000000
int data;
unsigned char nrf_SPI_Read(unsigned char reg);
void configure_RX(void);
void reset_RX(void);
void read(void);
void pulse(void);

void main(void) {

SYSTEM_Initialize();
int i = 1;
while(i < 10){
       LED1 = HIGH;
       LED2 = HIGH;
       __delay_ms(10);
       __delay_ms(10);
       LED1 = LOW;
       LED2 = LOW;
       __delay_ms(10);
       __delay_ms(10);
       i++;
    }
   SPI1_Initialize();
   configure_RX();
   while (1) {
       LED1 = LOW;
       if (IRQ == 0){
           LED1 = HIGH;
    
       }
       else if (IRQ == 1){
           LED1 = 0;
       }
       //nrf_SPI_Read(data);
       read();
       if(data == 1){
           LATBbits.LATB4 = HIGH;
           LATBbits.LATB5 = LOW;
       }
    else if (data == 0)
    {
        LATBbits.LATB4 = LOW;
        LATBbits.LATB5 = HIGH;
    }
    __delay_ms(10);
    __delay_ms(10);
    reset_RX();
    //__delay_ms(10);
   }
   return;

  }
  //============================================================

 void read(void)
{
 LED2 = HIGH;
 data = SPI1_Exchange8bit(0xFF);
 __delay_ms(10);
 LED2 = LOW;
}
//============================================================
void configure_RX(void)
{
unsigned char j;

CSN = 0;
CE = 0;

//PRX, CRC enabled
SPI1_Exchange8bit(0x20);
SPI1_Exchange8bit(0x39); 
pulse();

//disable auto-ack for all channels      
SPI1_Exchange8bit(0x21);
SPI1_Exchange8bit(0x00);     
pulse();

//address width = 5 bytes  
SPI1_Exchange8bit(0x23);
SPI1_Exchange8bit(0x03);    
pulse();

//data rate = 1MB   
SPI1_Exchange8bit(0x26);
SPI1_Exchange8bit(0x07);    
pulse();

//4 byte payload  
SPI1_Exchange8bit(0x31);
SPI1_Exchange8bit(0x04);    
pulse();

//set channel 2 
SPI1_Exchange8bit(0x25);
SPI1_Exchange8bit(0x02);    
pulse();

   //set address E7E7E7E7E7
   SPI1_Exchange8bit(0x30);
   for (j = 0; j < 5; j++){
	   SPI1_Exchange8bit(0xE7); 
 }
   pulse();

   //PWR_UP = 1   
   SPI1_Exchange8bit(0x20);
   SPI1_Exchange8bit(0x3B);   
   pulse();     
 }
//============================================================
void reset_RX(void)
{
   SPI1_Initialize();
   unsigned char j;
   unsigned char buffer[4];    

//Read RX payload   
      CSN = 0;    
   SPI1_Exchange8bit(0x61);    
   for (j = 0; j < 4; j++)
   {        
   	   buffer[j] = SPI1_Exchange8bit(0);        
   }    
   CSN = 1;    

   //Flush RX FIFO    
      CSN = 0;    
   SPI1_Exchange8bit(0xE2);    
   pulse();

   //reset int    
   SPI1_Exchange8bit(0x27);
   SPI1_Exchange8bit(0x40);    
   CSN = 1;
  }
  void pulse(void)
  {
   CSN = 1; 
   __delay_us(10);
   CSN = 0;
  }
  unsigned char nrf_SPI_Read(unsigned char reg) {
   unsigned char data;

   CSN = 0;                // CSN low, initialize SPI communication...
   SPI1_Exchange8bit(reg);                   // Select register to read from..
   data = SPI1_Exchange8bit(0);           // ..then read register value
   CSN = 0;                  // CSN high, terminate SPI communication

   return(data);               // return register value
  }
Related