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