This is a work in progress for an NRF2401P
Dependents: NRF_receiver sender locker4 Weather_Station_Ofiicial ... more
About
This is a simple library to drive the nRF24l01+.
Hardware
This uses the commonly available breakout. The connections are shown below
Software
Use case: For a simple transmitter
tx code snipet
#include "NRF2401P.h" int main() { * * long long addr1=0xAB00CD; // setup address - any 5 byte number - same as RX * int channel =0x12; // [0-126] setup channel, must be same as RX * bool txOK; * char msg[32]; * char ackData[32]; * char len; * * // Setup * NRF2401P nrf1(PTD6,PTD7, PTD5,PTD4, PTC12); //mosi, miso, sclk, csn, ce) * nrf1.quickTxSetup(channel, addr1); // sets nrf24l01+ as transmitter * * // transmit * strcpy (msg, "Hello"); * txOK= nrf1.transmitData(msg,strlen(msg)); * * // read ack data if available * if (nrf1.isAckData()) { * len= nrf1.getRxData(ackData); // len is number of bytes in ackData * } *}
Use case: For a simple receiver
rx code snipet
#include "NRF2401P.h" *int main(){ * * long long addr1=0xAB00CD; // setup address - any 5 byte number - same as TX * int channel =0x12; // [0-126] setup channel, must be same as TX * bool txOK; * char msg[32]; * char ackData[32]; * char len; * * // Setup * NRF2401P nrf1(PTD6,PTD7, PTD5,PTD4, PTC12); //mosi, miso, sclk, csn, ce) * nrf1.quickRxSetup(channel, addr1); // sets nrf24l01+ as receiver, using pipe 1 * * // set ack data * sprintf(ackData,"Ack data"); * nrf1.acknowledgeData(ackData, strlen(ackData),1); // ack for pipe 1 * * // receive * while (! nrf1.isRxData()); // note this blocks until RX data * len= nrf1.getRxData(msg); // gets the message, len is length of msg * *}
Diff: NRF2401P.cpp
- Revision:
- 16:a9b83d2b6915
- Parent:
- 14:976a876819ae
- Child:
- 17:b132fc1a27d2
--- a/NRF2401P.cpp Sat Jul 11 22:17:36 2015 +0000 +++ b/NRF2401P.cpp Thu Jan 28 14:37:32 2016 +0000 @@ -28,7 +28,7 @@ #include "nRF24l01.h" NRF2401P::NRF2401P ( PinName mosi, PinName miso, PinName sclk, PinName _csn, PinName _ce ) : - csn( DigitalOut( _csn ) ), ce( DigitalOut( _ce ) ) + csn( DigitalOut( _csn ) ), ce( DigitalOut( _ce ) ) { addressWidth = 5; pc = new Serial( USBTX, USBRX ); // tx, rx @@ -87,17 +87,19 @@ } /** -* Sets up a receiver using shockburst and dynamic payload. Uses pipe 1 +* Sets up a receiver using shockburst and dynamic payload. Uses pipe 0 * defaults to 5 bytes */ void NRF2401P::quickRxSetup(int channel,long long addrRx) { start(); setChannel(channel); - setRxAddress(addrRx,1); + // setRxAddress(addrRx,1); + setRxAddress(addrRx,0); setRxMode(); ce=1; wait(0.001f); + writeReg(FEATURE,0x06); // Enable dynamic ack packets } /** @@ -146,8 +148,11 @@ setChannel(channel); setTxAddress(addr); setTxMode(); + writeReg(FEATURE,0x06); ce=1; wait (0.0016f); // wait for pll to settle + flushTx(); + clearStatus(); } /** @@ -338,7 +343,7 @@ * Reads n bytes from a register **/ void NRF2401P::readReg( char address, char *data, char width ) -{ +{ char reg; csn = 0; int i; @@ -491,18 +496,18 @@ char reg = 0x0A + pipe; switch ( pipe ) { - case ( 0 ) : + case ( 0 ) : case ( 1 ) : { - writeReg(reg, address, addressWidth); //Write to RX_ADDR_P0 or _P1 - break; - } - case ( 2 ) : - case ( 3 ) : - case ( 4 ) : - case ( 5 ) : { - writeReg(reg, address, 1); //Write to RX_ADDR_P2 ... _P5 - break; - } + writeReg(reg, address, addressWidth); //Write to RX_ADDR_P0 or _P1 + break; + } + case ( 2 ) : + case ( 3 ) : + case ( 4 ) : + case ( 5 ) : { + writeReg(reg, address, 1); //Write to RX_ADDR_P2 ... _P5 + break; + } } readReg(EN_RXADDR, ®); @@ -572,7 +577,7 @@ } else { readReg(RX_PW_P1, &width); // width of p1 } - + return width; } @@ -715,8 +720,8 @@ if (newline) { printf("\r\n"); } -} - +} + void NRF2401P::printReg(char* name, char address, char width, bool newline) { char data[width];