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