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