From 61658a06bee823d549248c268cb88bddb9f4e76c Mon Sep 17 00:00:00 2001 From: Felix Rusu Date: Thu, 5 Sep 2013 19:32:11 -0400 Subject: [PATCH] Add readTemperature() and rcCalibration() --- Examples/Gateway/Gateway.ino | 10 ++++++++++ RFM69.cpp | 31 ++++++++++++++++--------------- RFM69.h | 9 ++++++--- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/Examples/Gateway/Gateway.ino b/Examples/Gateway/Gateway.ino index 912564a..09396ae 100644 --- a/Examples/Gateway/Gateway.ino +++ b/Examples/Gateway/Gateway.ino @@ -73,6 +73,16 @@ void loop() { Serial.print("DeviceID: "); word jedecid = flash.readDeviceId(); Serial.println(jedecid, HEX); + } + if (input == 't') + { + byte temperature = radio.readTemperature(-1); // -1 = user cal factor, adjust for correct ambient + byte fTemp = 1.8 * temperature + 32; // 9/5=1.8 + Serial.print( "Radio Temp is "); + Serial.print(temperature); + Serial.print("C, "); + Serial.print(fTemp); //converting to F loses some resolution, obvious when C is on edge between 2 values (ie 26C=78F, 27C=80F) + Serial.println('F'); } } diff --git a/RFM69.cpp b/RFM69.cpp index c7340a6..f34901f 100644 --- a/RFM69.cpp +++ b/RFM69.cpp @@ -25,7 +25,7 @@ bool RFM69::initialize(byte freqBand, byte nodeID, byte networkID) const byte CONFIG[][2] = { /* 0x01 */ { REG_OPMODE, RF_OPMODE_SEQUENCER_ON | RF_OPMODE_LISTEN_OFF | RF_OPMODE_STANDBY }, - /* 0x02 */ { REG_DATAMODUL, RF_DATAMODUL_DATAMODE_PACKET | RF_DATAMODUL_MODULATIONTYPE_FSK | RF_DATAMODUL_MODULATIONSHAPING_00 }, //gaussian, bt=1.0 + /* 0x02 */ { REG_DATAMODUL, RF_DATAMODUL_DATAMODE_PACKET | RF_DATAMODUL_MODULATIONTYPE_FSK | RF_DATAMODUL_MODULATIONSHAPING_00 }, //no shaping /* 0x03 */ { REG_BITRATEMSB, RF_BITRATEMSB_55555}, //default:4.8 KBPS /* 0x04 */ { REG_BITRATELSB, RF_BITRATELSB_55555}, /* 0x05 */ { REG_FDEVMSB, RF_FDEVMSB_50000}, //default:5khz, (FDEV + BitRate/2 <= 500Khz) @@ -43,7 +43,7 @@ bool RFM69::initialize(byte freqBand, byte nodeID, byte networkID) ///* 0x11 */ { REG_PALEVEL, RF_PALEVEL_PA0_ON | RF_PALEVEL_PA1_OFF | RF_PALEVEL_PA2_OFF | RF_PALEVEL_OUTPUTPOWER_11111}, ///* 0x13 */ { REG_OCP, RF_OCP_ON | RF_OCP_TRIM_95 }, //over current protection (default is 95mA) - // RXBW defaults are {RF_RXBW_DCCFREQ_010 | RF_RXBW_MANT_24 | RF_RXBW_EXP_5} (RxBw: 10.4khz) + // RXBW defaults are { REG_RXBW, RF_RXBW_DCCFREQ_010 | RF_RXBW_MANT_24 | RF_RXBW_EXP_5} (RxBw: 10.4khz) /* 0x19 */ { REG_RXBW, RF_RXBW_DCCFREQ_010 | RF_RXBW_MANT_16 | RF_RXBW_EXP_2 }, //(BitRate < 2 * RxBw) /* 0x25 */ { REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_01 }, //DIO0 is the only IRQ we're using /* 0x29 */ { REG_RSSITHRESH, 220 }, //must be set to dBm = (-Sensitivity / 2) - default is 0xE4=228 so -114dBm @@ -235,7 +235,6 @@ void RFM69::interruptHandler() { select(); SPI.transfer(REG_FIFO & 0x7f); PAYLOADLEN = SPI.transfer(0); - DATALEN = PAYLOADLEN - 3; PAYLOADLEN = PAYLOADLEN > 66 ? 66 : PAYLOADLEN; //precaution TARGETID = SPI.transfer(0); if(!(_promiscuousMode || TARGETID==_address || TARGETID==0)) //match this node's address, or broadcast addr 0x0 or anything in promiscuous mode @@ -245,6 +244,7 @@ void RFM69::interruptHandler() { //digitalWrite(4, 0); return; } + DATALEN = PAYLOADLEN - 3; SENDERID = SPI.transfer(0); byte CTLbyte = SPI.transfer(0); @@ -258,7 +258,7 @@ void RFM69::interruptHandler() { unselect(); setMode(RF69_MODE_RX); } - //digitalWrite(4, 0); + //digitalWrite(4, 0); } void RFM69::isr0() { selfPointer->interruptHandler(); } @@ -395,15 +395,16 @@ void RFM69::readAllRegs() unselect(); } -//// doesnt seem to work as expected -// byte RFM69::readTemp(bool calibrate) { - // if (calibrate) - // { - // writeReg(REG_OSC1, RF_OSC1_RCCAL_START); - // while ((readReg(REG_OSC1) & RF_OSC1_RCCAL_DONE) == 0x00); - // } +byte RFM69::readTemperature(byte calFactor) //returns centigrade +{ + setMode(RF69_MODE_STANDBY); + writeReg(REG_TEMP1, RF_TEMP1_MEAS_START); + while ((readReg(REG_TEMP1) & RF_TEMP1_MEAS_RUNNING)) Serial.print('*'); + return ~readReg(REG_TEMP2) + COURSE_TEMP_COEF + calFactor; //'complement'corrects the slope, rising temp = rising val +} // COURSE_TEMP_COEF puts reading in the ballpark, user can add additional correction - // writeReg(REG_TEMP1, RF_TEMP1_MEAS_START); - // while ((readReg(REG_TEMP1) & RF_TEMP1_MEAS_RUNNING)); - // return readReg(REG_TEMP2); -// } +void RFM69::rcCalibration() +{ + writeReg(REG_OSC1, RF_OSC1_RCCAL_START); + while ((readReg(REG_OSC1) & RF_OSC1_RCCAL_DONE) == 0x00); +} \ No newline at end of file diff --git a/RFM69.h b/RFM69.h index 8543c27..d1d643e 100644 --- a/RFM69.h +++ b/RFM69.h @@ -28,6 +28,7 @@ #define RF69_915MHZ 91 #define null 0 +#define COURSE_TEMP_COEF -90 // puts the temperature reading in the ballpark, user can fine tune the returned value class RFM69 { public: @@ -65,7 +66,9 @@ class RFM69 { void setHighPower(bool onOFF=true); //have to call it after initialize for RFM69HW void setPowerLevel(byte level); //reduce/increase transmit power level void sleep(); - + byte readTemperature(byte calFactor=0); //get CMOS temperature (8bit) + void rcCalibration(); //calibrate the internal RC oscillator for use in wide temperature variations - see datasheet section [4.3.5. RC Timer Accuracy] + // allow hacking registers by making these public byte readReg(byte addr); void writeReg(byte addr, byte val); @@ -73,7 +76,7 @@ class RFM69 { protected: static void isr0(); - void interruptHandler(); + void virtual interruptHandler(); void sendFrame(byte toAddress, const void* buffer, byte size, bool requestACK=false, bool sendACK=false); static RFM69* selfPointer; @@ -91,4 +94,4 @@ class RFM69 { void unselect(); }; -#endif +#endif \ No newline at end of file