C++ driver software code for Maxim Integrated MAX31723/MAX31722 device temperature sensor. The MAX31723 provides 9 to 12 bits of resolution.

Dependents:   MAX31723_Thermostat_Thermometer_Sensor

Files at this revision

API Documentation at this revision

Comitter:
phonemacro
Date:
Wed Feb 13 22:26:50 2019 +0000
Parent:
7:92004aaa3e4d
Commit message:
Correct logic for negative values.

Changed in this revision

max31723.cpp Show annotated file Show diff for this revision Revisions of this file
max31723.h Show annotated file Show diff for this revision Revisions of this file
diff -r 92004aaa3e4d -r e310e55a687b max31723.cpp
--- a/max31723.cpp	Tue Feb 12 22:58:59 2019 +0000
+++ b/max31723.cpp	Wed Feb 13 22:26:50 2019 +0000
@@ -37,6 +37,10 @@
         uint8_t lsb;
         uint8_t msb;
     };
+    struct {
+        uint16_t magnitude_bits:15;
+        uint16_t sign_bit:1;
+    };
     uint16_t uwrd;
     int16_t swrd;
 };
@@ -70,12 +74,14 @@
 
 float MAX31723::read_reg_as_temperature(uint8_t reg)
 {
-    max31723_raw_data raw;
+    max31723_raw_data tmp;
     float temperature;
     if (reg >= MAX31723_REG_TEMP_LSB && reg <= MAX31723_REG_MAX) {
-        read_reg(raw.lsb, reg);
-        read_reg(raw.msb, reg+1);
-        temperature = raw.swrd * MAX31723_CF_LSB;
+        read_reg(tmp.lsb, reg);
+        read_reg(tmp.msb, reg+1);
+        temperature = tmp.magnitude_bits * MAX31723_CF_LSB;
+        if (tmp.sign_bit)
+            temperature = -temperature;
         return temperature;
     } else {
         printf("Input read_registers_as_temperature is invalid, %d r\n",
@@ -129,8 +135,13 @@
 {
     int ret;
     max31723_raw_data raw;
+    raw.uwrd = 0;
+    if (temperature < 0) {
+        raw.sign_bit = 1;
+        temperature = -temperature;
+    }
     temperature /= MAX31723_CF_LSB;
-    raw.swrd = int16_t(temperature);
+    raw.magnitude_bits = uint16_t(temperature);
     ret = write_reg(raw.lsb, MAX31723_WRITE_MASK | MAX31723_REG_TRIP_LO_LSB);
     wait(.015);
     ret = write_reg(raw.msb, MAX31723_WRITE_MASK | MAX31723_REG_TRIP_LO_MSB);
@@ -142,8 +153,13 @@
 {
     int ret;
     max31723_raw_data raw;
+    raw.uwrd = 0;
+    if (temperature < 0) {
+        raw.sign_bit = 1;
+        temperature = -temperature;
+    }
     temperature /= MAX31723_CF_LSB;
-    raw.swrd = int16_t(temperature);
+    raw.magnitude_bits = uint16_t(temperature);
     ret = write_reg(raw.lsb, MAX31723_WRITE_MASK | MAX31723_REG_TRIP_HI_LSB);
     wait(.015);
     ret = write_reg(raw.msb, MAX31723_WRITE_MASK | MAX31723_REG_TRIP_HI_MSB);
diff -r 92004aaa3e4d -r e310e55a687b max31723.h
--- a/max31723.h	Tue Feb 12 22:58:59 2019 +0000
+++ b/max31723.h	Wed Feb 13 22:26:50 2019 +0000
@@ -1,5 +1,5 @@
 /*******************************************************************************
-* Copyright (C) 2018 Maxim Integrated Products, Inc., All Rights Reserved.
+* Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
@@ -78,7 +78,7 @@
 /**
  * @brief 9-bit to 12bit device temperature sensor with digital-to-analog converters (DACs)
  *        for the MAX31723.
- * @version 1.0000.0003
+ * @version 1.0000.0004
  *
  * @details The MAX31722/MAX31723 provides device temperature readings
  * for thermostats and thermometers. Communications may be either SPI or