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

Committer:
harryeakins
Date:
Thu Mar 10 10:56:15 2011 +0000
Revision:
0:bd1232f200be
Child:
1:42b124ed1f57
First Commit RF12B Library

Who changed what in which revision?

UserRevisionLine numberNew 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, CDDITCBW0
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 0:bd1232f200be 69 void RF12B::write(unsigned char data) {
harryeakins 0:bd1232f200be 70 if (!initialized) {
harryeakins 0:bd1232f200be 71 init(true); //Transmitter
harryeakins 0:bd1232f200be 72 trans = true;
harryeakins 0:bd1232f200be 73 initialized = true;
harryeakins 0:bd1232f200be 74 }
harryeakins 0:bd1232f200be 75 if (!trans) {
harryeakins 0:bd1232f200be 76 return; // Must be in transmitter mode!
harryeakins 0:bd1232f200be 77 }
harryeakins 0:bd1232f200be 78
harryeakins 0:bd1232f200be 79 while (NIRQ);
harryeakins 0:bd1232f200be 80 writeCmd(0xB800 + data);
harryeakins 0:bd1232f200be 81 }
harryeakins 0:bd1232f200be 82
harryeakins 0:bd1232f200be 83 /* Flushes all data from the RF module's buffer */
harryeakins 0:bd1232f200be 84 void RF12B::flush() {
harryeakins 0:bd1232f200be 85 if (!trans) {
harryeakins 0:bd1232f200be 86 writeCmd(0xCA81);
harryeakins 0:bd1232f200be 87 writeCmd(0xCA83);
harryeakins 0:bd1232f200be 88 }
harryeakins 0:bd1232f200be 89 };
harryeakins 0:bd1232f200be 90
harryeakins 0:bd1232f200be 91 unsigned int RF12B::writeCmd(unsigned int cmd) {
harryeakins 0:bd1232f200be 92 unsigned int recv;
harryeakins 0:bd1232f200be 93 NCS = 0;
harryeakins 0:bd1232f200be 94 recv = spi.write(cmd);
harryeakins 0:bd1232f200be 95 NCS = 1;
harryeakins 0:bd1232f200be 96 return recv;
harryeakins 0:bd1232f200be 97 }