Team Diana

Dependencies:   OneWire

Dependents:   BOX_1

Committer:
hudakz
Date:
Sat Jan 26 21:22:07 2019 +0000
Revision:
17:9ff584b9809f
Parent:
14:b02fa18b294a
Child:
20:98c261bcb399
Updated.

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