Library for the DS1721, 2-Wire Digital Thermometer and Thermostat from Dallas Semiconductor (Maxim Integrated)

Committer:
chaegle
Date:
Fri May 02 17:08:05 2014 +0000
Revision:
3:3c182751e655
Parent:
2:22dbeccb82be
Updated class constructors so as to set I2C bus frequency to 400KHz, in order to provide compatibility with FRDM-K64F platform.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chaegle 2:22dbeccb82be 1 /*
chaegle 2:22dbeccb82be 2 * @file DS1721.h
chaegle 2:22dbeccb82be 3 * @author Cameron Haegle
chaegle 2:22dbeccb82be 4 *
chaegle 2:22dbeccb82be 5 * @section LICENSE
chaegle 2:22dbeccb82be 6 *
chaegle 2:22dbeccb82be 7 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
chaegle 2:22dbeccb82be 8 * and associated documentation files (the "Software"), to deal in the Software without restriction,
chaegle 2:22dbeccb82be 9 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
chaegle 2:22dbeccb82be 10 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
chaegle 2:22dbeccb82be 11 * furnished to do so, subject to the following conditions:
chaegle 2:22dbeccb82be 12 *
chaegle 2:22dbeccb82be 13 * The above copyright notice and this permission notice shall be included in all copies or
chaegle 2:22dbeccb82be 14 * substantial portions of the Software.
chaegle 2:22dbeccb82be 15 *
chaegle 2:22dbeccb82be 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
chaegle 2:22dbeccb82be 17 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
chaegle 2:22dbeccb82be 18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
chaegle 2:22dbeccb82be 19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
chaegle 2:22dbeccb82be 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
chaegle 2:22dbeccb82be 21 *
chaegle 2:22dbeccb82be 22 * @section DESCRIPTION
chaegle 2:22dbeccb82be 23 *
chaegle 2:22dbeccb82be 24 * Library for the DS1721, 2-Wire Digital Thermometer and Thermostat, from Maxim Semiconductor
chaegle 2:22dbeccb82be 25 * (www.maiximintegrated.com).
chaegle 2:22dbeccb82be 26 *
chaegle 2:22dbeccb82be 27 * @section LIMITATIONS
chaegle 1:4fd830a97574 28 *
chaegle 2:22dbeccb82be 29 * This library was not written with for use with the mbed-RTOS, as the I2C read/write calls are
chaegle 2:22dbeccb82be 30 * not thread safe.
chaegle 2:22dbeccb82be 31 *
chaegle 2:22dbeccb82be 32 * ChangeLog
chaegle 2:22dbeccb82be 33 * 07/30/2013
chaegle 2:22dbeccb82be 34 * -
chaegle 2:22dbeccb82be 35 */
chaegle 1:4fd830a97574 36
chaegle 1:4fd830a97574 37 #ifndef DS1721_H
chaegle 1:4fd830a97574 38 #define DS1721_H
chaegle 1:4fd830a97574 39
chaegle 1:4fd830a97574 40 #include "mbed.h"
chaegle 1:4fd830a97574 41
chaegle 1:4fd830a97574 42 // default DS1721 I2C address
chaegle 1:4fd830a97574 43 #define DS1721_ADDR 0x48
chaegle 1:4fd830a97574 44
chaegle 1:4fd830a97574 45 // DS1721 commands
chaegle 1:4fd830a97574 46 #define CMD_START_CONVT 0x51
chaegle 1:4fd830a97574 47 #define CMD_STOP_CONVT 0x22
chaegle 1:4fd830a97574 48 #define CMD_READ_TEMP 0xAA
chaegle 1:4fd830a97574 49 #define CMD_ACCESS_CFG 0xAC
chaegle 1:4fd830a97574 50 #define CMD_ACCESS_TH 0xA1
chaegle 1:4fd830a97574 51 #define CMD_ACCESS_TL 0xA2
chaegle 1:4fd830a97574 52
chaegle 1:4fd830a97574 53 // thermometer config register values
chaegle 1:4fd830a97574 54 #define CONV_FOREVER 0x00
chaegle 1:4fd830a97574 55 #define CONV_ONE_SHOT 0x01
chaegle 1:4fd830a97574 56 #define POLARITY_ACTIVE_LOW 0x00
chaegle 1:4fd830a97574 57 #define POLARITY_ACTIVE_HIGH 0x02
chaegle 1:4fd830a97574 58 #define RES_9_BIT 0x00 // 0000
chaegle 1:4fd830a97574 59 #define RES_10_BIT 0x04 // 0100
chaegle 1:4fd830a97574 60 #define RES_11_BIT 0x08 // 1000
chaegle 1:4fd830a97574 61 #define RES_12_BIT 0x0C // 1100
chaegle 1:4fd830a97574 62
chaegle 1:4fd830a97574 63 // DS1721 operating modes (HEAT = active low, Cool = active high)
chaegle 1:4fd830a97574 64 #define MODE_HEAT POLARITY_ACTIVE_LOW
chaegle 1:4fd830a97574 65 #define MODE_COOL POLARITY_ACTIVE_HIGH
chaegle 1:4fd830a97574 66
chaegle 1:4fd830a97574 67
chaegle 1:4fd830a97574 68 /**
chaegle 1:4fd830a97574 69 * !Library for the DS1721 temperature sensor.
chaegle 1:4fd830a97574 70 * The DS1721 is an I2C digital temperature sensor, with a range of -55C to +125C and a 0.125C resolution.
chaegle 1:4fd830a97574 71 *
chaegle 1:4fd830a97574 72 * @code
chaegle 1:4fd830a97574 73 * #include "mbed.h"
chaegle 1:4fd830a97574 74 * #include "DS1721.h"
chaegle 1:4fd830a97574 75 *
chaegle 1:4fd830a97574 76 * #define DS1721_I2C_ADDRESS (0x48<<1)
chaegle 1:4fd830a97574 77 *
chaegle 1:4fd830a97574 78 * int main(void) {
chaegle 2:22dbeccb82be 79 * I2C m_i2c(PTC9, PTC8);
chaegle 2:22dbeccb82be 80 * DS1721 thermo(m_i2c, DS1721_I2C_ADDRESS);
chaegle 1:4fd830a97574 81 *
chaegle 1:4fd830a97574 82 * // initialize the temperature sensor
chaegle 1:4fd830a97574 83 * thermo.startConversion();
chaegle 2:22dbeccb82be 84 * thermo.setLowSp(25);
chaegle 2:22dbeccb82be 85 * thermo.setHighSp(27);
chaegle 1:4fd830a97574 86 * thermo.setPolarity(POLARITY_ACTIVE_HIGH);
chaegle 1:4fd830a97574 87 *
chaegle 1:4fd830a97574 88 * while (true) {
chaegle 1:4fd830a97574 89 * printf("Temp: %d\r\n", thermo.getTemp());
chaegle 1:4fd830a97574 90 * }
chaegle 1:4fd830a97574 91 * }
chaegle 1:4fd830a97574 92 * @endcode
chaegle 1:4fd830a97574 93 **/
chaegle 1:4fd830a97574 94 class DS1721
chaegle 1:4fd830a97574 95 {
chaegle 1:4fd830a97574 96 public:
chaegle 1:4fd830a97574 97 /**
chaegle 1:4fd830a97574 98 * DS1721 constructor
chaegle 1:4fd830a97574 99 *
chaegle 2:22dbeccb82be 100 * @param &i2c pointer to i2c object
chaegle 2:22dbeccb82be 101 * @param addr addr of the I2C peripheral default = (DS1721_ADDR<<1)
chaegle 1:4fd830a97574 102 **/
chaegle 2:22dbeccb82be 103 DS1721(I2C &i2c, int addr = (DS1721_ADDR<<1));
chaegle 1:4fd830a97574 104
chaegle 1:4fd830a97574 105 /**
chaegle 1:4fd830a97574 106 * DS1721 constructor
chaegle 1:4fd830a97574 107 *
chaegle 2:22dbeccb82be 108 * @param &i2c pointer to I2C object
chaegle 1:4fd830a97574 109 * @param resolution readout resolution of the theremometer (9, 10, 11, 12 bit)
chaegle 1:4fd830a97574 110 * @param polarity the state on which the thermostat output is enabled
chaegle 1:4fd830a97574 111 * @param mode the temperature conversion mode (single shot or continuous)
chaegle 2:22dbeccb82be 112 * @param addr addr of the I2C peripheral default = (DS1721_ADDR<<1)
chaegle 1:4fd830a97574 113 **/
chaegle 2:22dbeccb82be 114 DS1721(I2C &i2c,int resolution, int polarity, int mode, int addr = (DS1721_ADDR<<1));
chaegle 1:4fd830a97574 115
chaegle 1:4fd830a97574 116 /*!
chaegle 1:4fd830a97574 117 * DS1721 destructor
chaegle 1:4fd830a97574 118 */
chaegle 1:4fd830a97574 119 ~DS1721();
chaegle 1:4fd830a97574 120
chaegle 1:4fd830a97574 121 /**
chaegle 1:4fd830a97574 122 * Reads the temperature from the DS1721 and converts it to a useable value.
chaegle 1:4fd830a97574 123 *
chaegle 2:22dbeccb82be 124 * @returns the current temperature, as a float
chaegle 1:4fd830a97574 125 **/
chaegle 2:22dbeccb82be 126 float getTemp(void);
chaegle 1:4fd830a97574 127
chaegle 1:4fd830a97574 128 /**
chaegle 1:4fd830a97574 129 * Sets the temperature polarity (POL) bit of the sensor. This bit determines
chaegle 1:4fd830a97574 130 * upon which set point the theremostat output is active.
chaegle 1:4fd830a97574 131 *
chaegle 1:4fd830a97574 132 * @param int the new polarity value
chaegle 1:4fd830a97574 133 * @returns the result of the function (0 - Failure, 1 - Success)
chaegle 1:4fd830a97574 134 **/
chaegle 1:4fd830a97574 135 int setPolarity(int polarity);
chaegle 1:4fd830a97574 136
chaegle 1:4fd830a97574 137 /**
chaegle 1:4fd830a97574 138 * Gets the temperature polarity (POL) bit of the sensor. This bit determines
chaegle 2:22dbeccb82be 139 * upon which set point the theremostat output (Tout) is active.
chaegle 1:4fd830a97574 140 *
chaegle 1:4fd830a97574 141 * @returns the value of the POL bit (0 - active low, 1 - active high)
chaegle 1:4fd830a97574 142 **/
chaegle 1:4fd830a97574 143 int getPolarity(void);
chaegle 1:4fd830a97574 144
chaegle 1:4fd830a97574 145 /**
chaegle 1:4fd830a97574 146 * Initiates a temperature conversion. If the DS1721 is configured for single shot mode
chaegle 1:4fd830a97574 147 * this function must be called prior to reading the temperature (re: getTemp())
chaegle 1:4fd830a97574 148 *
chaegle 1:4fd830a97574 149 * @returns the result of the function (0 - Failure, 1 - Success)
chaegle 1:4fd830a97574 150 **/
chaegle 1:4fd830a97574 151 int startConversion(void);
chaegle 1:4fd830a97574 152
chaegle 1:4fd830a97574 153 /**
chaegle 1:4fd830a97574 154 * Stops a temperature conversion.
chaegle 1:4fd830a97574 155 *
chaegle 1:4fd830a97574 156 * @returns the result of the function (0 - Failure, 1 - Success)
chaegle 1:4fd830a97574 157 **/
chaegle 1:4fd830a97574 158 int stopConversion(void);
chaegle 1:4fd830a97574 159
chaegle 1:4fd830a97574 160 /**
chaegle 1:4fd830a97574 161 * Sets the configuration register of the DS1721.
chaegle 1:4fd830a97574 162 *
chaegle 1:4fd830a97574 163 * @param int the new configuration value (resolution | polarity | mode)
chaegle 1:4fd830a97574 164 * @returns the result of the function (0 - Failure, 1 - Success)
chaegle 1:4fd830a97574 165 **/
chaegle 1:4fd830a97574 166 int setConfig(int config);
chaegle 1:4fd830a97574 167
chaegle 1:4fd830a97574 168 /**
chaegle 1:4fd830a97574 169 * Retrieves the configuration register of the DS1721.
chaegle 1:4fd830a97574 170 *
chaegle 1:4fd830a97574 171 * @returns
chaegle 1:4fd830a97574 172 **/
chaegle 1:4fd830a97574 173 int getConfig(int* config);
chaegle 1:4fd830a97574 174
chaegle 1:4fd830a97574 175 /**
chaegle 1:4fd830a97574 176 * Retrieves the configured low temperature set point value.
chaegle 1:4fd830a97574 177 *
chaegle 1:4fd830a97574 178 * @returns the current low temperature set point value (degrees C)
chaegle 1:4fd830a97574 179 **/
chaegle 2:22dbeccb82be 180 float getLowSp(void);
chaegle 1:4fd830a97574 181
chaegle 1:4fd830a97574 182 /**
chaegle 1:4fd830a97574 183 * Sets the low temperature set point value.
chaegle 1:4fd830a97574 184 *
chaegle 1:4fd830a97574 185 * @param int the new low set point temperature (degrees C)
chaegle 1:4fd830a97574 186 * @returns the result of the function (0 - Failure, 1 - Success)
chaegle 1:4fd830a97574 187 **/
chaegle 2:22dbeccb82be 188 int setLowSp(float newSp);
chaegle 1:4fd830a97574 189
chaegle 1:4fd830a97574 190 /**
chaegle 1:4fd830a97574 191 * Retrieves the configured high temperature set point value.
chaegle 1:4fd830a97574 192 *
chaegle 1:4fd830a97574 193 * @returns the current high temperature set point value (degrees C)
chaegle 1:4fd830a97574 194 **/
chaegle 2:22dbeccb82be 195 float getHighSp(void);
chaegle 1:4fd830a97574 196
chaegle 1:4fd830a97574 197 /**
chaegle 1:4fd830a97574 198 * Sets the high temperature set point value.
chaegle 1:4fd830a97574 199 *
chaegle 1:4fd830a97574 200 * @param int the new high temperature set point value (degrees C)
chaegle 1:4fd830a97574 201 * @returns the result of the function (0 - Failure, 1 - Success)
chaegle 1:4fd830a97574 202 **/
chaegle 2:22dbeccb82be 203 int setHighSp(float newSp);
chaegle 2:22dbeccb82be 204
chaegle 2:22dbeccb82be 205 /*
chaegle 2:22dbeccb82be 206 * Helper function to convert degrees Celcius to Fahrenheit
chaegle 2:22dbeccb82be 207 *
chaegle 2:22dbeccb82be 208 * @param temperature in degrees Celcius
chaegle 2:22dbeccb82be 209 * @return temperature in degrees Fahrenheit
chaegle 2:22dbeccb82be 210 */
chaegle 2:22dbeccb82be 211 float temp_CtoF(float tempC);
chaegle 2:22dbeccb82be 212
chaegle 2:22dbeccb82be 213 /*
chaegle 2:22dbeccb82be 214 * Helper function to convert degrees Fahrenheit to Celcius
chaegle 2:22dbeccb82be 215 *
chaegle 2:22dbeccb82be 216 * @param temperature in degrees Fahrenheit
chaegle 2:22dbeccb82be 217 * @return temperature in degrees Celcius
chaegle 2:22dbeccb82be 218 */
chaegle 2:22dbeccb82be 219 float temp_FtoC(float tempF);
chaegle 2:22dbeccb82be 220
chaegle 1:4fd830a97574 221
chaegle 1:4fd830a97574 222 private:
chaegle 2:22dbeccb82be 223 I2C &m_i2c;
chaegle 1:4fd830a97574 224 int m_addr;
chaegle 1:4fd830a97574 225 int _resolution;
chaegle 1:4fd830a97574 226 int _polarity;
chaegle 1:4fd830a97574 227 int _mode;
chaegle 1:4fd830a97574 228 int _tl;
chaegle 1:4fd830a97574 229 int _num_read;
chaegle 1:4fd830a97574 230 int _th;
chaegle 2:22dbeccb82be 231
chaegle 2:22dbeccb82be 232 int writeData(char* data, int length);
chaegle 2:22dbeccb82be 233 int readData(char* data, int length);
chaegle 1:4fd830a97574 234 };
chaegle 1:4fd830a97574 235
chaegle 1:4fd830a97574 236 #endif
chaegle 1:4fd830a97574 237