Library for nRF24 module communications

Dependents:   Arts_DMX512_carteV3_MIDI_nRF IeTI_nRF24

Committer:
villemejane
Date:
Sun Dec 12 12:42:12 2021 +0000
Revision:
0:659c6ffdd56c
Library for nRF24 module communications

Who changed what in which revision?

UserRevisionLine numberNew contents of line
villemejane 0:659c6ffdd56c 1 /****************************************************************************/
villemejane 0:659c6ffdd56c 2 /* nRF24L1 module library */
villemejane 0:659c6ffdd56c 3 /****************************************************************************/
villemejane 0:659c6ffdd56c 4 /* LEnsE / Julien VILLEMEJANE / Institut d'Optique Graduate School */
villemejane 0:659c6ffdd56c 5 /****************************************************************************/
villemejane 0:659c6ffdd56c 6 /* Library - nRF24.cpp file */
villemejane 0:659c6ffdd56c 7 /****************************************************************************/
villemejane 0:659c6ffdd56c 8 /* Tested on Nucleo-L476RG / 4th nov 2021 */
villemejane 0:659c6ffdd56c 9 /****************************************************************************/
villemejane 0:659c6ffdd56c 10
villemejane 0:659c6ffdd56c 11 #include "nRF24.h"
villemejane 0:659c6ffdd56c 12
villemejane 0:659c6ffdd56c 13 #define TRANSFER_SIZE 8
villemejane 0:659c6ffdd56c 14
villemejane 0:659c6ffdd56c 15 nRF24L01P nRF24_mod(D11, D12, D13, D10, D9, PB_8);
villemejane 0:659c6ffdd56c 16 // MOSI, MISO, SCK, CSN, CE, IRQ
villemejane 0:659c6ffdd56c 17
villemejane 0:659c6ffdd56c 18 char k;
villemejane 0:659c6ffdd56c 19 char dataToSend[TRANSFER_SIZE] = {0xAA, 0x01, 0x10, 0xF0,0xAA, 0x01, 0x10, 0xF0};
villemejane 0:659c6ffdd56c 20 char dataReceived[TRANSFER_SIZE] = {0};
villemejane 0:659c6ffdd56c 21 char rxDataCnt;
villemejane 0:659c6ffdd56c 22
villemejane 0:659c6ffdd56c 23
villemejane 0:659c6ffdd56c 24 // Fonction d'initialisation du module BT nRF24L01
villemejane 0:659c6ffdd56c 25 void initNRF24(){
villemejane 0:659c6ffdd56c 26 nRF24_mod.powerUp();
villemejane 0:659c6ffdd56c 27 wait_us(100000);
villemejane 0:659c6ffdd56c 28 nRF24_mod.setAirDataRate(NRF24L01P_DATARATE_250_KBPS);
villemejane 0:659c6ffdd56c 29 nRF24_mod.setRfFrequency(2400);
villemejane 0:659c6ffdd56c 30 wait_us(100000);
villemejane 0:659c6ffdd56c 31 debug_pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", nRF24_mod.getRfFrequency() );
villemejane 0:659c6ffdd56c 32 debug_pc.printf( "nRF24L01+ Output power : %d dBm\r\n", nRF24_mod.getRfOutputPower() );
villemejane 0:659c6ffdd56c 33 debug_pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", nRF24_mod.getAirDataRate() );
villemejane 0:659c6ffdd56c 34 debug_pc.printf( "Transfers are grouped into %d characters\r\n", TRANSFER_SIZE );
villemejane 0:659c6ffdd56c 35 nRF24_mod.setTransferSize( TRANSFER_SIZE );
villemejane 0:659c6ffdd56c 36 nRF24_mod.setReceiveMode();
villemejane 0:659c6ffdd56c 37 nRF24_mod.enable();
villemejane 0:659c6ffdd56c 38 }
villemejane 0:659c6ffdd56c 39
villemejane 0:659c6ffdd56c 40 // Fonction de test du module BT nRF24L01
villemejane 0:659c6ffdd56c 41 void testNRF24(void){
villemejane 0:659c6ffdd56c 42 /* Lecture donnée depuis nRF24 */
villemejane 0:659c6ffdd56c 43 if ( nRF24_mod.readable() ) {
villemejane 0:659c6ffdd56c 44
villemejane 0:659c6ffdd56c 45 // ...read the data into the receive buffer
villemejane 0:659c6ffdd56c 46 rxDataCnt = nRF24_mod.read( NRF24L01P_PIPE_P0, dataReceived, TRANSFER_SIZE);
villemejane 0:659c6ffdd56c 47
villemejane 0:659c6ffdd56c 48 // Display the receive buffer contents via the host serial link
villemejane 0:659c6ffdd56c 49 debug_pc.printf("\tD = ");
villemejane 0:659c6ffdd56c 50 for ( int i = 0; i < rxDataCnt; i++ ) {
villemejane 0:659c6ffdd56c 51 debug_pc.printf(" %x \t", dataReceived[i]);
villemejane 0:659c6ffdd56c 52 }
villemejane 0:659c6ffdd56c 53 debug_pc.printf("\r\n");
villemejane 0:659c6ffdd56c 54 }
villemejane 0:659c6ffdd56c 55 /* Transmission donnée depuis nRF24 */
villemejane 0:659c6ffdd56c 56 if(bp_int == 0){
villemejane 0:659c6ffdd56c 57 nRF24_mod.setRfFrequency(2400);
villemejane 0:659c6ffdd56c 58 nRF24_mod.write( NRF24L01P_PIPE_P0, dataToSend, TRANSFER_SIZE );
villemejane 0:659c6ffdd56c 59 debug_pc.printf( "SENDED\r\n");
villemejane 0:659c6ffdd56c 60 wait_us(100000);
villemejane 0:659c6ffdd56c 61 }
villemejane 0:659c6ffdd56c 62 }