From 616c560b4a794c46282809f4fa646487b25a6a01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ladislav=20Kriv=C3=BD?= Date: Wed, 12 Aug 2020 22:23:45 +0200 Subject: [PATCH 1/2] Add simple function for measuring temperature --- src/LoRa.cpp | 27 +++++++++++++++++++++++++++ src/LoRa.h | 2 ++ 2 files changed, 29 insertions(+) diff --git a/src/LoRa.cpp b/src/LoRa.cpp index 4396518..fec24fb 100644 --- a/src/LoRa.cpp +++ b/src/LoRa.cpp @@ -35,12 +35,16 @@ #define REG_DETECTION_THRESHOLD 0x37 #define REG_SYNC_WORD 0x39 #define REG_INVERTIQ2 0x3b +#define REG_IMAGECAL 0x3b +#define REG_TEMP 0x3c #define REG_DIO_MAPPING_1 0x40 #define REG_VERSION 0x42 #define REG_PA_DAC 0x4d // modes #define MODE_LONG_RANGE_MODE 0x80 +#define MODE_FSK_MODE 0x00 + #define MODE_SLEEP 0x00 #define MODE_STDBY 0x01 #define MODE_TX 0x03 @@ -285,6 +289,29 @@ long LoRaClass::packetFrequencyError() return static_cast(fError); } +int LoRaClass::temperature() +{ + uint8_t opModeState = readRegister(REG_OP_MODE); + sleep(); //must be in sleep mode in order to switch between FSK and long range mode + writeRegister(REG_OP_MODE, MODE_FSK_MODE | MODE_SLEEP); //must be in FSK/OOK mode in order to access temp / imgage cal registers + writeRegister(REG_OP_MODE, MODE_FSK_MODE | MODE_RX_CONTINUOUS); //Temperature monitoring done in all modes except Sleep and Standby + + uint8_t imgCalState = readRegister(REG_IMAGECAL); + writeRegister(REG_IMAGECAL,imgCalState &!0x01); //enable temperature reading + delayMicroseconds(140); + writeRegister(REG_IMAGECAL,imgCalState | 0x01); //stop temperature reading + uint8_t rawTemp = readRegister(REG_TEMP); + + sleep(); //restore REG_OP_MODE trough sleep + writeRegister(REG_OP_MODE, opModeState); + + if((rawTemp&0x80)==0x80){ //datasheet unclear, int8_t would make more sense but this was example code + return(0xff - rawTemp); + } + return(-rawTemp); + +} + size_t LoRaClass::write(uint8_t byte) { return write(&byte, sizeof(byte)); diff --git a/src/LoRa.h b/src/LoRa.h index c1671c1..0457e85 100644 --- a/src/LoRa.h +++ b/src/LoRa.h @@ -44,6 +44,8 @@ class LoRaClass : public Stream { int packetRssi(); float packetSnr(); long packetFrequencyError(); + + int temperature(); // from Print virtual size_t write(uint8_t byte); From c744c8db9afb316c0de8b046ac276e1671a229f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ladislav=20Kriv=C3=BD?= Date: Wed, 12 Aug 2020 22:24:20 +0200 Subject: [PATCH 2/2] Add simple function for measuring temperature --- API.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/API.md b/API.md index 78f054d..b9f9c58 100644 --- a/API.md +++ b/API.md @@ -375,3 +375,12 @@ byte b = LoRa.random(); ``` Returns random byte. + +### Temperature + +Measures chip temperature in °C (blocking, ~150μs) +(uncalibrated accuracy ±10°C) + +``` +int temp = LoRa.temperature(); +```