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 12:48:08 2011 +0000
Revision:
2:99cf337cd23e
Parent:
1:42b124ed1f57
Child:
3:e72ad65868ab
Simplified Interface

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