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:
Tue Feb 12 22:57:17 2019 +0000
Parent:
5:8e70ec0923e2
Child:
7:92004aaa3e4d
Commit message:
Add Tlow, Thigh APIs

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	Wed Jan 30 02:13:51 2019 +0000
+++ b/max31723.cpp	Tue Feb 12 22:57:17 2019 +0000
@@ -31,7 +31,7 @@
 *******************************************************************************
 */
 #include "max31723.h"
-
+#include "USBSerial.h"
 union max31723_raw_data {
     struct {
         uint8_t lsb;
@@ -46,6 +46,59 @@
     m_chip_enable = 0;
 }
  
+
+int MAX31723::read_reg(uint8_t &val, uint8_t reg)
+{
+    if (reg <= MAX31723_REG_MAX) {
+        m_chip_enable = 1;
+        m_spi.write(reg);
+        val = m_spi.write(0);
+        m_chip_enable = 0;
+        return MAX31723_NO_ERROR;
+    } else {
+        printf("Input read_reg is invalid, 0x%02X \r\n", reg);
+        return MAX31723_ERROR;
+    }
+}
+
+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);
+        temperature = raw.swrd * MAX31723_CF_LSB;
+        return temperature;
+    } else {
+        printf("Input read_registers_as_temperature is invalid, %d r\n",
+            reg);
+        return 0;
+    }
+}
+
+float MAX31723::read_temperature(void)
+{
+    return read_reg_as_temperature(MAX31723_REG_TEMP_LSB);
+}
+
+float MAX31723::read_trip_low(void)
+{
+    return read_reg_as_temperature(MAX31723_REG_TRIP_LO_LSB);
+}
+
+float MAX31723::read_trip_high(void)
+{
+    return read_reg_as_temperature(MAX31723_REG_TRIP_HI_LSB);
+}
+
 int MAX31723::write_reg(uint8_t val, uint8_t reg)
 {
     if (reg == (MAX31723_REG_CFG | MAX31723_WRITE_MASK) ||
@@ -62,20 +115,43 @@
     }
 }
  
-int MAX31723::read_reg(uint8_t &val, uint8_t reg)
+int MAX31723::write_cfg(uint8_t val)
 {
-    if (reg <= MAX31723_REG_MAX) {
         m_chip_enable = 1;
-        m_spi.write(reg);
-        val = m_spi.write(0);
+        m_spi.write(MAX31723_REG_CFG );
+        m_spi.write(val);
         m_chip_enable = 0;
         return MAX31723_NO_ERROR;
-    } else {
-        printf("Input read_reg is invalid, 0x%02X \r\n", reg);
-        return MAX31723_ERROR;
-    }
+}
+
+
+int MAX31723::write_trip_low(float temperature)
+{
+    int ret;
+    max31723_raw_data raw;
+    temperature /= MAX31723_CF_LSB;
+    raw.swrd = int16_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);
+    wait(.015);
+    return ret;
 }
 
