Library to send and receive data using RF12B transceiver modules Big thanks to the tutorial at https://loee.jottit.com/rfm12b_and_avr_-_quick_start and madcowswe

Dependents:   Measure_system Quadcopter_copy

Revision:
5:a92c3f6d1711
Parent:
4:2a295db9ba1a
Child:
6:98da0571ec31
--- a/RF12B.cpp	Thu Mar 10 17:26:42 2011 +0000
+++ b/RF12B.cpp	Thu Mar 10 18:07:53 2011 +0000
@@ -1,11 +1,13 @@
 #include "RF12B.h"
 
+DigitalOut rfled(LED3);
+
 RF12B::RF12B(PinName _SDI,
              PinName _SDO,
              PinName _SCK,
              PinName _NCS,
              PinName _NIRQ):spi(_SDI, _SDO, _SCK),
-        NCS(_NCS), NIRQ(_NIRQ), NIRQ_in(_NIRQ) {
+        NCS(_NCS), NIRQ(_NIRQ), NIRQ_in(_NIRQ), rfled(LED3) {
 
     /* SPI frequency, word lenght, polarity and phase */
     spi.format(16,0);
@@ -16,7 +18,6 @@
 
     /* Initialise RF Module */
     init();
-    resetRX();
 
     /* Setup interrupt to happen on falling edge of NIRQ */
     NIRQ.fall(this, &RF12B::rxISR);
@@ -38,7 +39,7 @@
     }
 }
 
-/* Sends a byte of data to the RF module for transmission */
+/* Sends a packet of data to the RF module for transmission */
 void RF12B::write(unsigned char *data, unsigned char length) {
     /* Transmitter mode */
     changeMode(TX);
@@ -61,6 +62,7 @@
 
     /* Back to receiver mode */
     changeMode(RX);
+    status();
 }
 
 /* Transmit a 1-byte data packet */
@@ -88,6 +90,9 @@
     writeCmd(0xE000); //NOT USED
     writeCmd(0xC800); //NOT USED
     writeCmd(0xC040); //1.66MHz,2.2V
+
+    resetRX();
+    status();
 }
 
 /* Write a command to the RF Module */
@@ -124,19 +129,26 @@
         data = writeCmd(0xB000);
         packet_length = (data&0x00FF);
     }
-    
+
     /* Grab the packet's data */
-    while ((++i) < packet_length) {
-        data = writeCmd(0x0000);
-        if ( (data&0x8000) ) {
-            data = writeCmd(0xB000);
-            fifo.push(data&0x00FF);
+    while (i < packet_length) {
+        if (!NIRQ_in) {
+            data = writeCmd(0x0000);
+            if ( (data&0x8000) ) {
+                data = writeCmd(0xB000);
+                fifo.push(data&0x00FF);
+                i++;
+            }
         }
     }
     /* Tell RF Module we are finished */
     resetRX();
 }
 
+unsigned int RF12B::status() {
+    return writeCmd(0x0000);
+}
+
 /* Tell the RF Module this packet is received and wait for the next */
 void RF12B::resetRX() {
     writeCmd(0xCA81);