Gateway

Dependencies:   mbed nRF24L01P

Fork of JNP3_IOT_6_RADIO_ECHO_FIXED by JNP3_IOT_2016Z

Committer:
ttajmajer
Date:
Tue Nov 15 08:41:29 2016 +0000
Revision:
2:352f66e435da
Child:
3:335c4028bf36
JNP3 nrf24 echo app

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ttajmajer 2:352f66e435da 1 #include "mbed.h"
ttajmajer 2:352f66e435da 2 #include "nRF24L01P.h"
ttajmajer 2:352f66e435da 3
ttajmajer 2:352f66e435da 4 //***************************************************************************//
ttajmajer 2:352f66e435da 5 // !!! SETTINGS !!!
ttajmajer 2:352f66e435da 6
ttajmajer 2:352f66e435da 7 #define DATA_RATE NRF24L01P_DATARATE_250_KBPS
ttajmajer 2:352f66e435da 8 //#define DATA_RATE NRF24L01P_DATARATE_1_MBPS
ttajmajer 2:352f66e435da 9 //#define DATA_RATE NRF24L01P_DATARATE_2_MBPS
ttajmajer 2:352f66e435da 10
ttajmajer 2:352f66e435da 11 #define POWER NRF24L01P_TX_PWR_ZERO_DB
ttajmajer 2:352f66e435da 12 //#define POWER NRF24L01P_TX_PWR_MINUS_6_DB
ttajmajer 2:352f66e435da 13 //#define POWER NRF24L01P_TX_PWR_MINUS_12_DB
ttajmajer 2:352f66e435da 14 //#define POWER NRF24L01P_TX_PWR_MINUS_18_DB
ttajmajer 2:352f66e435da 15
ttajmajer 2:352f66e435da 16 #define CHANNEL 0 // 0 - 30
ttajmajer 2:352f66e435da 17
ttajmajer 2:352f66e435da 18
ttajmajer 2:352f66e435da 19 #define RX_ADDRESS ((unsigned long long) 0xABCDEF02)
ttajmajer 2:352f66e435da 20 #define TX_ADDRESS ((unsigned long long) 0xABCDEF00)
ttajmajer 2:352f66e435da 21 #define ECHO_DELAY 0.2
ttajmajer 2:352f66e435da 22
ttajmajer 2:352f66e435da 23 #define TRANSFER_SIZE 1
ttajmajer 2:352f66e435da 24
ttajmajer 2:352f66e435da 25 //***************************************************************************//
ttajmajer 2:352f66e435da 26
ttajmajer 2:352f66e435da 27
ttajmajer 2:352f66e435da 28
ttajmajer 2:352f66e435da 29 Serial pc(USBTX, USBRX); // tx, rx
ttajmajer 2:352f66e435da 30 nRF24L01P radio(PB_15, PB_14, PB_13, PB_12, PB_1, PB_2); // mosi, miso, sck, csn, ce, irq
ttajmajer 2:352f66e435da 31
ttajmajer 2:352f66e435da 32
ttajmajer 2:352f66e435da 33 DigitalOut myled1(LED1);
ttajmajer 2:352f66e435da 34 InterruptIn button(USER_BUTTON);
ttajmajer 2:352f66e435da 35
ttajmajer 2:352f66e435da 36 bool send_data = false;
ttajmajer 2:352f66e435da 37
ttajmajer 2:352f66e435da 38 void send_packet(){
ttajmajer 2:352f66e435da 39 send_data = true;
ttajmajer 2:352f66e435da 40 }
ttajmajer 2:352f66e435da 41
ttajmajer 2:352f66e435da 42 int main() {
ttajmajer 2:352f66e435da 43
ttajmajer 2:352f66e435da 44
ttajmajer 2:352f66e435da 45 char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE];
ttajmajer 2:352f66e435da 46
ttajmajer 2:352f66e435da 47 pc.baud(115200);
ttajmajer 2:352f66e435da 48 radio.powerDown();
ttajmajer 2:352f66e435da 49 radio.powerUp();
ttajmajer 2:352f66e435da 50
ttajmajer 2:352f66e435da 51 button.rise(&send_packet);
ttajmajer 2:352f66e435da 52
ttajmajer 2:352f66e435da 53 radio.setAirDataRate(DATA_RATE);
ttajmajer 2:352f66e435da 54 radio.setRfOutputPower(POWER);
ttajmajer 2:352f66e435da 55 radio.setRfFrequency(NRF24L01P_MIN_RF_FREQUENCY + 4 * CHANNEL);
ttajmajer 2:352f66e435da 56 radio.setRxAddress(RX_ADDRESS, 4);
ttajmajer 2:352f66e435da 57 radio.setTxAddress(TX_ADDRESS, 4);
ttajmajer 2:352f66e435da 58 radio.setCrcWidth(NRF24L01P_CRC_8_BIT);
ttajmajer 2:352f66e435da 59
ttajmajer 2:352f66e435da 60 // Display the (default) setup of the nRF24L01+ chip
ttajmajer 2:352f66e435da 61 pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", radio.getRfFrequency() );
ttajmajer 2:352f66e435da 62 pc.printf( "nRF24L01+ Output power : %d dBm\r\n", radio.getRfOutputPower() );
ttajmajer 2:352f66e435da 63 pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", radio.getAirDataRate() );
ttajmajer 2:352f66e435da 64 pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", radio.getTxAddress() );
ttajmajer 2:352f66e435da 65 pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", radio.getRxAddress() );
ttajmajer 2:352f66e435da 66
ttajmajer 2:352f66e435da 67
ttajmajer 2:352f66e435da 68 radio.setTransferSize( TRANSFER_SIZE );
ttajmajer 2:352f66e435da 69
ttajmajer 2:352f66e435da 70 myled1 = 0;
ttajmajer 2:352f66e435da 71
ttajmajer 2:352f66e435da 72 radio.setReceiveMode();
ttajmajer 2:352f66e435da 73 radio.enable();
ttajmajer 2:352f66e435da 74
ttajmajer 2:352f66e435da 75 while (1) {
ttajmajer 2:352f66e435da 76 int tx_bytes;
ttajmajer 2:352f66e435da 77 int rx_bytes;
ttajmajer 2:352f66e435da 78
ttajmajer 2:352f66e435da 79 if(radio.readable()){
ttajmajer 2:352f66e435da 80
ttajmajer 2:352f66e435da 81 rx_bytes = radio.read(NRF24L01P_PIPE_P0, rxData, sizeof(rxData));
ttajmajer 2:352f66e435da 82 myled1 = 1;
ttajmajer 2:352f66e435da 83
ttajmajer 2:352f66e435da 84 wait(ECHO_DELAY);
ttajmajer 2:352f66e435da 85
ttajmajer 2:352f66e435da 86 tx_bytes = radio.write(NRF24L01P_PIPE_P0, txData, TRANSFER_SIZE);
ttajmajer 2:352f66e435da 87 myled1 = 0;
ttajmajer 2:352f66e435da 88 }
ttajmajer 2:352f66e435da 89
ttajmajer 2:352f66e435da 90 if(send_data){
ttajmajer 2:352f66e435da 91 tx_bytes = radio.write(NRF24L01P_PIPE_P0, txData, TRANSFER_SIZE);
ttajmajer 2:352f66e435da 92 send_data = false;
ttajmajer 2:352f66e435da 93 }
ttajmajer 2:352f66e435da 94
ttajmajer 2:352f66e435da 95 }
ttajmajer 2:352f66e435da 96 }