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
Diff: RF12B.cpp
- Revision:
- 2:99cf337cd23e
- Parent:
- 1:42b124ed1f57
- Child:
- 3:e72ad65868ab
--- a/RF12B.cpp Thu Mar 10 11:26:20 2011 +0000 +++ b/RF12B.cpp Thu Mar 10 12:48:08 2011 +0000 @@ -12,21 +12,78 @@ this->spi.format(16,0); this->spi.frequency(2000000); this->NCS = 1; + + init(); + mode(false);// Receiver + flush(); } + +/* Reads a byte of data from the RF module's buffer + This is a blocking call */ +unsigned char RF12B::read() { + if (!initialized || trans) { + mode(false); //Receiver + trans = false; + initialized = true; + } + + unsigned int data; + while (1) { + data = writeCmd(0x0000); + if ( (data&0x8000) ) { + data = writeCmd(0xB000); + return (data&0x00FF); + } + } +} + +/* Sends a byte of data to the RF module for transmission */ +void RF12B::write(unsigned char *data) { + if (!initialized || !trans) { + mode(true); //Transmitter + trans = true; + initialized = true; + } + writeCmd(0x0000); + send(0xAA); // PREAMBLE + send(0xAA); + send(0xAA); + send(0x2D); // SYNC + send(0xD4); + for (int i=0; i<16; i++) { + send(data[i]); + } + send(0xAA); // DUMMY BYTES + send(0xAA); + send(0xAA); + + mode(false); + trans = false; +} + +void RF12B::send(unsigned char data) { + while (NIRQ); + writeCmd(0xB800 + data); +} + +/* Flushes all data from the RF module's buffer */ +void RF12B::flush() { + if (!trans) { + writeCmd(0xCA81); + writeCmd(0xCA83); + } +}; + /* Initialises the RF12B module as transmitter or receiver. This should be called after the RF module has fully started up (give it a few hundred ms) */ -void RF12B::init(bool _trans) { - trans = _trans; +void RF12B::init() { writeCmd(0x80E7); //EL,EF,868band,12.0pF - if (trans) { - writeCmd(0x8239); //!er,!ebb,ET,ES,EX,!eb,!ew,DC - } else { - writeCmd(0x8299); //er,!ebb,ET,ES,EX,!eb,!ew,DC - } + mode(false); + trans = false; writeCmd(0xA640); //frequency select writeCmd(0xC647); //4.8kbps writeCmd(0x94A0); //VDI,FAST,134kHz,0dBm,-103dBm @@ -43,66 +100,14 @@ initialized = true; } -/* Reads a byte of data from the RF module's buffer - This is a blocking call */ -unsigned char RF12B::read() { - if (!initialized) { - init(false); //Receiver - trans = false; - initialized = true; - } - if(trans) { - return 0; - } - - unsigned int data; - while (1) { - data = writeCmd(0x0000); - if ( (data&0x8000) ) { - data = writeCmd(0xB000); - return (data&0x00FF); - } +void RF12B::mode(bool trans) { + if (trans) { + writeCmd(0x8239); //!er,!ebb,ET,ES,EX,!eb,!ew,DC + } else { + writeCmd(0x8299); //er,!ebb,ET,ES,EX,!eb,!ew,DC } } -/* Sends a byte of data to the RF module for transmission */ -void RF12B::write(unsigned char *data) { - writeCmd(0x0000); - send(0xAA); // PREAMBLE - send(0xAA); - send(0xAA); - send(0x2D); // SYNC - send(0xD4); - for (int i=0; i<16; i++) { - send(data[i]); - } - send(0xAA); // DUMMY BYTES - send(0xAA); - send(0xAA); -} - -void RF12B::send(unsigned char data) { - if (!initialized) { - init(true); //Transmitter - trans = true; - initialized = true; - } - if (!trans) { - return; // Must be in transmitter mode! - } - - while (NIRQ); - writeCmd(0xB800 + data); -} - -/* Flushes all data from the RF module's buffer */ -void RF12B::flush() { - if (!trans) { - writeCmd(0xCA81); - writeCmd(0xCA83); - } -}; - unsigned int RF12B::writeCmd(unsigned int cmd) { unsigned int recv; NCS = 0;