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@0:433af64321d5, 2015-03-15 (annotated)
- Committer:
- hudakz
- Date:
- Sun Mar 15 14:16:16 2015 +0000
- Revision:
- 0:433af64321d5
- Child:
- 1:e4689408d617
Dallas' DS1820 family temperature sensor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:433af64321d5 | 1 | #include "DS1820.h" |
hudakz | 0:433af64321d5 | 2 | |
hudakz | 0:433af64321d5 | 3 | #define DEBUG 1 |
hudakz | 0:433af64321d5 | 4 | |
hudakz | 0:433af64321d5 | 5 | #if DEBUG |
hudakz | 0:433af64321d5 | 6 | extern Serial serial; |
hudakz | 0:433af64321d5 | 7 | #endif |
hudakz | 0:433af64321d5 | 8 | |
hudakz | 0:433af64321d5 | 9 | /** |
hudakz | 0:433af64321d5 | 10 | * @brief Constructs a generic DS1820 sensor |
hudakz | 0:433af64321d5 | 11 | * @note begin() must be called to detect and initialize the actual model |
hudakz | 0:433af64321d5 | 12 | * @param Name of data pin |
hudakz | 0:433af64321d5 | 13 | * @retval |
hudakz | 0:433af64321d5 | 14 | */ |
hudakz | 0:433af64321d5 | 15 | DS1820::DS1820(PinName pin) : |
hudakz | 0:433af64321d5 | 16 | oneWire(pin) { |
hudakz | 0:433af64321d5 | 17 | present = 0; |
hudakz | 0:433af64321d5 | 18 | type_s = 0; |
hudakz | 0:433af64321d5 | 19 | } |
hudakz | 0:433af64321d5 | 20 | |
hudakz | 0:433af64321d5 | 21 | /** |
hudakz | 0:433af64321d5 | 22 | * @brief Constructs a specific model |
hudakz | 0:433af64321d5 | 23 | * @note No need to call begin() to detect and initialize the model |
hudakz | 0:433af64321d5 | 24 | * @param One character model name: 'S', 's', 'B' or 'b' |
hudakz | 0:433af64321d5 | 25 | * Name of data pin |
hudakz | 0:433af64321d5 | 26 | * @retval |
hudakz | 0:433af64321d5 | 27 | */ |
hudakz | 0:433af64321d5 | 28 | DS1820::DS1820(char model, PinName pin) : |
hudakz | 0:433af64321d5 | 29 | oneWire(pin) { |
hudakz | 0:433af64321d5 | 30 | if((model == 'S') or (model == 's')) { |
hudakz | 0:433af64321d5 | 31 | present = 1; |
hudakz | 0:433af64321d5 | 32 | type_s = 1; |
hudakz | 0:433af64321d5 | 33 | } |
hudakz | 0:433af64321d5 | 34 | else if((model == 'B') or (model == 'b')) { |
hudakz | 0:433af64321d5 | 35 | present = 1; |
hudakz | 0:433af64321d5 | 36 | type_s = 0; |
hudakz | 0:433af64321d5 | 37 | } |
hudakz | 0:433af64321d5 | 38 | else |
hudakz | 0:433af64321d5 | 39 | present = 0; |
hudakz | 0:433af64321d5 | 40 | } |
hudakz | 0:433af64321d5 | 41 | |
hudakz | 0:433af64321d5 | 42 | /** |
hudakz | 0:433af64321d5 | 43 | * @brief Detects and initializes the actual DS1820 model |
hudakz | 0:433af64321d5 | 44 | * @note |
hudakz | 0:433af64321d5 | 45 | * @param |
hudakz | 0:433af64321d5 | 46 | * @retval true: if a DS1820 family sensor was detected and initialized |
hudakz | 0:433af64321d5 | 47 | false: otherwise |
hudakz | 0:433af64321d5 | 48 | */ |
hudakz | 0:433af64321d5 | 49 | bool DS1820::begin(void) { |
hudakz | 0:433af64321d5 | 50 | oneWire.reset_search(); |
hudakz | 0:433af64321d5 | 51 | wait_ms(250); |
hudakz | 0:433af64321d5 | 52 | if(!oneWire.search(addr)) { |
hudakz | 0:433af64321d5 | 53 | #if DEBUG |
hudakz | 0:433af64321d5 | 54 | serial.printf("No addresses.\r\n"); |
hudakz | 0:433af64321d5 | 55 | #endif |
hudakz | 0:433af64321d5 | 56 | oneWire.reset_search(); |
hudakz | 0:433af64321d5 | 57 | wait_ms(250); |
hudakz | 0:433af64321d5 | 58 | return false; |
hudakz | 0:433af64321d5 | 59 | } |
hudakz | 0:433af64321d5 | 60 | |
hudakz | 0:433af64321d5 | 61 | #if DEBUG |
hudakz | 0:433af64321d5 | 62 | serial.printf("ROM ="); |
hudakz | 0:433af64321d5 | 63 | for(uint8_t i = 0; i < 8; i++) { |
hudakz | 0:433af64321d5 | 64 | serial.printf(" %x", addr[i]); |
hudakz | 0:433af64321d5 | 65 | } |
hudakz | 0:433af64321d5 | 66 | serial.printf("\r\n"); |
hudakz | 0:433af64321d5 | 67 | #endif |
hudakz | 0:433af64321d5 | 68 | |
hudakz | 0:433af64321d5 | 69 | if(OneWire::crc8(addr, 7) == addr[7]) { |
hudakz | 0:433af64321d5 | 70 | present = 1; |
hudakz | 0:433af64321d5 | 71 | |
hudakz | 0:433af64321d5 | 72 | // the first ROM byte indicates which chip |
hudakz | 0:433af64321d5 | 73 | switch(addr[0]) { |
hudakz | 0:433af64321d5 | 74 | case 0x10: |
hudakz | 0:433af64321d5 | 75 | type_s = 1; |
hudakz | 0:433af64321d5 | 76 | #if DEBUG |
hudakz | 0:433af64321d5 | 77 | serial.printf("DS18S20 or old DS1820\r\n"); |
hudakz | 0:433af64321d5 | 78 | #endif |
hudakz | 0:433af64321d5 | 79 | break; |
hudakz | 0:433af64321d5 | 80 | |
hudakz | 0:433af64321d5 | 81 | case 0x28: |
hudakz | 0:433af64321d5 | 82 | type_s = 0; |
hudakz | 0:433af64321d5 | 83 | #if DEBUG |
hudakz | 0:433af64321d5 | 84 | serial.printf("DS18B20\r\n"); |
hudakz | 0:433af64321d5 | 85 | #endif |
hudakz | 0:433af64321d5 | 86 | break; |
hudakz | 0:433af64321d5 | 87 | |
hudakz | 0:433af64321d5 | 88 | case 0x22: |
hudakz | 0:433af64321d5 | 89 | type_s = 0; |
hudakz | 0:433af64321d5 | 90 | #if DEBUG |
hudakz | 0:433af64321d5 | 91 | serial.printf("DS1822\r\n"); |
hudakz | 0:433af64321d5 | 92 | #endif |
hudakz | 0:433af64321d5 | 93 | break; |
hudakz | 0:433af64321d5 | 94 | |
hudakz | 0:433af64321d5 | 95 | default: |
hudakz | 0:433af64321d5 | 96 | present = 0; |
hudakz | 0:433af64321d5 | 97 | #if DEBUG |
hudakz | 0:433af64321d5 | 98 | serial.printf("Device doesn't belong to the DS1820 family\r\n"); |
hudakz | 0:433af64321d5 | 99 | return false; |
hudakz | 0:433af64321d5 | 100 | #endif |
hudakz | 0:433af64321d5 | 101 | } |
hudakz | 0:433af64321d5 | 102 | return true; |
hudakz | 0:433af64321d5 | 103 | } |
hudakz | 0:433af64321d5 | 104 | #if DEBUG |
hudakz | 0:433af64321d5 | 105 | else { |
hudakz | 0:433af64321d5 | 106 | serial.printf("Invalid CRC!\r\n"); |
hudakz | 0:433af64321d5 | 107 | return false; |
hudakz | 0:433af64321d5 | 108 | } |
hudakz | 0:433af64321d5 | 109 | #endif |
hudakz | 0:433af64321d5 | 110 | } |
hudakz | 0:433af64321d5 | 111 | |
hudakz | 0:433af64321d5 | 112 | /** |
hudakz | 0:433af64321d5 | 113 | * @brief Starts temperature conversion |
hudakz | 0:433af64321d5 | 114 | * @note The time to complete the converion depends on the selected resolusion |
hudakz | 0:433af64321d5 | 115 | * @param |
hudakz | 0:433af64321d5 | 116 | * @retval |
hudakz | 0:433af64321d5 | 117 | */ |
hudakz | 0:433af64321d5 | 118 | void DS1820::startConversion(void) { |
hudakz | 0:433af64321d5 | 119 | if(present) { |
hudakz | 0:433af64321d5 | 120 | oneWire.reset(); |
hudakz | 0:433af64321d5 | 121 | |
hudakz | 0:433af64321d5 | 122 | //oneWire.select(addr); |
hudakz | 0:433af64321d5 | 123 | oneWire.skip(); |
hudakz | 0:433af64321d5 | 124 | oneWire.write(0x44); //start conversion |
hudakz | 0:433af64321d5 | 125 | } |
hudakz | 0:433af64321d5 | 126 | } |
hudakz | 0:433af64321d5 | 127 | |
hudakz | 0:433af64321d5 | 128 | /** |
hudakz | 0:433af64321d5 | 129 | * @brief Reads temperature from the chip's scratchpad |
hudakz | 0:433af64321d5 | 130 | * @note |
hudakz | 0:433af64321d5 | 131 | * @param |
hudakz | 0:433af64321d5 | 132 | * @retval Floating point temperature value |
hudakz | 0:433af64321d5 | 133 | */ |
hudakz | 0:433af64321d5 | 134 | float DS1820::read(void) { |
hudakz | 0:433af64321d5 | 135 | if(present) { |
hudakz | 0:433af64321d5 | 136 | oneWire.reset(); |
hudakz | 0:433af64321d5 | 137 | oneWire.skip(); |
hudakz | 0:433af64321d5 | 138 | oneWire.write(0xBE); // to read Scratchpad |
hudakz | 0:433af64321d5 | 139 | for(uint8_t i = 0; i < 9; i++) |
hudakz | 0:433af64321d5 | 140 | data[i] = oneWire.read(); |
hudakz | 0:433af64321d5 | 141 | |
hudakz | 0:433af64321d5 | 142 | // Convert the raw bytes to a 16bit signed fixed point value : |
hudakz | 0:433af64321d5 | 143 | // 1 sign bit, 7 integer bits, 8 fractional bits (two’s compliment |
hudakz | 0:433af64321d5 | 144 | // ie. the LSB of the 16bit binary number represents 1/256th of a unit). |
hudakz | 0:433af64321d5 | 145 | |
hudakz | 0:433af64321d5 | 146 | uint16_t* p_word = reinterpret_cast < uint16_t * > (&data[0]); |
hudakz | 0:433af64321d5 | 147 | |
hudakz | 0:433af64321d5 | 148 | #if DEBUG |
hudakz | 0:433af64321d5 | 149 | serial.printf("raw = %#x\r\n", *p_word); |
hudakz | 0:433af64321d5 | 150 | #endif |
hudakz | 0:433af64321d5 | 151 | |
hudakz | 0:433af64321d5 | 152 | |
hudakz | 0:433af64321d5 | 153 | if(type_s) { |
hudakz | 0:433af64321d5 | 154 | *p_word = *p_word << 3; // default 9 bit resolution |
hudakz | 0:433af64321d5 | 155 | if(data[7] == 0x10) { |
hudakz | 0:433af64321d5 | 156 | |
hudakz | 0:433af64321d5 | 157 | // "count remain" gives full 12 bit resolution |
hudakz | 0:433af64321d5 | 158 | *p_word = (*p_word & 0xFFF0) + 12 - data[6]; |
hudakz | 0:433af64321d5 | 159 | } |
hudakz | 0:433af64321d5 | 160 | |
hudakz | 0:433af64321d5 | 161 | *p_word = *p_word << 4; |
hudakz | 0:433af64321d5 | 162 | return(toFloat(*p_word)); |
hudakz | 0:433af64321d5 | 163 | } |
hudakz | 0:433af64321d5 | 164 | else { |
hudakz | 0:433af64321d5 | 165 | uint8_t cfg = (data[4] & 0x60); |
hudakz | 0:433af64321d5 | 166 | // at lower res, the low bits are undefined, so let's zero them |
hudakz | 0:433af64321d5 | 167 | |
hudakz | 0:433af64321d5 | 168 | if(cfg == 0x00) |
hudakz | 0:433af64321d5 | 169 | *p_word = *p_word &~7; // 9 bit resolution, 93.75 ms |
hudakz | 0:433af64321d5 | 170 | else |
hudakz | 0:433af64321d5 | 171 | if(cfg == 0x20) |
hudakz | 0:433af64321d5 | 172 | *p_word = *p_word &~3; // 10 bit res, 187.5 ms |
hudakz | 0:433af64321d5 | 173 | else |
hudakz | 0:433af64321d5 | 174 | if(cfg == 0x40) |
hudakz | 0:433af64321d5 | 175 | *p_word = *p_word &~1; // 11 bit res, 375 ms |
hudakz | 0:433af64321d5 | 176 | *p_word = *p_word << 4; // default is 12 bit resolution |
hudakz | 0:433af64321d5 | 177 | return(toFloat(*p_word)); |
hudakz | 0:433af64321d5 | 178 | } |
hudakz | 0:433af64321d5 | 179 | } |
hudakz | 0:433af64321d5 | 180 | else |
hudakz | 0:433af64321d5 | 181 | return 0; |
hudakz | 0:433af64321d5 | 182 | } |
hudakz | 0:433af64321d5 | 183 | |
hudakz | 0:433af64321d5 | 184 | /** |
hudakz | 0:433af64321d5 | 185 | * @brief Converts a 16bit signed fixed point value to float |
hudakz | 0:433af64321d5 | 186 | * @note The 16bit unsigned integer represnts actually |
hudakz | 0:433af64321d5 | 187 | * a 16bit signed fixed point value: |
hudakz | 0:433af64321d5 | 188 | * 1 sign bit, 7 integer bits, 8 fractional bits |
hudakz | 0:433af64321d5 | 189 | * (two’s compliment ie. the LSB of the 16bit binary number |
hudakz | 0:433af64321d5 | 190 | * represents 1/256th of a unit). |
hudakz | 0:433af64321d5 | 191 | * @param 16bit unsigned integer |
hudakz | 0:433af64321d5 | 192 | * @retval Floating point temperature value |
hudakz | 0:433af64321d5 | 193 | */ |
hudakz | 0:433af64321d5 | 194 | float DS1820::toFloat(uint16_t word) { |
hudakz | 0:433af64321d5 | 195 | //word = word << 4; |
hudakz | 0:433af64321d5 | 196 | if(word & 0x8000) |
hudakz | 0:433af64321d5 | 197 | return (-float(uint16_t(~word + 1)) / 256.0f); |
hudakz | 0:433af64321d5 | 198 | else |
hudakz | 0:433af64321d5 | 199 | return (float(word) / 256.0f); |
hudakz | 0:433af64321d5 | 200 | } |
hudakz | 0:433af64321d5 | 201 |