DS18B20
Revision 0:d7eea3b21e1d, committed 2017-09-06
- Comitter:
- jack__zen
- Date:
- Wed Sep 06 05:35:00 2017 +0000
- Commit message:
- ds18s20
Changed in this revision
DS18S20.cpp | Show annotated file Show diff for this revision Revisions of this file |
DS18S20.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r d7eea3b21e1d DS18S20.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DS18S20.cpp Wed Sep 06 05:35:00 2017 +0000 @@ -0,0 +1,55 @@ +/* +* DS18S20. Maxim DS18S20 One-Wire Thermometer. +* Uses the OneWireCRC library. +* +* Copyright (C) <2010> Petras Saduikis <petras@petras.co.uk> +* +* This file is part of OneWireThermometer. +* +* OneWireThermometer is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* OneWireThermometer is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with OneWireThermometer. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include "DS18S20.h" +#include "DebugTrace.h" + +DebugTrace pc_ds18S20(ON, TO_SERIAL); + +DS18S20::DS18S20(bool crcOn, bool useAddr, bool parasitic, PinName pin) : + OneWireThermometer(crcOn, useAddr, parasitic, pin, DS18S20_ID) +{ +} + +float DS18S20::calculateTemperature(BYTE* data) +{ + // DS18S20 basic resolution is always 9 bits, which can be enhanced as follows + bool signBit = false; + if (data[TEMPERATURE_MSB] & 0x80) signBit = true; + + int read_temp = (data[TEMPERATURE_MSB] << 8) + data[TEMPERATURE_LSB]; + if (signBit) + { + read_temp = (read_temp ^ 0xFFFF) + 1; // two's complement + read_temp *= -1; + } + + float readTemp = (float)read_temp/2 ; // divide by 2 + pc_ds18S20.traceOut("TEMP_READ: %f \r\n", readTemp); // 9 bit resolution value + + // convert to real temperature + float tempCount = float(data[COUNT_PER_DEG_BYTE] - data[COUNT_REMAIN_BYTE])/(float)data[COUNT_PER_DEG_BYTE]; + float realTemp = (readTemp - 0.25) + tempCount; + pc_ds18S20.traceOut("Temperature: %f \r\n", realTemp); // enhanced resolution value + + return realTemp; +} \ No newline at end of file
diff -r 000000000000 -r d7eea3b21e1d DS18S20.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DS18S20.h Wed Sep 06 05:35:00 2017 +0000 @@ -0,0 +1,39 @@ +/* +* DS18S20. Maxim DS18S20 One-Wire Thermometer. +* Uses the OneWireCRC library. +* +* Copyright (C) <2010> Petras Saduikis <petras@petras.co.uk> +* +* This file is part of OneWireThermometer. +* +* OneWireThermometer is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* OneWireThermometer is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with OneWireThermometer. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef SNATCH59_DS18S20_H +#define SNATCH59_DS18S20_H + +#include "OneWireThermometer.h" + +class DS18S20 : public OneWireThermometer +{ +public: + DS18S20(bool crcOn, bool useAddr, bool parasitic, PinName pin); + + virtual void setResolution(eResolution resln) { }; // do nothing + +protected: + virtual float calculateTemperature(BYTE* data); +}; + +#endif \ No newline at end of file