Library for Programmable Resolution 1-Wire Digital ThermometerDS18B20

Dependents:   DS18B20_HGC

Committer:
kriedel
Date:
Thu Feb 03 12:10:20 2011 +0000
Revision:
0:21c0c97a5d7c

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kriedel 0:21c0c97a5d7c 1 /*
kriedel 0:21c0c97a5d7c 2 * DS18S20. Maxim DS18S20 One-Wire Thermometer.
kriedel 0:21c0c97a5d7c 3 * Uses the OneWireCRC library.
kriedel 0:21c0c97a5d7c 4 *
kriedel 0:21c0c97a5d7c 5 * Copyright (C) <2010> Petras Saduikis <petras@petras.co.uk>
kriedel 0:21c0c97a5d7c 6 *
kriedel 0:21c0c97a5d7c 7 * This file is part of OneWireThermometer.
kriedel 0:21c0c97a5d7c 8 *
kriedel 0:21c0c97a5d7c 9 * OneWireThermometer is free software: you can redistribute it and/or modify
kriedel 0:21c0c97a5d7c 10 * it under the terms of the GNU General Public License as published by
kriedel 0:21c0c97a5d7c 11 * the Free Software Foundation, either version 3 of the License, or
kriedel 0:21c0c97a5d7c 12 * (at your option) any later version.
kriedel 0:21c0c97a5d7c 13 *
kriedel 0:21c0c97a5d7c 14 * OneWireThermometer is distributed in the hope that it will be useful,
kriedel 0:21c0c97a5d7c 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
kriedel 0:21c0c97a5d7c 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
kriedel 0:21c0c97a5d7c 17 * GNU General Public License for more details.
kriedel 0:21c0c97a5d7c 18 *
kriedel 0:21c0c97a5d7c 19 * You should have received a copy of the GNU General Public License
kriedel 0:21c0c97a5d7c 20 * along with OneWireThermometer. If not, see <http://www.gnu.org/licenses/>.
kriedel 0:21c0c97a5d7c 21 */
kriedel 0:21c0c97a5d7c 22
kriedel 0:21c0c97a5d7c 23 #include "DS18S20.h"
kriedel 0:21c0c97a5d7c 24 #include "DebugTrace.h"
kriedel 0:21c0c97a5d7c 25
kriedel 0:21c0c97a5d7c 26 DebugTrace pc_ds18S20(ON, TO_SERIAL);
kriedel 0:21c0c97a5d7c 27
kriedel 0:21c0c97a5d7c 28 DS18S20::DS18S20(bool crcOn, bool useAddr, bool parasitic, PinName pin) :
kriedel 0:21c0c97a5d7c 29 OneWireThermometer(crcOn, useAddr, parasitic, pin, DS18S20_ID)
kriedel 0:21c0c97a5d7c 30 {
kriedel 0:21c0c97a5d7c 31 }
kriedel 0:21c0c97a5d7c 32
kriedel 0:21c0c97a5d7c 33 float DS18S20::calculateTemperature(BYTE* data)
kriedel 0:21c0c97a5d7c 34 {
kriedel 0:21c0c97a5d7c 35 // DS18S20 basic resolution is always 9 bits, which can be enhanced as follows
kriedel 0:21c0c97a5d7c 36 bool signBit = false;
kriedel 0:21c0c97a5d7c 37 if (data[TEMPERATURE_MSB] & 0x80) signBit = true;
kriedel 0:21c0c97a5d7c 38
kriedel 0:21c0c97a5d7c 39 int read_temp = (data[TEMPERATURE_MSB] << 8) + data[TEMPERATURE_LSB];
kriedel 0:21c0c97a5d7c 40 if (signBit)
kriedel 0:21c0c97a5d7c 41 {
kriedel 0:21c0c97a5d7c 42 read_temp = (read_temp ^ 0xFFFF) + 1; // two's complement
kriedel 0:21c0c97a5d7c 43 read_temp *= -1;
kriedel 0:21c0c97a5d7c 44 }
kriedel 0:21c0c97a5d7c 45
kriedel 0:21c0c97a5d7c 46 float readTemp = (float)read_temp/2 ; // divide by 2
kriedel 0:21c0c97a5d7c 47 pc_ds18S20.traceOut("TEMP_READ: %f \r\n", readTemp); // 9 bit resolution value
kriedel 0:21c0c97a5d7c 48
kriedel 0:21c0c97a5d7c 49 // convert to real temperature
kriedel 0:21c0c97a5d7c 50 float tempCount = float(data[COUNT_PER_DEG_BYTE] - data[COUNT_REMAIN_BYTE])/(float)data[COUNT_PER_DEG_BYTE];
kriedel 0:21c0c97a5d7c 51 float realTemp = (readTemp - 0.25) + tempCount;
kriedel 0:21c0c97a5d7c 52 pc_ds18S20.traceOut("Temperature: %f \r\n", realTemp); // enhanced resolution value
kriedel 0:21c0c97a5d7c 53
kriedel 0:21c0c97a5d7c 54 return realTemp;
kriedel 0:21c0c97a5d7c 55 }