For amx31820

Dependencies:   OneWire

Dependents:   MAX31820

Fork of DS1820 by Zoltan Hudak

Committer:
hudakz
Date:
Thu Mar 26 17:36:56 2015 +0000
Revision:
6:518950e436be
Parent:
4:adf4e7972d73
Child:
8:8dfdd1603e4d
read() function optimized

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 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 6:518950e436be 19 * serial.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 3:a250babd0a9f 24 * serial.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 1:e4689408d617 35 #define DEBUG 0
hudakz 0:433af64321d5 36
hudakz 0:433af64321d5 37 #if DEBUG
hudakz 0:433af64321d5 38 extern Serial serial;
hudakz 0:433af64321d5 39 #endif
hudakz 0:433af64321d5 40
hudakz 0:433af64321d5 41 /**
hudakz 0:433af64321d5 42 * @brief Constructs a generic DS1820 sensor
hudakz 0:433af64321d5 43 * @note begin() must be called to detect and initialize the actual model
hudakz 3:a250babd0a9f 44 * @param pin: Name of data pin
hudakz 0:433af64321d5 45 * @retval
hudakz 0:433af64321d5 46 */
hudakz 0:433af64321d5 47 DS1820::DS1820(PinName pin) :
hudakz 0:433af64321d5 48 oneWire(pin) {
hudakz 0:433af64321d5 49 present = 0;
hudakz 0:433af64321d5 50 type_s = 0;
hudakz 0:433af64321d5 51 }
hudakz 0:433af64321d5 52
hudakz 0:433af64321d5 53 /**
hudakz 0:433af64321d5 54 * @brief Constructs a specific model
hudakz 0:433af64321d5 55 * @note No need to call begin() to detect and initialize the model
hudakz 3:a250babd0a9f 56 * @param model: One character model name: 'S', 's', 'B' or 'b'
hudakz 3:a250babd0a9f 57 * pin: Name of data pin
hudakz 0:433af64321d5 58 * @retval
hudakz 0:433af64321d5 59 */
hudakz 0:433af64321d5 60 DS1820::DS1820(char model, PinName pin) :
hudakz 0:433af64321d5 61 oneWire(pin) {
hudakz 0:433af64321d5 62 if((model == 'S') or (model == 's')) {
hudakz 0:433af64321d5 63 present = 1;
hudakz 0:433af64321d5 64 type_s = 1;
hudakz 0:433af64321d5 65 }
hudakz 0:433af64321d5 66 else if((model == 'B') or (model == 'b')) {
hudakz 0:433af64321d5 67 present = 1;
hudakz 0:433af64321d5 68 type_s = 0;
hudakz 0:433af64321d5 69 }
hudakz 0:433af64321d5 70 else
hudakz 0:433af64321d5 71 present = 0;
hudakz 0:433af64321d5 72 }
hudakz 0:433af64321d5 73
hudakz 0:433af64321d5 74 /**
hudakz 0:433af64321d5 75 * @brief Detects and initializes the actual DS1820 model
hudakz 0:433af64321d5 76 * @note
hudakz 0:433af64321d5 77 * @param
hudakz 0:433af64321d5 78 * @retval true: if a DS1820 family sensor was detected and initialized
hudakz 0:433af64321d5 79 false: otherwise
hudakz 0:433af64321d5 80 */
hudakz 0:433af64321d5 81 bool DS1820::begin(void) {
hudakz 0:433af64321d5 82 oneWire.reset_search();
hudakz 0:433af64321d5 83 wait_ms(250);
hudakz 0:433af64321d5 84 if(!oneWire.search(addr)) {
hudakz 0:433af64321d5 85 #if DEBUG
hudakz 0:433af64321d5 86 serial.printf("No addresses.\r\n");
hudakz 0:433af64321d5 87 #endif
hudakz 0:433af64321d5 88 oneWire.reset_search();
hudakz 0:433af64321d5 89 wait_ms(250);
hudakz 0:433af64321d5 90 return false;
hudakz 0:433af64321d5 91 }
hudakz 0:433af64321d5 92
hudakz 0:433af64321d5 93 #if DEBUG
hudakz 0:433af64321d5 94 serial.printf("ROM =");
hudakz 0:433af64321d5 95 for(uint8_t i = 0; i < 8; i++) {
hudakz 0:433af64321d5 96 serial.printf(" %x", addr[i]);
hudakz 0:433af64321d5 97 }
hudakz 0:433af64321d5 98 serial.printf("\r\n");
hudakz 0:433af64321d5 99 #endif
hudakz 0:433af64321d5 100
hudakz 0:433af64321d5 101 if(OneWire::crc8(addr, 7) == addr[7]) {
hudakz 0:433af64321d5 102 present = 1;
hudakz 0:433af64321d5 103
hudakz 0:433af64321d5 104 // the first ROM byte indicates which chip
hudakz 0:433af64321d5 105 switch(addr[0]) {
hudakz 0:433af64321d5 106 case 0x10:
hudakz 0:433af64321d5 107 type_s = 1;
hudakz 0:433af64321d5 108 #if DEBUG
hudakz 0:433af64321d5 109 serial.printf("DS18S20 or old DS1820\r\n");
hudakz 0:433af64321d5 110 #endif
hudakz 0:433af64321d5 111 break;
hudakz 0:433af64321d5 112
hudakz 0:433af64321d5 113 case 0x28:
hudakz 0:433af64321d5 114 type_s = 0;
hudakz 0:433af64321d5 115 #if DEBUG
hudakz 0:433af64321d5 116 serial.printf("DS18B20\r\n");
hudakz 0:433af64321d5 117 #endif
hudakz 0:433af64321d5 118 break;
hudakz 0:433af64321d5 119
hudakz 0:433af64321d5 120 case 0x22:
hudakz 0:433af64321d5 121 type_s = 0;
hudakz 0:433af64321d5 122 #if DEBUG
hudakz 0:433af64321d5 123 serial.printf("DS1822\r\n");
hudakz 0:433af64321d5 124 #endif
hudakz 0:433af64321d5 125 break;
hudakz 0:433af64321d5 126
hudakz 0:433af64321d5 127 default:
hudakz 0:433af64321d5 128 present = 0;
hudakz 0:433af64321d5 129 #if DEBUG
hudakz 0:433af64321d5 130 serial.printf("Device doesn't belong to the DS1820 family\r\n");
hudakz 2:b7ad1da7331a 131 #endif
hudakz 0:433af64321d5 132 return false;
hudakz 0:433af64321d5 133 }
hudakz 0:433af64321d5 134 return true;
hudakz 0:433af64321d5 135 }
hudakz 2:b7ad1da7331a 136 else {
hudakz 0:433af64321d5 137 #if DEBUG
hudakz 0:433af64321d5 138 serial.printf("Invalid CRC!\r\n");
hudakz 2:b7ad1da7331a 139 #endif
hudakz 0:433af64321d5 140 return false;
hudakz 0:433af64321d5 141 }
hudakz 0:433af64321d5 142 }
hudakz 0:433af64321d5 143
hudakz 0:433af64321d5 144 /**
hudakz 6:518950e436be 145 * @brief Sets temperature-to-digital conversion resolution.
hudakz 4:adf4e7972d73 146 * @note The configuration register allows the user to set the resolution
hudakz 6:518950e436be 147 * of the temperature-to-digital conversion to 9, 10, 11, or 12 bits.
hudakz 6:518950e436be 148 * Defaults to 12-bit resolution for DS18B20.
hudakz 6:518950e436be 149 * DS18S20 allows only 9-bit resolution.
hudakz 6:518950e436be 150 * @param res: Resolution of the temperature-to-digital conversion in bits.
hudakz 4:adf4e7972d73 151 * @retval
hudakz 4:adf4e7972d73 152 */
hudakz 4:adf4e7972d73 153 void DS1820::setResolution(uint8_t res) {
hudakz 4:adf4e7972d73 154 // keep resolution within limits
hudakz 4:adf4e7972d73 155 if(res > 12)
hudakz 4:adf4e7972d73 156 res = 12;
hudakz 4:adf4e7972d73 157 if(res < 9)
hudakz 4:adf4e7972d73 158 res = 9;
hudakz 4:adf4e7972d73 159 if(type_s)
hudakz 4:adf4e7972d73 160 res = 9;
hudakz 4:adf4e7972d73 161
hudakz 4:adf4e7972d73 162 oneWire.reset();
hudakz 4:adf4e7972d73 163 oneWire.skip();
hudakz 4:adf4e7972d73 164 oneWire.write(0xBE); // to read Scratchpad
hudakz 4:adf4e7972d73 165 for(uint8_t i = 0; i < 9; i++) // read Scratchpad bytes
hudakz 6:518950e436be 166 data[i] = oneWire.read();
hudakz 4:adf4e7972d73 167
hudakz 4:adf4e7972d73 168 data[4] |= (res - 9) << 5; // update configuration byte (set resolution)
hudakz 4:adf4e7972d73 169 oneWire.reset();
hudakz 4:adf4e7972d73 170 oneWire.skip();
hudakz 4:adf4e7972d73 171 oneWire.write(0x4E); // to write into Scratchpad
hudakz 4:adf4e7972d73 172 for(uint8_t i = 2; i < 5; i++) // write three bytes (2nd, 3rd, 4th) into Scratchpad
hudakz 4:adf4e7972d73 173 oneWire.write(data[i]);
hudakz 4:adf4e7972d73 174 }
hudakz 4:adf4e7972d73 175
hudakz 4:adf4e7972d73 176
hudakz 4:adf4e7972d73 177 /**
hudakz 0:433af64321d5 178 * @brief Starts temperature conversion
hudakz 6:518950e436be 179 * @note The time to complete the converion depends on the selected resolution:
hudakz 6:518950e436be 180 * 9-bit resolution -> max conversion time = 93.75ms
hudakz 6:518950e436be 181 * 10-bit resolution -> max conversion time = 187.5ms
hudakz 6:518950e436be 182 * 11-bit resolution -> max conversion time = 375ms
hudakz 6:518950e436be 183 * 12-bit resolution -> max conversion time = 750ms
hudakz 0:433af64321d5 184 * @param
hudakz 0:433af64321d5 185 * @retval
hudakz 0:433af64321d5 186 */
hudakz 0:433af64321d5 187 void DS1820::startConversion(void) {
hudakz 0:433af64321d5 188 if(present) {
hudakz 0:433af64321d5 189 oneWire.reset();
hudakz 0:433af64321d5 190 oneWire.skip();
hudakz 3:a250babd0a9f 191 oneWire.write(0x44); //start temperature conversion
hudakz 0:433af64321d5 192 }
hudakz 0:433af64321d5 193 }
hudakz 0:433af64321d5 194
hudakz 0:433af64321d5 195 /**
hudakz 6:518950e436be 196 * @brief Reads temperature from the chip's Scratchpad
hudakz 0:433af64321d5 197 * @note
hudakz 0:433af64321d5 198 * @param
hudakz 0:433af64321d5 199 * @retval Floating point temperature value
hudakz 0:433af64321d5 200 */
hudakz 0:433af64321d5 201 float DS1820::read(void) {
hudakz 0:433af64321d5 202 if(present) {
hudakz 0:433af64321d5 203 oneWire.reset();
hudakz 0:433af64321d5 204 oneWire.skip();
hudakz 0:433af64321d5 205 oneWire.write(0xBE); // to read Scratchpad
hudakz 6:518950e436be 206 for(uint8_t i = 0; i < 9; i++) // read Scratchpad bytes
hudakz 6:518950e436be 207 data[i] = oneWire.read();
hudakz 0:433af64321d5 208
hudakz 6:518950e436be 209 // Convert the raw bytes to a 16-bit unsigned value
hudakz 0:433af64321d5 210 uint16_t* p_word = reinterpret_cast < uint16_t * > (&data[0]);
hudakz 0:433af64321d5 211
hudakz 0:433af64321d5 212 #if DEBUG
hudakz 0:433af64321d5 213 serial.printf("raw = %#x\r\n", *p_word);
hudakz 0:433af64321d5 214 #endif
hudakz 0:433af64321d5 215
hudakz 0:433af64321d5 216 if(type_s) {
hudakz 6:518950e436be 217 *p_word = *p_word << 3; // 9-bit resolution
hudakz 0:433af64321d5 218 if(data[7] == 0x10) {
hudakz 0:433af64321d5 219
hudakz 6:518950e436be 220 // "count remain" gives full 12-bit resolution
hudakz 0:433af64321d5 221 *p_word = (*p_word & 0xFFF0) + 12 - data[6];
hudakz 0:433af64321d5 222 }
hudakz 0:433af64321d5 223 }
hudakz 0:433af64321d5 224 else {
hudakz 6:518950e436be 225 uint8_t cfg = (data[4] & 0x60); // default 12-bit resolution
hudakz 4:adf4e7972d73 226
hudakz 4:adf4e7972d73 227 // at lower resolution, the low bits are undefined, so let's clear them
hudakz 0:433af64321d5 228 if(cfg == 0x00)
hudakz 6:518950e436be 229 *p_word = *p_word &~7; // 9-bit resolution
hudakz 0:433af64321d5 230 else
hudakz 0:433af64321d5 231 if(cfg == 0x20)
hudakz 6:518950e436be 232 *p_word = *p_word &~3; // 10-bit resolution
hudakz 0:433af64321d5 233 else
hudakz 0:433af64321d5 234 if(cfg == 0x40)
hudakz 6:518950e436be 235 *p_word = *p_word &~1; // 11-bit resolution
hudakz 4:adf4e7972d73 236
hudakz 0:433af64321d5 237 }
hudakz 6:518950e436be 238
hudakz 6:518950e436be 239 // Convert the raw bytes to a 16-bit signed fixed point value :
hudakz 6:518950e436be 240 // 1 sign bit, 7 integer bits, 8 fractional bits (two’s compliment
hudakz 6:518950e436be 241 // and the LSB of the 16-bit binary number represents 1/256th of a unit).
hudakz 6:518950e436be 242 *p_word = *p_word << 4;
hudakz 6:518950e436be 243
hudakz 6:518950e436be 244 // Convert to floating point value
hudakz 6:518950e436be 245 return(toFloat(*p_word));
hudakz 0:433af64321d5 246 }
hudakz 0:433af64321d5 247 else
hudakz 0:433af64321d5 248 return 0;
hudakz 0:433af64321d5 249 }
hudakz 0:433af64321d5 250
hudakz 0:433af64321d5 251 /**
hudakz 6:518950e436be 252 * @brief Converts a 16-bit signed fixed point value to floating point value
hudakz 6:518950e436be 253 * @note The 16-bit unsigned integer represnts actually
hudakz 6:518950e436be 254 * a 16-bit signed fixed point value:
hudakz 4:adf4e7972d73 255 * 1 sign bit, 7 integer bits, 8 fractional bits (two’s compliment
hudakz 6:518950e436be 256 * and the LSB of the 16-bit binary number represents 1/256th of a unit).
hudakz 6:518950e436be 257 * @param 16-bit unsigned integer
hudakz 4:adf4e7972d73 258 * @retval Floating point value
hudakz 0:433af64321d5 259 */
hudakz 0:433af64321d5 260 float DS1820::toFloat(uint16_t word) {
hudakz 0:433af64321d5 261 if(word & 0x8000)
hudakz 0:433af64321d5 262 return (-float(uint16_t(~word + 1)) / 256.0f);
hudakz 0:433af64321d5 263 else
hudakz 0:433af64321d5 264 return (float(word) / 256.0f);
hudakz 0:433af64321d5 265 }
hudakz 0:433af64321d5 266