For amx31820

Dependencies:   OneWire

Dependents:   MAX31820

Fork of DS1820 by Zoltan Hudak

Committer:
schnf30
Date:
Mon Apr 30 07:56:28 2018 +0000
Revision:
15:3d3ba015f6af
Parent:
14:f375b7de7856
MAX31820

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 3:a250babd0a9f 1 /*
hudakz 3:a250babd0a9f 2 * Dallas' DS1820 family temperature sensor.
hudakz 3:a250babd0a9f 3 * This library depends on the OneWire library (Dallas' 1-Wire bus protocol implementation)
hudakz 6:518950e436be 4 * available at <http://developer.mbed.org/users/hudakz/code/OneWire/>
hudakz 3:a250babd0a9f 5 *
hudakz 3:a250babd0a9f 6 * Example of use:
schnf30 14:f375b7de7856 7 *
hudakz 3:a250babd0a9f 8 * #include "DS1820.h"
hudakz 6:518950e436be 9 *
hudakz 6:518950e436be 10 * Serial serial(USBTX, USBRX);
schnf30 14:f375b7de7856 11 *
hudakz 3:a250babd0a9f 12 * int main() {
hudakz 6:518950e436be 13 * DS1820 ds1820(PA_9); // substitute PA_9 with actual mbed pin name connected to the DS1820 data pin
hudakz 3:a250babd0a9f 14 *
hudakz 3:a250babd0a9f 15 * if(ds1820.begin()) {
hudakz 3:a250babd0a9f 16 * ds1820.startConversion();
hudakz 3:a250babd0a9f 17 * wait(1.0);
hudakz 3:a250babd0a9f 18 * while(1) {
hudakz 6:518950e436be 19 * serial.printf("temp = %3.1f\r\n", ds1820.read()); // read temperature
hudakz 6:518950e436be 20 * ds1820.startConversion(); // start temperature conversion
hudakz 6:518950e436be 21 * wait(1.0); // let DS1820 complete the temperature conversion
hudakz 3:a250babd0a9f 22 * }
hudakz 3:a250babd0a9f 23 * } else
hudakz 3:a250babd0a9f 24 * serial.printf("No DS1820 sensor found!\r\n");
hudakz 3:a250babd0a9f 25 * }
hudakz 6:518950e436be 26 *
schnf30 14:f375b7de7856 27 *
schnf30 14:f375b7de7856 28 * Note: Don't forget to connect a 4.7k Ohm resistor
hudakz 6:518950e436be 29 * between the DS1820's data pin and the +3.3V pin
hudakz 6:518950e436be 30 *
hudakz 3:a250babd0a9f 31 */
schnf30 14:f375b7de7856 32
hudakz 0:433af64321d5 33 #include "DS1820.h"
hudakz 0:433af64321d5 34
schnf30 14:f375b7de7856 35 //#define DEBUG 1
hudakz 0:433af64321d5 36
hudakz 0:433af64321d5 37 #if DEBUG
hudakz 0:433af64321d5 38 extern Serial serial;
hudakz 0:433af64321d5 39 #endif
hudakz 0:433af64321d5 40
hudakz 0:433af64321d5 41 /**
hudakz 0:433af64321d5 42 * @brief Constructs a generic DS1820 sensor
hudakz 0:433af64321d5 43 * @note begin() must be called to detect and initialize the actual model
hudakz 3:a250babd0a9f 44 * @param pin: Name of data pin
hudakz 0:433af64321d5 45 * @retval
hudakz 0:433af64321d5 46 */
hudakz 0:433af64321d5 47 DS1820::DS1820(PinName pin) :
schnf30 14:f375b7de7856 48 oneWire(pin)
schnf30 14:f375b7de7856 49 {
hudakz 8:8dfdd1603e4d 50 present = false;
hudakz 8:8dfdd1603e4d 51 model_s = false;
hudakz 0:433af64321d5 52 }
hudakz 0:433af64321d5 53
hudakz 0:433af64321d5 54 /**
hudakz 0:433af64321d5 55 * @brief Constructs a specific model
hudakz 0:433af64321d5 56 * @note No need to call begin() to detect and initialize the model
hudakz 3:a250babd0a9f 57 * @param model: One character model name: 'S', 's', 'B' or 'b'
hudakz 3:a250babd0a9f 58 * pin: Name of data pin
hudakz 0:433af64321d5 59 * @retval
hudakz 0:433af64321d5 60 */
hudakz 0:433af64321d5 61 DS1820::DS1820(char model, PinName pin) :
schnf30 14:f375b7de7856 62 oneWire(pin)
schnf30 14:f375b7de7856 63 {
hudakz 0:433af64321d5 64 if((model == 'S') or (model == 's')) {
hudakz 8:8dfdd1603e4d 65 present = true;
hudakz 8:8dfdd1603e4d 66 model_s = true;
schnf30 14:f375b7de7856 67 } else if((model == 'B') or (model == 'b')) {
hudakz 8:8dfdd1603e4d 68 present = true;
hudakz 8:8dfdd1603e4d 69 model_s = false;
schnf30 14:f375b7de7856 70 } else
hudakz 8:8dfdd1603e4d 71 present = false;
hudakz 0:433af64321d5 72 }
hudakz 0:433af64321d5 73
hudakz 0:433af64321d5 74 /**
hudakz 0:433af64321d5 75 * @brief Detects and initializes the actual DS1820 model
hudakz 0:433af64321d5 76 * @note
hudakz 0:433af64321d5 77 * @param
hudakz 0:433af64321d5 78 * @retval true: if a DS1820 family sensor was detected and initialized
hudakz 0:433af64321d5 79 false: otherwise
hudakz 0:433af64321d5 80 */
schnf30 14:f375b7de7856 81 bool DS1820::begin(void)
schnf30 14:f375b7de7856 82 {
hudakz 0:433af64321d5 83 oneWire.reset_search();
hudakz 0:433af64321d5 84 wait_ms(250);
hudakz 0:433af64321d5 85 if(!oneWire.search(addr)) {
hudakz 0:433af64321d5 86 #if DEBUG
hudakz 0:433af64321d5 87 serial.printf("No addresses.\r\n");
hudakz 0:433af64321d5 88 #endif
hudakz 0:433af64321d5 89 oneWire.reset_search();
hudakz 0:433af64321d5 90 wait_ms(250);
hudakz 0:433af64321d5 91 return false;
hudakz 0:433af64321d5 92 }
hudakz 0:433af64321d5 93
hudakz 0:433af64321d5 94 #if DEBUG
hudakz 0:433af64321d5 95 serial.printf("ROM =");
hudakz 0:433af64321d5 96 for(uint8_t i = 0; i < 8; i++) {
hudakz 0:433af64321d5 97 serial.printf(" %x", addr[i]);
hudakz 0:433af64321d5 98 }
hudakz 0:433af64321d5 99 serial.printf("\r\n");
hudakz 0:433af64321d5 100 #endif
hudakz 0:433af64321d5 101
hudakz 0:433af64321d5 102 if(OneWire::crc8(addr, 7) == addr[7]) {
hudakz 8:8dfdd1603e4d 103 present = true;
hudakz 0:433af64321d5 104
hudakz 0:433af64321d5 105 // the first ROM byte indicates which chip
hudakz 0:433af64321d5 106 switch(addr[0]) {
schnf30 14:f375b7de7856 107 case 0x10:
schnf30 14:f375b7de7856 108 model_s = true;
hudakz 0:433af64321d5 109 #if DEBUG
schnf30 14:f375b7de7856 110 serial.printf("DS18S20 or old DS1820\r\n");
schnf30 14:f375b7de7856 111 #endif
schnf30 14:f375b7de7856 112 break;
hudakz 0:433af64321d5 113
schnf30 14:f375b7de7856 114 case 0x28:
schnf30 14:f375b7de7856 115 model_s = false;
hudakz 0:433af64321d5 116 #if DEBUG
schnf30 14:f375b7de7856 117 serial.printf("DS18B20\r\n");
schnf30 14:f375b7de7856 118 #endif
schnf30 14:f375b7de7856 119 break;
hudakz 0:433af64321d5 120
schnf30 14:f375b7de7856 121 case 0x22:
schnf30 14:f375b7de7856 122 model_s = false;
hudakz 0:433af64321d5 123 #if DEBUG
schnf30 14:f375b7de7856 124 serial.printf("DS1822\r\n");
schnf30 14:f375b7de7856 125 #endif
schnf30 14:f375b7de7856 126 break;
hudakz 0:433af64321d5 127
schnf30 14:f375b7de7856 128 default:
schnf30 14:f375b7de7856 129 present = false;
hudakz 0:433af64321d5 130 #if DEBUG
schnf30 14:f375b7de7856 131 serial.printf("Device doesn't belong to the DS1820 family\r\n");
schnf30 14:f375b7de7856 132 #endif
schnf30 14:f375b7de7856 133 return false;
hudakz 0:433af64321d5 134 }
hudakz 0:433af64321d5 135 return true;
schnf30 14:f375b7de7856 136 } else {
schnf30 14:f375b7de7856 137 #if DEBUG
hudakz 0:433af64321d5 138 serial.printf("Invalid CRC!\r\n");
schnf30 14:f375b7de7856 139 #endif
hudakz 0:433af64321d5 140 return false;
hudakz 0:433af64321d5 141 }
hudakz 0:433af64321d5 142 }
hudakz 0:433af64321d5 143
hudakz 0:433af64321d5 144 /**
hudakz 8:8dfdd1603e4d 145 * @brief Informs about presence of a DS1820 sensor.
hudakz 8:8dfdd1603e4d 146 * @note begin() shall be called before using this function
schnf30 14:f375b7de7856 147 * if a generic DS1820 instance was created by the user.
hudakz 8:8dfdd1603e4d 148 * No need to call begin() for a specific DS1820 instance.
hudakz 8:8dfdd1603e4d 149 * @param
hudakz 8:8dfdd1603e4d 150 * @retval true: when a DS1820 sensor is present
hudakz 8:8dfdd1603e4d 151 * false: otherwise
hudakz 8:8dfdd1603e4d 152 */
schnf30 14:f375b7de7856 153 bool DS1820::isPresent(void)
schnf30 14:f375b7de7856 154 {
hudakz 8:8dfdd1603e4d 155 return present;
hudakz 8:8dfdd1603e4d 156 }
hudakz 8:8dfdd1603e4d 157
hudakz 8:8dfdd1603e4d 158 /**
hudakz 6:518950e436be 159 * @brief Sets temperature-to-digital conversion resolution.
hudakz 4:adf4e7972d73 160 * @note The configuration register allows the user to set the resolution
hudakz 6:518950e436be 161 * of the temperature-to-digital conversion to 9, 10, 11, or 12 bits.
hudakz 6:518950e436be 162 * Defaults to 12-bit resolution for DS18B20.
hudakz 6:518950e436be 163 * DS18S20 allows only 9-bit resolution.
hudakz 6:518950e436be 164 * @param res: Resolution of the temperature-to-digital conversion in bits.
hudakz 4:adf4e7972d73 165 * @retval
hudakz 4:adf4e7972d73 166 */
schnf30 14:f375b7de7856 167 void DS1820::setResolution(uint8_t res)
schnf30 14:f375b7de7856 168 {
hudakz 4:adf4e7972d73 169 // keep resolution within limits
hudakz 4:adf4e7972d73 170 if(res > 12)
hudakz 4:adf4e7972d73 171 res = 12;
hudakz 4:adf4e7972d73 172 if(res < 9)
schnf30 14:f375b7de7856 173 res = 9;
hudakz 8:8dfdd1603e4d 174 if(model_s)
hudakz 4:adf4e7972d73 175 res = 9;
schnf30 14:f375b7de7856 176
hudakz 4:adf4e7972d73 177 oneWire.reset();
hudakz 4:adf4e7972d73 178 oneWire.skip();
hudakz 4:adf4e7972d73 179 oneWire.write(0xBE); // to read Scratchpad
hudakz 4:adf4e7972d73 180 for(uint8_t i = 0; i < 9; i++) // read Scratchpad bytes
schnf30 14:f375b7de7856 181 data[i] = oneWire.read();
hudakz 4:adf4e7972d73 182
schnf30 14:f375b7de7856 183 data[4] |= (res - 9) << 5; // update configuration byte (set resolution)
hudakz 4:adf4e7972d73 184 oneWire.reset();
hudakz 4:adf4e7972d73 185 oneWire.skip();
hudakz 4:adf4e7972d73 186 oneWire.write(0x4E); // to write into Scratchpad
hudakz 4:adf4e7972d73 187 for(uint8_t i = 2; i < 5; i++) // write three bytes (2nd, 3rd, 4th) into Scratchpad
hudakz 4:adf4e7972d73 188 oneWire.write(data[i]);
hudakz 4:adf4e7972d73 189 }
hudakz 4:adf4e7972d73 190
hudakz 4:adf4e7972d73 191 /**
hudakz 0:433af64321d5 192 * @brief Starts temperature conversion
hudakz 6:518950e436be 193 * @note The time to complete the converion depends on the selected resolution:
hudakz 6:518950e436be 194 * 9-bit resolution -> max conversion time = 93.75ms
hudakz 6:518950e436be 195 * 10-bit resolution -> max conversion time = 187.5ms
hudakz 6:518950e436be 196 * 11-bit resolution -> max conversion time = 375ms
hudakz 6:518950e436be 197 * 12-bit resolution -> max conversion time = 750ms
hudakz 0:433af64321d5 198 * @param
hudakz 0:433af64321d5 199 * @retval
hudakz 0:433af64321d5 200 */
schnf30 14:f375b7de7856 201 void DS1820::startConversion(void)
schnf30 14:f375b7de7856 202 {
hudakz 0:433af64321d5 203 if(present) {
hudakz 0:433af64321d5 204 oneWire.reset();
hudakz 0:433af64321d5 205 oneWire.skip();
hudakz 3:a250babd0a9f 206 oneWire.write(0x44); //start temperature conversion
hudakz 0:433af64321d5 207 }
hudakz 0:433af64321d5 208 }
hudakz 0:433af64321d5 209
hudakz 0:433af64321d5 210 /**
hudakz 6:518950e436be 211 * @brief Reads temperature from the chip's Scratchpad
hudakz 0:433af64321d5 212 * @note
hudakz 0:433af64321d5 213 * @param
hudakz 0:433af64321d5 214 * @retval Floating point temperature value
hudakz 0:433af64321d5 215 */
schnf30 14:f375b7de7856 216 float DS1820::read(void)
schnf30 14:f375b7de7856 217 {
hudakz 0:433af64321d5 218 if(present) {
hudakz 0:433af64321d5 219 oneWire.reset();
hudakz 0:433af64321d5 220 oneWire.skip();
hudakz 0:433af64321d5 221 oneWire.write(0xBE); // to read Scratchpad
schnf30 14:f375b7de7856 222 for(uint8_t i = 0; i < 9; i++) { // read Scratchpad bytes
schnf30 14:f375b7de7856 223 data[i] = oneWire.read();
schnf30 14:f375b7de7856 224 #if DEBUG
schnf30 14:f375b7de7856 225 serial.printf("%x ", data[i]);
schnf30 14:f375b7de7856 226 #endif
schnf30 14:f375b7de7856 227 }
schnf30 14:f375b7de7856 228 #if DEBUG
schnf30 14:f375b7de7856 229 serial.printf("\r\n");
schnf30 14:f375b7de7856 230 #endif
hudakz 6:518950e436be 231 // Convert the raw bytes to a 16-bit unsigned value
hudakz 0:433af64321d5 232 uint16_t* p_word = reinterpret_cast < uint16_t * > (&data[0]);
hudakz 0:433af64321d5 233
hudakz 0:433af64321d5 234 #if DEBUG
hudakz 0:433af64321d5 235 serial.printf("raw = %#x\r\n", *p_word);
schnf30 14:f375b7de7856 236 #endif
hudakz 0:433af64321d5 237
hudakz 8:8dfdd1603e4d 238 if(model_s) {
hudakz 6:518950e436be 239 *p_word = *p_word << 3; // 9-bit resolution
hudakz 0:433af64321d5 240 if(data[7] == 0x10) {
hudakz 0:433af64321d5 241
hudakz 6:518950e436be 242 // "count remain" gives full 12-bit resolution
hudakz 0:433af64321d5 243 *p_word = (*p_word & 0xFFF0) + 12 - data[6];
hudakz 0:433af64321d5 244 }
schnf30 14:f375b7de7856 245 } else {
hudakz 6:518950e436be 246 uint8_t cfg = (data[4] & 0x60); // default 12-bit resolution
schnf30 14:f375b7de7856 247
hudakz 4:adf4e7972d73 248 // at lower resolution, the low bits are undefined, so let's clear them
hudakz 0:433af64321d5 249 if(cfg == 0x00)
hudakz 6:518950e436be 250 *p_word = *p_word &~7; // 9-bit resolution
schnf30 14:f375b7de7856 251 else if(cfg == 0x20)
hudakz 6:518950e436be 252 *p_word = *p_word &~3; // 10-bit resolution
schnf30 14:f375b7de7856 253 else if(cfg == 0x40)
hudakz 6:518950e436be 254 *p_word = *p_word &~1; // 11-bit resolution
schnf30 14:f375b7de7856 255
hudakz 0:433af64321d5 256 }
hudakz 6:518950e436be 257
hudakz 6:518950e436be 258 // Convert the raw bytes to a 16-bit signed fixed point value :
hudakz 6:518950e436be 259 // 1 sign bit, 7 integer bits, 8 fractional bits (two’s compliment
hudakz 6:518950e436be 260 // and the LSB of the 16-bit binary number represents 1/256th of a unit).
hudakz 6:518950e436be 261 *p_word = *p_word << 4;
schnf30 14:f375b7de7856 262
hudakz 6:518950e436be 263 // Convert to floating point value
hudakz 6:518950e436be 264 return(toFloat(*p_word));
schnf30 14:f375b7de7856 265 } else
hudakz 0:433af64321d5 266 return 0;
hudakz 0:433af64321d5 267 }
hudakz 0:433af64321d5 268
hudakz 0:433af64321d5 269 /**
hudakz 13:b593a82ce790 270 * @brief Reads temperature from chip's scratchpad.
hudakz 13:b593a82ce790 271 * @note Verifies data integrity by calculating cyclic redundancy check (CRC).
hudakz 13:b593a82ce790 272 * If the calculated CRC dosn't match the one stored in chip's scratchpad register
hudakz 13:b593a82ce790 273 * the temperature variable is not updated and CRC error code is returned.
hudakz 13:b593a82ce790 274 * @param temp: The temperature variable to be updated by this routine.
hudakz 13:b593a82ce790 275 * (It's passed as reference to floating point.)
hudakz 13:b593a82ce790 276 * @retval error code:
hudakz 13:b593a82ce790 277 * 0 - no errors ('temp' contains the temperature measured)
hudakz 13:b593a82ce790 278 * 1 - sensor not present ('temp' is not updated)
hudakz 13:b593a82ce790 279 * 2 - CRC error ('temp' is not updated)
hudakz 13:b593a82ce790 280 */
schnf30 14:f375b7de7856 281 uint8_t DS1820::read(float& temp)
schnf30 14:f375b7de7856 282 {
hudakz 13:b593a82ce790 283 if(present) {
hudakz 13:b593a82ce790 284 oneWire.reset();
hudakz 13:b593a82ce790 285 oneWire.skip();
hudakz 13:b593a82ce790 286 oneWire.write(0xBE); // to read Scratchpad
hudakz 13:b593a82ce790 287 for(uint8_t i = 0; i < 9; i++) // reading scratchpad registers
hudakz 13:b593a82ce790 288 data[i] = oneWire.read();
hudakz 13:b593a82ce790 289
hudakz 13:b593a82ce790 290 if(oneWire.crc8(data, 8) != data[8]) // if calculated CRC does not match the stored one
hudakz 13:b593a82ce790 291 return 2; // return with CRC error
hudakz 13:b593a82ce790 292
hudakz 13:b593a82ce790 293 // Convert the raw bytes to a 16bit unsigned value
hudakz 13:b593a82ce790 294 uint16_t* p_word = reinterpret_cast < uint16_t * > (&data[0]);
hudakz 13:b593a82ce790 295
hudakz 13:b593a82ce790 296 #if DEBUG
hudakz 13:b593a82ce790 297 serial.printf("raw = %#x\r\n", *p_word);
hudakz 13:b593a82ce790 298 #endif
hudakz 13:b593a82ce790 299
hudakz 13:b593a82ce790 300 if(model_s) {
hudakz 13:b593a82ce790 301 *p_word = *p_word << 3; // 9 bit resolution, max conversion time = 750ms
hudakz 13:b593a82ce790 302 if(data[7] == 0x10) {
hudakz 13:b593a82ce790 303
hudakz 13:b593a82ce790 304 // "count remain" gives full 12 bit resolution
hudakz 13:b593a82ce790 305 *p_word = (*p_word & 0xFFF0) + 12 - data[6];
hudakz 13:b593a82ce790 306 }
hudakz 13:b593a82ce790 307
hudakz 13:b593a82ce790 308 // Convert the raw bytes to a 16bit signed fixed point value :
hudakz 13:b593a82ce790 309 // 1 sign bit, 7 integer bits, 8 fractional bits (two's compliment
hudakz 13:b593a82ce790 310 // and the LSB of the 16bit binary number represents 1/256th of a unit).
hudakz 13:b593a82ce790 311 *p_word = *p_word << 4;
hudakz 13:b593a82ce790 312 // Convert to floating point value
hudakz 13:b593a82ce790 313 temp = toFloat(*p_word);
hudakz 13:b593a82ce790 314 return 0; // return with no errors
schnf30 14:f375b7de7856 315 } else {
hudakz 13:b593a82ce790 316 uint8_t cfg = (data[4] & 0x60); // default 12bit resolution, max conversion time = 750ms
hudakz 13:b593a82ce790 317
hudakz 13:b593a82ce790 318 // at lower resolution, the low bits are undefined, so let's clear them
hudakz 13:b593a82ce790 319 if(cfg == 0x00)
hudakz 13:b593a82ce790 320 *p_word = *p_word &~7; // 9bit resolution, max conversion time = 93.75ms
schnf30 14:f375b7de7856 321 else if(cfg == 0x20)
hudakz 13:b593a82ce790 322 *p_word = *p_word &~3; // 10bit resolution, max conversion time = 187.5ms
schnf30 14:f375b7de7856 323 else if(cfg == 0x40)
hudakz 13:b593a82ce790 324 *p_word = *p_word &~1; // 11bit resolution, max conversion time = 375ms
hudakz 13:b593a82ce790 325
hudakz 13:b593a82ce790 326 // Convert the raw bytes to a 16bit signed fixed point value :
hudakz 13:b593a82ce790 327 // 1 sign bit, 7 integer bits, 8 fractional bits (two's compliment
hudakz 13:b593a82ce790 328 // and the LSB of the 16bit binary number represents 1/256th of a unit).
hudakz 13:b593a82ce790 329 *p_word = *p_word << 4;
hudakz 13:b593a82ce790 330 // Convert to floating point value
hudakz 13:b593a82ce790 331 temp = toFloat(*p_word);
hudakz 13:b593a82ce790 332 return 0; // return with no errors
hudakz 13:b593a82ce790 333 }
schnf30 14:f375b7de7856 334 } else
hudakz 13:b593a82ce790 335 return 1; // error, sensor is not present
hudakz 13:b593a82ce790 336 }
hudakz 13:b593a82ce790 337
hudakz 13:b593a82ce790 338 /**
hudakz 6:518950e436be 339 * @brief Converts a 16-bit signed fixed point value to floating point value
hudakz 6:518950e436be 340 * @note The 16-bit unsigned integer represnts actually
hudakz 6:518950e436be 341 * a 16-bit signed fixed point value:
hudakz 4:adf4e7972d73 342 * 1 sign bit, 7 integer bits, 8 fractional bits (two’s compliment
schnf30 14:f375b7de7856 343 * and the LSB of the 16-bit binary number represents 1/256th of a unit).
hudakz 6:518950e436be 344 * @param 16-bit unsigned integer
hudakz 4:adf4e7972d73 345 * @retval Floating point value
hudakz 0:433af64321d5 346 */
schnf30 14:f375b7de7856 347 float DS1820::toFloat(uint16_t word)
schnf30 14:f375b7de7856 348 {
hudakz 0:433af64321d5 349 if(word & 0x8000)
hudakz 0:433af64321d5 350 return (-float(uint16_t(~word + 1)) / 256.0f);
hudakz 0:433af64321d5 351 else
hudakz 0:433af64321d5 352 return (float(word) / 256.0f);
hudakz 0:433af64321d5 353 }
hudakz 0:433af64321d5 354
hudakz 8:8dfdd1603e4d 355