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
00001 #include "mbed.h" 00002 #include "nrf24l01.h" 00003 00004 Serial pc(USBTX, USBRX); // tx, rx 00005 00006 DigitalOut led1(LED1); 00007 DigitalOut led2(LED2); 00008 DigitalOut led3(LED3); 00009 DigitalOut led4(LED4); 00010 DigitalIn mode(p20); 00011 00012 DigitalOut nRF_CSN(p8); 00013 DigitalOut nRF_CE(p11); 00014 DigitalIn nRF_IRQ(p12); 00015 SPI nRF_spi(p5, p6, p7); 00016 00017 int main() { 00018 uint8_t dump[35]; 00019 unsigned char scratch[5]; 00020 pc.baud(115200); 00021 pc.printf("mbed: nRF24L01 tranceiver\r\n"); 00022 00023 #define _NRF24L01P_SPI_MAX_DATA_RATE 10000000 00024 nRF_spi.frequency(_NRF24L01P_SPI_MAX_DATA_RATE/5); // 2Mbit, 1/5th the maximum transfer rate for the SPI bus 00025 nRF_spi.format(8,0); // 8-bit, ClockPhase = 0, ClockPolarity = 0 00026 00027 //scratch[0] = 0x01; 00028 //nrf24l01_write_register(nrf24l01_RF_SETUP, scratch , 1); // -18 dbm, air rate 1 mbps 00029 00030 pc.printf("Reading all registers:\r\n"); 00031 nrf24l01_get_all_registers(dump); 00032 for(int i=0; i<sizeof(dump); i++){ 00033 pc.printf("0x%02x\r\n",dump[i]); 00034 } 00035 00036 /* "fork" */ 00037 if(mode){ 00038 // TX Mode 00039 pc.printf("Set to transmitter mode p20 high\r\n"); 00040 //vars 00041 unsigned char data; //register to hold letter sent and received 00042 unsigned int count; //counter for for loop 00043 // init 00044 nrf24l01_initialize_debug(false, 1, true); //initialize the 24L01 to the debug configuration as TX, 1 data byte, and auto-ack enabled 00045 //main program loop 00046 while(1) 00047 { 00048 //check UART status register to see if data has been received. if so, process 00049 while(pc.readable()) 00050 { 00051 data = pc.getc(); //get data from UART 00052 nrf24l01_write_tx_payload(&data, 1, true); //transmit received char over RF 00053 00054 //wait until the packet has been sent or the maximum number of retries has been active 00055 while(!(nrf24l01_irq_pin_active() && (nrf24l01_irq_tx_ds_active() || nrf24l01_irq_max_rt_active()))); 00056 00057 //check to see if the maximum number of retries has been hit. if not, wait for the RX device 00058 // to send the char back. if so, assume the packet is lost and send "*" back to UART 00059 if(!nrf24l01_irq_max_rt_active()) 00060 { 00061 nrf24l01_irq_clear_all(); //clear all interrupts in the 24L01 00062 nrf24l01_set_as_rx(true); //change the device to an RX to get the character back from the other 24L01 00063 00064 //wait a while to see if we get the data back (change the loop maximum and the lower if 00065 // argument (should be loop maximum - 1) to lengthen or shorten this time frame 00066 for(count = 0; count < 25000; count++) 00067 { 00068 //check to see if the data has been received. if so, get the data and exit the loop. 00069 // if the loop is at its last count, assume the packet has been lost and set the data 00070 // to go to the UART to "?". If neither of these is true, keep looping. 00071 if((nrf24l01_irq_pin_active() && nrf24l01_irq_rx_dr_active())) 00072 { 00073 nrf24l01_read_rx_payload(&data, 1); //get the payload into data 00074 break; 00075 } 00076 00077 //if loop is on its last iteration, assume packet has been lost. 00078 if(count == 24999) 00079 data = '?'; 00080 } 00081 00082 nrf24l01_irq_clear_all(); //clear interrupts again 00083 pc.printf("%c", data); //print the received data (or ? if none) to the screen 00084 00085 wait_us(130); //wait for receiver to come from standby to RX 00086 nrf24l01_set_as_tx(); //resume normal operation as a TX 00087 } 00088 else 00089 { 00090 nrf24l01_flush_tx(); //get the unsent character out of the TX FIFO 00091 nrf24l01_irq_clear_all(); //clear all interrupts 00092 pc.printf("*"); //print "*" to the screen to show that the receiver did not receive the packet 00093 } 00094 00095 led1 = !led1; //toggle the on-board LED as visual indication that the loop has completed 00096 } 00097 } 00098 /* ########################################################################## */ 00099 }else{ 00100 // RX Mode 00101 pc.printf("Set to receiver mode p20 low\r\n"); 00102 unsigned char data; //register to hold letter received and sent 00103 // Init 00104 nrf24l01_initialize_debug(true, 1, true); //initialize the 24L01 to the debug configuration as RX, 1 data byte, and auto-ack enabled 00105 //main program loop 00106 while(1) 00107 { 00108 //wait until a packet has been received 00109 while(!(nrf24l01_irq_pin_active() && nrf24l01_irq_rx_dr_active())); 00110 00111 nrf24l01_read_rx_payload(&data, 1); //read the packet into data 00112 nrf24l01_irq_clear_all(); //clear all interrupts in the 24L01 00113 00114 pc.putc(data); 00115 wait_us(130); //wait for the other 24L01 to come from standby to RX 00116 00117 nrf24l01_set_as_tx(); //change the device to a TX to send back from the other 24L01 00118 nrf24l01_write_tx_payload(&data, 1, true); //transmit received char over RF 00119 00120 //wait until the packet has been sent or the maximum number of retries has been reached 00121 while(!(nrf24l01_irq_pin_active() && (nrf24l01_irq_tx_ds_active() || nrf24l01_irq_max_rt_active()))); 00122 00123 nrf24l01_irq_clear_all(); //clear interrupts again 00124 nrf24l01_set_as_rx(true); //resume normal operation as an RX 00125 00126 led1 = !led1; //toggle the on-board LED as visual indication that the loop has completed 00127 } 00128 } 00129 }
Generated on Tue Jul 12 2022 18:14:07 by
1.7.2