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.cpp@3:e72ad65868ab, 2011-03-10 (annotated)
- Committer:
- harryeakins
- Date:
- Thu Mar 10 13:14:48 2011 +0000
- Revision:
- 3:e72ad65868ab
- Parent:
- 2:99cf337cd23e
- Child:
- 4:2a295db9ba1a
Updated write functions
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
harryeakins | 3:e72ad65868ab | 1 | #include "RF12B.h" |
harryeakins | 3:e72ad65868ab | 2 | |
harryeakins | 3:e72ad65868ab | 3 | RF12B::RF12B(PinName SDI, |
harryeakins | 3:e72ad65868ab | 4 | PinName SDO, |
harryeakins | 3:e72ad65868ab | 5 | PinName SCK, |
harryeakins | 3:e72ad65868ab | 6 | PinName NCS, |
harryeakins | 3:e72ad65868ab | 7 | PinName NIRQ):spi(SDI, SDO, SCK), |
harryeakins | 3:e72ad65868ab | 8 | NCS(NCS), NIRQ(NIRQ) { |
harryeakins | 3:e72ad65868ab | 9 | this->initialized = false; |
harryeakins | 3:e72ad65868ab | 10 | this->trans = false; |
harryeakins | 3:e72ad65868ab | 11 | |
harryeakins | 3:e72ad65868ab | 12 | this->spi.format(16,0); |
harryeakins | 3:e72ad65868ab | 13 | this->spi.frequency(2000000); |
harryeakins | 3:e72ad65868ab | 14 | this->NCS = 1; |
harryeakins | 3:e72ad65868ab | 15 | |
harryeakins | 3:e72ad65868ab | 16 | init(); |
harryeakins | 3:e72ad65868ab | 17 | mode(false);// Receiver |
harryeakins | 3:e72ad65868ab | 18 | flush(); |
harryeakins | 3:e72ad65868ab | 19 | } |
harryeakins | 3:e72ad65868ab | 20 | |
harryeakins | 3:e72ad65868ab | 21 | |
harryeakins | 3:e72ad65868ab | 22 | /* Reads a byte of data from the RF module's buffer |
harryeakins | 3:e72ad65868ab | 23 | This is a blocking call */ |
harryeakins | 3:e72ad65868ab | 24 | unsigned char RF12B::read() { |
harryeakins | 3:e72ad65868ab | 25 | if (!initialized || trans) { |
harryeakins | 3:e72ad65868ab | 26 | mode(false); //Receiver |
harryeakins | 3:e72ad65868ab | 27 | trans = false; |
harryeakins | 3:e72ad65868ab | 28 | initialized = true; |
harryeakins | 3:e72ad65868ab | 29 | } |
harryeakins | 3:e72ad65868ab | 30 | |
harryeakins | 3:e72ad65868ab | 31 | unsigned int data; |
harryeakins | 3:e72ad65868ab | 32 | while (1) { |
harryeakins | 3:e72ad65868ab | 33 | data = writeCmd(0x0000); |
harryeakins | 3:e72ad65868ab | 34 | if ( (data&0x8000) ) { |
harryeakins | 3:e72ad65868ab | 35 | data = writeCmd(0xB000); |
harryeakins | 3:e72ad65868ab | 36 | return (data&0x00FF); |
harryeakins | 3:e72ad65868ab | 37 | } |
harryeakins | 3:e72ad65868ab | 38 | } |
harryeakins | 3:e72ad65868ab | 39 | } |
harryeakins | 3:e72ad65868ab | 40 | |
harryeakins | 3:e72ad65868ab | 41 | /* Sends a byte of data to the RF module for transmission */ |
harryeakins | 3:e72ad65868ab | 42 | void RF12B::write(unsigned char *data, unsigned int length) { |
harryeakins | 3:e72ad65868ab | 43 | if (!initialized || !trans) { |
harryeakins | 3:e72ad65868ab | 44 | mode(true); //Transmitter |
harryeakins | 3:e72ad65868ab | 45 | trans = true; |
harryeakins | 3:e72ad65868ab | 46 | initialized = true; |
harryeakins | 3:e72ad65868ab | 47 | } |
harryeakins | 3:e72ad65868ab | 48 | writeCmd(0x0000); |
harryeakins | 3:e72ad65868ab | 49 | send(0xAA); // PREAMBLE |
harryeakins | 3:e72ad65868ab | 50 | send(0xAA); |
harryeakins | 3:e72ad65868ab | 51 | send(0xAA); |
harryeakins | 3:e72ad65868ab | 52 | send(0x2D); // SYNC |
harryeakins | 3:e72ad65868ab | 53 | send(0xD4); |
harryeakins | 3:e72ad65868ab | 54 | for (int i=0; i<length; i++) { |
harryeakins | 3:e72ad65868ab | 55 | send(data[i]); |
harryeakins | 3:e72ad65868ab | 56 | } |
harryeakins | 3:e72ad65868ab | 57 | send(0xAA); // DUMMY BYTES |
harryeakins | 3:e72ad65868ab | 58 | send(0xAA); |
harryeakins | 3:e72ad65868ab | 59 | send(0xAA); |
harryeakins | 3:e72ad65868ab | 60 | |
harryeakins | 3:e72ad65868ab | 61 | mode(false); |
harryeakins | 3:e72ad65868ab | 62 | trans = false; |
harryeakins | 3:e72ad65868ab | 63 | } |
harryeakins | 3:e72ad65868ab | 64 | |
harryeakins | 3:e72ad65868ab | 65 | void RF12B::send(unsigned char data) { |
harryeakins | 3:e72ad65868ab | 66 | while (NIRQ); |
harryeakins | 3:e72ad65868ab | 67 | writeCmd(0xB800 + data); |
harryeakins | 3:e72ad65868ab | 68 | } |
harryeakins | 3:e72ad65868ab | 69 | |
harryeakins | 3:e72ad65868ab | 70 | /* Flushes all data from the RF module's buffer */ |
harryeakins | 3:e72ad65868ab | 71 | void RF12B::flush() { |
harryeakins | 3:e72ad65868ab | 72 | if (!trans) { |
harryeakins | 3:e72ad65868ab | 73 | writeCmd(0xCA81); |
harryeakins | 3:e72ad65868ab | 74 | writeCmd(0xCA83); |
harryeakins | 3:e72ad65868ab | 75 | } |
harryeakins | 3:e72ad65868ab | 76 | }; |
harryeakins | 3:e72ad65868ab | 77 | |
harryeakins | 3:e72ad65868ab | 78 | /* Initialises the RF12B module as transmitter |
harryeakins | 3:e72ad65868ab | 79 | or receiver. This should be called after the |
harryeakins | 3:e72ad65868ab | 80 | RF module has fully started up (give it a |
harryeakins | 3:e72ad65868ab | 81 | few hundred ms) */ |
harryeakins | 3:e72ad65868ab | 82 | void RF12B::init() { |
harryeakins | 3:e72ad65868ab | 83 | |
harryeakins | 3:e72ad65868ab | 84 | writeCmd(0x80E7); //EL,EF,868band,12.0pF |
harryeakins | 3:e72ad65868ab | 85 | mode(false); |
harryeakins | 3:e72ad65868ab | 86 | trans = false; |
harryeakins | 3:e72ad65868ab | 87 | writeCmd(0xA640); //frequency select |
harryeakins | 3:e72ad65868ab | 88 | writeCmd(0xC647); //4.8kbps |
harryeakins | 3:e72ad65868ab | 89 | writeCmd(0x94A0); //VDI,FAST,134kHz,0dBm,-103dBm |
harryeakins | 3:e72ad65868ab | 90 | writeCmd(0xC2AC); //AL,!ml,DIG,DQD4 |
harryeakins | 3:e72ad65868ab | 91 | writeCmd(0xCA81); //FIFO8,SYNC,!ff,DR |
harryeakins | 3:e72ad65868ab | 92 | writeCmd(0xCED4); //SYNC=2DD4 |
harryeakins | 3:e72ad65868ab | 93 | writeCmd(0xC483); //@PWR,NO RSTRIC,!st,!fi,OE,EN |
harryeakins | 3:e72ad65868ab | 94 | writeCmd(0x9850); //!mp,90kHz,MAX OUT |
harryeakins | 3:e72ad65868ab | 95 | writeCmd(0xCC17); //OB1, COB0, LPX, Iddy, CDDITCBW0 |
harryeakins | 3:e72ad65868ab | 96 | writeCmd(0xE000); //NOT USED |
harryeakins | 3:e72ad65868ab | 97 | writeCmd(0xC800); //NOT USED |
harryeakins | 3:e72ad65868ab | 98 | writeCmd(0xC040); //1.66MHz,2.2V |
harryeakins | 3:e72ad65868ab | 99 | |
harryeakins | 3:e72ad65868ab | 100 | initialized = true; |
harryeakins | 3:e72ad65868ab | 101 | } |
harryeakins | 3:e72ad65868ab | 102 | |
harryeakins | 3:e72ad65868ab | 103 | void RF12B::mode(bool trans) { |
harryeakins | 3:e72ad65868ab | 104 | if (trans) { |
harryeakins | 3:e72ad65868ab | 105 | writeCmd(0x8239); //!er,!ebb,ET,ES,EX,!eb,!ew,DC |
harryeakins | 3:e72ad65868ab | 106 | } else { |
harryeakins | 3:e72ad65868ab | 107 | writeCmd(0x8299); //er,!ebb,ET,ES,EX,!eb,!ew,DC |
harryeakins | 3:e72ad65868ab | 108 | } |
harryeakins | 3:e72ad65868ab | 109 | } |
harryeakins | 3:e72ad65868ab | 110 | |
harryeakins | 3:e72ad65868ab | 111 | unsigned int RF12B::writeCmd(unsigned int cmd) { |
harryeakins | 3:e72ad65868ab | 112 | unsigned int recv; |
harryeakins | 3:e72ad65868ab | 113 | NCS = 0; |
harryeakins | 3:e72ad65868ab | 114 | recv = spi.write(cmd); |
harryeakins | 3:e72ad65868ab | 115 | NCS = 1; |
harryeakins | 3:e72ad65868ab | 116 | return recv; |
harryeakins | 3:e72ad65868ab | 117 | } |