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: BSP_DISCO_L476VG LCD_DISCO_L476VG mbed nRF24L01P
Fork of Final_project by
main.cpp@3:2544ec35c76f, 2018-04-12 (annotated)
- Committer:
- dnaples02
- Date:
- Thu Apr 12 23:00:18 2018 +0000
- Revision:
- 3:2544ec35c76f
- Parent:
- 2:9ecf15bfb0b4
- Child:
- 4:dcee65c6cebf
dom
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" |
dnaples02 | 3:2544ec35c76f | 3 | #include "LCD_DISCO_L476VG.h" |
Owen | 0:a51a6e7da590 | 4 | |
Owen | 0:a51a6e7da590 | 5 | Serial pc(USBTX, USBRX); // tx, rx |
Owen | 0:a51a6e7da590 | 6 | |
dnaples02 | 2:9ecf15bfb0b4 | 7 | nRF24L01P my_nrf24l01p(PE_15, PE_14, PE_13, PE_12, PE_10, PE_11); // mosi, miso, sck, csn, ce, irq |
Owen | 0:a51a6e7da590 | 8 | |
Owen | 0:a51a6e7da590 | 9 | DigitalOut myled1(LED1); |
Owen | 0:a51a6e7da590 | 10 | DigitalOut myled2(LED2); |
dnaples02 | 3:2544ec35c76f | 11 | LCD_DISCO_L476VG lcd; |
Owen | 0:a51a6e7da590 | 12 | |
Owen | 0:a51a6e7da590 | 13 | int main() { |
Owen | 0:a51a6e7da590 | 14 | |
Owen | 0:a51a6e7da590 | 15 | // The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's |
Owen | 0:a51a6e7da590 | 16 | // "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019) |
Owen | 0:a51a6e7da590 | 17 | // only handles 4 byte transfers in the ATMega code. |
Owen | 0:a51a6e7da590 | 18 | #define TRANSFER_SIZE 4 |
Owen | 0:a51a6e7da590 | 19 | |
Owen | 0:a51a6e7da590 | 20 | char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE]; |
Owen | 0:a51a6e7da590 | 21 | int txDataCnt = 0; |
Owen | 0:a51a6e7da590 | 22 | int rxDataCnt = 0; |
dnaples02 | 3:2544ec35c76f | 23 | uint32_t temp = 0; |
dnaples02 | 3:2544ec35c76f | 24 | uint8_t stemp[7] = {0}; |
dnaples02 | 3:2544ec35c76f | 25 | //uint8_t title[] = " DISCOVERY STM32L476"; |
Owen | 0:a51a6e7da590 | 26 | my_nrf24l01p.powerUp(); |
dnaples02 | 3:2544ec35c76f | 27 | |
Owen | 0:a51a6e7da590 | 28 | |
Owen | 0:a51a6e7da590 | 29 | // Display the (default) setup of the nRF24L01+ chip |
Owen | 0:a51a6e7da590 | 30 | pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); |
Owen | 0:a51a6e7da590 | 31 | pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); |
Owen | 0:a51a6e7da590 | 32 | pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); |
Owen | 0:a51a6e7da590 | 33 | pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() ); |
Owen | 0:a51a6e7da590 | 34 | pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() ); |
Owen | 0:a51a6e7da590 | 35 | |
Owen | 0:a51a6e7da590 | 36 | pc.printf( "Type keys to test transfers:\r\n (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE ); |
Owen | 0:a51a6e7da590 | 37 | |
Owen | 0:a51a6e7da590 | 38 | my_nrf24l01p.setTransferSize( TRANSFER_SIZE ); |
Owen | 0:a51a6e7da590 | 39 | |
Owen | 0:a51a6e7da590 | 40 | my_nrf24l01p.setReceiveMode(); |
Owen | 0:a51a6e7da590 | 41 | my_nrf24l01p.enable(); |
dnaples02 | 3:2544ec35c76f | 42 | |
dnaples02 | 3:2544ec35c76f | 43 | printf("Hello\n"); |
dnaples02 | 3:2544ec35c76f | 44 | //led_green = 1; |
dnaples02 | 3:2544ec35c76f | 45 | lcd.Clear(); |
dnaples02 | 3:2544ec35c76f | 46 | lcd.DisplayString((uint8_t *)"HELLO"); |
dnaples02 | 3:2544ec35c76f | 47 | wait(1); |
dnaples02 | 3:2544ec35c76f | 48 | |
Owen | 0:a51a6e7da590 | 49 | while (1) { |
Owen | 0:a51a6e7da590 | 50 | |
Owen | 0:a51a6e7da590 | 51 | // If we've received anything over the host serial link... |
Owen | 0:a51a6e7da590 | 52 | if ( pc.readable() ) { |
Owen | 0:a51a6e7da590 | 53 | |
Owen | 0:a51a6e7da590 | 54 | // ...add it to the transmit buffer |
dnaples02 | 3:2544ec35c76f | 55 | txData[txDataCnt++] = pc.getc();} |
Owen | 0:a51a6e7da590 | 56 | |
Owen | 0:a51a6e7da590 | 57 | // If the transmit buffer is full |
Owen | 0:a51a6e7da590 | 58 | if ( txDataCnt >= sizeof( txData ) ) { |
Owen | 0:a51a6e7da590 | 59 | |
Owen | 0:a51a6e7da590 | 60 | // Send the transmitbuffer via the nRF24L01+ |
Owen | 0:a51a6e7da590 | 61 | my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt ); |
Owen | 0:a51a6e7da590 | 62 | |
Owen | 0:a51a6e7da590 | 63 | txDataCnt = 0; |
dnaples02 | 3:2544ec35c76f | 64 | |
Owen | 0:a51a6e7da590 | 65 | |
Owen | 0:a51a6e7da590 | 66 | // Toggle LED1 (to help debug Host -> nRF24L01+ communication) |
Owen | 0:a51a6e7da590 | 67 | myled1 = !myled1; |
Owen | 0:a51a6e7da590 | 68 | } |
Owen | 0:a51a6e7da590 | 69 | |
Owen | 0:a51a6e7da590 | 70 | // If we've received anything in the nRF24L01+... |
Owen | 0:a51a6e7da590 | 71 | if ( my_nrf24l01p.readable() ) { |
Owen | 0:a51a6e7da590 | 72 | |
Owen | 0:a51a6e7da590 | 73 | // ...read the data into the receive buffer |
Owen | 0:a51a6e7da590 | 74 | rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) ); |
Owen | 0:a51a6e7da590 | 75 | |
Owen | 0:a51a6e7da590 | 76 | // Display the receive buffer contents via the host serial link |
Owen | 0:a51a6e7da590 | 77 | for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) { |
Owen | 0:a51a6e7da590 | 78 | |
Owen | 0:a51a6e7da590 | 79 | pc.putc( rxData[i] ); |
Owen | 0:a51a6e7da590 | 80 | } |
dnaples02 | 3:2544ec35c76f | 81 | } |
dnaples02 | 3:2544ec35c76f | 82 | |
dnaples02 | 3:2544ec35c76f | 83 | |
dnaples02 | 3:2544ec35c76f | 84 | |
dnaples02 | 3:2544ec35c76f | 85 | // printf("Scroll sequence\n"); |
dnaples02 | 3:2544ec35c76f | 86 | //led_green = 0; |
dnaples02 | 3:2544ec35c76f | 87 | //lcd.Clear(); |
dnaples02 | 3:2544ec35c76f | 88 | //lcd.ScrollSentence(title, 2, 200); |
dnaples02 | 3:2544ec35c76f | 89 | //lcd.Clear(); |
dnaples02 | 3:2544ec35c76f | 90 | |
dnaples02 | 3:2544ec35c76f | 91 | // while(1) { |
dnaples02 | 3:2544ec35c76f | 92 | // printf("BAR = %d\n", temp); |
dnaples02 | 3:2544ec35c76f | 93 | // sprintf((char *)stemp, "BAR %d", temp); |
dnaples02 | 3:2544ec35c76f | 94 | //lcd.DisplayString(stemp); |
dnaples02 | 3:2544ec35c76f | 95 | //lcd.BarLevelConfig((uint8_t)temp); |
dnaples02 | 3:2544ec35c76f | 96 | //temp++; |
dnaples02 | 3:2544ec35c76f | 97 | //if (temp > 4) temp = 0; |
dnaples02 | 3:2544ec35c76f | 98 | //led_green = !led_green; |
dnaples02 | 3:2544ec35c76f | 99 | // wait(1); |
dnaples02 | 3:2544ec35c76f | 100 | |
Owen | 0:a51a6e7da590 | 101 | // Toggle LED2 (to help debug nRF24L01+ -> Host communication) |
Owen | 0:a51a6e7da590 | 102 | myled2 = !myled2; |
Owen | 0:a51a6e7da590 | 103 | } |
Owen | 0:a51a6e7da590 | 104 | } |
dnaples02 | 3:2544ec35c76f | 105 |