Dallas' DS1820 family temperature sensor. For more details see [https://developer.mbed.org/users/hudakz/code/DS1820/wiki/Homepage]
Dependents: BLE_nRF24L01 frdm_serialfgmp gather_sensor_data UltiSaverController ... more
Some programs using the DS1820 library:
Import programDS1820_Hello
Simple DS1820 sensor demo showing how to use the DS1820 library [https://developer.mbed.org/users/hudakz/code/DS1820/]
Import programBLE_nRF24L01
Bluetooth Low Energy (BLE) beacon with nRF24L01(+). Data is received and displayed by Android device (Android app source code is attached).
Examples of use:
Single DS1820 sensor
/* * Single DS1820 sensor GPIO driven * * Note: Don't forget to connect a 4.7k Ohm resistor * between the DS1820's data pin and the +3.3V pin * * ---------------- * | | -----------------------> +3.3V * | MBED BOARD | | * | | | ------ * | +3.3V |--o--| 4.7k |------- * | | ------ | * | | | * | | | * | | | * | | | * | GPIO |--------------------o-----> 1-wire bus/line * | | * | | * | GND |--------------------------> GND * | | * ---------------- * */ #include "mbed.h" #include "DS1820.h" Serial serial(USBTX, USBRX); int main() { DS1820 ds1820(D8); // substitute D8 with actual mbed pin name connected to the DS1820 data pin if(ds1820.begin()) { while(1) { ds1820.startConversion(); // start temperature conversion ThisThread::sleep_for(1000); // let DS1820 complete the temperature conversion serial.printf("temp = %3.1f\r\n", ds1820.read()); // read temperature } } else serial.printf("No DS1820 sensor found!\r\n"); }
Single DS1820 sensor. Data integrity is assured by performing CRC.
/* * Single DS1820 sensor GPIO driven + performing CRC * * Note: Don't forget to connect a 4.7k Ohm resistor * between the DS1820's data pin and the +3.3V pin * * ---------------- * | | -----------------------> +3.3V * | MBED BOARD | | * | | | ------ * | +3.3V |--o--| 4.7k |------- * | | ------ | * | | | * | | | * | | | * | | | * | GPIO |--------------------o-----> 1-wire bus/line * | | * | | * | GND |--------------------------> GND * | | * ---------------- * */ #include "mbed.h" #include "DS1820.h" Serial serial(USBTX, USBRX); int main() { DS1820 ds1820(D8); // substitute D8 with actual mbed pin name connected to the DS1820 data pin float temp = 0; int error = 0; if(ds1820.begin()) { while(1) { ds1820.startConversion(); // start temperature conversion ThisThread::sleep_for(1000); // let DS1820 complete the temperature conversion error = ds1820.read(temp); // read temperature from DS1820 and perform cyclic redundancy check (CRC) switch(error) { case 0: // no errors -> 'temp' contains the value of measured temperature serial.printf("temp = %3.1f\r\n", temp); break; case 1: // no sensor present -> 'temp' is not updated serial.printf("no sensor present\n\r"); break; case 2: // CRC error -> 'temp' is not updated serial.printf("CRC error\r\n"); } } } else serial.printf("No DS1820 sensor found!\r\n"); }
Several DS1820 sensors connected to the same 1-wire bus.
/* * Multiple sensors GPIO driven * * Note: Don't forget to connect a 4.7k Ohm resistor * between the 1-wire bus data line and the +3.3V rail * * ---------------- * | | -----------------------> +3.3V * | MBED BOARD | | * | | | ------ * | +3.3V |--o--| 4.7k |------- * | | ------ | * | | | * | | | * | | | * | | | * | GPIO |--------------------o-----> 1-wire bus/line * | | * | | * | GND |--------------------------> GND * | | * ---------------- * */ #include "mbed.h" #include "DS1820.h" #define MAX_SENSOSRS 32 // max number of DS1820 sensors to be connected to the 1-wire bus (max 256) DS1820* ds1820[MAX_SENSOSRS]; Serial pc(USBTX, USBRX); DigitalOut led(LED1); OneWire oneWire(D8); // substitute D8 with the actual pin name connected to the 1-wire bus int sensorsFound = 0; // counts the actually found DS1820 sensors int main() { pc.printf("\r\n--Starting--\r\n"); //Enumerate (i.e. detect) DS1820 sensors on the 1-wire bus for (sensorsFound = 0; sensorsFound < MAX_SENSOSRS; sensorsFound++) { ds1820[sensorsFound] = new DS1820(&oneWire); if (!ds1820[sensorsFound]->begin()) { delete ds1820[sensorsFound]; break; } } switch (sensorsFound) { case 0: pc.printf("No DS1820 sensor found!\r\n"); return -1; case 1: pc.printf("One DS1820 sensor found.\r\n"); break; default: pc.printf("Found %d DS1820 sensors.\r\n", sensorsFound); } while (1) { pc.printf("----------------\r\n"); for (int i = 0; i < sensorsFound; i++) ds1820[i]->startConversion(); // start temperature conversion from analog to digital ThisThread::sleep_for(1000); // let DS1820 sensors complete the temperature conversion for (int i = 0; i < sensorsFound; i++) { if (ds1820[i]->isPresent()) pc.printf("temp[%d] = %3.1f%cC\r\n", i, ds1820[i]->read(), 176); // read temperature } } }
Several DS1820 sensors connected to the same 1-wire bus. UART is used to implement the bus
/* * Multiple sensors UART driven: * * UART is driving the 1-Wire Bus Master according to Maxim Integrated application note * * https://www.maximintegrated.com/en/design/technical-documents/tutorials/2/214.html * * In addition to the 4.7k Ohm resistor between the 1-wire data bus/line and the +3.3V pin, * a 470 Ohm resistor shall be tied to the UART's tx and rx pin. UART's rx pin is then used * as 1-wire data bus/line. * * ---------------- * | | -----------------------> +3.3V * | MBED BOARD | | * | | | ------ * | +3.3V |--o--| 4.7k |------- * | | ------ | * | | ------ | * | UART TX |-----| 470 |--- | * | | ------ | | * | | | | * | UART RX |----------------o---o-----> 1-wire bus/line * | | * | | * | GND |--------------------------> GND * | | * ---------------- * */ #include "mbed.h" #include "DS1820.h" #define MAX_SENSOSRS 32 // max number of DS1820 sensors to be connected to the 1-wire bus (max 256) DS1820* ds1820[MAX_SENSOSRS]; DigitalOut led(LED1); OneWire oneWire(p9, p10); // LPC1768 (UART Tx pin, UART Rx pin) //OneWire oneWire(PA_0, PA_1); // NUCLE0-F446RE (UART Tx pin, UART Rx pin) int sensorsFound = 0; // counts the actually found DS1820 sensors /** * @brief * @note * @param * @retval */ int main() { printf("\r\n--Starting--\r\n"); //Enumerate (i.e. detect) DS1820 sensors on the 1-wire bus for (sensorsFound = 0; sensorsFound < MAX_SENSOSRS; sensorsFound++) { ds1820[sensorsFound] = new DS1820(&oneWire); if (!ds1820[sensorsFound]->begin()) { delete ds1820[sensorsFound]; break; } } switch (sensorsFound) { case 0: printf("No DS1820 sensor found!\r\n"); return -1; case 1: printf("One DS1820 sensor found.\r\n"); break; default: printf("Found %d DS1820 sensors.\r\n", sensorsFound); } while (1) { led = !led; printf("----------------\r\n"); for (int i = 0; i < sensorsFound; i++) ds1820[i]->startConversion(); // start temperature conversion from analog to digital #if (MBED_MAJOR_VERSION > 5) ThisThread::sleep_for(1s); #else wait(1); #endif for (int i = 0; i < sensorsFound; i++) { if (ds1820[i]->isPresent()) printf("temp[%d] = %3.1f%cC\r\n", i, ds1820[i]->read(), 176); // read temperature } } }
DS1820.cpp@4:adf4e7972d73, 2015-03-20 (annotated)
- Committer:
- hudakz
- Date:
- Fri Mar 20 10:07:22 2015 +0000
- Revision:
- 4:adf4e7972d73
- Parent:
- 3:a250babd0a9f
- Child:
- 6:518950e436be
Function to set temperature-to-digital conversion resolution added.
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 | 3:a250babd0a9f | 4 | * which is 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 | 3:a250babd0a9f | 9 | * |
hudakz | 3:a250babd0a9f | 10 | * int main() { |
hudakz | 3:a250babd0a9f | 11 | * DS1820 ds1820(PA_9); |
hudakz | 3:a250babd0a9f | 12 | * |
hudakz | 3:a250babd0a9f | 13 | * if(ds1820.begin()) { |
hudakz | 3:a250babd0a9f | 14 | * ds1820.startConversion(); |
hudakz | 3:a250babd0a9f | 15 | * wait(1.0); |
hudakz | 3:a250babd0a9f | 16 | * while(1) { |
hudakz | 3:a250babd0a9f | 17 | * serial.printf("temp = %3.1f\r\n", ds1820.read()); |
hudakz | 3:a250babd0a9f | 18 | * ds1820.startConversion(); |
hudakz | 3:a250babd0a9f | 19 | * wait(1.0); |
hudakz | 3:a250babd0a9f | 20 | * } |
hudakz | 3:a250babd0a9f | 21 | * } else |
hudakz | 3:a250babd0a9f | 22 | * serial.printf("No DS1820 sensor found!\r\n"); |
hudakz | 3:a250babd0a9f | 23 | * } |
hudakz | 3:a250babd0a9f | 24 | */ |
hudakz | 3:a250babd0a9f | 25 | |
hudakz | 0:433af64321d5 | 26 | #include "DS1820.h" |
hudakz | 0:433af64321d5 | 27 | |
hudakz | 1:e4689408d617 | 28 | #define DEBUG 0 |
hudakz | 0:433af64321d5 | 29 | |
hudakz | 0:433af64321d5 | 30 | #if DEBUG |
hudakz | 0:433af64321d5 | 31 | extern Serial serial; |
hudakz | 0:433af64321d5 | 32 | #endif |
hudakz | 0:433af64321d5 | 33 | |
hudakz | 0:433af64321d5 | 34 | /** |
hudakz | 0:433af64321d5 | 35 | * @brief Constructs a generic DS1820 sensor |
hudakz | 0:433af64321d5 | 36 | * @note begin() must be called to detect and initialize the actual model |
hudakz | 3:a250babd0a9f | 37 | * @param pin: Name of data pin |
hudakz | 0:433af64321d5 | 38 | * @retval |
hudakz | 0:433af64321d5 | 39 | */ |
hudakz | 0:433af64321d5 | 40 | DS1820::DS1820(PinName pin) : |
hudakz | 0:433af64321d5 | 41 | oneWire(pin) { |
hudakz | 0:433af64321d5 | 42 | present = 0; |
hudakz | 0:433af64321d5 | 43 | type_s = 0; |
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) : |
hudakz | 0:433af64321d5 | 54 | oneWire(pin) { |
hudakz | 0:433af64321d5 | 55 | if((model == 'S') or (model == 's')) { |
hudakz | 0:433af64321d5 | 56 | present = 1; |
hudakz | 0:433af64321d5 | 57 | type_s = 1; |
hudakz | 0:433af64321d5 | 58 | } |
hudakz | 0:433af64321d5 | 59 | else if((model == 'B') or (model == 'b')) { |
hudakz | 0:433af64321d5 | 60 | present = 1; |
hudakz | 0:433af64321d5 | 61 | type_s = 0; |
hudakz | 0:433af64321d5 | 62 | } |
hudakz | 0:433af64321d5 | 63 | else |
hudakz | 0:433af64321d5 | 64 | present = 0; |
hudakz | 0:433af64321d5 | 65 | } |
hudakz | 0:433af64321d5 | 66 | |
hudakz | 0:433af64321d5 | 67 | /** |
hudakz | 0:433af64321d5 | 68 | * @brief Detects and initializes the actual DS1820 model |
hudakz | 0:433af64321d5 | 69 | * @note |
hudakz | 0:433af64321d5 | 70 | * @param |
hudakz | 0:433af64321d5 | 71 | * @retval true: if a DS1820 family sensor was detected and initialized |
hudakz | 0:433af64321d5 | 72 | false: otherwise |
hudakz | 0:433af64321d5 | 73 | */ |
hudakz | 0:433af64321d5 | 74 | bool DS1820::begin(void) { |
hudakz | 0:433af64321d5 | 75 | oneWire.reset_search(); |
hudakz | 0:433af64321d5 | 76 | wait_ms(250); |
hudakz | 0:433af64321d5 | 77 | if(!oneWire.search(addr)) { |
hudakz | 0:433af64321d5 | 78 | #if DEBUG |
hudakz | 0:433af64321d5 | 79 | serial.printf("No addresses.\r\n"); |
hudakz | 0:433af64321d5 | 80 | #endif |
hudakz | 0:433af64321d5 | 81 | oneWire.reset_search(); |
hudakz | 0:433af64321d5 | 82 | wait_ms(250); |
hudakz | 0:433af64321d5 | 83 | return false; |
hudakz | 0:433af64321d5 | 84 | } |
hudakz | 0:433af64321d5 | 85 | |
hudakz | 0:433af64321d5 | 86 | #if DEBUG |
hudakz | 0:433af64321d5 | 87 | serial.printf("ROM ="); |
hudakz | 0:433af64321d5 | 88 | for(uint8_t i = 0; i < 8; i++) { |
hudakz | 0:433af64321d5 | 89 | serial.printf(" %x", addr[i]); |
hudakz | 0:433af64321d5 | 90 | } |
hudakz | 0:433af64321d5 | 91 | serial.printf("\r\n"); |
hudakz | 0:433af64321d5 | 92 | #endif |
hudakz | 0:433af64321d5 | 93 | |
hudakz | 0:433af64321d5 | 94 | if(OneWire::crc8(addr, 7) == addr[7]) { |
hudakz | 0:433af64321d5 | 95 | present = 1; |
hudakz | 0:433af64321d5 | 96 | |
hudakz | 0:433af64321d5 | 97 | // the first ROM byte indicates which chip |
hudakz | 0:433af64321d5 | 98 | switch(addr[0]) { |
hudakz | 0:433af64321d5 | 99 | case 0x10: |
hudakz | 0:433af64321d5 | 100 | type_s = 1; |
hudakz | 0:433af64321d5 | 101 | #if DEBUG |
hudakz | 0:433af64321d5 | 102 | serial.printf("DS18S20 or old DS1820\r\n"); |
hudakz | 0:433af64321d5 | 103 | #endif |
hudakz | 0:433af64321d5 | 104 | break; |
hudakz | 0:433af64321d5 | 105 | |
hudakz | 0:433af64321d5 | 106 | case 0x28: |
hudakz | 0:433af64321d5 | 107 | type_s = 0; |
hudakz | 0:433af64321d5 | 108 | #if DEBUG |
hudakz | 0:433af64321d5 | 109 | serial.printf("DS18B20\r\n"); |
hudakz | 0:433af64321d5 | 110 | #endif |
hudakz | 0:433af64321d5 | 111 | break; |
hudakz | 0:433af64321d5 | 112 | |
hudakz | 0:433af64321d5 | 113 | case 0x22: |
hudakz | 0:433af64321d5 | 114 | type_s = 0; |
hudakz | 0:433af64321d5 | 115 | #if DEBUG |
hudakz | 0:433af64321d5 | 116 | serial.printf("DS1822\r\n"); |
hudakz | 0:433af64321d5 | 117 | #endif |
hudakz | 0:433af64321d5 | 118 | break; |
hudakz | 0:433af64321d5 | 119 | |
hudakz | 0:433af64321d5 | 120 | default: |
hudakz | 0:433af64321d5 | 121 | present = 0; |
hudakz | 0:433af64321d5 | 122 | #if DEBUG |
hudakz | 0:433af64321d5 | 123 | serial.printf("Device doesn't belong to the DS1820 family\r\n"); |
hudakz | 2:b7ad1da7331a | 124 | #endif |
hudakz | 0:433af64321d5 | 125 | return false; |
hudakz | 0:433af64321d5 | 126 | } |
hudakz | 0:433af64321d5 | 127 | return true; |
hudakz | 0:433af64321d5 | 128 | } |
hudakz | 2:b7ad1da7331a | 129 | else { |
hudakz | 0:433af64321d5 | 130 | #if DEBUG |
hudakz | 0:433af64321d5 | 131 | serial.printf("Invalid CRC!\r\n"); |
hudakz | 2:b7ad1da7331a | 132 | #endif |
hudakz | 0:433af64321d5 | 133 | return false; |
hudakz | 0:433af64321d5 | 134 | } |
hudakz | 0:433af64321d5 | 135 | } |
hudakz | 0:433af64321d5 | 136 | |
hudakz | 0:433af64321d5 | 137 | /** |
hudakz | 4:adf4e7972d73 | 138 | * @brief Sets temperature-to-digital conversion resolution |
hudakz | 4:adf4e7972d73 | 139 | * @note The configuration register allows the user to set the resolution |
hudakz | 4:adf4e7972d73 | 140 | * of the temperature-to-digital conversion to 9, 10, 11, or 12 bits |
hudakz | 4:adf4e7972d73 | 141 | * Defaults to 12bit resolution for DS18B20. |
hudakz | 4:adf4e7972d73 | 142 | * DS18S20 allows only 9bit resolution. |
hudakz | 4:adf4e7972d73 | 143 | * @param res: Resolution of the temperature-to-digital conversion in bits |
hudakz | 4:adf4e7972d73 | 144 | * @retval |
hudakz | 4:adf4e7972d73 | 145 | */ |
hudakz | 4:adf4e7972d73 | 146 | void DS1820::setResolution(uint8_t res) { |
hudakz | 4:adf4e7972d73 | 147 | // keep resolution within limits |
hudakz | 4:adf4e7972d73 | 148 | if(res > 12) |
hudakz | 4:adf4e7972d73 | 149 | res = 12; |
hudakz | 4:adf4e7972d73 | 150 | if(res < 9) |
hudakz | 4:adf4e7972d73 | 151 | res = 9; |
hudakz | 4:adf4e7972d73 | 152 | if(type_s) |
hudakz | 4:adf4e7972d73 | 153 | res = 9; |
hudakz | 4:adf4e7972d73 | 154 | |
hudakz | 4:adf4e7972d73 | 155 | oneWire.reset(); |
hudakz | 4:adf4e7972d73 | 156 | oneWire.skip(); |
hudakz | 4:adf4e7972d73 | 157 | oneWire.write(0xBE); // to read Scratchpad |
hudakz | 4:adf4e7972d73 | 158 | for(uint8_t i = 0; i < 9; i++) // read Scratchpad bytes |
hudakz | 4:adf4e7972d73 | 159 | data[i] = oneWire.read(); |
hudakz | 4:adf4e7972d73 | 160 | |
hudakz | 4:adf4e7972d73 | 161 | data[4] |= (res - 9) << 5; // update configuration byte (set resolution) |
hudakz | 4:adf4e7972d73 | 162 | oneWire.reset(); |
hudakz | 4:adf4e7972d73 | 163 | oneWire.skip(); |
hudakz | 4:adf4e7972d73 | 164 | oneWire.write(0x4E); // to write into Scratchpad |
hudakz | 4:adf4e7972d73 | 165 | for(uint8_t i = 2; i < 5; i++) // write three bytes (2nd, 3rd, 4th) into Scratchpad |
hudakz | 4:adf4e7972d73 | 166 | oneWire.write(data[i]); |
hudakz | 4:adf4e7972d73 | 167 | } |
hudakz | 4:adf4e7972d73 | 168 | |
hudakz | 4:adf4e7972d73 | 169 | |
hudakz | 4:adf4e7972d73 | 170 | /** |
hudakz | 0:433af64321d5 | 171 | * @brief Starts temperature conversion |
hudakz | 0:433af64321d5 | 172 | * @note The time to complete the converion depends on the selected resolusion |
hudakz | 0:433af64321d5 | 173 | * @param |
hudakz | 0:433af64321d5 | 174 | * @retval |
hudakz | 0:433af64321d5 | 175 | */ |
hudakz | 0:433af64321d5 | 176 | void DS1820::startConversion(void) { |
hudakz | 0:433af64321d5 | 177 | if(present) { |
hudakz | 0:433af64321d5 | 178 | oneWire.reset(); |
hudakz | 0:433af64321d5 | 179 | oneWire.skip(); |
hudakz | 3:a250babd0a9f | 180 | oneWire.write(0x44); //start temperature conversion |
hudakz | 0:433af64321d5 | 181 | } |
hudakz | 0:433af64321d5 | 182 | } |
hudakz | 0:433af64321d5 | 183 | |
hudakz | 0:433af64321d5 | 184 | /** |
hudakz | 0:433af64321d5 | 185 | * @brief Reads temperature from the chip's scratchpad |
hudakz | 0:433af64321d5 | 186 | * @note |
hudakz | 0:433af64321d5 | 187 | * @param |
hudakz | 0:433af64321d5 | 188 | * @retval Floating point temperature value |
hudakz | 0:433af64321d5 | 189 | */ |
hudakz | 0:433af64321d5 | 190 | float DS1820::read(void) { |
hudakz | 0:433af64321d5 | 191 | if(present) { |
hudakz | 0:433af64321d5 | 192 | oneWire.reset(); |
hudakz | 0:433af64321d5 | 193 | oneWire.skip(); |
hudakz | 0:433af64321d5 | 194 | oneWire.write(0xBE); // to read Scratchpad |
hudakz | 0:433af64321d5 | 195 | for(uint8_t i = 0; i < 9; i++) |
hudakz | 0:433af64321d5 | 196 | data[i] = oneWire.read(); |
hudakz | 0:433af64321d5 | 197 | |
hudakz | 4:adf4e7972d73 | 198 | // Convert the raw bytes to a 16bit unsigned value |
hudakz | 0:433af64321d5 | 199 | uint16_t* p_word = reinterpret_cast < uint16_t * > (&data[0]); |
hudakz | 0:433af64321d5 | 200 | |
hudakz | 0:433af64321d5 | 201 | #if DEBUG |
hudakz | 0:433af64321d5 | 202 | serial.printf("raw = %#x\r\n", *p_word); |
hudakz | 0:433af64321d5 | 203 | #endif |
hudakz | 0:433af64321d5 | 204 | |
hudakz | 0:433af64321d5 | 205 | if(type_s) { |
hudakz | 4:adf4e7972d73 | 206 | *p_word = *p_word << 3; // 9 bit resolution, max conversion time = 750ms |
hudakz | 0:433af64321d5 | 207 | if(data[7] == 0x10) { |
hudakz | 0:433af64321d5 | 208 | |
hudakz | 0:433af64321d5 | 209 | // "count remain" gives full 12 bit resolution |
hudakz | 0:433af64321d5 | 210 | *p_word = (*p_word & 0xFFF0) + 12 - data[6]; |
hudakz | 0:433af64321d5 | 211 | } |
hudakz | 0:433af64321d5 | 212 | |
hudakz | 4:adf4e7972d73 | 213 | // Convert the raw bytes to a 16bit signed fixed point value : |
hudakz | 4:adf4e7972d73 | 214 | // 1 sign bit, 7 integer bits, 8 fractional bits (two’s compliment |
hudakz | 4:adf4e7972d73 | 215 | // and the LSB of the 16bit binary number represents 1/256th of a unit). |
hudakz | 0:433af64321d5 | 216 | *p_word = *p_word << 4; |
hudakz | 4:adf4e7972d73 | 217 | // Convert to floating point value |
hudakz | 0:433af64321d5 | 218 | return(toFloat(*p_word)); |
hudakz | 0:433af64321d5 | 219 | } |
hudakz | 0:433af64321d5 | 220 | else { |
hudakz | 4:adf4e7972d73 | 221 | uint8_t cfg = (data[4] & 0x60); // default 12bit resolution, max conversion time = 750ms |
hudakz | 4:adf4e7972d73 | 222 | |
hudakz | 4:adf4e7972d73 | 223 | // at lower resolution, the low bits are undefined, so let's clear them |
hudakz | 0:433af64321d5 | 224 | if(cfg == 0x00) |
hudakz | 4:adf4e7972d73 | 225 | *p_word = *p_word &~7; // 9bit resolution, max conversion time = 93.75ms |
hudakz | 0:433af64321d5 | 226 | else |
hudakz | 0:433af64321d5 | 227 | if(cfg == 0x20) |
hudakz | 4:adf4e7972d73 | 228 | *p_word = *p_word &~3; // 10bit resolution, max conversion time = 187.5ms |
hudakz | 0:433af64321d5 | 229 | else |
hudakz | 0:433af64321d5 | 230 | if(cfg == 0x40) |
hudakz | 4:adf4e7972d73 | 231 | *p_word = *p_word &~1; // 11bit resolution, max conversion time = 375ms |
hudakz | 4:adf4e7972d73 | 232 | |
hudakz | 4:adf4e7972d73 | 233 | // Convert the raw bytes to a 16bit signed fixed point value : |
hudakz | 4:adf4e7972d73 | 234 | // 1 sign bit, 7 integer bits, 8 fractional bits (two’s compliment |
hudakz | 4:adf4e7972d73 | 235 | // and the LSB of the 16bit binary number represents 1/256th of a unit). |
hudakz | 4:adf4e7972d73 | 236 | *p_word = *p_word << 4; |
hudakz | 4:adf4e7972d73 | 237 | // Convert to floating point value |
hudakz | 0:433af64321d5 | 238 | return(toFloat(*p_word)); |
hudakz | 0:433af64321d5 | 239 | } |
hudakz | 0:433af64321d5 | 240 | } |
hudakz | 0:433af64321d5 | 241 | else |
hudakz | 0:433af64321d5 | 242 | return 0; |
hudakz | 0:433af64321d5 | 243 | } |
hudakz | 0:433af64321d5 | 244 | |
hudakz | 0:433af64321d5 | 245 | /** |
hudakz | 3:a250babd0a9f | 246 | * @brief Converts a 16bit signed fixed point value to floating point value |
hudakz | 0:433af64321d5 | 247 | * @note The 16bit unsigned integer represnts actually |
hudakz | 0:433af64321d5 | 248 | * a 16bit signed fixed point value: |
hudakz | 4:adf4e7972d73 | 249 | * 1 sign bit, 7 integer bits, 8 fractional bits (two’s compliment |
hudakz | 4:adf4e7972d73 | 250 | * and the LSB of the 16bit binary number represents 1/256th of a unit). |
hudakz | 0:433af64321d5 | 251 | * @param 16bit unsigned integer |
hudakz | 4:adf4e7972d73 | 252 | * @retval Floating point value |
hudakz | 0:433af64321d5 | 253 | */ |
hudakz | 0:433af64321d5 | 254 | float DS1820::toFloat(uint16_t word) { |
hudakz | 0:433af64321d5 | 255 | if(word & 0x8000) |
hudakz | 0:433af64321d5 | 256 | return (-float(uint16_t(~word + 1)) / 256.0f); |
hudakz | 0:433af64321d5 | 257 | else |
hudakz | 0:433af64321d5 | 258 | return (float(word) / 256.0f); |
hudakz | 0:433af64321d5 | 259 | } |
hudakz | 0:433af64321d5 | 260 |