Dallas' DS1820 family temperature sensor. For more details see [https://developer.mbed.org/users/hudakz/code/DS1820/wiki/Homepage]

Dependencies:   OneWire

Fork of DS1820 by Zoltan Hudak

Committer:
hudakz
Date:
Thu Mar 19 19:11:12 2015 +0000
Revision:
3:a250babd0a9f
Parent:
2:b7ad1da7331a
Child:
4:adf4e7972d73
Comments added

Who changed what in which revision?

UserRevisionLine numberNew 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 0:433af64321d5 138 * @brief Starts temperature conversion
hudakz 0:433af64321d5 139 * @note The time to complete the converion depends on the selected resolusion
hudakz 0:433af64321d5 140 * @param
hudakz 0:433af64321d5 141 * @retval
hudakz 0:433af64321d5 142 */
hudakz 0:433af64321d5 143 void DS1820::startConversion(void) {
hudakz 0:433af64321d5 144 if(present) {
hudakz 0:433af64321d5 145 oneWire.reset();
hudakz 0:433af64321d5 146 oneWire.skip();
hudakz 3:a250babd0a9f 147 oneWire.write(0x44); //start temperature conversion
hudakz 0:433af64321d5 148 }
hudakz 0:433af64321d5 149 }
hudakz 0:433af64321d5 150
hudakz 0:433af64321d5 151 /**
hudakz 0:433af64321d5 152 * @brief Reads temperature from the chip's scratchpad
hudakz 0:433af64321d5 153 * @note
hudakz 0:433af64321d5 154 * @param
hudakz 0:433af64321d5 155 * @retval Floating point temperature value
hudakz 0:433af64321d5 156 */
hudakz 0:433af64321d5 157 float DS1820::read(void) {
hudakz 0:433af64321d5 158 if(present) {
hudakz 0:433af64321d5 159 oneWire.reset();
hudakz 0:433af64321d5 160 oneWire.skip();
hudakz 0:433af64321d5 161 oneWire.write(0xBE); // to read Scratchpad
hudakz 0:433af64321d5 162 for(uint8_t i = 0; i < 9; i++)
hudakz 0:433af64321d5 163 data[i] = oneWire.read();
hudakz 0:433af64321d5 164
hudakz 0:433af64321d5 165 // Convert the raw bytes to a 16bit signed fixed point value :
hudakz 0:433af64321d5 166 // 1 sign bit, 7 integer bits, 8 fractional bits (two’s compliment
hudakz 0:433af64321d5 167 // ie. the LSB of the 16bit binary number represents 1/256th of a unit).
hudakz 3:a250babd0a9f 168 // Finally the 16bit signed fixed point value is converted
hudakz 3:a250babd0a9f 169 // to floating point value by calling the toFloat(value) function.
hudakz 0:433af64321d5 170
hudakz 0:433af64321d5 171 uint16_t* p_word = reinterpret_cast < uint16_t * > (&data[0]);
hudakz 0:433af64321d5 172
hudakz 0:433af64321d5 173 #if DEBUG
hudakz 0:433af64321d5 174 serial.printf("raw = %#x\r\n", *p_word);
hudakz 0:433af64321d5 175 #endif
hudakz 0:433af64321d5 176
hudakz 0:433af64321d5 177
hudakz 0:433af64321d5 178 if(type_s) {
hudakz 0:433af64321d5 179 *p_word = *p_word << 3; // default 9 bit resolution
hudakz 0:433af64321d5 180 if(data[7] == 0x10) {
hudakz 0:433af64321d5 181
hudakz 0:433af64321d5 182 // "count remain" gives full 12 bit resolution
hudakz 0:433af64321d5 183 *p_word = (*p_word & 0xFFF0) + 12 - data[6];
hudakz 0:433af64321d5 184 }
hudakz 0:433af64321d5 185
hudakz 0:433af64321d5 186 *p_word = *p_word << 4;
hudakz 0:433af64321d5 187 return(toFloat(*p_word));
hudakz 0:433af64321d5 188 }
hudakz 0:433af64321d5 189 else {
hudakz 0:433af64321d5 190 uint8_t cfg = (data[4] & 0x60);
hudakz 0:433af64321d5 191 // at lower res, the low bits are undefined, so let's zero them
hudakz 0:433af64321d5 192
hudakz 0:433af64321d5 193 if(cfg == 0x00)
hudakz 0:433af64321d5 194 *p_word = *p_word &~7; // 9 bit resolution, 93.75 ms
hudakz 0:433af64321d5 195 else
hudakz 0:433af64321d5 196 if(cfg == 0x20)
hudakz 0:433af64321d5 197 *p_word = *p_word &~3; // 10 bit res, 187.5 ms
hudakz 0:433af64321d5 198 else
hudakz 0:433af64321d5 199 if(cfg == 0x40)
hudakz 0:433af64321d5 200 *p_word = *p_word &~1; // 11 bit res, 375 ms
hudakz 0:433af64321d5 201 *p_word = *p_word << 4; // default is 12 bit resolution
hudakz 0:433af64321d5 202 return(toFloat(*p_word));
hudakz 0:433af64321d5 203 }
hudakz 0:433af64321d5 204 }
hudakz 0:433af64321d5 205 else
hudakz 0:433af64321d5 206 return 0;
hudakz 0:433af64321d5 207 }
hudakz 0:433af64321d5 208
hudakz 0:433af64321d5 209 /**
hudakz 3:a250babd0a9f 210 * @brief Converts a 16bit signed fixed point value to floating point value
hudakz 0:433af64321d5 211 * @note The 16bit unsigned integer represnts actually
hudakz 0:433af64321d5 212 * a 16bit signed fixed point value:
hudakz 0:433af64321d5 213 * 1 sign bit, 7 integer bits, 8 fractional bits
hudakz 0:433af64321d5 214 * (two’s compliment ie. the LSB of the 16bit binary number
hudakz 0:433af64321d5 215 * represents 1/256th of a unit).
hudakz 0:433af64321d5 216 * @param 16bit unsigned integer
hudakz 0:433af64321d5 217 * @retval Floating point temperature value
hudakz 0:433af64321d5 218 */
hudakz 0:433af64321d5 219 float DS1820::toFloat(uint16_t word) {
hudakz 0:433af64321d5 220 if(word & 0x8000)
hudakz 0:433af64321d5 221 return (-float(uint16_t(~word + 1)) / 256.0f);
hudakz 0:433af64321d5 222 else
hudakz 0:433af64321d5 223 return (float(word) / 256.0f);
hudakz 0:433af64321d5 224 }
hudakz 0:433af64321d5 225