DS7505 C, C++ driver source code: The DS7505 low-voltage (1.7V to 3.7V) digital thermometer and thermostat provides 9 to 12-bit digital temperature readings over a -55°C to +125°C range with +-0.5°C accuracy in the 0°C (32°F) to +70°C (158°F) range.

Dependents:   DS7505_Temperature_Sensor_Low_Power

Committer:
phonemacro
Date:
Sat Apr 27 09:50:48 2019 +0000
Revision:
25:93287f99d225
Initial Commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
phonemacro 25:93287f99d225 1 /*******************************************************************************
phonemacro 25:93287f99d225 2 * Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved.
phonemacro 25:93287f99d225 3 *
phonemacro 25:93287f99d225 4 * Permission is hereby granted, free of charge, to any person obtaining a
phonemacro 25:93287f99d225 5 * copy of this software and associated documentation files (the "Software"),
phonemacro 25:93287f99d225 6 * to deal in the Software without restriction, including without limitation
phonemacro 25:93287f99d225 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
phonemacro 25:93287f99d225 8 * and/or sell copies of the Software, and to permit persons to whom the
phonemacro 25:93287f99d225 9 * Software is furnished to do so, subject to the following conditions:
phonemacro 25:93287f99d225 10 *
phonemacro 25:93287f99d225 11 * The above copyright notice and this permission notice shall be included
phonemacro 25:93287f99d225 12 * in all copies or substantial portions of the Software.
phonemacro 25:93287f99d225 13 *
phonemacro 25:93287f99d225 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
phonemacro 25:93287f99d225 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
phonemacro 25:93287f99d225 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
phonemacro 25:93287f99d225 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
phonemacro 25:93287f99d225 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
phonemacro 25:93287f99d225 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
phonemacro 25:93287f99d225 20 * OTHER DEALINGS IN THE SOFTWARE.
phonemacro 25:93287f99d225 21 *
phonemacro 25:93287f99d225 22 * Except as contained in this notice, the name of Maxim Integrated
phonemacro 25:93287f99d225 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
phonemacro 25:93287f99d225 24 * Products, Inc. Branding Policy.
phonemacro 25:93287f99d225 25 *
phonemacro 25:93287f99d225 26 * The mere transfer of this software does not imply any licenses
phonemacro 25:93287f99d225 27 * of trade secrets, proprietary technology, copyrights, patents,
phonemacro 25:93287f99d225 28 * trademarks, maskwork rights, or any other form of intellectual
phonemacro 25:93287f99d225 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
phonemacro 25:93287f99d225 30 * ownership rights.
phonemacro 25:93287f99d225 31 *******************************************************************************
phonemacro 25:93287f99d225 32 * @file ds7505_cpp.h
phonemacro 25:93287f99d225 33 *******************************************************************************
phonemacro 25:93287f99d225 34 */
phonemacro 25:93287f99d225 35 #ifndef DS7505_CPP_H
phonemacro 25:93287f99d225 36 #define DS7505_CPP_H
phonemacro 25:93287f99d225 37 #include "mbed.h"
phonemacro 25:93287f99d225 38
phonemacro 25:93287f99d225 39
phonemacro 25:93287f99d225 40 /**
phonemacro 25:93287f99d225 41 * @brief Digital thermometer, thermostat, temperature sensor.
phonemacro 25:93287f99d225 42 * @version 1.0000.0000
phonemacro 25:93287f99d225 43 *
phonemacro 25:93287f99d225 44 * @details The DS7505 low-voltage (1.7V to 3.7V) digital thermometer
phonemacro 25:93287f99d225 45 * and thermostat provides 9-, 10-, 11-, or 12-bit digital
phonemacro 25:93287f99d225 46 * temperature readings over a -55°C to +125°C range with
phonemacro 25:93287f99d225 47 * ±0.5°C accuracy over a -0°C to +70°C range. The 9-bit resolution mode
phonemacro 25:93287f99d225 48 * is software compatible with the LM75.
phonemacro 25:93287f99d225 49 * Available in 8-pin SO (1.27 mm pitch) or uMAX (0.65 mm pitch) packages.
phonemacro 25:93287f99d225 50 * Operating temperature: -55°C to +125°C (-67°F to +257°).
phonemacro 25:93287f99d225 51 * VDD: 1.7V to 3.7V.
phonemacro 25:93287f99d225 52 *
phonemacro 25:93287f99d225 53 * @code
phonemacro 25:93287f99d225 54 * #include "mbed.h"
phonemacro 25:93287f99d225 55 * #include "max32630fthr.h"
phonemacro 25:93287f99d225 56 * #include "ds7505.h"
phonemacro 25:93287f99d225 57 * #include "USBSerial.h"
phonemacro 25:93287f99d225 58 * MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
phonemacro 25:93287f99d225 59 * I2C i2cBus(P3_4, P3_5);
phonemacro 25:93287f99d225 60 * int main()
phonemacro 25:93287f99d225 61 * {
phonemacro 25:93287f99d225 62 * float temperature;
phonemacro 25:93287f99d225 63 * DigitalOut rLED(LED1, LED_OFF);
phonemacro 25:93287f99d225 64 * DigitalOut gLED(LED2, LED_OFF);
phonemacro 25:93287f99d225 65 * DigitalOut bLED(LED3, LED_OFF);
phonemacro 25:93287f99d225 66 * gLED = LED_ON;
phonemacro 25:93287f99d225 67 * DS7505 temp_sensor(i2cBus, DS7505_I2C_SLAVE_ADR_00);
phonemacro 25:93287f99d225 68 * i2cBus.frequency(400000);
phonemacro 25:93287f99d225 69 * temperature =
phonemacro 25:93287f99d225 70 * temp_sensor.read_reg_as_temperature(DS7505_REG_TEMPERATURE);
phonemacro 25:93287f99d225 71 * printf("Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n",
phonemacro 25:93287f99d225 72 * temperature, temp_sensor.celsius_to_fahrenheit(temperature));
phonemacro 25:93287f99d225 73 * }
phonemacro 25:93287f99d225 74 * @endcode
phonemacro 25:93287f99d225 75 */
phonemacro 25:93287f99d225 76
phonemacro 25:93287f99d225 77 class DS7505
phonemacro 25:93287f99d225 78 {
phonemacro 25:93287f99d225 79 public:
phonemacro 25:93287f99d225 80
phonemacro 25:93287f99d225 81 /**********************************************************//**
phonemacro 25:93287f99d225 82 * @brief Constructor for DS7505 Class.
phonemacro 25:93287f99d225 83 *
phonemacro 25:93287f99d225 84 * @details Allows user to use existing I2C object
phonemacro 25:93287f99d225 85 *
phonemacro 25:93287f99d225 86 * On Entry:
phonemacro 25:93287f99d225 87 * @param[in] i2c_bus - pointer to existing I2C object
phonemacro 25:93287f99d225 88 * @param[in] i2c_adrs - 7-bit slave address of DS7505
phonemacro 25:93287f99d225 89 *
phonemacro 25:93287f99d225 90 * On Exit:
phonemacro 25:93287f99d225 91 *
phonemacro 25:93287f99d225 92 * @return None
phonemacro 25:93287f99d225 93 **************************************************************/
phonemacro 25:93287f99d225 94 DS7505(I2C &i2c_bus, uint8_t slave_address);
phonemacro 25:93287f99d225 95
phonemacro 25:93287f99d225 96 /**********************************************************//**
phonemacro 25:93287f99d225 97 * @brief Default destructor for DS7505 Class.
phonemacro 25:93287f99d225 98 *
phonemacro 25:93287f99d225 99 * @details Destroys I2C object if owner
phonemacro 25:93287f99d225 100 *
phonemacro 25:93287f99d225 101 * On Entry:
phonemacro 25:93287f99d225 102 *
phonemacro 25:93287f99d225 103 * On Exit:
phonemacro 25:93287f99d225 104 *
phonemacro 25:93287f99d225 105 * @return None
phonemacro 25:93287f99d225 106 **************************************************************/
phonemacro 25:93287f99d225 107 ~DS7505();
phonemacro 25:93287f99d225 108
phonemacro 25:93287f99d225 109 /**
phonemacro 25:93287f99d225 110 * @brief Read register of device at slave address
phonemacro 25:93287f99d225 111 * @param[out] value - Read data on success
phonemacro 25:93287f99d225 112 * @return 0 on success, negative number on failure
phonemacro 25:93287f99d225 113 */
phonemacro 25:93287f99d225 114 int read_cfg_reg(uint8_t *value);
phonemacro 25:93287f99d225 115
phonemacro 25:93287f99d225 116 /**
phonemacro 25:93287f99d225 117 * @brief Read register of device at slave address
phonemacro 25:93287f99d225 118 * @param[out] value - Read data on success
phonemacro 25:93287f99d225 119 * @param reg - Register address
phonemacro 25:93287f99d225 120 * @return 0 on success, negative number on failure
phonemacro 25:93287f99d225 121 */
phonemacro 25:93287f99d225 122 int read_reg16(int16_t *value, char reg);
phonemacro 25:93287f99d225 123
phonemacro 25:93287f99d225 124 /**
phonemacro 25:93287f99d225 125 * @brief Reads the temperature registers
phonemacro 25:93287f99d225 126 * @param reg - the address of the temperature register
phonemacro 25:93287f99d225 127 * @return temperature in degrees Celsius
phonemacro 25:93287f99d225 128 */
phonemacro 25:93287f99d225 129 float read_reg_as_temperature(uint8_t reg);
phonemacro 25:93287f99d225 130
phonemacro 25:93287f99d225 131 /**
phonemacro 25:93287f99d225 132 * @brief Writes to the configuration register
phonemacro 25:93287f99d225 133 * @param cfg - configurate word
phonemacro 25:93287f99d225 134 * @return 0 on success, negative number on failure
phonemacro 25:93287f99d225 135 */
phonemacro 25:93287f99d225 136 int write_cfg_reg(uint8_t cfg);
phonemacro 25:93287f99d225 137
phonemacro 25:93287f99d225 138 /**
phonemacro 25:93287f99d225 139 * @brief Writes to the THYST register
phonemacro 25:93287f99d225 140 * @param temperature - the temperature in Celsius degrees
phonemacro 25:93287f99d225 141 * @return 0 on success, negative number on failure
phonemacro 25:93287f99d225 142 */
phonemacro 25:93287f99d225 143 int write_trip_low_thyst(float temperature);
phonemacro 25:93287f99d225 144
phonemacro 25:93287f99d225 145 /**
phonemacro 25:93287f99d225 146 * @brief Writes to the TOS register
phonemacro 25:93287f99d225 147 * @param temperature - the temperature in Celsius degrees
phonemacro 25:93287f99d225 148 * @return 0 on success, negative number on failure
phonemacro 25:93287f99d225 149 */
phonemacro 25:93287f99d225 150 int write_trip_high_tos(float temperature);
phonemacro 25:93287f99d225 151
phonemacro 25:93287f99d225 152 /**
phonemacro 25:93287f99d225 153 * @brief Converts Celsius degrees to Fahrenheit
phonemacro 25:93287f99d225 154 * @param temp_c - the temperature in Celsius degrees
phonemacro 25:93287f99d225 155 * @return temperature in Celsius degrees
phonemacro 25:93287f99d225 156 */
phonemacro 25:93287f99d225 157 float celsius_to_fahrenheit(float temp_c);
phonemacro 25:93287f99d225 158
phonemacro 25:93287f99d225 159 protected:
phonemacro 25:93287f99d225 160 /**
phonemacro 25:93287f99d225 161 * @brief Write a value to a register
phonemacro 25:93287f99d225 162 * @param value - value to write to the register
phonemacro 25:93287f99d225 163 * @param reg - register address
phonemacro 25:93287f99d225 164 * @return 0 on success, negative number on failure
phonemacro 25:93287f99d225 165 */
phonemacro 25:93287f99d225 166 int write_reg16(int16_t value, char reg);
phonemacro 25:93287f99d225 167
phonemacro 25:93287f99d225 168 private:
phonemacro 25:93287f99d225 169 /** @var m_i2c
phonemacro 25:93287f99d225 170 * @brief I2C object
phonemacro 25:93287f99d225 171 */
phonemacro 25:93287f99d225 172 I2C &m_i2c;
phonemacro 25:93287f99d225 173 /** @var m_write_address, m_read_address
phonemacro 25:93287f99d225 174 * @brief I2C address
phonemacro 25:93287f99d225 175 */
phonemacro 25:93287f99d225 176 uint8_t m_write_address, m_read_address;
phonemacro 25:93287f99d225 177
phonemacro 25:93287f99d225 178 };
phonemacro 25:93287f99d225 179
phonemacro 25:93287f99d225 180 #endif/* DS7505_CPP_H */