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