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
RF12B.h@5:a92c3f6d1711, 2011-03-10 (annotated)
- Committer:
- harryeakins
- Date:
- Thu Mar 10 18:07:53 2011 +0000
- Revision:
- 5:a92c3f6d1711
- Parent:
- 4:2a295db9ba1a
- Child:
- 6:98da0571ec31
Fixed some problems in initialization. Pretty much fully working now!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
harryeakins | 0:bd1232f200be | 1 | #ifndef _RF12B_H |
harryeakins | 0:bd1232f200be | 2 | #define _RF12B_H |
harryeakins | 0:bd1232f200be | 3 | |
harryeakins | 0:bd1232f200be | 4 | #include "mbed.h" |
harryeakins | 4:2a295db9ba1a | 5 | #include <queue> |
harryeakins | 4:2a295db9ba1a | 6 | |
harryeakins | 4:2a295db9ba1a | 7 | #define PACKET_LEN 16 |
harryeakins | 4:2a295db9ba1a | 8 | |
harryeakins | 4:2a295db9ba1a | 9 | enum rfmode_t{RX, TX}; |
harryeakins | 0:bd1232f200be | 10 | |
harryeakins | 0:bd1232f200be | 11 | class RF12B { |
harryeakins | 0:bd1232f200be | 12 | public: |
harryeakins | 4:2a295db9ba1a | 13 | /* Constructor */ |
harryeakins | 0:bd1232f200be | 14 | RF12B(PinName SDI, |
harryeakins | 2:99cf337cd23e | 15 | PinName SDO, |
harryeakins | 2:99cf337cd23e | 16 | PinName SCK, |
harryeakins | 2:99cf337cd23e | 17 | PinName NCS, |
harryeakins | 2:99cf337cd23e | 18 | PinName NIRQ); |
harryeakins | 2:99cf337cd23e | 19 | |
harryeakins | 4:2a295db9ba1a | 20 | /* Reads a byte of data from the receive buffer |
harryeakins | 4:2a295db9ba1a | 21 | Returns 0xFF if there is no data */ |
harryeakins | 0:bd1232f200be | 22 | unsigned char read(); |
harryeakins | 2:99cf337cd23e | 23 | |
harryeakins | 4:2a295db9ba1a | 24 | /* Transmits a packet of data */ |
harryeakins | 4:2a295db9ba1a | 25 | void write(unsigned char * data, unsigned char length); |
harryeakins | 4:2a295db9ba1a | 26 | |
harryeakins | 4:2a295db9ba1a | 27 | /* Transmits a 1-byte packet of data */ |
harryeakins | 4:2a295db9ba1a | 28 | void write(unsigned char data); |
harryeakins | 4:2a295db9ba1a | 29 | |
harryeakins | 4:2a295db9ba1a | 30 | /* Returns true if data is available in the receive buffer*/ |
harryeakins | 4:2a295db9ba1a | 31 | bool available(); |
harryeakins | 0:bd1232f200be | 32 | |
harryeakins | 0:bd1232f200be | 33 | private: |
harryeakins | 4:2a295db9ba1a | 34 | /* Receive FIFO buffer */ |
harryeakins | 4:2a295db9ba1a | 35 | queue<unsigned char> fifo; |
harryeakins | 4:2a295db9ba1a | 36 | |
harryeakins | 4:2a295db9ba1a | 37 | /* SPI module */ |
harryeakins | 0:bd1232f200be | 38 | SPI spi; |
harryeakins | 4:2a295db9ba1a | 39 | |
harryeakins | 4:2a295db9ba1a | 40 | /* Other digital pins */ |
harryeakins | 0:bd1232f200be | 41 | DigitalOut NCS; |
harryeakins | 4:2a295db9ba1a | 42 | InterruptIn NIRQ; |
harryeakins | 4:2a295db9ba1a | 43 | DigitalIn NIRQ_in; |
harryeakins | 5:a92c3f6d1711 | 44 | DigitalOut rfled; |
harryeakins | 3:e72ad65868ab | 45 | |
harryeakins | 2:99cf337cd23e | 46 | /* Initialises the RF12B module */ |
harryeakins | 3:e72ad65868ab | 47 | void init(); |
harryeakins | 4:2a295db9ba1a | 48 | |
harryeakins | 4:2a295db9ba1a | 49 | /* Write a command to the RF Module */ |
harryeakins | 4:2a295db9ba1a | 50 | unsigned int writeCmd(unsigned int cmd); |
harryeakins | 4:2a295db9ba1a | 51 | |
harryeakins | 4:2a295db9ba1a | 52 | /* Sends a byte of data across RF */ |
harryeakins | 4:2a295db9ba1a | 53 | void send(unsigned char data); |
harryeakins | 4:2a295db9ba1a | 54 | |
harryeakins | 3:e72ad65868ab | 55 | /* Switch module between receive and transmit modes */ |
harryeakins | 4:2a295db9ba1a | 56 | void changeMode(rfmode_t mode); |
harryeakins | 4:2a295db9ba1a | 57 | |
harryeakins | 4:2a295db9ba1a | 58 | /* Interrupt routine for data reception */ |
harryeakins | 4:2a295db9ba1a | 59 | void rxISR(); |
harryeakins | 4:2a295db9ba1a | 60 | |
harryeakins | 4:2a295db9ba1a | 61 | /* Tell the RF Module this packet is received and wait for the next */ |
harryeakins | 4:2a295db9ba1a | 62 | void resetRX(); |
harryeakins | 5:a92c3f6d1711 | 63 | |
harryeakins | 5:a92c3f6d1711 | 64 | /* Return the RF Module Status word */ |
harryeakins | 5:a92c3f6d1711 | 65 | unsigned int status(); |
harryeakins | 0:bd1232f200be | 66 | }; |
harryeakins | 0:bd1232f200be | 67 | |
harryeakins | 0:bd1232f200be | 68 | #endif /* _RF12B_H */ |