iforce2d Chris
/
nRF24L01SimpleExample
Basic port of the gcopeland nRF24L01 library
main.cpp@1:5be2682710c6, 2011-01-19 (annotated)
- Committer:
- Owen
- Date:
- Wed Jan 19 23:53:19 2011 +0000
- Revision:
- 1:5be2682710c6
- Parent:
- 0:a51a6e7da590
- Child:
- 2:75a5b58b2338
Simplified the comments.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Owen | 0:a51a6e7da590 | 1 | #include "mbed.h" |
Owen | 0:a51a6e7da590 | 2 | #include "nRF24L01P.h" |
Owen | 0:a51a6e7da590 | 3 | |
Owen | 0:a51a6e7da590 | 4 | Serial pc(USBTX, USBRX); // tx, rx |
Owen | 0:a51a6e7da590 | 5 | |
Owen | 0:a51a6e7da590 | 6 | nRF24L01P my_nrf24l01p(p5, p6, p7, p8, p9, p10); // mosi, miso, sck, csn, ce, irq |
Owen | 0:a51a6e7da590 | 7 | |
Owen | 0:a51a6e7da590 | 8 | DigitalOut myled1(LED1); |
Owen | 0:a51a6e7da590 | 9 | DigitalOut myled2(LED2); |
Owen | 0:a51a6e7da590 | 10 | |
Owen | 0:a51a6e7da590 | 11 | int main() { |
Owen | 0:a51a6e7da590 | 12 | |
Owen | 0:a51a6e7da590 | 13 | // The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's |
Owen | 0:a51a6e7da590 | 14 | // "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019) |
Owen | 0:a51a6e7da590 | 15 | // only handles 4 byte transfers in the ATMega code. |
Owen | 0:a51a6e7da590 | 16 | #define TRANSFER_SIZE 4 |
Owen | 0:a51a6e7da590 | 17 | |
Owen | 0:a51a6e7da590 | 18 | char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE]; |
Owen | 0:a51a6e7da590 | 19 | int txDataCnt = 0; |
Owen | 0:a51a6e7da590 | 20 | int rxDataCnt = 0; |
Owen | 0:a51a6e7da590 | 21 | |
Owen | 0:a51a6e7da590 | 22 | my_nrf24l01p.powerUp(); |
Owen | 0:a51a6e7da590 | 23 | |
Owen | 0:a51a6e7da590 | 24 | // Display the (default) setup of the nRF24L01+ chip |
Owen | 0:a51a6e7da590 | 25 | pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); |
Owen | 0:a51a6e7da590 | 26 | pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); |
Owen | 0:a51a6e7da590 | 27 | pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); |
Owen | 0:a51a6e7da590 | 28 | pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() ); |
Owen | 0:a51a6e7da590 | 29 | pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() ); |
Owen | 0:a51a6e7da590 | 30 | |
Owen | 0:a51a6e7da590 | 31 | pc.printf( "Type keys to test transfers:\r\n (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE ); |
Owen | 0:a51a6e7da590 | 32 | |
Owen | 0:a51a6e7da590 | 33 | my_nrf24l01p.setTransferSize( TRANSFER_SIZE ); |
Owen | 0:a51a6e7da590 | 34 | |
Owen | 0:a51a6e7da590 | 35 | my_nrf24l01p.setReceiveMode(); |
Owen | 0:a51a6e7da590 | 36 | my_nrf24l01p.enable(); |
Owen | 0:a51a6e7da590 | 37 | |
Owen | 0:a51a6e7da590 | 38 | while (1) { |
Owen | 0:a51a6e7da590 | 39 | |
Owen | 0:a51a6e7da590 | 40 | // If we've received anything over the host serial link... |
Owen | 0:a51a6e7da590 | 41 | if ( pc.readable() ) { |
Owen | 0:a51a6e7da590 | 42 | |
Owen | 0:a51a6e7da590 | 43 | // ...add it to the transmit buffer |
Owen | 0:a51a6e7da590 | 44 | txData[txDataCnt++] = pc.getc(); |
Owen | 0:a51a6e7da590 | 45 | |
Owen | 0:a51a6e7da590 | 46 | // If the transmit buffer is full |
Owen | 0:a51a6e7da590 | 47 | if ( txDataCnt >= sizeof( txData ) ) { |
Owen | 0:a51a6e7da590 | 48 | |
Owen | 0:a51a6e7da590 | 49 | // Send the transmitbuffer via the nRF24L01+ |
Owen | 0:a51a6e7da590 | 50 | my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt ); |
Owen | 0:a51a6e7da590 | 51 | |
Owen | 0:a51a6e7da590 | 52 | txDataCnt = 0; |
Owen | 0:a51a6e7da590 | 53 | } |
Owen | 0:a51a6e7da590 | 54 | |
Owen | 0:a51a6e7da590 | 55 | // Toggle LED1 (to help debug Host -> nRF24L01+ communication) |
Owen | 0:a51a6e7da590 | 56 | myled1 = !myled1; |
Owen | 0:a51a6e7da590 | 57 | } |
Owen | 0:a51a6e7da590 | 58 | |
Owen | 0:a51a6e7da590 | 59 | // If we've received anything in the nRF24L01+... |
Owen | 0:a51a6e7da590 | 60 | if ( my_nrf24l01p.readable() ) { |
Owen | 0:a51a6e7da590 | 61 | |
Owen | 0:a51a6e7da590 | 62 | // ...read the data into the receive buffer |
Owen | 0:a51a6e7da590 | 63 | rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) ); |
Owen | 0:a51a6e7da590 | 64 | |
Owen | 0:a51a6e7da590 | 65 | // Display the receive buffer contents via the host serial link |
Owen | 0:a51a6e7da590 | 66 | for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) { |
Owen | 0:a51a6e7da590 | 67 | |
Owen | 0:a51a6e7da590 | 68 | pc.putc( rxData[i] ); |
Owen | 0:a51a6e7da590 | 69 | } |
Owen | 0:a51a6e7da590 | 70 | |
Owen | 0:a51a6e7da590 | 71 | // Toggle LED2 (to help debug nRF24L01+ -> Host communication) |
Owen | 0:a51a6e7da590 | 72 | myled2 = !myled2; |
Owen | 0:a51a6e7da590 | 73 | } |
Owen | 0:a51a6e7da590 | 74 | } |
Owen | 0:a51a6e7da590 | 75 | } |