diyembedded nrf24l01 tutorial 2 ported to mbed lpc1768

Dependencies:   mbed

See diyembedded tutorials for more info

-Pull down p20 for receive mode

-Pull up p20 for transmitter mode

Committer:
jeroen3
Date:
Sat Dec 08 22:11:23 2012 +0000
Revision:
0:2286a98ea739
diyembedded.com nrf24l01 lib with shockburs tutorial 2 on mbed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jeroen3 0:2286a98ea739 1 #include "mbed.h"
jeroen3 0:2286a98ea739 2 #include "nrf24l01.h"
jeroen3 0:2286a98ea739 3
jeroen3 0:2286a98ea739 4 Serial pc(USBTX, USBRX); // tx, rx
jeroen3 0:2286a98ea739 5
jeroen3 0:2286a98ea739 6 DigitalOut led1(LED1);
jeroen3 0:2286a98ea739 7 DigitalOut led2(LED2);
jeroen3 0:2286a98ea739 8 DigitalOut led3(LED3);
jeroen3 0:2286a98ea739 9 DigitalOut led4(LED4);
jeroen3 0:2286a98ea739 10 DigitalIn mode(p20);
jeroen3 0:2286a98ea739 11
jeroen3 0:2286a98ea739 12 DigitalOut nRF_CSN(p8);
jeroen3 0:2286a98ea739 13 DigitalOut nRF_CE(p11);
jeroen3 0:2286a98ea739 14 DigitalIn nRF_IRQ(p12);
jeroen3 0:2286a98ea739 15 SPI nRF_spi(p5, p6, p7);
jeroen3 0:2286a98ea739 16
jeroen3 0:2286a98ea739 17 int main() {
jeroen3 0:2286a98ea739 18 uint8_t dump[35];
jeroen3 0:2286a98ea739 19 unsigned char scratch[5];
jeroen3 0:2286a98ea739 20 pc.baud(115200);
jeroen3 0:2286a98ea739 21 pc.printf("mbed: nRF24L01 tranceiver\r\n");
jeroen3 0:2286a98ea739 22
jeroen3 0:2286a98ea739 23 #define _NRF24L01P_SPI_MAX_DATA_RATE 10000000
jeroen3 0:2286a98ea739 24 nRF_spi.frequency(_NRF24L01P_SPI_MAX_DATA_RATE/5); // 2Mbit, 1/5th the maximum transfer rate for the SPI bus
jeroen3 0:2286a98ea739 25 nRF_spi.format(8,0); // 8-bit, ClockPhase = 0, ClockPolarity = 0
jeroen3 0:2286a98ea739 26
jeroen3 0:2286a98ea739 27 //scratch[0] = 0x01;
jeroen3 0:2286a98ea739 28 //nrf24l01_write_register(nrf24l01_RF_SETUP, scratch , 1); // -18 dbm, air rate 1 mbps
jeroen3 0:2286a98ea739 29
jeroen3 0:2286a98ea739 30 pc.printf("Reading all registers:\r\n");
jeroen3 0:2286a98ea739 31 nrf24l01_get_all_registers(dump);
jeroen3 0:2286a98ea739 32 for(int i=0; i<sizeof(dump); i++){
jeroen3 0:2286a98ea739 33 pc.printf("0x%02x\r\n",dump[i]);
jeroen3 0:2286a98ea739 34 }
jeroen3 0:2286a98ea739 35
jeroen3 0:2286a98ea739 36 /* "fork" */
jeroen3 0:2286a98ea739 37 if(mode){
jeroen3 0:2286a98ea739 38 // TX Mode
jeroen3 0:2286a98ea739 39 pc.printf("Set to transmitter mode p20 high\r\n");
jeroen3 0:2286a98ea739 40 //vars
jeroen3 0:2286a98ea739 41 unsigned char data; //register to hold letter sent and received
jeroen3 0:2286a98ea739 42 unsigned int count; //counter for for loop
jeroen3 0:2286a98ea739 43 // init
jeroen3 0:2286a98ea739 44 nrf24l01_initialize_debug(false, 1, true); //initialize the 24L01 to the debug configuration as TX, 1 data byte, and auto-ack enabled
jeroen3 0:2286a98ea739 45 //main program loop
jeroen3 0:2286a98ea739 46 while(1)
jeroen3 0:2286a98ea739 47 {
jeroen3 0:2286a98ea739 48 //check UART status register to see if data has been received. if so, process
jeroen3 0:2286a98ea739 49 while(pc.readable())
jeroen3 0:2286a98ea739 50 {
jeroen3 0:2286a98ea739 51 data = pc.getc(); //get data from UART
jeroen3 0:2286a98ea739 52 nrf24l01_write_tx_payload(&data, 1, true); //transmit received char over RF
jeroen3 0:2286a98ea739 53
jeroen3 0:2286a98ea739 54 //wait until the packet has been sent or the maximum number of retries has been active
jeroen3 0:2286a98ea739 55 while(!(nrf24l01_irq_pin_active() && (nrf24l01_irq_tx_ds_active() || nrf24l01_irq_max_rt_active())));
jeroen3 0:2286a98ea739 56
jeroen3 0:2286a98ea739 57 //check to see if the maximum number of retries has been hit. if not, wait for the RX device
jeroen3 0:2286a98ea739 58 // to send the char back. if so, assume the packet is lost and send "*" back to UART
jeroen3 0:2286a98ea739 59 if(!nrf24l01_irq_max_rt_active())
jeroen3 0:2286a98ea739 60 {
jeroen3 0:2286a98ea739 61 nrf24l01_irq_clear_all(); //clear all interrupts in the 24L01
jeroen3 0:2286a98ea739 62 nrf24l01_set_as_rx(true); //change the device to an RX to get the character back from the other 24L01
jeroen3 0:2286a98ea739 63
jeroen3 0:2286a98ea739 64 //wait a while to see if we get the data back (change the loop maximum and the lower if
jeroen3 0:2286a98ea739 65 // argument (should be loop maximum - 1) to lengthen or shorten this time frame
jeroen3 0:2286a98ea739 66 for(count = 0; count < 25000; count++)
jeroen3 0:2286a98ea739 67 {
jeroen3 0:2286a98ea739 68 //check to see if the data has been received. if so, get the data and exit the loop.
jeroen3 0:2286a98ea739 69 // if the loop is at its last count, assume the packet has been lost and set the data
jeroen3 0:2286a98ea739 70 // to go to the UART to "?". If neither of these is true, keep looping.
jeroen3 0:2286a98ea739 71 if((nrf24l01_irq_pin_active() && nrf24l01_irq_rx_dr_active()))
jeroen3 0:2286a98ea739 72 {
jeroen3 0:2286a98ea739 73 nrf24l01_read_rx_payload(&data, 1); //get the payload into data
jeroen3 0:2286a98ea739 74 break;
jeroen3 0:2286a98ea739 75 }
jeroen3 0:2286a98ea739 76
jeroen3 0:2286a98ea739 77 //if loop is on its last iteration, assume packet has been lost.
jeroen3 0:2286a98ea739 78 if(count == 24999)
jeroen3 0:2286a98ea739 79 data = '?';
jeroen3 0:2286a98ea739 80 }
jeroen3 0:2286a98ea739 81
jeroen3 0:2286a98ea739 82 nrf24l01_irq_clear_all(); //clear interrupts again
jeroen3 0:2286a98ea739 83 pc.printf("%c", data); //print the received data (or ? if none) to the screen
jeroen3 0:2286a98ea739 84
jeroen3 0:2286a98ea739 85 wait_us(130); //wait for receiver to come from standby to RX
jeroen3 0:2286a98ea739 86 nrf24l01_set_as_tx(); //resume normal operation as a TX
jeroen3 0:2286a98ea739 87 }
jeroen3 0:2286a98ea739 88 else
jeroen3 0:2286a98ea739 89 {
jeroen3 0:2286a98ea739 90 nrf24l01_flush_tx(); //get the unsent character out of the TX FIFO
jeroen3 0:2286a98ea739 91 nrf24l01_irq_clear_all(); //clear all interrupts
jeroen3 0:2286a98ea739 92 pc.printf("*"); //print "*" to the screen to show that the receiver did not receive the packet
jeroen3 0:2286a98ea739 93 }
jeroen3 0:2286a98ea739 94
jeroen3 0:2286a98ea739 95 led1 = !led1; //toggle the on-board LED as visual indication that the loop has completed
jeroen3 0:2286a98ea739 96 }
jeroen3 0:2286a98ea739 97 }
jeroen3 0:2286a98ea739 98 /* ########################################################################## */
jeroen3 0:2286a98ea739 99 }else{
jeroen3 0:2286a98ea739 100 // RX Mode
jeroen3 0:2286a98ea739 101 pc.printf("Set to receiver mode p20 low\r\n");
jeroen3 0:2286a98ea739 102 unsigned char data; //register to hold letter received and sent
jeroen3 0:2286a98ea739 103 // Init
jeroen3 0:2286a98ea739 104 nrf24l01_initialize_debug(true, 1, true); //initialize the 24L01 to the debug configuration as RX, 1 data byte, and auto-ack enabled
jeroen3 0:2286a98ea739 105 //main program loop
jeroen3 0:2286a98ea739 106 while(1)
jeroen3 0:2286a98ea739 107 {
jeroen3 0:2286a98ea739 108 //wait until a packet has been received
jeroen3 0:2286a98ea739 109 while(!(nrf24l01_irq_pin_active() && nrf24l01_irq_rx_dr_active()));
jeroen3 0:2286a98ea739 110
jeroen3 0:2286a98ea739 111 nrf24l01_read_rx_payload(&data, 1); //read the packet into data
jeroen3 0:2286a98ea739 112 nrf24l01_irq_clear_all(); //clear all interrupts in the 24L01
jeroen3 0:2286a98ea739 113
jeroen3 0:2286a98ea739 114 pc.putc(data);
jeroen3 0:2286a98ea739 115 wait_us(130); //wait for the other 24L01 to come from standby to RX
jeroen3 0:2286a98ea739 116
jeroen3 0:2286a98ea739 117 nrf24l01_set_as_tx(); //change the device to a TX to send back from the other 24L01
jeroen3 0:2286a98ea739 118 nrf24l01_write_tx_payload(&data, 1, true); //transmit received char over RF
jeroen3 0:2286a98ea739 119
jeroen3 0:2286a98ea739 120 //wait until the packet has been sent or the maximum number of retries has been reached
jeroen3 0:2286a98ea739 121 while(!(nrf24l01_irq_pin_active() && (nrf24l01_irq_tx_ds_active() || nrf24l01_irq_max_rt_active())));
jeroen3 0:2286a98ea739 122
jeroen3 0:2286a98ea739 123 nrf24l01_irq_clear_all(); //clear interrupts again
jeroen3 0:2286a98ea739 124 nrf24l01_set_as_rx(true); //resume normal operation as an RX
jeroen3 0:2286a98ea739 125
jeroen3 0:2286a98ea739 126 led1 = !led1; //toggle the on-board LED as visual indication that the loop has completed
jeroen3 0:2286a98ea739 127 }
jeroen3 0:2286a98ea739 128 }
jeroen3 0:2286a98ea739 129 }