First Publish

Dependencies:   BridgeDriver2 FrontPanelButtons MAX31855 MCP23017 SDFileSystem TextLCD mbed

Committer:
mehatfie
Date:
Mon Nov 10 22:59:49 2014 +0000
Revision:
1:9954bf6d7d25
Parent:
0:20e78c9d2ea9
First Commit

Who changed what in which revision?

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