//Configuration words initialize
//------------------------------------------------------------------------------
void nRF24L01_Tx_Config_Words(void)
{
CE_Low;
CSN_High;
SCK_Low;
CE_Low;
nRF24L01_FLUSH_Tx(); // Clear Tx buffer
nRF24L01_Write_Reg(CONFIG,0x0C); // Power down,2byte CRC
nRF24L01_Write_Reg(EN_AA,0x01); // Enable Auto ACK
nRF24L01_Write_Reg(EN_RXADDR,0x01); // Enable data pipe 0
nRF24L01_Write_Reg(SETUP_RETR,RE_TRANSMIT_TIME); // 336us*2
nRF24L01_Write_Reg(RX_PW_P0,PAYLOAD_LENGTH); // 2 bytes payload data
nRF24L01_Write_Reg(STATUS,0x70); // Clear all the interrupts
nRF24L01_Write_Reg(CONFIG,0x0E); // Power up
DelayXms(2);
nRF24L01_Write_TxAddr_P0();
nRF24L01_Write_RxAddr_P0();
}
void nRF24L01_Rx_Config_Words(void)
{
CE_Low;
CSN_High;
SCK_Low;
CE_Low;
nRF24L01_FLUSH_Tx(); // Clear Tx buffer
nRF24L01_FLUSH_Rx(); // Clear Rx buffer
nRF24L01_Write_Reg(CONFIG,0x0D); // Power down
nRF24L01_Write_Reg(EN_AA,0x01); // Enable AutoAck
nRF24L01_Write_Reg(EN_RXADDR,0x01); // Enable data pipe 0
nRF24L01_Write_Reg(SETUP_RETR,RE_TRANSMIT_TIME); // 336us*2
nRF24L01_Write_Reg(RX_PW_P0,PAYLOAD_LENGTH); // 2 bytes payload data
nRF24L01_Write_Reg(STATUS,0x70); // Clear all the interrupts
nRF24L01_Write_Reg(CONFIG,0x0F); // Power up
DelayXms(2);
nRF24L01_Write_TxAddr_P0();
nRF24L01_Write_RxAddr_P0();
}
//Actions
//------------------------------------------------------------------------------
void nRF24L01_Tx_Payload(void)
{
nRF24L01_FLUSH_Tx();
nRF24L01_Write_Payload(); // Write payload data
CE_High;
DelayXus(20);
CE_Low;
while( PINB&(1<<IRQ) ); // Wait for Tx or Max interrupt
nrf24l01_status = nRF24L01_Read_Reg(STATUS); // Read the STATUS
switch( nrf24l01_status & 0xF0 )
{
case 0x20: LED_G_Flash; // Transmit sign
nRF24L01_Write_Reg(STATUS,0x20); // Clear TX_DS
nrf24l01_status = PACKAGE_SENT; // Store status
break;
case 0x10: LED_R_On; // Package lost
nRF24L01_Write_Reg(STATUS,0x10); // Clear MAX_RT
nrf24l01_status = PACKAGE_LOST; // Store status break;
}
nRF24L01_FLUSH_Tx();
}
void nRF24L01_Rx_Payload(void)
{
CE_High;
while( PINB&(1<<IRQ) ); // Wait for Rx interrupt
nRF24L01_Read_Payload();
nrf24l01_status = nRF24L01_Read_Reg(STATUS); // Read the STATUS
switch( nrf24l01_status & 0xF0 )
{
case 0x60: //LED_G_Flash;
nRF24L01_Write_Reg(STATUS,0x60); // Clear RX_DR
nrf24l01_status = PACKAGE_RECEIVED; // Store status
break;
}
nRF24L01_FLUSH_Rx();
}
//Partial operation
//------------------------------------------------------------------------------
// R/W byte
u08 nRF24L01_RW_Byte(u08 data)
{
SPDR=data;
while( !(SPSR&(1<<SPIF)) );
SPSR|=(1<<SPIF);
return SPDR;
}
// Read Register
u08 nRF24L01_Read_Reg(u08 reg)
{
u08 status_buffer=0;
CSN_Low;
status_buffer = nRF24L01_RW_Byte(reg);
status_buffer = nRF24L01_RW_Byte(NO_OP);
CSN_High;
return status_buffer;
}
// Write Register
void nRF24L01_Write_Reg(u08 reg,u08 value)
{
CSN_Low;
nRF24L01_RW_Byte(0x20+reg);
nRF24L01_RW_Byte(value);
CSN_High;
}
// Read Payload
void nRF24L01_Read_Payload(void)
{
u08 counter=0;
CSN_Low;
nRF24L01_RW_Byte(R_RX_PAYLOAD);
for(counter=0;counter<PAYLOAD_LENGTH;counter++)
{ nrf24l01_rx_buffer[counter] = nRF24L01_RW_Byte(NO_OP); }
CSN_High;
}
// Write Payload
void nRF24L01_Write_Payload(void)
{
u08 counter=0;
CSN_Low;
nRF24L01_RW_Byte(W_TX_PAYLOAD);
for(counter=0;counter<PAYLOAD_LENGTH;counter++)
{ nRF24L01_RW_Byte( nrf24l01_tx_buffer[counter] ); }
CSN_High;
}
// Write Tx address P0
void nRF24L01_Write_TxAddr_P0(void)
{
CSN_Low;
nRF24L01_RW_Byte(0x20+TX_ADDR);
nRF24L01_RW_Byte(0xCC);
nRF24L01_RW_Byte(0x3C);
nRF24L01_RW_Byte(0xC3);
nRF24L01_RW_Byte(0x3C);
nRF24L01_RW_Byte(0xCC);
CSN_High;
}
// Write Rx address P0
void nRF24L01_Write_RxAddr_P0(void)
{
CSN_Low;
nRF24L01_RW_Byte(0x20+RX_ADDR_P0);
nRF24L01_RW_Byte(0xCC);
nRF24L01_RW_Byte(0x3C);
nRF24L01_RW_Byte(0xC3);
nRF24L01_RW_Byte(0x3C);
nRF24L01_RW_Byte(0xCC);
CSN_High;
}
// Clear Tx buffer
void nRF24L01_FLUSH_Tx(void)
{
CSN_Low;
nRF24L01_RW_Byte(0xE1);
CSN_High;
}
// Clear Rx buffer
void nRF24L01_FLUSH_Rx(void)
{
CSN_Low;
nRF24L01_RW_Byte(0xE3);
CSN_High;
}