watersenor_and_temp

Dependencies:   OneWire

Dependents:   IDW01M1-MQTT3 DS1820_IDW01M1 DS1820_IDW01M1_finish Dissolved_oxygen_sensor_online ... more

Fork of DS1820 by Zoltan Hudak

Committer:
e58136782000
Date:
Thu Nov 02 10:57:40 2017 +0000
Revision:
14:af21738cf2da
Parent:
8:8dfdd1603e4d
watersensor and temp

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 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 *
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 1:e4689408d617 35 #define DEBUG 0
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) :
hudakz 0:433af64321d5 48 oneWire(pin) {
hudakz 8:8dfdd1603e4d 49 present = false;
hudakz 8:8dfdd1603e4d 50 model_s = false;
hudakz 0:433af64321d5 51 }
hudakz 0:433af64321d5 52
hudakz 0:433af64321d5 53 /**
hudakz 0:433af64321d5 54 * @brief Constructs a specific model
hudakz 0:433af64321d5 55 * @note No need to call begin() to detect and initialize the model
hudakz 3:a250babd0a9f 56 * @param model: One character model name: 'S', 's', 'B' or 'b'
hudakz 3:a250babd0a9f 57 * pin: Name of data pin
hudakz 0:433af64321d5 58 * @retval
hudakz 0:433af64321d5 59 */
hudakz 0:433af64321d5 60 DS1820::DS1820(char model, PinName pin) :
hudakz 0:433af64321d5 61 oneWire(pin) {
hudakz 0:433af64321d5 62 if((model == 'S') or (model == 's')) {
hudakz 8:8dfdd1603e4d 63 present = true;
hudakz 8:8dfdd1603e4d 64 model_s = true;
hudakz 0:433af64321d5 65 }
hudakz 0:433af64321d5 66 else if((model == 'B') or (model == 'b')) {
hudakz 8:8dfdd1603e4d 67 present = true;
hudakz 8:8dfdd1603e4d 68 model_s = false;
hudakz 0:433af64321d5 69 }
hudakz 0:433af64321d5 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 */
hudakz 0:433af64321d5 81 bool DS1820::begin(void) {
hudakz 0:433af64321d5 82 oneWire.reset_search();
e58136782000 14:af21738cf2da 83 wait_ms(100);
hudakz 0:433af64321d5 84 if(!oneWire.search(addr)) {
hudakz 0:433af64321d5 85 #if DEBUG
hudakz 0:433af64321d5 86 serial.printf("No addresses.\r\n");
hudakz 0:433af64321d5 87 #endif
hudakz 0:433af64321d5 88 oneWire.reset_search();
e58136782000 14:af21738cf2da 89 wait_ms(100);
hudakz 0:433af64321d5 90 return false;
hudakz 0:433af64321d5 91 }
hudakz 0:433af64321d5 92
hudakz 0:433af64321d5 93 #if DEBUG
hudakz 0:433af64321d5 94 serial.printf("ROM =");
hudakz 0:433af64321d5 95 for(uint8_t i = 0; i < 8; i++) {
hudakz 0:433af64321d5 96 serial.printf(" %x", addr[i]);
hudakz 0:433af64321d5 97 }
hudakz 0:433af64321d5 98 serial.printf("\r\n");
hudakz 0:433af64321d5 99 #endif
hudakz 0:433af64321d5 100
hudakz 0:433af64321d5 101 if(OneWire::crc8(addr, 7) == addr[7]) {
hudakz 8:8dfdd1603e4d 102 present = true;
hudakz 0:433af64321d5 103
hudakz 0:433af64321d5 104 // the first ROM byte indicates which chip
hudakz 0:433af64321d5 105 switch(addr[0]) {
hudakz 0:433af64321d5 106 case 0x10:
hudakz 8:8dfdd1603e4d 107 model_s = true;
hudakz 0:433af64321d5 108 #if DEBUG
hudakz 0:433af64321d5 109 serial.printf("DS18S20 or old DS1820\r\n");
hudakz 0:433af64321d5 110 #endif
hudakz 0:433af64321d5 111 break;
hudakz 0:433af64321d5 112
hudakz 0:433af64321d5 113 case 0x28:
hudakz 8:8dfdd1603e4d 114 model_s = false;
hudakz 0:433af64321d5 115 #if DEBUG
hudakz 0:433af64321d5 116 serial.printf("DS18B20\r\n");
hudakz 0:433af64321d5 117 #endif
hudakz 0:433af64321d5 118 break;
hudakz 0:433af64321d5 119
hudakz 0:433af64321d5 120 case 0x22:
hudakz 8:8dfdd1603e4d 121 model_s = false;
hudakz 0:433af64321d5 122 #if DEBUG
hudakz 0:433af64321d5 123 serial.printf("DS1822\r\n");
hudakz 0:433af64321d5 124 #endif
hudakz 0:433af64321d5 125 break;
hudakz 0:433af64321d5 126
hudakz 0:433af64321d5 127 default:
hudakz 8:8dfdd1603e4d 128 present = false;
hudakz 0:433af64321d5 129 #if DEBUG
hudakz 0:433af64321d5 130 serial.printf("Device doesn't belong to the DS1820 family\r\n");
hudakz 2:b7ad1da7331a 131 #endif
hudakz 0:433af64321d5 132 return false;
hudakz 0:433af64321d5 133 }
hudakz 0:433af64321d5 134 return true;
hudakz 0:433af64321d5 135 }
hudakz 2:b7ad1da7331a 136 else {
hudakz 0:433af64321d5 137 #if DEBUG
hudakz 0:433af64321d5 138 serial.printf("Invalid CRC!\r\n");
hudakz 2:b7ad1da7331a 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
hudakz 8:8dfdd1603e4d 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 */
hudakz 8:8dfdd1603e4d 153 bool DS1820::isPresent(void) {
hudakz 8:8dfdd1603e4d 154 return present;
hudakz 8:8dfdd1603e4d 155 }
hudakz 8:8dfdd1603e4d 156
hudakz 8:8dfdd1603e4d 157 /**
hudakz 6:518950e436be 158 * @brief Sets temperature-to-digital conversion resolution.
hudakz 4:adf4e7972d73 159 * @note The configuration register allows the user to set the resolution
hudakz 6:518950e436be 160 * of the temperature-to-digital conversion to 9, 10, 11, or 12 bits.
hudakz 6:518950e436be 161 * Defaults to 12-bit resolution for DS18B20.
hudakz 6:518950e436be 162 * DS18S20 allows only 9-bit resolution.
hudakz 6:518950e436be 163 * @param res: Resolution of the temperature-to-digital conversion in bits.
hudakz 4:adf4e7972d73 164 * @retval
hudakz 4:adf4e7972d73 165 */
hudakz 4:adf4e7972d73 166 void DS1820::setResolution(uint8_t res) {
hudakz 4:adf4e7972d73 167 // keep resolution within limits
hudakz 4:adf4e7972d73 168 if(res > 12)
hudakz 4:adf4e7972d73 169 res = 12;
hudakz 4:adf4e7972d73 170 if(res < 9)
hudakz 4:adf4e7972d73 171 res = 9;
hudakz 8:8dfdd1603e4d 172 if(model_s)
hudakz 4:adf4e7972d73 173 res = 9;
hudakz 4:adf4e7972d73 174
hudakz 4:adf4e7972d73 175 oneWire.reset();
hudakz 4:adf4e7972d73 176 oneWire.skip();
hudakz 4:adf4e7972d73 177 oneWire.write(0xBE); // to read Scratchpad
hudakz 4:adf4e7972d73 178 for(uint8_t i = 0; i < 9; i++) // read Scratchpad bytes
hudakz 6:518950e436be 179 data[i] = oneWire.read();
hudakz 4:adf4e7972d73 180
hudakz 4:adf4e7972d73 181 data[4] |= (res - 9) << 5; // update configuration byte (set resolution)
hudakz 4:adf4e7972d73 182 oneWire.reset();
hudakz 4:adf4e7972d73 183 oneWire.skip();
hudakz 4:adf4e7972d73 184 oneWire.write(0x4E); // to write into Scratchpad
hudakz 4:adf4e7972d73 185 for(uint8_t i = 2; i < 5; i++) // write three bytes (2nd, 3rd, 4th) into Scratchpad
hudakz 4:adf4e7972d73 186 oneWire.write(data[i]);
hudakz 4:adf4e7972d73 187 }
hudakz 4:adf4e7972d73 188
hudakz 4:adf4e7972d73 189 /**
hudakz 0:433af64321d5 190 * @brief Starts temperature conversion
hudakz 6:518950e436be 191 * @note The time to complete the converion depends on the selected resolution:
hudakz 6:518950e436be 192 * 9-bit resolution -> max conversion time = 93.75ms
hudakz 6:518950e436be 193 * 10-bit resolution -> max conversion time = 187.5ms
hudakz 6:518950e436be 194 * 11-bit resolution -> max conversion time = 375ms
hudakz 6:518950e436be 195 * 12-bit resolution -> max conversion time = 750ms
hudakz 0:433af64321d5 196 * @param
hudakz 0:433af64321d5 197 * @retval
hudakz 0:433af64321d5 198 */
hudakz 0:433af64321d5 199 void DS1820::startConversion(void) {
hudakz 0:433af64321d5 200 if(present) {
hudakz 0:433af64321d5 201 oneWire.reset();
hudakz 0:433af64321d5 202 oneWire.skip();
hudakz 3:a250babd0a9f 203 oneWire.write(0x44); //start temperature conversion
hudakz 0:433af64321d5 204 }
hudakz 0:433af64321d5 205 }
hudakz 0:433af64321d5 206
hudakz 0:433af64321d5 207 /**
hudakz 6:518950e436be 208 * @brief Reads temperature from the chip's Scratchpad
hudakz 0:433af64321d5 209 * @note
hudakz 0:433af64321d5 210 * @param
hudakz 0:433af64321d5 211 * @retval Floating point temperature value
hudakz 0:433af64321d5 212 */
hudakz 0:433af64321d5 213 float DS1820::read(void) {
hudakz 0:433af64321d5 214 if(present) {
hudakz 0:433af64321d5 215 oneWire.reset();
hudakz 0:433af64321d5 216 oneWire.skip();
hudakz 0:433af64321d5 217 oneWire.write(0xBE); // to read Scratchpad
hudakz 6:518950e436be 218 for(uint8_t i = 0; i < 9; i++) // read Scratchpad bytes
hudakz 6:518950e436be 219 data[i] = oneWire.read();
hudakz 0:433af64321d5 220
hudakz 6:518950e436be 221 // Convert the raw bytes to a 16-bit unsigned value
hudakz 0:433af64321d5 222 uint16_t* p_word = reinterpret_cast < uint16_t * > (&data[0]);
hudakz 0:433af64321d5 223
hudakz 0:433af64321d5 224 #if DEBUG
hudakz 0:433af64321d5 225 serial.printf("raw = %#x\r\n", *p_word);
hudakz 0:433af64321d5 226 #endif
hudakz 0:433af64321d5 227
hudakz 8:8dfdd1603e4d 228 if(model_s) {
hudakz 6:518950e436be 229 *p_word = *p_word << 3; // 9-bit resolution
hudakz 0:433af64321d5 230 if(data[7] == 0x10) {
hudakz 0:433af64321d5 231
hudakz 6:518950e436be 232 // "count remain" gives full 12-bit resolution
hudakz 0:433af64321d5 233 *p_word = (*p_word & 0xFFF0) + 12 - data[6];
hudakz 0:433af64321d5 234 }
hudakz 0:433af64321d5 235 }
hudakz 0:433af64321d5 236 else {
hudakz 6:518950e436be 237 uint8_t cfg = (data[4] & 0x60); // default 12-bit resolution
hudakz 4:adf4e7972d73 238
hudakz 4:adf4e7972d73 239 // at lower resolution, the low bits are undefined, so let's clear them
hudakz 0:433af64321d5 240 if(cfg == 0x00)
hudakz 6:518950e436be 241 *p_word = *p_word &~7; // 9-bit resolution
hudakz 0:433af64321d5 242 else
hudakz 0:433af64321d5 243 if(cfg == 0x20)
hudakz 6:518950e436be 244 *p_word = *p_word &~3; // 10-bit resolution
hudakz 0:433af64321d5 245 else
hudakz 0:433af64321d5 246 if(cfg == 0x40)
hudakz 6:518950e436be 247 *p_word = *p_word &~1; // 11-bit resolution
hudakz 4:adf4e7972d73 248
hudakz 0:433af64321d5 249 }
hudakz 6:518950e436be 250
hudakz 6:518950e436be 251 // Convert the raw bytes to a 16-bit signed fixed point value :
hudakz 6:518950e436be 252 // 1 sign bit, 7 integer bits, 8 fractional bits (two’s compliment
hudakz 6:518950e436be 253 // and the LSB of the 16-bit binary number represents 1/256th of a unit).
hudakz 6:518950e436be 254 *p_word = *p_word << 4;
hudakz 6:518950e436be 255
hudakz 6:518950e436be 256 // Convert to floating point value
hudakz 6:518950e436be 257 return(toFloat(*p_word));
hudakz 0:433af64321d5 258 }
hudakz 0:433af64321d5 259 else
hudakz 0:433af64321d5 260 return 0;
hudakz 0:433af64321d5 261 }
hudakz 0:433af64321d5 262
hudakz 0:433af64321d5 263 /**
hudakz 6:518950e436be 264 * @brief Converts a 16-bit signed fixed point value to floating point value
hudakz 6:518950e436be 265 * @note The 16-bit unsigned integer represnts actually
hudakz 6:518950e436be 266 * a 16-bit signed fixed point value:
hudakz 4:adf4e7972d73 267 * 1 sign bit, 7 integer bits, 8 fractional bits (two’s compliment
hudakz 6:518950e436be 268 * and the LSB of the 16-bit binary number represents 1/256th of a unit).
hudakz 6:518950e436be 269 * @param 16-bit unsigned integer
hudakz 4:adf4e7972d73 270 * @retval Floating point value
hudakz 0:433af64321d5 271 */
hudakz 0:433af64321d5 272 float DS1820::toFloat(uint16_t word) {
hudakz 0:433af64321d5 273 if(word & 0x8000)
hudakz 0:433af64321d5 274 return (-float(uint16_t(~word + 1)) / 256.0f);
hudakz 0:433af64321d5 275 else
hudakz 0:433af64321d5 276 return (float(word) / 256.0f);
hudakz 0:433af64321d5 277 }
hudakz 0:433af64321d5 278
hudakz 8:8dfdd1603e4d 279