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
Revision 0:4a3d6ac5042d, committed 2019-01-26
- Comitter:
- phonemacro
- Date:
- Sat Jan 26 23:55:38 2019 +0000
- Child:
- 1:bb6f11be119f
- Commit message:
- Initial commit for one-shot temperature readings.
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 |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/max31723.cpp Sat Jan 26 23:55:38 2019 +0000
@@ -0,0 +1,97 @@
+/*******************************************************************************
+* 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"),
+* to deal in the Software without restriction, including without limitation
+* the rights to use, copy, modify, merge, publish, distribute, sublicense,
+* and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included
+* in all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
+* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+* OTHER DEALINGS IN THE SOFTWARE.
+*
+* Except as contained in this notice, the name of Maxim Integrated
+* Products, Inc. shall not be used except as stated in the Maxim Integrated
+* Products, Inc. Branding Policy.
+*
+* The mere transfer of this software does not imply any licenses
+* of trade secrets, proprietary technology, copyrights, patents,
+* trademarks, maskwork rights, or any other form of intellectual
+* property whatsoever. Maxim Integrated Products, Inc. retains all
+* ownership rights.
+*******************************************************************************
+*/
+#include "max31723.h"
+
+MAX31723::MAX31723(SPI &spi, DigitalOut &ce_pin) : m_spi(spi), m_chip_enable(ce_pin)
+{
+ m_chip_enable = 0;
+}
+
+int MAX31723::write_reg(uint8_t val, uint8_t reg)
+{
+
+ m_chip_enable = 1;
+ m_spi.write(reg);
+ m_spi.write(val);
+ m_chip_enable = 0;
+ return MAX31723_NO_ERROR;
+}
+
+int MAX31723::read_reg(uint8_t &val, uint8_t reg)
+{
+
+ m_chip_enable = 1;
+ m_spi.write(reg);
+ val = m_spi.write(0);
+ m_chip_enable = 0;
+ return MAX31723_NO_ERROR;
+}
+
+int MAX31723::perform_one_shot(uint8_t resolution)
+{
+ if (resolution <= MAX31723_CFG_RESOLUTION_12BIT)
+ write_reg(resolution | MAX31723_CFG_1SHOT | MAX31723_CFG_STANDBY,
+ MAX31723_REG_CFG | MAX31723_WRITE_MASK);
+ else
+ return MAX31723_ERROR;
+ return MAX31723_NO_ERROR;
+}
+
+float MAX31723::read_temp()
+{
+ uint8_t lsb, msb;
+ uint16_t raw_temp;
+ float temperature;
+
+ 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;
+}
+
+
+float MAX31723::celsius_to_fahrenheit(float temp_c)
+{
+ float temp_f;
+ temp_f = ((temp_c * 9)/5) + 32;
+ return temp_f;
+}
+
+MAX31723::~MAX31723(void)
+{
+ //empty block
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/max31723.h Sat Jan 26 23:55:38 2019 +0000
@@ -0,0 +1,163 @@
+/*******************************************************************************
+* Copyright (C) 2018 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"),
+* to deal in the Software without restriction, including without limitation
+* the rights to use, copy, modify, merge, publish, distribute, sublicense,
+* and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included
+* in all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
+* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+* OTHER DEALINGS IN THE SOFTWARE.
+*
+* Except as contained in this notice, the name of Maxim Integrated
+* Products, Inc. shall not be used except as stated in the Maxim Integrated
+* Products, Inc. Branding Policy.
+*
+* The mere transfer of this software does not imply any licenses
+* of trade secrets, proprietary technology, copyrights, patents,
+* trademarks, maskwork rights, or any other form of intellectual
+* property whatsoever. Maxim Integrated Products, Inc. retains all
+* ownership rights.
+*******************************************************************************
+* @file MAX31723.h
+*******************************************************************************
+*/
+#ifndef MAX31723_H
+#define MAX31723_H
+
+#include "mbed.h"
+
+#define MAX31723_NO_ERROR 0
+#define MAX31723_ERROR -1
+
+#define MAX31723_WRITE_MASK 0x80
+
+#define MAX31723_REG_CFG 0x00
+#define MAX31723_REG_TEMP_LSB 0x01
+#define MAX31723_REG_TEMP_MSB 0x02
+
+/* Configuration, Status masks */
+#define MAX31723_CFG_CONTINUOUS 0x00
+#define MAX31723_CFG_STANDBY 0x01
+
+#define MAX31723_CFG_RESOLUTION_9BIT 0x00
+#define MAX31723_CFG_RESOLUTION_10BIT 0x02
+#define MAX31723_CFG_RESOLUTION_11BIT 0x04
+#define MAX31723_CFG_RESOLUTION_12BIT 0x06
+
+#define MAX31723_CFG_TM_MODE_INTERRUPT 0x08
+#define MAX31723_CFG_1SHOT 0x10
+#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
+
+/**
+* @brief 9-bit to 12bit device temperature sensor with digital-to-analog converters (DACs)
+* for the MAX31723, MAX5214.
+* @version 1.0000.0000
+*
+* @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.
+*
+*/
+
+class MAX31723{
+ public:
+
+ /**************************************************************
+ * @brief Constructor for MAX31723 Class.
+ *
+ * @details Requires an existing SPI object as well as a DigitalOut object.
+ * The DigitalOut object is used for a chip enable signal
+ *
+ * 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:
+ *
+ * @return None
+ ***************************************************************
+ */
+ MAX31723(SPI &spi, DigitalOut &ce_pin);
+
+ /**
+ * @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);
+
+ /**
+ * @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
+ * @return temprature in degrees celsius
+ */
+ float read_temp();
+
+ /**
+ * @brief Configures the device to perform a one-shot temperature reading
+ * and places the device in standby 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);
+
+ /**
+ * @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 Default destructor for MAX31723 Class.
+ *
+ * @details Destroys SPI object if owner
+ *
+ * On Entry:
+ *
+ * On Exit:
+ *
+ * @return None
+ *************************************************************
+ */
+ ~MAX31723();
+ private:
+ // SPI object
+ SPI &m_spi;
+ // chip enable pin
+ DigitalOut &m_chip_enable;
+};
+
+#endif
\ No newline at end of file