sad
Dependencies: OneWire
DS1820.cpp@26:ab2080dd9e46, 20 months ago (annotated)
- Committer:
- brose
- Date:
- Thu Sep 15 12:35:33 2022 +0000
- Revision:
- 26:ab2080dd9e46
- Parent:
- 25:b78d40247b97
small bugs
Who changed what in which revision?
User | Revision | Line number | New 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 | 24:d683d826dccd | 7 | * |
hudakz | 20:98c261bcb399 | 8 | * Single sensor. |
hudakz | 6:518950e436be | 9 | * |
hudakz | 20:98c261bcb399 | 10 | * #include "mbed.h" |
hudakz | 20:98c261bcb399 | 11 | * #include "DS1820.h" |
hudakz | 24:d683d826dccd | 12 | * |
hudakz | 20:98c261bcb399 | 13 | * Serial pc(USBTX, USBRX); |
hudakz | 20:98c261bcb399 | 14 | * DigitalOut led(LED1); |
hudakz | 20:98c261bcb399 | 15 | * OneWire oneWire(D8); // substitute D8 with actual mbed pin name connected 1-wire bus |
hudakz | 20:98c261bcb399 | 16 | * float temp = 0; |
hudakz | 20:98c261bcb399 | 17 | * int result = 0; |
hudakz | 24:d683d826dccd | 18 | * |
hudakz | 20:98c261bcb399 | 19 | * int main() |
hudakz | 20:98c261bcb399 | 20 | * { |
hudakz | 20:98c261bcb399 | 21 | * pc.printf("\r\n--Starting--\r\n"); |
hudakz | 20:98c261bcb399 | 22 | * if (ds1820.begin()) { |
hudakz | 20:98c261bcb399 | 23 | * while (1) { |
hudakz | 20:98c261bcb399 | 24 | * ds1820.startConversion(); // start temperature conversion from analog to digital |
hudakz | 24:d683d826dccd | 25 | * ThisThread::sleep_for(1000ms);// let DS1820 complete the temperature conversion |
hudakz | 24:d683d826dccd | 26 | * result = ds1820.read(temp); // read temperature from DS1820 and perform cyclic redundancy check (CRC) |
hudakz | 20:98c261bcb399 | 27 | * switch (result) { |
hudakz | 24:d683d826dccd | 28 | * case 0: // no errors -> 'temp' contains the value of measured temperature |
hudakz | 20:98c261bcb399 | 29 | * pc.printf("temp = %3.1f%cC\r\n", temp, 176); |
hudakz | 20:98c261bcb399 | 30 | * break; |
hudakz | 24:d683d826dccd | 31 | * |
hudakz | 24:d683d826dccd | 32 | * case 1: // no sensor present -> 'temp' is not updated |
hudakz | 20:98c261bcb399 | 33 | * pc.printf("no sensor present\n\r"); |
hudakz | 20:98c261bcb399 | 34 | * break; |
hudakz | 24:d683d826dccd | 35 | * |
hudakz | 24:d683d826dccd | 36 | * case 2: // CRC error -> 'temp' is not updated |
hudakz | 20:98c261bcb399 | 37 | * pc.printf("CRC error\r\n"); |
hudakz | 20:98c261bcb399 | 38 | * } |
hudakz | 24:d683d826dccd | 39 | * |
hudakz | 20:98c261bcb399 | 40 | * led = !led; |
hudakz | 20:98c261bcb399 | 41 | * } |
hudakz | 20:98c261bcb399 | 42 | * } |
hudakz | 20:98c261bcb399 | 43 | * else |
hudakz | 20:98c261bcb399 | 44 | * pc.printf("No DS1820 sensor found!\r\n"); |
hudakz | 20:98c261bcb399 | 45 | * } |
hudakz | 24:d683d826dccd | 46 | * |
hudakz | 24:d683d826dccd | 47 | * |
hudakz | 20:98c261bcb399 | 48 | * More sensors connected to the same 1-wire bus. |
hudakz | 24:d683d826dccd | 49 | * |
hudakz | 20:98c261bcb399 | 50 | * #include "mbed.h" |
hudakz | 20:98c261bcb399 | 51 | * #include "DS1820.h" |
hudakz | 24:d683d826dccd | 52 | * |
hudakz | 20:98c261bcb399 | 53 | * #define SENSORS_COUNT 64 // number of DS1820 sensors to be connected to the 1-wire bus (max 256) |
hudakz | 24:d683d826dccd | 54 | * |
hudakz | 20:98c261bcb399 | 55 | * Serial pc(USBTX, USBRX); |
hudakz | 20:98c261bcb399 | 56 | * DigitalOut led(LED1); |
hudakz | 20:98c261bcb399 | 57 | * OneWire oneWire(D8); // substitute D8 with actual mbed pin name connected to the DS1820 data pin |
hudakz | 20:98c261bcb399 | 58 | * DS1820* ds1820[SENSORS_COUNT]; |
hudakz | 20:98c261bcb399 | 59 | * int sensors_found = 0; // counts the actually found DS1820 sensors |
hudakz | 20:98c261bcb399 | 60 | * float temp = 0; |
hudakz | 20:98c261bcb399 | 61 | * int result = 0; |
hudakz | 24:d683d826dccd | 62 | * |
hudakz | 3:a250babd0a9f | 63 | * int main() { |
hudakz | 20:98c261bcb399 | 64 | * int i = 0; |
hudakz | 24:d683d826dccd | 65 | * |
hudakz | 20:98c261bcb399 | 66 | * pc.printf("\r\n Starting \r\n"); |
hudakz | 20:98c261bcb399 | 67 | * //Enumerate (i.e. detect) DS1820 sensors on the 1-wire bus |
hudakz | 20:98c261bcb399 | 68 | * for(i = 0; i < SENSORS_COUNT; i++) { |
hudakz | 20:98c261bcb399 | 69 | * ds1820[i] = new DS1820(&oneWire); |
hudakz | 20:98c261bcb399 | 70 | * if(!ds1820[i]->begin()) { |
hudakz | 20:98c261bcb399 | 71 | * delete ds1820[i]; |
hudakz | 20:98c261bcb399 | 72 | * break; |
hudakz | 20:98c261bcb399 | 73 | * } |
hudakz | 20:98c261bcb399 | 74 | * } |
hudakz | 24:d683d826dccd | 75 | * |
hudakz | 20:98c261bcb399 | 76 | * sensors_found = i; |
hudakz | 24:d683d826dccd | 77 | * |
hudakz | 20:98c261bcb399 | 78 | * if (sensors_found == 0) { |
hudakz | 20:98c261bcb399 | 79 | * pc.printf("No DS1820 sensor found!\r\n"); |
hudakz | 20:98c261bcb399 | 80 | * return -1; |
hudakz | 20:98c261bcb399 | 81 | * } |
hudakz | 20:98c261bcb399 | 82 | * else |
hudakz | 20:98c261bcb399 | 83 | * pc.printf("Found %d sensors.\r\n", sensors_found); |
hudakz | 24:d683d826dccd | 84 | * |
hudakz | 20:98c261bcb399 | 85 | * while(1) { |
hudakz | 20:98c261bcb399 | 86 | * pc.printf("-------------------\r\n"); |
hudakz | 20:98c261bcb399 | 87 | * for(i = 0; i < sensors_found; i++) |
hudakz | 24:d683d826dccd | 88 | * ds1820[i]->startConversion(); // start temperature conversion from analog to digital |
hudakz | 24:d683d826dccd | 89 | * ThisThread::sleep_for(1000ms); // let DS1820s complete the temperature conversion |
hudakz | 20:98c261bcb399 | 90 | * for(int i = 0; i < sensors_found; i++) { |
hudakz | 20:98c261bcb399 | 91 | * if(ds1820[i]->isPresent()) |
hudakz | 20:98c261bcb399 | 92 | * pc.printf("temp[%d] = %3.1f%cC\r\n", i, ds1820[i]->read(), 176); // read temperature |
hudakz | 20:98c261bcb399 | 93 | * } |
hudakz | 20:98c261bcb399 | 94 | * } |
hudakz | 3:a250babd0a9f | 95 | * } |
hudakz | 24:d683d826dccd | 96 | * |
hudakz | 3:a250babd0a9f | 97 | */ |
hudakz | 0:433af64321d5 | 98 | #include "DS1820.h" |
hudakz | 0:433af64321d5 | 99 | |
brose | 25:b78d40247b97 | 100 | //u#define DEBUG 1 |
hudakz | 20:98c261bcb399 | 101 | |
hudakz | 20:98c261bcb399 | 102 | //* Initializing static members |
hudakz | 24:d683d826dccd | 103 | uint8_t DS1820:: _lastAddr[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
hudakz | 24:d683d826dccd | 104 | |
hudakz | 20:98c261bcb399 | 105 | /** |
hudakz | 20:98c261bcb399 | 106 | * @brief Constructs a generic DS1820 sensor |
hudakz | 20:98c261bcb399 | 107 | * @note begin() must be called to detect and initialize the actual model |
hudakz | 24:d683d826dccd | 108 | * @param gpioPin: Name of the GPIO pin |
hudakz | 20:98c261bcb399 | 109 | * @retval |
hudakz | 20:98c261bcb399 | 110 | */ |
hudakz | 24:d683d826dccd | 111 | DS1820::DS1820(PinName gpioPin, int samplePoint_us /*=13*/ ) |
hudakz | 24:d683d826dccd | 112 | { |
hudakz | 24:d683d826dccd | 113 | _oneWire = new OneWire(gpioPin, samplePoint_us); |
hudakz | 24:d683d826dccd | 114 | _present = false; |
hudakz | 24:d683d826dccd | 115 | _model_s = false; |
hudakz | 24:d683d826dccd | 116 | } |
hudakz | 24:d683d826dccd | 117 | |
hudakz | 24:d683d826dccd | 118 | /** |
hudakz | 24:d683d826dccd | 119 | * @brief Constructs a generic DS1820 sensor |
hudakz | 24:d683d826dccd | 120 | * @note begin() must be called to detect and initialize the actual model |
hudakz | 24:d683d826dccd | 121 | * @param txPin: UART's Tx pin |
hudakz | 24:d683d826dccd | 122 | * @param rxPin: UART's Rx pin |
hudakz | 24:d683d826dccd | 123 | * @retval |
hudakz | 24:d683d826dccd | 124 | */ |
hudakz | 24:d683d826dccd | 125 | DS1820::DS1820(PinName txPin, PinName rxPin) |
hudakz | 24:d683d826dccd | 126 | { |
hudakz | 24:d683d826dccd | 127 | _oneWire = new OneWire(txPin, rxPin); |
hudakz | 24:d683d826dccd | 128 | _present = false; |
hudakz | 24:d683d826dccd | 129 | _model_s = false; |
hudakz | 20:98c261bcb399 | 130 | } |
hudakz | 0:433af64321d5 | 131 | |
hudakz | 0:433af64321d5 | 132 | /** |
hudakz | 0:433af64321d5 | 133 | * @brief Constructs a generic DS1820 sensor |
hudakz | 0:433af64321d5 | 134 | * @note begin() must be called to detect and initialize the actual model |
hudakz | 3:a250babd0a9f | 135 | * @param pin: Name of data pin |
hudakz | 0:433af64321d5 | 136 | * @retval |
hudakz | 0:433af64321d5 | 137 | */ |
hudakz | 24:d683d826dccd | 138 | DS1820::DS1820(OneWire* oneWire) : |
hudakz | 24:d683d826dccd | 139 | _oneWire(oneWire) |
hudakz | 24:d683d826dccd | 140 | { |
hudakz | 24:d683d826dccd | 141 | _present = false; |
hudakz | 24:d683d826dccd | 142 | _model_s = false; |
hudakz | 0:433af64321d5 | 143 | } |
hudakz | 0:433af64321d5 | 144 | |
hudakz | 0:433af64321d5 | 145 | /** |
hudakz | 0:433af64321d5 | 146 | * @brief Detects and initializes the actual DS1820 model |
hudakz | 0:433af64321d5 | 147 | * @note |
hudakz | 0:433af64321d5 | 148 | * @param |
hudakz | 0:433af64321d5 | 149 | * @retval true: if a DS1820 family sensor was detected and initialized |
hudakz | 0:433af64321d5 | 150 | false: otherwise |
hudakz | 0:433af64321d5 | 151 | */ |
hudakz | 24:d683d826dccd | 152 | bool DS1820::begin(void) |
hudakz | 24:d683d826dccd | 153 | { |
hudakz | 20:98c261bcb399 | 154 | #if DEBUG |
hudakz | 20:98c261bcb399 | 155 | printf("lastAddr ="); |
hudakz | 24:d683d826dccd | 156 | for (uint8_t i = 0; i < 8; i++) { |
brose | 25:b78d40247b97 | 157 | printf(" %x", _lastAddr[i]); |
hudakz | 20:98c261bcb399 | 158 | } |
hudakz | 24:d683d826dccd | 159 | |
hudakz | 20:98c261bcb399 | 160 | printf("\r\n"); |
hudakz | 20:98c261bcb399 | 161 | #endif |
hudakz | 24:d683d826dccd | 162 | if (!_oneWire->search(_lastAddr)) |
hudakz | 24:d683d826dccd | 163 | { |
hudakz | 0:433af64321d5 | 164 | #if DEBUG |
hudakz | 17:9ff584b9809f | 165 | printf("No addresses.\r\n"); |
hudakz | 0:433af64321d5 | 166 | #endif |
hudakz | 24:d683d826dccd | 167 | _oneWire->reset_search(); |
hudakz | 24:d683d826dccd | 168 | #if MBED_MAJOR_VERSION == 2 |
hudakz | 24:d683d826dccd | 169 | wait_ms(250); |
hudakz | 24:d683d826dccd | 170 | #else |
hudakz | 24:d683d826dccd | 171 | ThisThread::sleep_for(250ms); |
brose | 26:ab2080dd9e46 | 172 | //wait_ms(250); |
hudakz | 24:d683d826dccd | 173 | #endif |
hudakz | 0:433af64321d5 | 174 | return false; |
hudakz | 0:433af64321d5 | 175 | } |
hudakz | 24:d683d826dccd | 176 | |
hudakz | 20:98c261bcb399 | 177 | for (int i = 0; i < 8; i++) |
hudakz | 24:d683d826dccd | 178 | _addr[i] = _lastAddr[i]; |
hudakz | 0:433af64321d5 | 179 | |
hudakz | 0:433af64321d5 | 180 | #if DEBUG |
hudakz | 17:9ff584b9809f | 181 | printf("ROM ="); |
hudakz | 24:d683d826dccd | 182 | for (uint8_t i = 0; i < 8; i++) { |
brose | 25:b78d40247b97 | 183 | printf(" %x", _addr[i]); |
hudakz | 0:433af64321d5 | 184 | } |
hudakz | 24:d683d826dccd | 185 | |
hudakz | 17:9ff584b9809f | 186 | printf("\r\n"); |
hudakz | 0:433af64321d5 | 187 | #endif |
hudakz | 24:d683d826dccd | 188 | if (OneWire::crc8(_addr, 7) == _addr[7]) { |
hudakz | 24:d683d826dccd | 189 | _present = true; |
hudakz | 0:433af64321d5 | 190 | |
hudakz | 0:433af64321d5 | 191 | // the first ROM byte indicates which chip |
hudakz | 24:d683d826dccd | 192 | switch (_addr[0]) { |
hudakz | 24:d683d826dccd | 193 | case 0x10: |
hudakz | 24:d683d826dccd | 194 | _model_s = true; |
hudakz | 24:d683d826dccd | 195 | #if DEBUG |
hudakz | 24:d683d826dccd | 196 | printf("DS18S20 or old DS1820\r\n"); |
hudakz | 24:d683d826dccd | 197 | #endif |
hudakz | 24:d683d826dccd | 198 | break; |
hudakz | 0:433af64321d5 | 199 | |
hudakz | 24:d683d826dccd | 200 | case 0x28: |
hudakz | 24:d683d826dccd | 201 | _model_s = false; |
hudakz | 24:d683d826dccd | 202 | #if DEBUG |
hudakz | 24:d683d826dccd | 203 | printf("DS18B20\r\n"); |
hudakz | 24:d683d826dccd | 204 | #endif |
hudakz | 24:d683d826dccd | 205 | break; |
hudakz | 0:433af64321d5 | 206 | |
hudakz | 24:d683d826dccd | 207 | case 0x22: |
hudakz | 24:d683d826dccd | 208 | _model_s = false; |
hudakz | 24:d683d826dccd | 209 | #if DEBUG |
hudakz | 24:d683d826dccd | 210 | printf("DS1822\r\n"); |
hudakz | 24:d683d826dccd | 211 | #endif |
hudakz | 24:d683d826dccd | 212 | break; |
hudakz | 0:433af64321d5 | 213 | |
hudakz | 24:d683d826dccd | 214 | default: |
hudakz | 24:d683d826dccd | 215 | _present = false; |
hudakz | 24:d683d826dccd | 216 | #if DEBUG |
hudakz | 24:d683d826dccd | 217 | printf("Device doesn't belong to the DS1820 family\r\n"); |
hudakz | 24:d683d826dccd | 218 | #endif |
hudakz | 24:d683d826dccd | 219 | return false; |
hudakz | 0:433af64321d5 | 220 | } |
hudakz | 24:d683d826dccd | 221 | |
hudakz | 0:433af64321d5 | 222 | return true; |
hudakz | 0:433af64321d5 | 223 | } |
hudakz | 24:d683d826dccd | 224 | else |
hudakz | 24:d683d826dccd | 225 | { |
hudakz | 24:d683d826dccd | 226 | #if DEBUG |
hudakz | 17:9ff584b9809f | 227 | printf("Invalid CRC!\r\n"); |
hudakz | 24:d683d826dccd | 228 | #endif |
hudakz | 0:433af64321d5 | 229 | return false; |
hudakz | 0:433af64321d5 | 230 | } |
hudakz | 0:433af64321d5 | 231 | } |
hudakz | 0:433af64321d5 | 232 | |
hudakz | 0:433af64321d5 | 233 | /** |
hudakz | 8:8dfdd1603e4d | 234 | * @brief Informs about presence of a DS1820 sensor. |
hudakz | 8:8dfdd1603e4d | 235 | * @note begin() shall be called before using this function |
hudakz | 24:d683d826dccd | 236 | * if a generic DS1820 instance was created by the user. |
hudakz | 8:8dfdd1603e4d | 237 | * No need to call begin() for a specific DS1820 instance. |
hudakz | 8:8dfdd1603e4d | 238 | * @param |
hudakz | 8:8dfdd1603e4d | 239 | * @retval true: when a DS1820 sensor is present |
hudakz | 8:8dfdd1603e4d | 240 | * false: otherwise |
hudakz | 8:8dfdd1603e4d | 241 | */ |
hudakz | 24:d683d826dccd | 242 | bool DS1820::isPresent(void) |
hudakz | 24:d683d826dccd | 243 | { |
hudakz | 24:d683d826dccd | 244 | return _present; |
hudakz | 8:8dfdd1603e4d | 245 | } |
hudakz | 8:8dfdd1603e4d | 246 | |
hudakz | 8:8dfdd1603e4d | 247 | /** |
hudakz | 6:518950e436be | 248 | * @brief Sets temperature-to-digital conversion resolution. |
hudakz | 4:adf4e7972d73 | 249 | * @note The configuration register allows the user to set the resolution |
hudakz | 6:518950e436be | 250 | * of the temperature-to-digital conversion to 9, 10, 11, or 12 bits. |
hudakz | 6:518950e436be | 251 | * Defaults to 12-bit resolution for DS18B20. |
hudakz | 6:518950e436be | 252 | * DS18S20 allows only 9-bit resolution. |
hudakz | 6:518950e436be | 253 | * @param res: Resolution of the temperature-to-digital conversion in bits. |
hudakz | 4:adf4e7972d73 | 254 | * @retval |
hudakz | 4:adf4e7972d73 | 255 | */ |
hudakz | 24:d683d826dccd | 256 | void DS1820::setResolution(uint8_t res) |
hudakz | 24:d683d826dccd | 257 | { |
hudakz | 4:adf4e7972d73 | 258 | // keep resolution within limits |
hudakz | 24:d683d826dccd | 259 | |
hudakz | 24:d683d826dccd | 260 | if (res > 12) |
hudakz | 4:adf4e7972d73 | 261 | res = 12; |
hudakz | 24:d683d826dccd | 262 | if (res < 9) |
hudakz | 24:d683d826dccd | 263 | res = 9; |
hudakz | 24:d683d826dccd | 264 | if (_model_s) |
hudakz | 4:adf4e7972d73 | 265 | res = 9; |
hudakz | 24:d683d826dccd | 266 | |
hudakz | 24:d683d826dccd | 267 | _oneWire->reset(); |
hudakz | 24:d683d826dccd | 268 | _oneWire->select(_addr); |
hudakz | 24:d683d826dccd | 269 | _oneWire->write_byte(0xBE); // to read Scratchpad |
hudakz | 24:d683d826dccd | 270 | for (uint8_t i = 0; i < 9; i++) // read Scratchpad bytes |
hudakz | 24:d683d826dccd | 271 | _data[i] = _oneWire->read_byte(); |
hudakz | 4:adf4e7972d73 | 272 | |
hudakz | 24:d683d826dccd | 273 | _data[4] |= (res - 9) << 5; // update configuration byte (set resolution) |
hudakz | 24:d683d826dccd | 274 | _oneWire->reset(); |
hudakz | 24:d683d826dccd | 275 | _oneWire->select(_addr); |
hudakz | 24:d683d826dccd | 276 | _oneWire->write_byte(0x4E); // to write into Scratchpad |
hudakz | 24:d683d826dccd | 277 | for (uint8_t i = 2; i < 5; i++) // write three bytes (2nd, 3rd, 4th) into Scratchpad |
hudakz | 24:d683d826dccd | 278 | _oneWire->write_byte(_data[i]); |
hudakz | 4:adf4e7972d73 | 279 | } |
hudakz | 4:adf4e7972d73 | 280 | |
hudakz | 4:adf4e7972d73 | 281 | /** |
hudakz | 0:433af64321d5 | 282 | * @brief Starts temperature conversion |
hudakz | 6:518950e436be | 283 | * @note The time to complete the converion depends on the selected resolution: |
hudakz | 6:518950e436be | 284 | * 9-bit resolution -> max conversion time = 93.75ms |
hudakz | 6:518950e436be | 285 | * 10-bit resolution -> max conversion time = 187.5ms |
hudakz | 6:518950e436be | 286 | * 11-bit resolution -> max conversion time = 375ms |
hudakz | 6:518950e436be | 287 | * 12-bit resolution -> max conversion time = 750ms |
hudakz | 0:433af64321d5 | 288 | * @param |
hudakz | 0:433af64321d5 | 289 | * @retval |
hudakz | 0:433af64321d5 | 290 | */ |
hudakz | 24:d683d826dccd | 291 | void DS1820::startConversion(void) |
hudakz | 24:d683d826dccd | 292 | { |
hudakz | 24:d683d826dccd | 293 | if (_present) { |
hudakz | 24:d683d826dccd | 294 | _oneWire->reset(); |
hudakz | 24:d683d826dccd | 295 | _oneWire->select(_addr); |
hudakz | 24:d683d826dccd | 296 | _oneWire->write_byte(0x44); //start temperature conversion |
hudakz | 0:433af64321d5 | 297 | } |
hudakz | 0:433af64321d5 | 298 | } |
hudakz | 0:433af64321d5 | 299 | |
hudakz | 0:433af64321d5 | 300 | /** |
hudakz | 6:518950e436be | 301 | * @brief Reads temperature from the chip's Scratchpad |
hudakz | 0:433af64321d5 | 302 | * @note |
hudakz | 0:433af64321d5 | 303 | * @param |
hudakz | 0:433af64321d5 | 304 | * @retval Floating point temperature value |
hudakz | 0:433af64321d5 | 305 | */ |
hudakz | 24:d683d826dccd | 306 | float DS1820::read(void) |
hudakz | 24:d683d826dccd | 307 | { |
hudakz | 24:d683d826dccd | 308 | if (_present) { |
hudakz | 24:d683d826dccd | 309 | _oneWire->reset(); |
hudakz | 24:d683d826dccd | 310 | _oneWire->select(_addr); |
hudakz | 24:d683d826dccd | 311 | _oneWire->write_byte(0xBE); // to read Scratchpad |
hudakz | 24:d683d826dccd | 312 | for (uint8_t i = 0; i < 9; i++) // reading scratchpad registers |
hudakz | 24:d683d826dccd | 313 | _data[i] = _oneWire->read_byte(); |
hudakz | 0:433af64321d5 | 314 | |
hudakz | 6:518950e436be | 315 | // Convert the raw bytes to a 16-bit unsigned value |
hudakz | 24:d683d826dccd | 316 | uint16_t* p_word = reinterpret_cast < uint16_t * > (&_data[0]); |
hudakz | 0:433af64321d5 | 317 | |
hudakz | 0:433af64321d5 | 318 | #if DEBUG |
hudakz | 17:9ff584b9809f | 319 | printf("raw = %#x\r\n", *p_word); |
hudakz | 24:d683d826dccd | 320 | #endif |
hudakz | 24:d683d826dccd | 321 | if (_model_s) { |
hudakz | 6:518950e436be | 322 | *p_word = *p_word << 3; // 9-bit resolution |
hudakz | 24:d683d826dccd | 323 | if (_data[7] == 0x10) { |
hudakz | 0:433af64321d5 | 324 | |
hudakz | 6:518950e436be | 325 | // "count remain" gives full 12-bit resolution |
hudakz | 24:d683d826dccd | 326 | *p_word = (*p_word & 0xFFF0) + 12 - _data[6]; |
hudakz | 0:433af64321d5 | 327 | } |
hudakz | 0:433af64321d5 | 328 | } |
hudakz | 0:433af64321d5 | 329 | else { |
hudakz | 24:d683d826dccd | 330 | uint8_t cfg = (_data[4] & 0x60); // default 12-bit resolution |
hudakz | 24:d683d826dccd | 331 | |
hudakz | 4:adf4e7972d73 | 332 | // at lower resolution, the low bits are undefined, so let's clear them |
hudakz | 24:d683d826dccd | 333 | if (cfg == 0x00) |
hudakz | 6:518950e436be | 334 | *p_word = *p_word &~7; // 9-bit resolution |
hudakz | 0:433af64321d5 | 335 | else |
hudakz | 24:d683d826dccd | 336 | if (cfg == 0x20) |
hudakz | 6:518950e436be | 337 | *p_word = *p_word &~3; // 10-bit resolution |
hudakz | 0:433af64321d5 | 338 | else |
hudakz | 24:d683d826dccd | 339 | if (cfg == 0x40) |
hudakz | 6:518950e436be | 340 | *p_word = *p_word &~1; // 11-bit resolution |
hudakz | 0:433af64321d5 | 341 | } |
hudakz | 6:518950e436be | 342 | |
hudakz | 6:518950e436be | 343 | // Convert the raw bytes to a 16-bit signed fixed point value : |
hudakz | 6:518950e436be | 344 | // 1 sign bit, 7 integer bits, 8 fractional bits (two’s compliment |
hudakz | 6:518950e436be | 345 | // and the LSB of the 16-bit binary number represents 1/256th of a unit). |
hudakz | 6:518950e436be | 346 | *p_word = *p_word << 4; |
hudakz | 24:d683d826dccd | 347 | |
hudakz | 6:518950e436be | 348 | // Convert to floating point value |
hudakz | 6:518950e436be | 349 | return(toFloat(*p_word)); |
hudakz | 0:433af64321d5 | 350 | } |
hudakz | 0:433af64321d5 | 351 | else |
hudakz | 0:433af64321d5 | 352 | return 0; |
hudakz | 0:433af64321d5 | 353 | } |
hudakz | 0:433af64321d5 | 354 | |
hudakz | 0:433af64321d5 | 355 | /** |
hudakz | 13:b593a82ce790 | 356 | * @brief Reads temperature from chip's scratchpad. |
hudakz | 13:b593a82ce790 | 357 | * @note Verifies data integrity by calculating cyclic redundancy check (CRC). |
hudakz | 13:b593a82ce790 | 358 | * If the calculated CRC dosn't match the one stored in chip's scratchpad register |
hudakz | 13:b593a82ce790 | 359 | * the temperature variable is not updated and CRC error code is returned. |
hudakz | 13:b593a82ce790 | 360 | * @param temp: The temperature variable to be updated by this routine. |
hudakz | 13:b593a82ce790 | 361 | * (It's passed as reference to floating point.) |
hudakz | 13:b593a82ce790 | 362 | * @retval error code: |
hudakz | 13:b593a82ce790 | 363 | * 0 - no errors ('temp' contains the temperature measured) |
hudakz | 13:b593a82ce790 | 364 | * 1 - sensor not present ('temp' is not updated) |
hudakz | 13:b593a82ce790 | 365 | * 2 - CRC error ('temp' is not updated) |
hudakz | 13:b593a82ce790 | 366 | */ |
hudakz | 24:d683d826dccd | 367 | uint8_t DS1820::read(float& temp) |
hudakz | 24:d683d826dccd | 368 | { |
hudakz | 24:d683d826dccd | 369 | if (_present) { |
hudakz | 24:d683d826dccd | 370 | _oneWire->reset(); |
hudakz | 24:d683d826dccd | 371 | _oneWire->select(_addr); |
hudakz | 24:d683d826dccd | 372 | _oneWire->write_byte(0xBE); // to read Scratchpad |
hudakz | 24:d683d826dccd | 373 | for (uint8_t i = 0; i < 9; i++) // reading scratchpad registers |
hudakz | 24:d683d826dccd | 374 | _data[i] = _oneWire->read_byte(); |
hudakz | 13:b593a82ce790 | 375 | |
hudakz | 24:d683d826dccd | 376 | if (_oneWire->crc8(_data, 8) != _data[8]) // if calculated CRC does not match the stored one |
hudakz | 17:9ff584b9809f | 377 | { |
hudakz | 17:9ff584b9809f | 378 | #if DEBUG |
hudakz | 24:d683d826dccd | 379 | for (uint8_t i = 0; i < 9; i++) |
brose | 25:b78d40247b97 | 380 | printf("data[%d]=0x%.2x\r\n", i, _data[i]); |
hudakz | 24:d683d826dccd | 381 | #endif |
hudakz | 13:b593a82ce790 | 382 | return 2; // return with CRC error |
hudakz | 17:9ff584b9809f | 383 | } |
hudakz | 13:b593a82ce790 | 384 | |
hudakz | 13:b593a82ce790 | 385 | // Convert the raw bytes to a 16bit unsigned value |
hudakz | 24:d683d826dccd | 386 | uint16_t* p_word = reinterpret_cast < uint16_t * > (&_data[0]); |
hudakz | 13:b593a82ce790 | 387 | |
hudakz | 13:b593a82ce790 | 388 | #if DEBUG |
hudakz | 17:9ff584b9809f | 389 | printf("raw = %#x\r\n", *p_word); |
hudakz | 13:b593a82ce790 | 390 | #endif |
hudakz | 24:d683d826dccd | 391 | if (_model_s) { |
hudakz | 24:d683d826dccd | 392 | *p_word = *p_word << 3; // 9 bit resolution, max conversion time = 750ms |
hudakz | 24:d683d826dccd | 393 | if (_data[7] == 0x10) { |
hudakz | 13:b593a82ce790 | 394 | |
hudakz | 13:b593a82ce790 | 395 | // "count remain" gives full 12 bit resolution |
hudakz | 24:d683d826dccd | 396 | *p_word = (*p_word & 0xFFF0) + 12 - _data[6]; |
hudakz | 13:b593a82ce790 | 397 | } |
hudakz | 13:b593a82ce790 | 398 | |
hudakz | 13:b593a82ce790 | 399 | // Convert the raw bytes to a 16bit signed fixed point value : |
hudakz | 13:b593a82ce790 | 400 | // 1 sign bit, 7 integer bits, 8 fractional bits (two's compliment |
hudakz | 13:b593a82ce790 | 401 | // and the LSB of the 16bit binary number represents 1/256th of a unit). |
hudakz | 13:b593a82ce790 | 402 | *p_word = *p_word << 4; |
hudakz | 24:d683d826dccd | 403 | |
hudakz | 13:b593a82ce790 | 404 | // Convert to floating point value |
hudakz | 13:b593a82ce790 | 405 | temp = toFloat(*p_word); |
hudakz | 24:d683d826dccd | 406 | return 0; // return with no errors |
hudakz | 13:b593a82ce790 | 407 | } |
hudakz | 13:b593a82ce790 | 408 | else { |
hudakz | 24:d683d826dccd | 409 | uint8_t cfg = (_data[4] & 0x60); // default 12bit resolution, max conversion time = 750ms |
hudakz | 13:b593a82ce790 | 410 | |
hudakz | 13:b593a82ce790 | 411 | // at lower resolution, the low bits are undefined, so let's clear them |
hudakz | 24:d683d826dccd | 412 | if (cfg == 0x00) |
hudakz | 24:d683d826dccd | 413 | *p_word = *p_word &~7; // 9bit resolution, max conversion time = 93.75ms |
hudakz | 13:b593a82ce790 | 414 | else |
hudakz | 24:d683d826dccd | 415 | if (cfg == 0x20) |
hudakz | 24:d683d826dccd | 416 | *p_word = *p_word &~3; // 10bit resolution, max conversion time = 187.5ms |
hudakz | 13:b593a82ce790 | 417 | else |
hudakz | 24:d683d826dccd | 418 | if (cfg == 0x40) |
hudakz | 24:d683d826dccd | 419 | *p_word = *p_word &~1; // 11bit resolution, max conversion time = 375ms |
hudakz | 13:b593a82ce790 | 420 | |
hudakz | 13:b593a82ce790 | 421 | // Convert the raw bytes to a 16bit signed fixed point value : |
hudakz | 14:b02fa18b294a | 422 | // 1 sign bit, 7 integer bits, 8 fractional bits (two's complement |
hudakz | 13:b593a82ce790 | 423 | // and the LSB of the 16bit binary number represents 1/256th of a unit). |
hudakz | 13:b593a82ce790 | 424 | *p_word = *p_word << 4; |
hudakz | 24:d683d826dccd | 425 | |
hudakz | 13:b593a82ce790 | 426 | // Convert to floating point value |
hudakz | 13:b593a82ce790 | 427 | temp = toFloat(*p_word); |
hudakz | 24:d683d826dccd | 428 | return 0; // return with no errors |
hudakz | 13:b593a82ce790 | 429 | } |
hudakz | 13:b593a82ce790 | 430 | } |
hudakz | 13:b593a82ce790 | 431 | else |
hudakz | 24:d683d826dccd | 432 | return 1; // error, sensor is not present |
hudakz | 13:b593a82ce790 | 433 | } |
hudakz | 13:b593a82ce790 | 434 | |
hudakz | 13:b593a82ce790 | 435 | /** |
hudakz | 6:518950e436be | 436 | * @brief Converts a 16-bit signed fixed point value to floating point value |
hudakz | 6:518950e436be | 437 | * @note The 16-bit unsigned integer represnts actually |
hudakz | 6:518950e436be | 438 | * a 16-bit signed fixed point value: |
hudakz | 14:b02fa18b294a | 439 | * 1 sign bit, 7 integer bits, 8 fractional bits (two’s complement |
hudakz | 24:d683d826dccd | 440 | * and the LSB of the 16-bit binary number represents 1/256th of a unit). |
hudakz | 6:518950e436be | 441 | * @param 16-bit unsigned integer |
hudakz | 4:adf4e7972d73 | 442 | * @retval Floating point value |
hudakz | 0:433af64321d5 | 443 | */ |
hudakz | 24:d683d826dccd | 444 | float DS1820::toFloat(uint16_t word) |
hudakz | 24:d683d826dccd | 445 | { |
hudakz | 24:d683d826dccd | 446 | if (word & 0x8000) |
hudakz | 24:d683d826dccd | 447 | return(-float(uint16_t(~word + 1)) / 256.0f); |
hudakz | 0:433af64321d5 | 448 | else |
hudakz | 24:d683d826dccd | 449 | return(float(word) / 256.0f); |
hudakz | 0:433af64321d5 | 450 | } |
brose | 25:b78d40247b97 | 451 | |
brose | 25:b78d40247b97 | 452 | // new from brose |
brose | 25:b78d40247b97 | 453 | void DS1820::getAddress(char * b) |
brose | 25:b78d40247b97 | 454 | { |
brose | 25:b78d40247b97 | 455 | //char b[20]= {}; |
brose | 25:b78d40247b97 | 456 | |
brose | 25:b78d40247b97 | 457 | sprintf (b, "%hhx%hhx%hhx%hhx%hhx%hhx%hhx%hhx", _addr[0],_addr[1],_addr[2],_addr[3],_addr[4],_addr[5],_addr[6],_addr[7]); |
brose | 25:b78d40247b97 | 458 | //printf(b); |
brose | 25:b78d40247b97 | 459 | |
brose | 25:b78d40247b97 | 460 | } |