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:
Mon Jan 28 00:39:50 2019 +0000
Parent:
2:7a20e65da621
Child:
4:ac75d4d62471
Commit message:
Use 1.8V for VDD. Updated the interface for reading registers.

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
--- a/max31723.cpp	Sun Jan 27 07:26:37 2019 +0000
+++ b/max31723.cpp	Mon Jan 28 00:39:50 2019 +0000
@@ -31,6 +31,14 @@
 *******************************************************************************
 */
 #include "max31723.h"
+
+union max31723_raw_data {
+    struct {
+        uint8_t lsb;
+        uint8_t msb;
+    };
+    uint16_t wrd;
+};
  
 MAX31723::MAX31723(SPI &spi, DigitalOut &ce_pin) : m_spi(spi), m_chip_enable(ce_pin)
 {
@@ -55,11 +63,11 @@
     return MAX31723_NO_ERROR;
 }
 
-int MAX31723::perform_one_shot_int(uint8_t resolution)
+int MAX31723::perform_one_shot(uint8_t resolution, uint8_t interrupt_mode)
 {
     if (resolution <= MAX31723_CFG_RESOLUTION_12BIT) {
         write_reg(0, MAX31723_REG_CFG | MAX31723_WRITE_MASK);
-        write_reg(MAX31723_CFG_TM_MODE_INTERRUPT | MAX31723_CFG_1SHOT |
+        write_reg(interrupt_mode | MAX31723_CFG_1SHOT |
             resolution | MAX31723_CFG_STANDBY,
             MAX31723_REG_CFG | MAX31723_WRITE_MASK);
     } else
@@ -67,20 +75,43 @@
     return MAX31723_NO_ERROR;
 }
 
-float MAX31723::read_temp()
+int MAX31723::perform_one_shot_int(uint8_t resolution)
 {
-    uint8_t lsb, msb;
-    uint16_t raw_temp;
-    float temperature;
-    wait(0.2);
-    read_reg(lsb, MAX31723_REG_TEMP_LSB);
-    read_reg(msb, MAX31723_REG_TEMP_MSB);
-    raw_temp =  int16_t((msb << 8) | (lsb));
-    raw_temp =  raw_temp >> 4;
-    temperature = raw_temp * 0.0625 ;
-    return temperature;
+    int ret;
+    ret = perform_one_shot(resolution, MAX31723_CFG_TM_MODE_INTERRUPT);
+    return ret;
+}
+
+int MAX31723::perform_one_shot_comparator(uint8_t resolution)
+{
+    int ret;
+    ret = perform_one_shot(resolution, 0);
+    return ret;
 }
 
+uint8_t MAX31723::read_cfg()
+{
+    uint8_t cfg;
+    read_reg(cfg, MAX31723_REG_CFG);
+    return cfg;
+}
+
+float MAX31723::read_reg_as_temperature(uint8_t reg)
+{
+    max31723_raw_data raw;
+    float temperature;
+    if (reg >= MAX31723_REG_TEMP_LSB && reg <= MAX31723_REG_MAX) {
+        read_reg(raw.lsb, reg);
+        read_reg(raw.msb, reg+1);
+        raw.wrd =  raw.wrd >> MAX31723_UNUSED_BITS;
+        temperature = raw.wrd * MAX31723_CF_LSB;
+        return temperature;
+    } else {
+        printf("Input read_registers_as_temperature is invalid, %d r\n",
+            reg);
+        return 0;
+    }
+}
 
 float MAX31723::celsius_to_fahrenheit(float temp_c)
 {
--- a/max31723.h	Sun Jan 27 07:26:37 2019 +0000
+++ b/max31723.h	Mon Jan 28 00:39:50 2019 +0000
@@ -45,6 +45,11 @@
 #define MAX31723_REG_CFG                0x00
 #define MAX31723_REG_TEMP_LSB           0x01
 #define MAX31723_REG_TEMP_MSB           0x02
+#define MAX31723_REG_TRIP_HI_LSB        0x03
+#define MAX31723_REG_TRIP_HI_MSB        0x04
+#define MAX31723_REG_TRIP_LO_LSB        0x05
+#define MAX31723_REG_TRIP_LO_MSB        0x06
+#define MAX31723_REG_MAX                MAX31723_REG_TRIP_LO_MSB
 
 /* Configuration, Status masks */
 #define MAX31723_CFG_CONTINUOUS         0x00
@@ -60,15 +65,20 @@
 #define MAX31723_CFG_NVB_NVRAM_BUSY     0x20
 #define MAX31723_CFG_MEMW_CFG_WR_EEPROM 0x40
 
-#define MAX31723_CONV_TIME_MSEC_9BIT    .025
-#define MAX31723_CONV_TIME_MSEC_10BIT   .050
-#define MAX31723_CONV_TIME_MSEC_11BIT   .100
-#define MAX31723_CONV_TIME_MSEC_12BIT   .200
+#define MAX31723_CONV_TIME_MSEC_9BIT    0.025
+#define MAX31723_CONV_TIME_MSEC_10BIT   0.050
+#define MAX31723_CONV_TIME_MSEC_11BIT   0.100
+#define MAX31723_CONV_TIME_MSEC_12BIT   0.200
+
+#define MAX31723_NVRAM_WRITE_TIME_MSEC  0.015
+
+#define MAX31723_CF_LSB                 0.0625f
+#define MAX31723_UNUSED_BITS            4
 
 /**
 * @brief 9-bit to 12bit device temperature sensor with digital-to-analog converters (DACs)
 *        for the MAX31723, MAX5214.
-* @version 1.0000.0001
+* @version 1.0000.0002
 *
 * @details The MAX31722/MAX31723 provides device temperature readings
 * for thermostats and thermometers. Communications may be either SPI or
@@ -109,20 +119,11 @@
     int write_reg(uint8_t val, uint8_t reg);
 
     /** 
-     * @brief Read a value from a register
-     * @param val - 8-bit value read from the register
-     * @param reg - register address
-     * @return 0 on success, negative number on failure
-     */
-    int read_reg(uint8_t &val, uint8_t reg);
-
-    /** 
-     * @brief Write a value to a register
-     * @param val - 8-bit value to write to the register
-     * @param reg - register address
+     * @brief Reads the temperature registers
+     * @param reg - the low byte address of the temperature register
      * @return temprature in degrees celsius
      */
-    float read_temp();
+    float read_reg_as_temperature(uint8_t reg);
 
     /** 
      * @brief Configures the device to perform a one-shot temperature reading
@@ -133,12 +134,26 @@
     int perform_one_shot_int(uint8_t resolution);
 
     /** 
+     * @brief Configures the device to perform a one-shot temperature reading
+     *        and places the device in standby mode, comparator mode.
+     * @param resolution - the resolution of the calculation is set to this
+     * @return 0 on success, negative number on failure
+     */
+    int perform_one_shot_comparator(uint8_t resolution);
+
+    /** 
      * @brief Converts Celcius degrees to Fahrenheit
      * @param temp_c - the temperature in Celsius degrees
      * @return 0 on success, negative number on failure
      */
     float celsius_to_fahrenheit(float temp_c);
 
+    /** 
+     * @brief Reads the configuration register
+     * @return value of the configuration register
+     */
+    uint8_t read_cfg();
+
 
     /************************************************************
      * @brief Default destructor for MAX31723 Class.  
@@ -153,10 +168,28 @@
      *************************************************************
      */
     ~MAX31723();
+    protected:
+    /** 
+     * @brief Configures the device to perform a one-shot temperature reading
+     *        and places the device in standby mode, interrupt mode.
+     * @param resolution - the resolution of the calculation is set to this
+     * @return 0 on success, negative number on failure
+     */
+    int perform_one_shot(uint8_t resolution, uint8_t interrupt_mode);
+
+
     private:
-    // SPI object
+    /** 
+     * @brief Read a value from a register
+     * @param val - 8-bit value read from the register
+     * @param reg - register address
+     * @return 0 on success, negative number on failure
+     */
+    int read_reg(uint8_t &val, uint8_t reg);
+
+    /* SPI object */
     SPI &m_spi;
-    // chip enable pin
+    /* chip enable pin */
     DigitalOut &m_chip_enable;
 };