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

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 3:f39791139435 34
phonemacro 3:f39791139435 35 union max31723_raw_data {
phonemacro 3:f39791139435 36 struct {
phonemacro 3:f39791139435 37 uint8_t lsb;
phonemacro 3:f39791139435 38 uint8_t msb;
phonemacro 3:f39791139435 39 };
phonemacro 3:f39791139435 40 uint16_t wrd;
phonemacro 3:f39791139435 41 };
phonemacro 0:4a3d6ac5042d 42
phonemacro 0:4a3d6ac5042d 43 MAX31723::MAX31723(SPI &spi, DigitalOut &ce_pin) : m_spi(spi), m_chip_enable(ce_pin)
phonemacro 0:4a3d6ac5042d 44 {
phonemacro 0:4a3d6ac5042d 45 m_chip_enable = 0;
phonemacro 0:4a3d6ac5042d 46 }
phonemacro 0:4a3d6ac5042d 47
phonemacro 0:4a3d6ac5042d 48 int MAX31723::write_reg(uint8_t val, uint8_t reg)
phonemacro 0:4a3d6ac5042d 49 {
phonemacro 0:4a3d6ac5042d 50 m_chip_enable = 1;
phonemacro 0:4a3d6ac5042d 51 m_spi.write(reg);
phonemacro 0:4a3d6ac5042d 52 m_spi.write(val);
phonemacro 0:4a3d6ac5042d 53 m_chip_enable = 0;
phonemacro 0:4a3d6ac5042d 54 return MAX31723_NO_ERROR;
phonemacro 0:4a3d6ac5042d 55 }
phonemacro 0:4a3d6ac5042d 56
phonemacro 0:4a3d6ac5042d 57 int MAX31723::read_reg(uint8_t &val, uint8_t reg)
phonemacro 0:4a3d6ac5042d 58 {
phonemacro 0:4a3d6ac5042d 59 m_chip_enable = 1;
phonemacro 0:4a3d6ac5042d 60 m_spi.write(reg);
phonemacro 0:4a3d6ac5042d 61 val = m_spi.write(0);
phonemacro 0:4a3d6ac5042d 62 m_chip_enable = 0;
phonemacro 0:4a3d6ac5042d 63 return MAX31723_NO_ERROR;
phonemacro 0:4a3d6ac5042d 64 }
phonemacro 0:4a3d6ac5042d 65
phonemacro 3:f39791139435 66 int MAX31723::perform_one_shot(uint8_t resolution, uint8_t interrupt_mode)
phonemacro 0:4a3d6ac5042d 67 {
phonemacro 2:7a20e65da621 68 if (resolution <= MAX31723_CFG_RESOLUTION_12BIT) {
phonemacro 2:7a20e65da621 69 write_reg(0, MAX31723_REG_CFG | MAX31723_WRITE_MASK);
phonemacro 3:f39791139435 70 write_reg(interrupt_mode | MAX31723_CFG_1SHOT |
phonemacro 2:7a20e65da621 71 resolution | MAX31723_CFG_STANDBY,
phonemacro 0:4a3d6ac5042d 72 MAX31723_REG_CFG | MAX31723_WRITE_MASK);
phonemacro 2:7a20e65da621 73 } else
phonemacro 0:4a3d6ac5042d 74 return MAX31723_ERROR;
phonemacro 0:4a3d6ac5042d 75 return MAX31723_NO_ERROR;
phonemacro 0:4a3d6ac5042d 76 }
phonemacro 0:4a3d6ac5042d 77
phonemacro 3:f39791139435 78 int MAX31723::perform_one_shot_int(uint8_t resolution)
phonemacro 0:4a3d6ac5042d 79 {
phonemacro 3:f39791139435 80 int ret;
phonemacro 3:f39791139435 81 ret = perform_one_shot(resolution, MAX31723_CFG_TM_MODE_INTERRUPT);
phonemacro 3:f39791139435 82 return ret;
phonemacro 3:f39791139435 83 }
phonemacro 3:f39791139435 84
phonemacro 3:f39791139435 85 int MAX31723::perform_one_shot_comparator(uint8_t resolution)
phonemacro 3:f39791139435 86 {
phonemacro 3:f39791139435 87 int ret;
phonemacro 3:f39791139435 88 ret = perform_one_shot(resolution, 0);
phonemacro 3:f39791139435 89 return ret;
phonemacro 0:4a3d6ac5042d 90 }
phonemacro 0:4a3d6ac5042d 91
phonemacro 3:f39791139435 92 uint8_t MAX31723::read_cfg()
phonemacro 3:f39791139435 93 {
phonemacro 3:f39791139435 94 uint8_t cfg;
phonemacro 3:f39791139435 95 read_reg(cfg, MAX31723_REG_CFG);
phonemacro 3:f39791139435 96 return cfg;
phonemacro 3:f39791139435 97 }
phonemacro 3:f39791139435 98
phonemacro 3:f39791139435 99 float MAX31723::read_reg_as_temperature(uint8_t reg)
phonemacro 3:f39791139435 100 {
phonemacro 3:f39791139435 101 max31723_raw_data raw;
phonemacro 3:f39791139435 102 float temperature;
phonemacro 3:f39791139435 103 if (reg >= MAX31723_REG_TEMP_LSB && reg <= MAX31723_REG_MAX) {
phonemacro 3:f39791139435 104 read_reg(raw.lsb, reg);
phonemacro 3:f39791139435 105 read_reg(raw.msb, reg+1);
phonemacro 3:f39791139435 106 raw.wrd = raw.wrd >> MAX31723_UNUSED_BITS;
phonemacro 3:f39791139435 107 temperature = raw.wrd * MAX31723_CF_LSB;
phonemacro 3:f39791139435 108 return temperature;
phonemacro 3:f39791139435 109 } else {
phonemacro 3:f39791139435 110 printf("Input read_registers_as_temperature is invalid, %d r\n",
phonemacro 3:f39791139435 111 reg);
phonemacro 3:f39791139435 112 return 0;
phonemacro 3:f39791139435 113 }
phonemacro 3:f39791139435 114 }
phonemacro 0:4a3d6ac5042d 115
phonemacro 0:4a3d6ac5042d 116 float MAX31723::celsius_to_fahrenheit(float temp_c)
phonemacro 0:4a3d6ac5042d 117 {
phonemacro 0:4a3d6ac5042d 118 float temp_f;
phonemacro 0:4a3d6ac5042d 119 temp_f = ((temp_c * 9)/5) + 32;
phonemacro 0:4a3d6ac5042d 120 return temp_f;
phonemacro 0:4a3d6ac5042d 121 }
phonemacro 0:4a3d6ac5042d 122
phonemacro 0:4a3d6ac5042d 123 MAX31723::~MAX31723(void)
phonemacro 0:4a3d6ac5042d 124 {
phonemacro 0:4a3d6ac5042d 125 //empty block
phonemacro 0:4a3d6ac5042d 126 }
phonemacro 0:4a3d6ac5042d 127