/* InitSPI */
void InitSPI(void)
{
SSP2CON1=0b00100001; // set SPI master mode
SSP2STAT=0b01000000;
}
/* SPI waitforidle */
void spi_waitForIdle(void)
{
while ( PIR3bits.SSP2IF==0 )
{
/* EasyCODE < */
// wait for idle and not writing
/* EasyCODE > */
}
PIR3bits.SSP2IF=0;
}
/* Read SPI */
void Read_SPI (unsigned char adress, unsigned char DATAIN[5], unsigned char n)
{
unsigned char i=0;
CSN=0;
//read register
SSP2BUF=adress;
spi_waitForIdle();
STATUSREG=SSP2BUF;
for ( i=0;i<n;i++ )
{
SSP2BUF=255;//dummy data
spi_waitForIdle();
DATAIN=SSP2BUF;
}
CSN=1;
}
/* Write SPI */
void write_spi(unsigned char adress, unsigned char DATAIN)
{
unsigned char i=0;
CSN=0;
SSP2BUF=(0b100000|adress);
spi_waitForIdle();
STATUSREG=SSP2BUF;
SSP2BUF=DATAIN;
spi_waitForIdle();
CSN=1;
}
/* Read registers nRF24L01 */
void Read_24L01 (void)
{
Read_SPI(ACONFIG, &CONFIG, 1);
Read_SPI(AEN_AA, &EN_AA, 1);
Read_SPI(AEN_RXADDR, &EN_RXADDR, 1);
Read_SPI(ASETUP_AW, &SETUP_AW, 1);
Read_SPI(ASETUP_RETR, &SETUP_RETR, 1);
Read_SPI(ARF_CH, &RF_CH, 1);
Read_SPI(ARF_SETUP, &RF_SETUP, 1);
Read_SPI(ASTATUSREG, &STATUSREG, 1);
Read_SPI(AOBSERVE_TX, &OBSERVE_TX, 1);
Read_SPI(ACD, &CD, 1);
Read_SPI(ARX_ADDR_P0, RX_ADDR_P0, 5);
Read_SPI(ARX_ADDR_P1, RX_ADDR_P1, 5);
Read_SPI(ARX_ADDR_P2, &RX_ADDR_P2, 1);
Read_SPI(ARX_ADDR_P3, &RX_ADDR_P3, 1);
Read_SPI(ARX_ADDR_P4, &RX_ADDR_P4, 1);
Read_SPI(ARX_ADDR_P5, &RX_ADDR_P5, 1);
Read_SPI(ATX_ADDR, TX_ADDR, 5);
Read_SPI(ARX_PW_P0, &RX_PW_P0, 1);
Read_SPI(ARX_PW_P1, &RX_PW_P1, 1);
Read_SPI(ARX_PW_P2, &RX_PW_P2, 1);
Read_SPI(ARX_PW_P3, &RX_PW_P3, 1);
Read_SPI(ARX_PW_P4, &RX_PW_P4, 1);
Read_SPI(ARX_PW_P5, &RX_PW_P5, 1);
Read_SPI(AFIFO_STATUS, &FIFO_STATUS, 1);
Read_SPI(ADYNPD, &DYNPD, 1);
Read_SPI(AFEATURE, &FEATURE, 1);
}
/* Write nRF24L01 */
void write_rf(unsigned char adress, unsigned char DATAOUT[], unsigned char n)
{
unsigned char i=0;
write_spi(ASTATUSREG, 0b01111110);
write_spi(ACONFIG, 0b00001010);
write_spi(FLUSH_TX,0);
CSN=0;
SSP2BUF=adress;
spi_waitForIdle();
STATUSREG=SSP2BUF;
for ( i=0;i<n;i++ )
{
SSP2BUF=DATAOUT;
spi_waitForIdle();
}
CSN=1;
}
/* void SETTX(void) */
void SETTX(void)
{
CE=0;
write_spi(ACONFIG, 0b00001000);
write_spi(ASETUP_RETR,0);
write_spi(ASETUP_AW,0b00000111);
write_spi(ARF_SETUP, 0b00000111);
write_spi(ARF_CH, 0b00000010);
write_spi(AEN_AA, 0b00000000);
DLCD5MS;
Read_24L01();
}
void SETRX(void)
{
CE=0;
write_spi(ACONFIG, 0b00001001);
write_spi(ASETUP_RETR,0);
write_spi(ASETUP_AW,0b00000111);
write_spi(ARF_SETUP, 0b00000111);
write_spi(ARF_CH, 0b00000010);
write_spi(AEN_AA, 0b00000000);
Read_24L01();
CE=1;
}
MAIN PROGRAM TX (part of it)
write_rf(W_TX_PAYLOAD, RFDATA, 1);
Read_24L01();
DLCD5MS;
CE=1;
DLCD1MS;
DLCD1MS;
CE=0;
DLCD5MS;
Read_24L01();
MAIN PROGRAM RX(part of it)
if ( IRQ==0 )
{
GLEDS=1;
Read_24L01();
}
else
{
GLEDS=0;
Read_24L01();
}
This works for the TX, the IRQ pin goes low after I toggle CE bit and the STATUSREG reports send data.
But on the receiver site nothing IRQ pin doesn’t go low.
Both new chips, antenna’s lie next to each other.
What could it be ?