+int MAX31723::write_trip_high(float temperature)
+{
+    int ret;
+    max31723_raw_data raw;
+    temperature /= MAX31723_CF_LSB;
+    raw.swrd = int16_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);
+    wait(.015);
+    return ret;
+}
+
+
 int MAX31723::perform_one_shot(uint8_t resolution, uint8_t interrupt_mode)
 {
     if (resolution <= MAX31723_CFG_RESOLUTION_12BIT) {
@@ -102,29 +178,6 @@
     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.swrd = raw.swrd >> MAX31723_UNUSED_BITS;
-        temperature = raw.swrd * 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	Wed Jan 30 02:13:51 2019 +0000
+++ b/max31723.h	Tue Feb 12 22:57:17 2019 +0000
@@ -59,6 +59,7 @@
 #define MAX31723_CFG_RESOLUTION_10BIT   0x02
 #define MAX31723_CFG_RESOLUTION_11BIT   0x04
 #define MAX31723_CFG_RESOLUTION_12BIT   0x06
+#define MAX31723_CFG_RESOLUTION_MASK    0x06
 
 #define MAX31723_CFG_TM_MODE_INTERRUPT  0x08
 #define MAX31723_CFG_1SHOT              0x10
@@ -72,22 +73,60 @@
 
 #define MAX31723_NVRAM_WRITE_TIME_MSEC  0.015
 
-#define MAX31723_CF_LSB                 0.0625
-#define MAX31723_UNUSED_BITS            4
+#define MAX31723_CF_LSB                 (0.00390625f)
 
 /**
-* @brief 9-bit to 12bit device temperature sensor with digital-to-analog converters (DACs)
-*        for the MAX31723, MAX5214.
-* @version 1.0000.0002
-*
-* @details The MAX31722/MAX31723 provides device temperature readings
-* for thermostats and thermometers. Communications may be either SPI or
-* 3-wire. There are two operating modes available: comparator or interrupt.
-* Temperature limits may be stored in NVRAM so that the TOUT_N pin can be 
-* triggered when the limits are exceeded while in the comparator mode.
-* Typically applications are for temperature sensitive systems.
-*
-*/
+ * @brief 9-bit to 12bit device temperature sensor with digital-to-analog converters (DACs)
+ *        for the MAX31723.
+ * @version 1.0000.0002
+ *
+ * @details The MAX31722/MAX31723 provides device temperature readings
+ * for thermostats and thermometers. Communications may be either SPI or
+ * 3-wire. There are two operating modes available: comparator or interrupt.
+ * Temperature limits may be stored in NVRAM so that the TOUT_N pin can be 
+ * triggered when the limits are exceeded while in the comparator mode.
+ * Typically applications are for temperature sensitive systems.
+ *
+ * @code 
+ * #include "mbed.h"
+ * #include "max32630fthr.h"
+ * #include "max31723.h"
+ * #include "USBSerial.h"
+ * 
+ * MAX32630FTHR pegasus(MAX32630FTHR::VIO_1V8); 
+ * 
+ * DigitalOut gLED(LED2);
+ * 
+ * DigitalOut selectPin(P3_0);
+ * SPI spi(P5_1, P5_2, P5_0);
+ * 
+ * int main()
+ * {
+ *     int i, ret;
+ *     float temperature;
+ *     DigitalOut gLED(LED2, LED_OFF);
+ *     printf("MAX31723 Temperature Sensor example code.\r\n");
+ *     printf("\r\n");
+ *     
+ *     gLED = LED_ON;
+ * 
+ *     MAX31723 temp_sensor(spi, selectPin);
+ *     spi.format(8,3);
+ *     spi.frequency(5000000);
+ *     ret = temp_sensor.perform_one_shot_int(MAX31723_CFG_RESOLUTION_12BIT);
+ *     
+ *     for (i = 0; i < 5; i++) {
+ *         ret = temp_sensor.perform_one_shot_int(MAX31723_CFG_RESOLUTION_12BIT);
+ *         wait(MAX31723_CONV_TIME_MSEC_12BIT);
+ *         temperature = temp_sensor.read_temperature();
+ *         printf("Temperature = %4.4f Celsius, %4.4f Fahrenheit\r\n", 
+ *             temperature, temp_sensor.celsius_to_fahrenheit(temperature));
+ *         wait(2);
+ *     } 
+ *     printf("Configuration Register = 0x%02Xh \r\n", temp_sensor.read_cfg());
+ * }
+ * 
+ */
 
 class MAX31723{
     public:
@@ -101,7 +140,6 @@
     * On Entry:
     *     @param[in] spi - pointer to existing SPI object
     *     @param[in] ce_pin - pointer to a DigitalOut pin object
-    *     @param[in] ic_variant - which type of MAX521x is used
     *
     * On Exit:
     *
@@ -111,25 +149,62 @@
     MAX31723(SPI &spi, DigitalOut &ce_pin); 
 
     /** 
-     * @brief Read a value from a register
-     * @param val - 8-bit value read from the register
-     * @param reg - register address
+     * @brief Reads the register
+     * @param &val - register value
+     * @param reg  - address of the register
      * @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 configuration register
+     * @return value of the configuration register
+     */
+    uint8_t read_cfg();
+
+    /** 
+     * @brief Reads the temperature register
+     * @return temperature in degrees Celsius
+     */
+    float read_temperature(void);
+
+    /** 
+     * @brief Reads the trip low temperature register
+     * @return trip low temperature in degrees Celsius
+     */
+    float read_trip_low(void);
+
+    /** 
+     * @brief Reads the trip high temperature register
+     * @return trip high temperature in degrees Celsius
+     */
+    float read_trip_high(void);
+    
+    /** 
+     * @brief Writes to the configuration register
+     * @param val - the configuration value
      * @return 0 on success, negative number on failure
      */
-    int write_reg(uint8_t val, uint8_t reg);
+    int write_cfg(uint8_t val);
+
+    /** 
+     * @brief Writes to the Tlow register (the write takes 30 msec)
+     * @param temperature - the temperature in Celsius degrees
+     * @return 0 on success, negative number on failure
+     */
+     int write_trip_low(float temperature);
+
+    /** 
+     * @brief Writes to the Thigh register (the write takes 30 msec)
+     * @param temperature - the temperature in Celsius degrees
+     * @return 0 on success, negative number on failure
+     */
+     int write_trip_high(float temperature);
 
     /** 
      * @brief Reads the temperature registers
-     * @param reg - the low byte address of the temperature register
-     * @return temprature in degrees celsius
+     * @param reg - the address of the temperature register
+     * @return temprature in degrees Celsius
      */
     float read_reg_as_temperature(uint8_t reg);
 
@@ -149,12 +224,6 @@
      */
     int perform_one_shot_comparator(uint8_t resolution);
 
-
-    /** 
-     * @brief Reads the configuration register
-     * @return value of the configuration register
-     */
-    uint8_t read_cfg();
     /** 
      * @brief Converts Celcius degrees to Fahrenheit
      * @param temp_c - the temperature in Celsius degrees
@@ -175,6 +244,7 @@
      *************************************************************
      */
     ~MAX31723();
+
     protected:
     /** 
      * @brief Configures the device to perform a one-shot temperature reading
@@ -184,12 +254,25 @@
      */
     int perform_one_shot(uint8_t resolution, uint8_t interrupt_mode);
 
-
     private:
+    /** 
+     * @brief Write a value to a register
+     * @param val - 8-bit value to write to the register
+     * @param reg - register address
+     * @return 0 on success, negative number on failure
+     */
+    int write_reg(uint8_t val, uint8_t reg);
 
-    /* SPI object */
+    /** @var m_spi
+     *  @brief SPI object
+     */
     SPI &m_spi;
+
+    /** @var m_chip_enable
+     *  @brief Chip Enable pin
+     */
     /* chip enable pin */
+
     DigitalOut &m_chip_enable;
 };