Library for the DS1721, 2-Wire Digital Thermometer and Thermostat from Dallas Semiconductor (Maxim Integrated)
Fork of DS1721 by
DS1721.cpp@3:38d2fcae2f7c, 2014-04-29 (annotated)
- Committer:
- chaegle
- Date:
- Tue Apr 29 23:54:46 2014 +0000
- Revision:
- 3:38d2fcae2f7c
- Parent:
- 2:22dbeccb82be
Modified class constructor so as to specify an I2C bus speed of 400KHz. Not doing so prohibits this library from working properly on the FRDM-K64F platform.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chaegle | 2:22dbeccb82be | 1 | /* |
chaegle | 2:22dbeccb82be | 2 | * @file DS1721.cpp |
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 | 1:4fd830a97574 | 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 | |
chaegle | 2:22dbeccb82be | 38 | |
chaegle | 2:22dbeccb82be | 39 | #include "DS1721.h" |
chaegle | 2:22dbeccb82be | 40 | |
chaegle | 1:4fd830a97574 | 41 | |
chaegle | 2:22dbeccb82be | 42 | DS1721::DS1721(I2C &i2c, int addr) : m_i2c(i2c) , m_addr(addr) |
chaegle | 1:4fd830a97574 | 43 | { |
chaegle | 3:38d2fcae2f7c | 44 | m_i2c.frequency(400000); |
chaegle | 3:38d2fcae2f7c | 45 | _resolution = RES_9_BIT; |
chaegle | 3:38d2fcae2f7c | 46 | _polarity = POLARITY_ACTIVE_HIGH; |
chaegle | 3:38d2fcae2f7c | 47 | _mode = CONV_FOREVER; |
chaegle | 3:38d2fcae2f7c | 48 | _num_read = 1; |
chaegle | 1:4fd830a97574 | 49 | |
chaegle | 3:38d2fcae2f7c | 50 | //setConfig(_resolution | _polarity | _mode); |
chaegle | 1:4fd830a97574 | 51 | } |
chaegle | 1:4fd830a97574 | 52 | |
chaegle | 2:22dbeccb82be | 53 | DS1721::DS1721(I2C &i2c, int resolution, int polarity, int mode, int addr) |
chaegle | 2:22dbeccb82be | 54 | : m_i2c(i2c) , _resolution(resolution) , _polarity(polarity) , _mode(mode) , m_addr(addr) |
chaegle | 3:38d2fcae2f7c | 55 | { |
chaegle | 3:38d2fcae2f7c | 56 | m_i2c.frequency(400000); |
chaegle | 1:4fd830a97574 | 57 | _num_read = 1; |
chaegle | 2:22dbeccb82be | 58 | //setConfig(_resolution | _polarity | _mode); |
chaegle | 1:4fd830a97574 | 59 | } |
chaegle | 1:4fd830a97574 | 60 | |
chaegle | 1:4fd830a97574 | 61 | DS1721::~DS1721(void) |
chaegle | 1:4fd830a97574 | 62 | { |
chaegle | 1:4fd830a97574 | 63 | } |
chaegle | 1:4fd830a97574 | 64 | |
chaegle | 1:4fd830a97574 | 65 | int DS1721::setConfig(int config) |
chaegle | 1:4fd830a97574 | 66 | { |
chaegle | 1:4fd830a97574 | 67 | char cmd[2]; |
chaegle | 1:4fd830a97574 | 68 | cmd[0] = CMD_ACCESS_CFG; |
chaegle | 1:4fd830a97574 | 69 | cmd[1] = (config & 0xFF); |
chaegle | 2:22dbeccb82be | 70 | |
chaegle | 2:22dbeccb82be | 71 | if(writeData(cmd, 2) != 0) |
chaegle | 1:4fd830a97574 | 72 | { |
chaegle | 2:22dbeccb82be | 73 | return 1; |
chaegle | 1:4fd830a97574 | 74 | } |
chaegle | 2:22dbeccb82be | 75 | return 0; |
chaegle | 1:4fd830a97574 | 76 | } |
chaegle | 1:4fd830a97574 | 77 | |
chaegle | 1:4fd830a97574 | 78 | int DS1721::getConfig(int* config) |
chaegle | 1:4fd830a97574 | 79 | { |
chaegle | 1:4fd830a97574 | 80 | char cmd[2]; |
chaegle | 1:4fd830a97574 | 81 | |
chaegle | 1:4fd830a97574 | 82 | cmd[0] = CMD_ACCESS_CFG; // 0xAC |
chaegle | 2:22dbeccb82be | 83 | |
chaegle | 2:22dbeccb82be | 84 | if(writeData(cmd, 1) != 0) |
chaegle | 1:4fd830a97574 | 85 | { |
chaegle | 2:22dbeccb82be | 86 | return 1; |
chaegle | 1:4fd830a97574 | 87 | } |
chaegle | 2:22dbeccb82be | 88 | |
chaegle | 2:22dbeccb82be | 89 | if(readData(cmd, 1) != 0) |
chaegle | 1:4fd830a97574 | 90 | { |
chaegle | 2:22dbeccb82be | 91 | return 1; |
chaegle | 1:4fd830a97574 | 92 | } |
chaegle | 1:4fd830a97574 | 93 | *config = cmd[0]; |
chaegle | 2:22dbeccb82be | 94 | return 0; |
chaegle | 1:4fd830a97574 | 95 | } |
chaegle | 1:4fd830a97574 | 96 | |
chaegle | 1:4fd830a97574 | 97 | |
chaegle | 2:22dbeccb82be | 98 | float DS1721::getTemp(void) |
chaegle | 1:4fd830a97574 | 99 | { |
chaegle | 1:4fd830a97574 | 100 | char cmd[3]; |
chaegle | 1:4fd830a97574 | 101 | |
chaegle | 1:4fd830a97574 | 102 | cmd[0] = CMD_READ_TEMP; |
chaegle | 1:4fd830a97574 | 103 | |
chaegle | 1:4fd830a97574 | 104 | // if this is the first read, since power up, perform the read twice |
chaegle | 1:4fd830a97574 | 105 | // this eliminate an erroneous initial read. |
chaegle | 1:4fd830a97574 | 106 | do |
chaegle | 1:4fd830a97574 | 107 | { |
chaegle | 2:22dbeccb82be | 108 | if(writeData(cmd, 1) != 0) |
chaegle | 1:4fd830a97574 | 109 | { |
chaegle | 2:22dbeccb82be | 110 | return 1; |
chaegle | 1:4fd830a97574 | 111 | } |
chaegle | 2:22dbeccb82be | 112 | |
chaegle | 1:4fd830a97574 | 113 | // read 1 byte, ignoring fractional portion of temperature |
chaegle | 2:22dbeccb82be | 114 | if(readData(cmd, 2) != 0) |
chaegle | 1:4fd830a97574 | 115 | { |
chaegle | 2:22dbeccb82be | 116 | return 1; |
chaegle | 1:4fd830a97574 | 117 | } |
chaegle | 1:4fd830a97574 | 118 | _num_read--; |
chaegle | 1:4fd830a97574 | 119 | }while(_num_read); |
chaegle | 1:4fd830a97574 | 120 | |
chaegle | 1:4fd830a97574 | 121 | _num_read = 1; |
chaegle | 1:4fd830a97574 | 122 | |
chaegle | 2:22dbeccb82be | 123 | return ((float)(cmd[1] + (cmd[0] << 8))/256); |
chaegle | 1:4fd830a97574 | 124 | } |
chaegle | 1:4fd830a97574 | 125 | |
chaegle | 1:4fd830a97574 | 126 | int DS1721::setPolarity(int polarity) |
chaegle | 1:4fd830a97574 | 127 | { |
chaegle | 1:4fd830a97574 | 128 | _polarity = polarity; |
chaegle | 2:22dbeccb82be | 129 | return setConfig(_resolution | _polarity | _mode); |
chaegle | 1:4fd830a97574 | 130 | } |
chaegle | 1:4fd830a97574 | 131 | |
chaegle | 1:4fd830a97574 | 132 | int DS1721::getPolarity(void) |
chaegle | 1:4fd830a97574 | 133 | { |
chaegle | 2:22dbeccb82be | 134 | int config = 0; |
chaegle | 1:4fd830a97574 | 135 | getConfig(&config); |
chaegle | 1:4fd830a97574 | 136 | return ((config & (1<<1)) ? 1: 0); |
chaegle | 1:4fd830a97574 | 137 | } |
chaegle | 1:4fd830a97574 | 138 | |
chaegle | 1:4fd830a97574 | 139 | int DS1721::startConversion(void) |
chaegle | 1:4fd830a97574 | 140 | { |
chaegle | 1:4fd830a97574 | 141 | char cmd[1]; |
chaegle | 1:4fd830a97574 | 142 | |
chaegle | 1:4fd830a97574 | 143 | cmd[0] = CMD_START_CONVT; // 0x51 |
chaegle | 1:4fd830a97574 | 144 | |
chaegle | 2:22dbeccb82be | 145 | if(writeData(cmd, 1) != 0) |
chaegle | 1:4fd830a97574 | 146 | { |
chaegle | 1:4fd830a97574 | 147 | return 0; |
chaegle | 1:4fd830a97574 | 148 | } |
chaegle | 1:4fd830a97574 | 149 | return 1; |
chaegle | 1:4fd830a97574 | 150 | } |
chaegle | 1:4fd830a97574 | 151 | |
chaegle | 1:4fd830a97574 | 152 | int DS1721::stopConversion(void) |
chaegle | 1:4fd830a97574 | 153 | { |
chaegle | 1:4fd830a97574 | 154 | char cmd[1]; |
chaegle | 1:4fd830a97574 | 155 | |
chaegle | 1:4fd830a97574 | 156 | cmd[0] = CMD_STOP_CONVT; // 0x51 |
chaegle | 1:4fd830a97574 | 157 | |
chaegle | 2:22dbeccb82be | 158 | if(writeData(cmd, 1) != 0) |
chaegle | 1:4fd830a97574 | 159 | { |
chaegle | 1:4fd830a97574 | 160 | return 0; |
chaegle | 1:4fd830a97574 | 161 | } |
chaegle | 1:4fd830a97574 | 162 | return 1; |
chaegle | 1:4fd830a97574 | 163 | } |
chaegle | 1:4fd830a97574 | 164 | |
chaegle | 2:22dbeccb82be | 165 | float DS1721::getLowSp(void) |
chaegle | 1:4fd830a97574 | 166 | { |
chaegle | 1:4fd830a97574 | 167 | char cmd[4]; |
chaegle | 1:4fd830a97574 | 168 | |
chaegle | 1:4fd830a97574 | 169 | cmd[0] = CMD_ACCESS_TL; |
chaegle | 2:22dbeccb82be | 170 | |
chaegle | 2:22dbeccb82be | 171 | if(writeData(cmd, 1) != 0) |
chaegle | 1:4fd830a97574 | 172 | { |
chaegle | 1:4fd830a97574 | 173 | //return FALSE; |
chaegle | 1:4fd830a97574 | 174 | } |
chaegle | 1:4fd830a97574 | 175 | |
chaegle | 1:4fd830a97574 | 176 | // read back TL msb & lsb bytes |
chaegle | 2:22dbeccb82be | 177 | if(readData(cmd, 2) != 0) |
chaegle | 1:4fd830a97574 | 178 | { |
chaegle | 1:4fd830a97574 | 179 | return 0; |
chaegle | 1:4fd830a97574 | 180 | } |
chaegle | 2:22dbeccb82be | 181 | |
chaegle | 2:22dbeccb82be | 182 | _tl = ((cmd[0] << 8) + cmd[1]); |
chaegle | 2:22dbeccb82be | 183 | |
chaegle | 2:22dbeccb82be | 184 | return ((float)_tl/256); |
chaegle | 1:4fd830a97574 | 185 | } |
chaegle | 1:4fd830a97574 | 186 | |
chaegle | 2:22dbeccb82be | 187 | int DS1721::setLowSp(float newSp) |
chaegle | 1:4fd830a97574 | 188 | { |
chaegle | 1:4fd830a97574 | 189 | char cmd[3]; |
chaegle | 1:4fd830a97574 | 190 | |
chaegle | 2:22dbeccb82be | 191 | _tl = (newSp*256); |
chaegle | 1:4fd830a97574 | 192 | |
chaegle | 1:4fd830a97574 | 193 | cmd[0] = CMD_ACCESS_TL; |
chaegle | 2:22dbeccb82be | 194 | cmd[1] = (char)(_tl >> 8); // temp MSB |
chaegle | 2:22dbeccb82be | 195 | cmd[2] = (char)(_tl & 0x00FF); // temp LSB (+/-0.5) |
chaegle | 2:22dbeccb82be | 196 | |
chaegle | 2:22dbeccb82be | 197 | if(writeData(cmd, 3) != 0) |
chaegle | 1:4fd830a97574 | 198 | { |
chaegle | 1:4fd830a97574 | 199 | return 0; |
chaegle | 1:4fd830a97574 | 200 | } |
chaegle | 1:4fd830a97574 | 201 | return 1; |
chaegle | 1:4fd830a97574 | 202 | } |
chaegle | 1:4fd830a97574 | 203 | |
chaegle | 2:22dbeccb82be | 204 | float DS1721::getHighSp(void) |
chaegle | 1:4fd830a97574 | 205 | { |
chaegle | 1:4fd830a97574 | 206 | char cmd[3]; |
chaegle | 1:4fd830a97574 | 207 | |
chaegle | 1:4fd830a97574 | 208 | cmd[0] = CMD_ACCESS_TH; |
chaegle | 2:22dbeccb82be | 209 | |
chaegle | 2:22dbeccb82be | 210 | if(writeData(cmd, 1) != 0) |
chaegle | 1:4fd830a97574 | 211 | { |
chaegle | 1:4fd830a97574 | 212 | //return 0; |
chaegle | 1:4fd830a97574 | 213 | } |
chaegle | 1:4fd830a97574 | 214 | // read back TL msb & lsb bytes |
chaegle | 2:22dbeccb82be | 215 | if(readData(cmd, 2) != 0) |
chaegle | 1:4fd830a97574 | 216 | { |
chaegle | 1:4fd830a97574 | 217 | //return 0; |
chaegle | 1:4fd830a97574 | 218 | } |
chaegle | 2:22dbeccb82be | 219 | |
chaegle | 2:22dbeccb82be | 220 | _th = ((cmd[0] << 8) + cmd[1]); |
chaegle | 2:22dbeccb82be | 221 | |
chaegle | 2:22dbeccb82be | 222 | return ((float)_th/256); |
chaegle | 1:4fd830a97574 | 223 | } |
chaegle | 1:4fd830a97574 | 224 | |
chaegle | 2:22dbeccb82be | 225 | int DS1721::setHighSp(float newSp) |
chaegle | 1:4fd830a97574 | 226 | { |
chaegle | 1:4fd830a97574 | 227 | char cmd[3]; |
chaegle | 1:4fd830a97574 | 228 | |
chaegle | 2:22dbeccb82be | 229 | _th = (newSp * 256); |
chaegle | 1:4fd830a97574 | 230 | |
chaegle | 1:4fd830a97574 | 231 | cmd[0] = CMD_ACCESS_TH; |
chaegle | 2:22dbeccb82be | 232 | cmd[1] = (char)(_th >> 8); // setpoint MSB |
chaegle | 2:22dbeccb82be | 233 | cmd[2] = (char)(_th & 0x00FF); // setpoint LSB (+/-0.5) |
chaegle | 1:4fd830a97574 | 234 | |
chaegle | 2:22dbeccb82be | 235 | if(writeData(cmd, 3) != 0) |
chaegle | 1:4fd830a97574 | 236 | { |
chaegle | 1:4fd830a97574 | 237 | return 0; |
chaegle | 1:4fd830a97574 | 238 | } |
chaegle | 1:4fd830a97574 | 239 | return 1; |
chaegle | 1:4fd830a97574 | 240 | } |
chaegle | 2:22dbeccb82be | 241 | |
chaegle | 2:22dbeccb82be | 242 | float DS1721::temp_CtoF(float tempC) |
chaegle | 2:22dbeccb82be | 243 | { |
chaegle | 2:22dbeccb82be | 244 | return ((tempC * 1.8) + 32); |
chaegle | 2:22dbeccb82be | 245 | } |
chaegle | 2:22dbeccb82be | 246 | |
chaegle | 2:22dbeccb82be | 247 | float DS1721::temp_FtoC(float tempF) |
chaegle | 2:22dbeccb82be | 248 | { |
chaegle | 2:22dbeccb82be | 249 | return ((tempF - 32)* 0.55); |
chaegle | 2:22dbeccb82be | 250 | } |
chaegle | 2:22dbeccb82be | 251 | |
chaegle | 2:22dbeccb82be | 252 | int DS1721::writeData(char* data, int length) |
chaegle | 2:22dbeccb82be | 253 | { |
chaegle | 2:22dbeccb82be | 254 | return (m_i2c.write(m_addr, data, length)); |
chaegle | 2:22dbeccb82be | 255 | } |
chaegle | 2:22dbeccb82be | 256 | |
chaegle | 2:22dbeccb82be | 257 | int DS1721::readData(char* data, int length) |
chaegle | 2:22dbeccb82be | 258 | { |
chaegle | 2:22dbeccb82be | 259 | return (m_i2c.read(m_addr, data, length)); |
chaegle | 2:22dbeccb82be | 260 | } |