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

Committer:
phonemacro
Date:
Sun Jan 27 07:26:37 2019 +0000
Revision:
2:7a20e65da621
Parent:
1:bb6f11be119f
Child:
3:f39791139435
Correct configuration register not being updating.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
phonemacro 0:4a3d6ac5042d 1 /*******************************************************************************
phonemacro 0:4a3d6ac5042d 2 * Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved.
phonemacro 0:4a3d6ac5042d 3 *
phonemacro 0:4a3d6ac5042d 4 * Permission is hereby granted, free of charge, to any person obtaining a
phonemacro 0:4a3d6ac5042d 5 * copy of this software and associated documentation files (the "Software"),
phonemacro 0:4a3d6ac5042d 6 * to deal in the Software without restriction, including without limitation
phonemacro 0:4a3d6ac5042d 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
phonemacro 0:4a3d6ac5042d 8 * and/or sell copies of the Software, and to permit persons to whom the
phonemacro 0:4a3d6ac5042d 9 * Software is furnished to do so, subject to the following conditions:
phonemacro 0:4a3d6ac5042d 10 *
phonemacro 0:4a3d6ac5042d 11 * The above copyright notice and this permission notice shall be included
phonemacro 0:4a3d6ac5042d 12 * in all copies or substantial portions of the Software.
phonemacro 0:4a3d6ac5042d 13 *
phonemacro 0:4a3d6ac5042d 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
phonemacro 0:4a3d6ac5042d 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
phonemacro 0:4a3d6ac5042d 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
phonemacro 0:4a3d6ac5042d 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
phonemacro 0:4a3d6ac5042d 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
phonemacro 0:4a3d6ac5042d 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
phonemacro 0:4a3d6ac5042d 20 * OTHER DEALINGS IN THE SOFTWARE.
phonemacro 0:4a3d6ac5042d 21 *
phonemacro 0:4a3d6ac5042d 22 * Except as contained in this notice, the name of Maxim Integrated
phonemacro 0:4a3d6ac5042d 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
phonemacro 0:4a3d6ac5042d 24 * Products, Inc. Branding Policy.
phonemacro 0:4a3d6ac5042d 25 *
phonemacro 0:4a3d6ac5042d 26 * The mere transfer of this software does not imply any licenses
phonemacro 0:4a3d6ac5042d 27 * of trade secrets, proprietary technology, copyrights, patents,
phonemacro 0:4a3d6ac5042d 28 * trademarks, maskwork rights, or any other form of intellectual
phonemacro 0:4a3d6ac5042d 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
phonemacro 0:4a3d6ac5042d 30 * ownership rights.
phonemacro 0:4a3d6ac5042d 31 *******************************************************************************
phonemacro 0:4a3d6ac5042d 32 */
phonemacro 0:4a3d6ac5042d 33 #include "max31723.h"
phonemacro 0:4a3d6ac5042d 34
phonemacro 0:4a3d6ac5042d 35 MAX31723::MAX31723(SPI &spi, DigitalOut &ce_pin) : m_spi(spi), m_chip_enable(ce_pin)
phonemacro 0:4a3d6ac5042d 36 {
phonemacro 0:4a3d6ac5042d 37 m_chip_enable = 0;
phonemacro 0:4a3d6ac5042d 38 }
phonemacro 0:4a3d6ac5042d 39
phonemacro 0:4a3d6ac5042d 40 int MAX31723::write_reg(uint8_t val, uint8_t reg)
phonemacro 0:4a3d6ac5042d 41 {
phonemacro 0:4a3d6ac5042d 42 m_chip_enable = 1;
phonemacro 0:4a3d6ac5042d 43 m_spi.write(reg);
phonemacro 0:4a3d6ac5042d 44 m_spi.write(val);
phonemacro 0:4a3d6ac5042d 45 m_chip_enable = 0;
phonemacro 0:4a3d6ac5042d 46 return MAX31723_NO_ERROR;
phonemacro 0:4a3d6ac5042d 47 }
phonemacro 0:4a3d6ac5042d 48
phonemacro 0:4a3d6ac5042d 49 int MAX31723::read_reg(uint8_t &val, uint8_t reg)
phonemacro 0:4a3d6ac5042d 50 {
phonemacro 0:4a3d6ac5042d 51 m_chip_enable = 1;
phonemacro 0:4a3d6ac5042d 52 m_spi.write(reg);
phonemacro 0:4a3d6ac5042d 53 val = m_spi.write(0);
phonemacro 0:4a3d6ac5042d 54 m_chip_enable = 0;
phonemacro 0:4a3d6ac5042d 55 return MAX31723_NO_ERROR;
phonemacro 0:4a3d6ac5042d 56 }
phonemacro 0:4a3d6ac5042d 57
phonemacro 2:7a20e65da621 58 int MAX31723::perform_one_shot_int(uint8_t resolution)
phonemacro 0:4a3d6ac5042d 59 {
phonemacro 2:7a20e65da621 60 if (resolution <= MAX31723_CFG_RESOLUTION_12BIT) {
phonemacro 2:7a20e65da621 61 write_reg(0, MAX31723_REG_CFG | MAX31723_WRITE_MASK);
phonemacro 2:7a20e65da621 62 write_reg(MAX31723_CFG_TM_MODE_INTERRUPT | MAX31723_CFG_1SHOT |
phonemacro 2:7a20e65da621 63 resolution | MAX31723_CFG_STANDBY,
phonemacro 0:4a3d6ac5042d 64 MAX31723_REG_CFG | MAX31723_WRITE_MASK);
phonemacro 2:7a20e65da621 65 } else
phonemacro 0:4a3d6ac5042d 66 return MAX31723_ERROR;
phonemacro 0:4a3d6ac5042d 67 return MAX31723_NO_ERROR;
phonemacro 0:4a3d6ac5042d 68 }
phonemacro 0:4a3d6ac5042d 69
phonemacro 0:4a3d6ac5042d 70 float MAX31723::read_temp()
phonemacro 0:4a3d6ac5042d 71 {
phonemacro 0:4a3d6ac5042d 72 uint8_t lsb, msb;
phonemacro 0:4a3d6ac5042d 73 uint16_t raw_temp;
phonemacro 0:4a3d6ac5042d 74 float temperature;
phonemacro 2:7a20e65da621 75 wait(0.2);
phonemacro 0:4a3d6ac5042d 76 read_reg(lsb, MAX31723_REG_TEMP_LSB);
phonemacro 0:4a3d6ac5042d 77 read_reg(msb, MAX31723_REG_TEMP_MSB);
phonemacro 0:4a3d6ac5042d 78 raw_temp = int16_t((msb << 8) | (lsb));
phonemacro 0:4a3d6ac5042d 79 raw_temp = raw_temp >> 4;
phonemacro 0:4a3d6ac5042d 80 temperature = raw_temp * 0.0625 ;
phonemacro 0:4a3d6ac5042d 81 return temperature;
phonemacro 0:4a3d6ac5042d 82 }
phonemacro 0:4a3d6ac5042d 83
phonemacro 0:4a3d6ac5042d 84
phonemacro 0:4a3d6ac5042d 85 float MAX31723::celsius_to_fahrenheit(float temp_c)
phonemacro 0:4a3d6ac5042d 86 {
phonemacro 0:4a3d6ac5042d 87 float temp_f;
phonemacro 0:4a3d6ac5042d 88 temp_f = ((temp_c * 9)/5) + 32;
phonemacro 0:4a3d6ac5042d 89 return temp_f;
phonemacro 0:4a3d6ac5042d 90 }
phonemacro 0:4a3d6ac5042d 91
phonemacro 0:4a3d6ac5042d 92 MAX31723::~MAX31723(void)
phonemacro 0:4a3d6ac5042d 93 {
phonemacro 0:4a3d6ac5042d 94 //empty block
phonemacro 0:4a3d6ac5042d 95 }
phonemacro 0:4a3d6ac5042d 96