Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@2:e309f19062b7, 2020-12-04 (annotated)
- Committer:
- Fab2A
- Date:
- Fri Dec 04 08:16:27 2020 +0000
- Revision:
- 2:e309f19062b7
- Parent:
- 1:5be2682710c6
- Child:
- 3:fdd8560c4739
Test du module nRF24L01+
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 | |
Fab2A | 2:e309f19062b7 | 6 | //Définition des E/S de la liaison SPI vers le module nRF24L01+ |
Fab2A | 2:e309f19062b7 | 7 | nRF24L01P my_nrf24l01p(D11, D12, D13, A3, D9, A0); // mosi, miso, sck, csn, ce, irq |
Owen | 0:a51a6e7da590 | 8 | |
Fab2A | 2:e309f19062b7 | 9 | DigitalOut myled1(D4); |
Fab2A | 2:e309f19062b7 | 10 | DigitalOut myled2(D5); |
Owen | 0:a51a6e7da590 | 11 | |
Owen | 0:a51a6e7da590 | 12 | int main() { |
Owen | 0:a51a6e7da590 | 13 | |
Fab2A | 2:e309f19062b7 | 14 | // Nombre de caractères transmis à chaque envoi |
Owen | 0:a51a6e7da590 | 15 | #define TRANSFER_SIZE 4 |
Owen | 0:a51a6e7da590 | 16 | |
Owen | 0:a51a6e7da590 | 17 | char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE]; |
Owen | 0:a51a6e7da590 | 18 | int txDataCnt = 0; |
Owen | 0:a51a6e7da590 | 19 | int rxDataCnt = 0; |
Fab2A | 2:e309f19062b7 | 20 | |
Fab2A | 2:e309f19062b7 | 21 | pc.baud(115200); // Débit de la liaison série PC |
Owen | 0:a51a6e7da590 | 22 | |
Owen | 0:a51a6e7da590 | 23 | my_nrf24l01p.powerUp(); |
Fab2A | 2:e309f19062b7 | 24 | |
Fab2A | 2:e309f19062b7 | 25 | my_nrf24l01p.setRfFrequency(2416); // Définition de la fréquence du canal d'E/R |
Fab2A | 2:e309f19062b7 | 26 | my_nrf24l01p.setAirDataRate(NRF24L01P_DATARATE_1_MBPS); // Définition du débit de la communication RF |
Fab2A | 2:e309f19062b7 | 27 | |
Owen | 0:a51a6e7da590 | 28 | |
Fab2A | 2:e309f19062b7 | 29 | // Affichage de l'état de la configuration du nRF24L01+ |
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() ); |
Fab2A | 2:e309f19062b7 | 34 | pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() ); |
Fab2A | 2:e309f19062b7 | 35 | |
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 | my_nrf24l01p.setReceiveMode(); |
Owen | 0:a51a6e7da590 | 41 | my_nrf24l01p.enable(); |
Fab2A | 2:e309f19062b7 | 42 | |
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() ) { |
Fab2A | 2:e309f19062b7 | 48 | |
Owen | 0:a51a6e7da590 | 49 | |
Owen | 0:a51a6e7da590 | 50 | // ...add it to the transmit buffer |
Owen | 0:a51a6e7da590 | 51 | txData[txDataCnt++] = pc.getc(); |
Owen | 0:a51a6e7da590 | 52 | |
Owen | 0:a51a6e7da590 | 53 | // If the transmit buffer is full |
Owen | 0:a51a6e7da590 | 54 | if ( txDataCnt >= sizeof( txData ) ) { |
Owen | 0:a51a6e7da590 | 55 | |
Owen | 0:a51a6e7da590 | 56 | // Send the transmitbuffer via the nRF24L01+ |
Owen | 0:a51a6e7da590 | 57 | my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt ); |
Owen | 0:a51a6e7da590 | 58 | |
Owen | 0:a51a6e7da590 | 59 | txDataCnt = 0; |
Owen | 0:a51a6e7da590 | 60 | } |
Owen | 0:a51a6e7da590 | 61 | |
Owen | 0:a51a6e7da590 | 62 | // Toggle LED1 (to help debug Host -> nRF24L01+ communication) |
Owen | 0:a51a6e7da590 | 63 | myled1 = !myled1; |
Owen | 0:a51a6e7da590 | 64 | } |
Owen | 0:a51a6e7da590 | 65 | |
Fab2A | 2:e309f19062b7 | 66 | // If we've received anything in the nRF24L01+2... |
Owen | 0:a51a6e7da590 | 67 | if ( my_nrf24l01p.readable() ) { |
Owen | 0:a51a6e7da590 | 68 | |
Owen | 0:a51a6e7da590 | 69 | // ...read the data into the receive buffer |
Owen | 0:a51a6e7da590 | 70 | rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) ); |
Owen | 0:a51a6e7da590 | 71 | |
Owen | 0:a51a6e7da590 | 72 | // Display the receive buffer contents via the host serial link |
Owen | 0:a51a6e7da590 | 73 | for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) { |
Owen | 0:a51a6e7da590 | 74 | |
Owen | 0:a51a6e7da590 | 75 | pc.putc( rxData[i] ); |
Owen | 0:a51a6e7da590 | 76 | } |
Owen | 0:a51a6e7da590 | 77 | |
Owen | 0:a51a6e7da590 | 78 | // Toggle LED2 (to help debug nRF24L01+ -> Host communication) |
Owen | 0:a51a6e7da590 | 79 | myled2 = !myled2; |
Owen | 0:a51a6e7da590 | 80 | } |
Owen | 0:a51a6e7da590 | 81 | } |
Owen | 0:a51a6e7da590 | 82 | } |