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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DS18S20.cpp Source File

DS18S20.cpp

00001 /*
00002 * DS18S20. Maxim DS18S20 One-Wire Thermometer. 
00003 * Uses the OneWireCRC library.
00004 *
00005 * Copyright (C) <2010> Petras Saduikis <petras@petras.co.uk>
00006 *
00007 * This file is part of OneWireThermometer.
00008 *
00009 * OneWireThermometer is free software: you can redistribute it and/or modify
00010 * it under the terms of the GNU General Public License as published by
00011 * the Free Software Foundation, either version 3 of the License, or
00012 * (at your option) any later version.
00013 * 
00014 * OneWireThermometer is distributed in the hope that it will be useful,
00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 * GNU General Public License for more details.
00018 *
00019 * You should have received a copy of the GNU General Public License
00020 * along with OneWireThermometer.  If not, see <http://www.gnu.org/licenses/>.
00021 */
00022 
00023 #include "DS18S20.h"
00024 #include "DebugTrace.h"
00025 
00026 DebugTrace pc_ds18S20(ON, TO_SERIAL);
00027 
00028 DS18S20::DS18S20(bool crcOn, bool useAddr, bool parasitic, PinName pin) : 
00029     OneWireThermometer(crcOn, useAddr, parasitic, pin, DS18S20_ID)
00030 {
00031 }
00032 
00033 float DS18S20::calculateTemperature(BYTE* data)
00034 {    
00035     // DS18S20 basic resolution is always 9 bits, which can be enhanced as follows
00036     bool signBit = false;
00037     if (data[TEMPERATURE_MSB] & 0x80) signBit = true;
00038         
00039     int read_temp = (data[TEMPERATURE_MSB] << 8) + data[TEMPERATURE_LSB];
00040     if (signBit)
00041     {
00042         read_temp = (read_temp ^ 0xFFFF) + 1;    // two's complement
00043         read_temp *= -1;
00044     }
00045                  
00046     float readTemp = (float)read_temp/2 ;            // divide by 2
00047     pc_ds18S20.traceOut("TEMP_READ: %f \r\n", readTemp);     // 9 bit resolution value
00048                
00049     // convert to real temperature
00050     float tempCount = float(data[COUNT_PER_DEG_BYTE] - data[COUNT_REMAIN_BYTE])/(float)data[COUNT_PER_DEG_BYTE];        
00051     float realTemp = (readTemp - 0.25) + tempCount;        
00052     pc_ds18S20.traceOut("Temperature: %f \r\n", realTemp);   // enhanced resolution value
00053     
00054     return realTemp;
00055 }