Team Diana

Dependencies:   OneWire

Dependents:   BOX_1

Committer:
Alessio_Zaino
Date:
Mon Jun 10 12:49:37 2019 +0000
Revision:
22:44b91be5b3f6
Parent:
20:98c261bcb399
Still in developing

Who changed what in which revision?

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