Nino Der Kinderen
/
nRF24L01P_ProjectSensor
se
Fork of nRF24L01P_Project by
main.cpp@2:178e089b3ce9, 2015-05-07 (annotated)
- Committer:
- ninoderkinderen
- Date:
- Thu May 07 16:39:31 2015 +0000
- Revision:
- 2:178e089b3ce9
- Parent:
- 1:5be2682710c6
- Child:
- 3:1f92895432d2
projectwireless
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. |
ninoderkinderen | 2:178e089b3ce9 | 16 | #define TRANSFER_SIZE 32 |
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(); |
ninoderkinderen | 2:178e089b3ce9 | 23 | |
ninoderkinderen | 2:178e089b3ce9 | 24 | //Set Device |
ninoderkinderen | 2:178e089b3ce9 | 25 | my_nrf24l01p.setTxAddress(0xE7E7E7E7E7,5); |
ninoderkinderen | 2:178e089b3ce9 | 26 | //my_nrf24l01p.setRxAddress(0xE7E7E7E7E7,5); |
ninoderkinderen | 2:178e089b3ce9 | 27 | my_nrf24l01p.setRfFrequency(2450); |
ninoderkinderen | 2:178e089b3ce9 | 28 | my_nrf24l01p.setRfOutputPower(0); |
Owen | 0:a51a6e7da590 | 29 | |
Owen | 0:a51a6e7da590 | 30 | // Display the (default) setup of the nRF24L01+ chip |
Owen | 0:a51a6e7da590 | 31 | pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); |
Owen | 0:a51a6e7da590 | 32 | pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); |
Owen | 0:a51a6e7da590 | 33 | pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); |
Owen | 0:a51a6e7da590 | 34 | pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() ); |
ninoderkinderen | 2:178e089b3ce9 | 35 | pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress(0) ); |
Owen | 0:a51a6e7da590 | 36 | |
Owen | 0:a51a6e7da590 | 37 | pc.printf( "Type keys to test transfers:\r\n (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE ); |
Owen | 0:a51a6e7da590 | 38 | |
Owen | 0:a51a6e7da590 | 39 | my_nrf24l01p.setTransferSize( TRANSFER_SIZE ); |
Owen | 0:a51a6e7da590 | 40 | |
Owen | 0:a51a6e7da590 | 41 | my_nrf24l01p.setReceiveMode(); |
Owen | 0:a51a6e7da590 | 42 | my_nrf24l01p.enable(); |
Owen | 0:a51a6e7da590 | 43 | |
Owen | 0:a51a6e7da590 | 44 | while (1) { |
Owen | 0:a51a6e7da590 | 45 | |
Owen | 0:a51a6e7da590 | 46 | // If we've received anything over the host serial link... |
Owen | 0:a51a6e7da590 | 47 | if ( pc.readable() ) { |
Owen | 0:a51a6e7da590 | 48 | |
Owen | 0:a51a6e7da590 | 49 | // ...add it to the transmit buffer |
Owen | 0:a51a6e7da590 | 50 | txData[txDataCnt++] = pc.getc(); |
ninoderkinderen | 2:178e089b3ce9 | 51 | |
ninoderkinderen | 2:178e089b3ce9 | 52 | //pc.printf("lengte txData: %d \n\r", sizeof(txData)); |
ninoderkinderen | 2:178e089b3ce9 | 53 | //pc.printf("txData count: %d \n\r", txDataCnt); |
Owen | 0:a51a6e7da590 | 54 | |
Owen | 0:a51a6e7da590 | 55 | // If the transmit buffer is full |
ninoderkinderen | 2:178e089b3ce9 | 56 | if ( txDataCnt >= sizeof(txData) ) { |
ninoderkinderen | 2:178e089b3ce9 | 57 | //pc.printf("eerste if \n\r"); |
Owen | 0:a51a6e7da590 | 58 | // Send the transmitbuffer via the nRF24L01+ |
Owen | 0:a51a6e7da590 | 59 | my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt ); |
Owen | 0:a51a6e7da590 | 60 | |
Owen | 0:a51a6e7da590 | 61 | txDataCnt = 0; |
Owen | 0:a51a6e7da590 | 62 | } |
Owen | 0:a51a6e7da590 | 63 | |
Owen | 0:a51a6e7da590 | 64 | // Toggle LED1 (to help debug Host -> nRF24L01+ communication) |
Owen | 0:a51a6e7da590 | 65 | myled1 = !myled1; |
Owen | 0:a51a6e7da590 | 66 | } |
Owen | 0:a51a6e7da590 | 67 | |
Owen | 0:a51a6e7da590 | 68 | // If we've received anything in the nRF24L01+... |
ninoderkinderen | 2:178e089b3ce9 | 69 | if ( my_nrf24l01p.readable(NRF24L01P_PIPE_P0) ) { |
ninoderkinderen | 2:178e089b3ce9 | 70 | //pc.printf("tweede if \n\r"); |
Owen | 0:a51a6e7da590 | 71 | // ...read the data into the receive buffer |
Owen | 0:a51a6e7da590 | 72 | rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) ); |
Owen | 0:a51a6e7da590 | 73 | |
Owen | 0:a51a6e7da590 | 74 | // Display the receive buffer contents via the host serial link |
Owen | 0:a51a6e7da590 | 75 | for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) { |
Owen | 0:a51a6e7da590 | 76 | |
Owen | 0:a51a6e7da590 | 77 | pc.putc( rxData[i] ); |
Owen | 0:a51a6e7da590 | 78 | } |
Owen | 0:a51a6e7da590 | 79 | |
Owen | 0:a51a6e7da590 | 80 | // Toggle LED2 (to help debug nRF24L01+ -> Host communication) |
Owen | 0:a51a6e7da590 | 81 | myled2 = !myled2; |
Owen | 0:a51a6e7da590 | 82 | } |
Owen | 0:a51a6e7da590 | 83 | } |
Owen | 0:a51a6e7da590 | 84 | } |