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