void read_RX(unsigned char *RX_Data)
{
    unsigned char i, j;  
    
	// flush data buffer
	for (i = 0; i < RF_PAYLOAD; i++)
		RX_Data[i] = 0;

	// Read RX payload e coloca no buffer RX_Data 
    SPI_CSN = 0;    
   	spi_Send_Read(0x61);    
    for (j = 0; j < RF_PAYLOAD; j++)
    {        
       	RX_Data[j] = spi_Send_Read(0);        
    }    
    SPI_CSN = 1;    
    
	//Flush RX FIFO    
    SPI_CSN = 0;    
 	spi_Send_Read(0xE2);    
    SPI_CSN = 1;
    SPI_CSN = 0;
 
	//reset int    
  	spi_Send_Read(0x27);
	spi_Send_Read(0x40);    
    SPI_CSN = 1;
}//
unsigned char Test_SPI (void)
{
	unsigned char data [] = {RADIO_ADDRESS_1, RADIO_ADDRESS_2, RADIO_ADDRESS_3, RADIO_ADDRESS_4, RADIO_ADDRESS_5};
	unsigned char data_read = 0;
	unsigned char status, i;

	//write TX_ADDRESS register
	SPI_CSN = 0;			//CSN low
	status = spi_Send_Read(0x30);
	spi_Send_Read(data[0]);
	spi_Send_Read(data[1]);
	spi_Send_Read(data[2]);
	spi_Send_Read(data[3]);
	spi_Send_Read(data[4]);
	SPI_CSN = 1;			//CSN high

	//read TX_ADDRESS register
	//Check that values are correct using the MPLAB debugger
	SPI_CSN = 0;			//CSN low
	status = spi_Send_Read(0x10);
	for (i = 0; i < 5; i++)
	{
		data_read = spi_Send_Read(0x00);
		nop();
		if (data_read != data[i])  	// se lido não for igual
			return 1;				// retorna 0 = erro comunicação SPi PIC/nRF24L01
	}

	SPI_CSN = 1;					// CSN high

	// retorna 0 = sucesso comunicação SPi PIC/nRF24L01
	return 0;

} //
void reset(void)
{
    unsigned char i;
    unsigned char buffer[4];    
    
	//Read RX payload   
    CSN = 0;    
   	spi_Send_Read(0x61); //0110 0001, R_RX_PAYLOAD   
    for (i = 0; i < 4; i++) //check number of buffers/pipes;
    {        
       	buffer[i] = spi_Send_Read(0); //read operation always starts at byte 0.        
    }    
    
    CSN = 1;       
    CSN = 0;    
 	spi_Send_Read(0xE2); //1110 0010 = FLUSH_RX
    
    CSN = 1;
    CSN = 0;  
  	spi_Send_Read(0x27); //0010 0111 = W_REGISTER, 07=STATUS
	spi_Send_Read(0x40); //0100 0000 = Asserted when new data arrives RX FIFO, clear bit
                         
    CSN = 1;
}
void transmit_data( unsigned char *TX_Data )
{
    unsigned char i, data, cmd;   
    
    SPI_CSN = 0;
    
	//clear previous ints
  	spi_Send_Read(0x27);
 	spi_Send_Read(0x7E);
	SPI_CSN = 1;
    SPI_CSN = 0;
    
	//PWR_UP = 1
   	spi_Send_Read(0x20);
 	spi_Send_Read(0x3A);
    SPI_CSN = 1;
    SPI_CSN = 0;
    
    //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
   	spi_Send_Read(0xE1);
    SPI_CSN = 1;
    SPI_CSN = 0;
    
	//fill byte payload
   	spi_Send_Read(0xA0);
   	
	for (i = 0; i< RF_PAYLOAD; i++)
		spi_Send_Read( TX_Data[i] );

    SPI_CSN = 1;
    
    //Pulse CE to start transmission
    SPI_CE = 1;
    Delay100TCYx(2);
    SPI_CE = 0;
}//
// complete configure nRF24L01 for Rx/Tx
void configure_Radio(void)
{
	
    SPI_CSN = 0;
    SPI_CE = 0;
    
	//PRX, CRC enabled
	spi_Send_Read(0x20);
	spi_Send_Read(0x39); 
	SPI_CSN = 1;   
	SPI_CSN = 0;
    
	//disable auto-ack for all channels      
	spi_Send_Read(0x21);
	spi_Send_Read(0x00);     
	SPI_CSN = 1;    
	SPI_CSN = 0;
    
	//address width = 5 bytes  
  	spi_Send_Read(0x23);
	spi_Send_Read(0x03);    
    SPI_CSN = 1;    
    SPI_CSN = 0;
    
	//auto retransmit off
 	spi_Send_Read(0x24);    
   	spi_Send_Read(0x00);    
    SPI_CSN = 1;
    SPI_CSN = 0;

    //set channel 
   	spi_Send_Read(0x25);
	spi_Send_Read(RF_CHANNEL);    
    SPI_CSN = 1;     
    SPI_CSN = 0; 

	//data rate =   
  	spi_Send_Read(0x26);
#if defined ( RF_DATA_RATE_250KBPS ) 
	spi_Send_Read(0x27);    		// 0x07 = 1MBPS - 0x27 = 250Kbps
#elif defined ( RF_DATA_RATE_1MBPS )
	spi_Send_Read(0x07);    		
#else 
	#error "Definir RF_DATA_RATE"
#endif

    SPI_CSN = 1;
  	SPI_CSN = 0;

	//4 byte payload  
 	spi_Send_Read(0x31);
	spi_Send_Read(RF_PAYLOAD);    
    SPI_CSN = 1;    
    SPI_CSN = 0;

    //set TX address 
    spi_Send_Read(0x30);
	spi_Send_Read(RADIO_ADDRESS_1);
	spi_Send_Read(RADIO_ADDRESS_2);
	spi_Send_Read(RADIO_ADDRESS_3);
	spi_Send_Read(RADIO_ADDRESS_4);
	spi_Send_Read(RADIO_ADDRESS_5);

    SPI_CSN = 1;  
    SPI_CSN = 0;

    //set RX address 
    spi_Send_Read(0x2A);
	spi_Send_Read(RADIO_ADDRESS_1);
	spi_Send_Read(RADIO_ADDRESS_2);
	spi_Send_Read(RADIO_ADDRESS_3);
	spi_Send_Read(RADIO_ADDRESS_4);
	spi_Send_Read(RADIO_ADDRESS_5);

    SPI_CSN = 1;  
    SPI_CSN = 0;
    
	//PWR_UP = 1   
 	spi_Send_Read(0x20);
	spi_Send_Read(0x3B);    // 3B
    SPI_CSN = 1;    
    SPI_CE = 1;  
	
}//
// configure nRF24L01 for transmit only
void configure_transmitter(void)
{
	int  i, j;
    unsigned char data, cmd;

    SPI_CE = 0;
    SPI_CSN = 0;

	// PTX, CRC enabled, mask a couple of ints    
 	spi_Send_Read(0x20);
  	spi_Send_Read(0x38);
	SPI_CSN = 1; 
    SPI_CSN = 0;
    
	//auto retransmit off
 	spi_Send_Read(0x24);    
   	spi_Send_Read(0x00);    
    SPI_CSN = 1;
    SPI_CSN = 0;
    
	//address width = 5
   	spi_Send_Read(0x23);
 	spi_Send_Read(0x03);    
    SPI_CSN = 1;
    SPI_CSN = 0;
    
	//data rate = 
   	spi_Send_Read(0x26);
#if defined ( RF_DATA_RATE_250KBPS ) 
	spi_Send_Read(0x27);    		// 0x07 = 1MBPS - 0x27 = 250Kbps
#elif defined ( RF_DATA_RATE_1MBPS )
	spi_Send_Read(0x07);    		
#else 
	#error "Definir RF_DATA_RATE"
#endif
    SPI_CSN = 1; 
    SPI_CSN = 0;
    
	//set channel 
   	spi_Send_Read(0x25);
 	spi_Send_Read(RF_CHANNEL);
    SPI_CSN = 1;
    SPI_CSN = 0;
    
	//set address
   	spi_Send_Read(0x30);    
	spi_Send_Read(RADIO_ADDRESS_1);
	spi_Send_Read(RADIO_ADDRESS_2);
	spi_Send_Read(RADIO_ADDRESS_3);
	spi_Send_Read(RADIO_ADDRESS_4);
	spi_Send_Read(RADIO_ADDRESS_5);
//    for (j = 0; j < 5; j++)
// 		spi_Send_Read(0xE7); 

    SPI_CSN = 1;
    SPI_CSN = 0;
    
    //disable auto-ack, RX mode
    //shouldn't have to do this, but it won't TX if you don't
   	spi_Send_Read(0x21);
 	spi_Send_Read(0x00);
    SPI_CSN = 1;
}//
// configure nRF24L01 for receive only
void configure_RX(void)
{
    unsigned char i, j;

    SPI_CSN = 0;
    SPI_CE = 0;
    
	//PRX, CRC enabled
	spi_Send_Read(0x20);
	spi_Send_Read(0x39); 
	SPI_CSN = 1;   
	SPI_CSN = 0;
    
	//disable auto-ack for all channels      
	spi_Send_Read(0x21);
	spi_Send_Read(0x00);     
	SPI_CSN = 1;    
	SPI_CSN = 0;
    
	//address width = 5 bytes  
  	spi_Send_Read(0x23);
	spi_Send_Read(0x03);    
    SPI_CSN = 1;    
    SPI_CSN = 0;
    
	//data rate =   
  	spi_Send_Read(0x26);
#if defined ( RF_DATA_RATE_250KBPS ) 
	spi_Send_Read(0x27);    		// 0x07 = 1MBPS - 0x27 = 250Kbps
#elif defined ( RF_DATA_RATE_1MBPS )
	spi_Send_Read(0x07);    		
#else 
	#error "Definir RF_DATA_RATE"
#endif

    SPI_CSN = 1;
  	SPI_CSN = 0;

	//4 byte payload  
 	spi_Send_Read(0x31);
	spi_Send_Read(RF_PAYLOAD);    
    SPI_CSN = 1;    
    SPI_CSN = 0;

    //set channel 
   	spi_Send_Read(0x25);
	spi_Send_Read(RF_CHANNEL);    
    SPI_CSN = 1;     
    SPI_CSN = 0; 

    //set address 
    spi_Send_Read(0x30);
	spi_Send_Read(RADIO_ADDRESS_1);
	spi_Send_Read(RADIO_ADDRESS_2);
	spi_Send_Read(RADIO_ADDRESS_3);
	spi_Send_Read(RADIO_ADDRESS_4);
	spi_Send_Read(RADIO_ADDRESS_5);

//    for (j = 0; j < 5; j++)
// 		spi_Send_Read(0xE7); 
    SPI_CSN = 1;  
    SPI_CSN = 0;
    
	//PWR_UP = 1   
 	spi_Send_Read(0x20);
	spi_Send_Read(0x3B);   
    SPI_CSN = 1;    
    SPI_CE = 1;     
}//
void initialize_module(void)
{
    //Every new command must be started by a 
    //high to low transition on CSN
    //<Command word: MSBit to LSBit (one byte)>
    //<Data bytes: LSByte to MSByte, MSBit in each byte first>
    
    //W_REGISTER=001A AAAA (where LSByte is first)
    CSN=0;
    CE=0;
    
    
    //Interrupt not reflected on the IRQ pin, Enable CRC, PRX
    spi_Send_Read(0x20); //00 = CONFIG
    spi_Send_Read(0x39); //0011 1001
    
    //Auto ACK disabled on all data pipes
    CSN = 1;   
	CSN = 0;
    spi_Send_Read(0x21);//01 = Enhanced ShockBurst
    spi_Send_Read(0x00);//0000 0000 //auto ACK disabled on all data pipes
     
    CSN = 1;   
	CSN = 0;
    spi_Send_Read(0x23); //0010 0011= SETUP_AW
	spi_Send_Read(0x03); //0000 0011= RX/TX Address field width of 5 bytes
    
    //Air Data Rate=1Mbps, RF output power in TX mode=0dBm, Setup LNA gain
    CSN = 1;   
	CSN = 0;
    spi_Send_Read(0x26); //0010 0101= RF_SETUP
	spi_Send_Read(0x07); //0000 0111
    
    CSN = 1;   
	CSN = 0;
    spi_Send_Read(0x31); //0011 0001 = 11, RX_PW_P0
	spi_Send_Read(0x04); //0000 0100= 4 bytes in RX payload in data pipe 0
      
    //**for debugging
    //Sets the frequency channel nRF24L01 operates on
    CSN = 1;   
	CSN = 0;
  	spi_Send_Read(0x25);//0010 0101 = 5, RF_CH
	spi_Send_Read(0x02);//0000 0010 =  channel 2
    
    ////**for debugging
    CSN = 1;   
	CSN = 0;
    spi_Send_Read(0x30);//0011 0000 = 10, TX_ADDR
    spi_Send_Read(0xE7);//reset value = 0xE7E7E7E7E7
    spi_Send_Read(0xE7);
    spi_Send_Read(0xE7);
    spi_Send_Read(0xE7);
    spi_Send_Read(0xE7);

    CSN = 1;   
	CSN = 0;
    spi_Send_Read(0x20);//0010 0000= 0,CONFIG
    spi_Send_Read(0x3B);//0011 1011= POWER UP *look at first part of this function
    
    CSN = 1;    
    CE = 1;
}