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