Test program for the Nordic Semi nRF24L01 Transceiver Module (http://www.sparkfun.com/products/691), talking to another module connected to SparkFun\'s Nordic Serial Interface Board (http://www.sparkfun.com/products/9019).
main.cpp@4:7fa578849b23, 2018-12-18 (annotated)
- Committer:
- ViriJay
- Date:
- Tue Dec 18 10:57:49 2018 +0000
- Revision:
- 4:7fa578849b23
- Parent:
- 3:37c053b30ca0
- Child:
- 6:c55f867da01d
enableART
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 | |
jeroenkoster | 2:766f2f117453 | 6 | nRF24L01P my_nrf24l01p(D11, D12, D13, D8, D9, D7); |
jeroenkoster | 2:766f2f117453 | 7 | |
ViriJay | 3:37c053b30ca0 | 8 | #define TRANSFER_SIZE 4 |
Owen | 0:a51a6e7da590 | 9 | |
ViriJay | 3:37c053b30ca0 | 10 | void setDefaults() { |
ViriJay | 3:37c053b30ca0 | 11 | my_nrf24l01p.setRfFrequency(DEFAULT_NRF24L01P_RF_FREQUENCY); |
ViriJay | 3:37c053b30ca0 | 12 | my_nrf24l01p.setRfOutputPower(DEFAULT_NRF24L01P_TX_PWR); |
ViriJay | 3:37c053b30ca0 | 13 | my_nrf24l01p.setAirDataRate(DEFAULT_NRF24L01P_DATARATE); |
ViriJay | 3:37c053b30ca0 | 14 | } |
ViriJay | 3:37c053b30ca0 | 15 | |
ViriJay | 3:37c053b30ca0 | 16 | void setFrequency(int frequency) { |
ViriJay | 3:37c053b30ca0 | 17 | my_nrf24l01p.setRfFrequency(frequency); |
jeroenkoster | 2:766f2f117453 | 18 | } |
jeroenkoster | 2:766f2f117453 | 19 | |
ViriJay | 3:37c053b30ca0 | 20 | void setPower(int power) { |
ViriJay | 3:37c053b30ca0 | 21 | my_nrf24l01p.setRfOutputPower(power); |
ViriJay | 3:37c053b30ca0 | 22 | } |
jeroenkoster | 2:766f2f117453 | 23 | |
ViriJay | 3:37c053b30ca0 | 24 | void setDataReet(int datareet) { |
ViriJay | 3:37c053b30ca0 | 25 | my_nrf24l01p.setAirDataRate(datareet); |
ViriJay | 3:37c053b30ca0 | 26 | } |
ViriJay | 3:37c053b30ca0 | 27 | |
ViriJay | 3:37c053b30ca0 | 28 | void printSettings() { |
ViriJay | 3:37c053b30ca0 | 29 | // Display the (default) setup of the nRF24L01+ chip |
ViriJay | 3:37c053b30ca0 | 30 | pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); |
ViriJay | 3:37c053b30ca0 | 31 | pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); |
ViriJay | 3:37c053b30ca0 | 32 | pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); |
ViriJay | 3:37c053b30ca0 | 33 | pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() ); |
ViriJay | 3:37c053b30ca0 | 34 | pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() ); |
ViriJay | 3:37c053b30ca0 | 35 | pc.printf( "Type keys to test transfers:\r\n (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE ); |
jeroenkoster | 2:766f2f117453 | 36 | } |
Owen | 0:a51a6e7da590 | 37 | |
Owen | 0:a51a6e7da590 | 38 | int main() { |
Owen | 0:a51a6e7da590 | 39 | |
Owen | 0:a51a6e7da590 | 40 | // The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's |
Owen | 0:a51a6e7da590 | 41 | // "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019) |
Owen | 0:a51a6e7da590 | 42 | // only handles 4 byte transfers in the ATMega code. |
ViriJay | 4:7fa578849b23 | 43 | |
Owen | 0:a51a6e7da590 | 44 | char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE]; |
ViriJay | 3:37c053b30ca0 | 45 | int txDataCnt = 0; |
ViriJay | 3:37c053b30ca0 | 46 | int rxDataCnt = 0; |
Owen | 0:a51a6e7da590 | 47 | |
Owen | 0:a51a6e7da590 | 48 | my_nrf24l01p.powerUp(); |
ViriJay | 3:37c053b30ca0 | 49 | |
ViriJay | 3:37c053b30ca0 | 50 | setFrequency(2450); |
ViriJay | 3:37c053b30ca0 | 51 | |
ViriJay | 3:37c053b30ca0 | 52 | printSettings(); |
ViriJay | 4:7fa578849b23 | 53 | my_nrf24l01p.enableAutoAcknowledge(NRF24L01P_PIPE_P0); |
ViriJay | 4:7fa578849b23 | 54 | my_nrf24l01p.enableAutoRetransmit(100000, 10); |
Owen | 0:a51a6e7da590 | 55 | my_nrf24l01p.setTransferSize( TRANSFER_SIZE ); |
Owen | 0:a51a6e7da590 | 56 | |
Owen | 0:a51a6e7da590 | 57 | my_nrf24l01p.setReceiveMode(); |
Owen | 0:a51a6e7da590 | 58 | my_nrf24l01p.enable(); |
Owen | 0:a51a6e7da590 | 59 | |
Owen | 0:a51a6e7da590 | 60 | while (1) { |
ViriJay | 3:37c053b30ca0 | 61 | |
ViriJay | 3:37c053b30ca0 | 62 | // If we've received anything over the host serial link... |
ViriJay | 3:37c053b30ca0 | 63 | if ( pc.readable() ) { |
ViriJay | 3:37c053b30ca0 | 64 | |
ViriJay | 3:37c053b30ca0 | 65 | // ...add it to the transmit buffer |
ViriJay | 3:37c053b30ca0 | 66 | txData[txDataCnt++] = pc.getc(); |
ViriJay | 3:37c053b30ca0 | 67 | |
ViriJay | 3:37c053b30ca0 | 68 | // If the transmit buffer is full |
ViriJay | 3:37c053b30ca0 | 69 | if ( txDataCnt >= sizeof( txData ) ) { |
ViriJay | 3:37c053b30ca0 | 70 | |
ViriJay | 3:37c053b30ca0 | 71 | // Send the transmitbuffer via the nRF24L01+ |
ViriJay | 3:37c053b30ca0 | 72 | my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt ); |
ViriJay | 3:37c053b30ca0 | 73 | |
ViriJay | 3:37c053b30ca0 | 74 | txDataCnt = 0; |
ViriJay | 3:37c053b30ca0 | 75 | } |
ViriJay | 3:37c053b30ca0 | 76 | |
Owen | 0:a51a6e7da590 | 77 | } |
ViriJay | 3:37c053b30ca0 | 78 | |
Owen | 0:a51a6e7da590 | 79 | // If we've received anything in the nRF24L01+... |
Owen | 0:a51a6e7da590 | 80 | if ( my_nrf24l01p.readable() ) { |
ViriJay | 3:37c053b30ca0 | 81 | |
Owen | 0:a51a6e7da590 | 82 | // ...read the data into the receive buffer |
ViriJay | 3:37c053b30ca0 | 83 | rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) ); |
Owen | 0:a51a6e7da590 | 84 | |
ViriJay | 3:37c053b30ca0 | 85 | // Display the receive buffer contents via the host serial link |
ViriJay | 3:37c053b30ca0 | 86 | for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) { |
ViriJay | 3:37c053b30ca0 | 87 | |
ViriJay | 3:37c053b30ca0 | 88 | pc.putc( rxData[i] ); |
Owen | 0:a51a6e7da590 | 89 | } |
ViriJay | 3:37c053b30ca0 | 90 | |
Owen | 0:a51a6e7da590 | 91 | } |
Owen | 0:a51a6e7da590 | 92 | } |
ViriJay | 3:37c053b30ca0 | 93 | } |