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).

Dependencies:   mbed nRF24L01P

Committer:
ViriJay
Date:
Thu Dec 13 11:30:36 2018 +0000
Revision:
3:37c053b30ca0
Parent:
2:766f2f117453
Child:
4:7fa578849b23
schtuff

Who changed what in which revision?

UserRevisionLine numberNew 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.
Owen 0:a51a6e7da590 43 char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE];
ViriJay 3:37c053b30ca0 44 int txDataCnt = 0;
ViriJay 3:37c053b30ca0 45 int rxDataCnt = 0;
Owen 0:a51a6e7da590 46
Owen 0:a51a6e7da590 47 my_nrf24l01p.powerUp();
ViriJay 3:37c053b30ca0 48
ViriJay 3:37c053b30ca0 49 printSettings();
ViriJay 3:37c053b30ca0 50
ViriJay 3:37c053b30ca0 51 setFrequency(2450);
ViriJay 3:37c053b30ca0 52 setPower(-6);
ViriJay 3:37c053b30ca0 53 setDataReet(1000);
ViriJay 3:37c053b30ca0 54
ViriJay 3:37c053b30ca0 55 printSettings();
ViriJay 3:37c053b30ca0 56
ViriJay 3:37c053b30ca0 57 setDefaults();
ViriJay 3:37c053b30ca0 58
ViriJay 3:37c053b30ca0 59 printSettings();
Owen 0:a51a6e7da590 60
Owen 0:a51a6e7da590 61 my_nrf24l01p.setTransferSize( TRANSFER_SIZE );
Owen 0:a51a6e7da590 62
Owen 0:a51a6e7da590 63 my_nrf24l01p.setReceiveMode();
Owen 0:a51a6e7da590 64 my_nrf24l01p.enable();
Owen 0:a51a6e7da590 65
Owen 0:a51a6e7da590 66 while (1) {
ViriJay 3:37c053b30ca0 67
ViriJay 3:37c053b30ca0 68 // If we've received anything over the host serial link...
ViriJay 3:37c053b30ca0 69 if ( pc.readable() ) {
ViriJay 3:37c053b30ca0 70
ViriJay 3:37c053b30ca0 71 // ...add it to the transmit buffer
ViriJay 3:37c053b30ca0 72 txData[txDataCnt++] = pc.getc();
ViriJay 3:37c053b30ca0 73
ViriJay 3:37c053b30ca0 74 // If the transmit buffer is full
ViriJay 3:37c053b30ca0 75 if ( txDataCnt >= sizeof( txData ) ) {
ViriJay 3:37c053b30ca0 76
ViriJay 3:37c053b30ca0 77 // Send the transmitbuffer via the nRF24L01+
ViriJay 3:37c053b30ca0 78 my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt );
ViriJay 3:37c053b30ca0 79
ViriJay 3:37c053b30ca0 80 txDataCnt = 0;
ViriJay 3:37c053b30ca0 81 }
ViriJay 3:37c053b30ca0 82
Owen 0:a51a6e7da590 83 }
ViriJay 3:37c053b30ca0 84
Owen 0:a51a6e7da590 85 // If we've received anything in the nRF24L01+...
Owen 0:a51a6e7da590 86 if ( my_nrf24l01p.readable() ) {
ViriJay 3:37c053b30ca0 87
Owen 0:a51a6e7da590 88 // ...read the data into the receive buffer
ViriJay 3:37c053b30ca0 89 rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) );
Owen 0:a51a6e7da590 90
ViriJay 3:37c053b30ca0 91 // Display the receive buffer contents via the host serial link
ViriJay 3:37c053b30ca0 92 for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) {
ViriJay 3:37c053b30ca0 93
ViriJay 3:37c053b30ca0 94 pc.putc( rxData[i] );
Owen 0:a51a6e7da590 95 }
ViriJay 3:37c053b30ca0 96
Owen 0:a51a6e7da590 97 }
Owen 0:a51a6e7da590 98 }
ViriJay 3:37c053b30ca0 99 }