Now supports DS18B20 and DS18S20 Maxim/Dallas one-wire thermometer devices. Also supports DS18S20 in 9, 10, 11, and 12 bit resolution modes. 'Use Address' mode now checks if the correct device type is present, and informs the user which device to use. Correct temperature conversion times now used in non-parasitic mode. The device should be placed at least 6 inches (15 cm) from the mbed board in order to accurately read ambient temperature.

Dependencies:   mbed

Committer:
snatch59
Date:
Sun Jan 03 11:57:31 2010 +0000
Revision:
0:01a6a40578c9

        

Who changed what in which revision?

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