Sensor with Web Server
Dependencies: EthernetInterface mbed-rpc mbed-rtos mbed
DS18B20.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 | * DS18B20. Maxim DS18B20 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 "DS18B20.h" |
afilipem | 0:c385e589a779 | 24 | #include "DebugTrace.h" |
afilipem | 0:c385e589a779 | 25 | |
afilipem | 0:c385e589a779 | 26 | DebugTrace pc_ds18B20(ON, TO_SERIAL); |
afilipem | 0:c385e589a779 | 27 | |
afilipem | 0:c385e589a779 | 28 | DS18B20::DS18B20(bool crcOn, bool useAddr, bool parasitic, PinName pin) : |
afilipem | 0:c385e589a779 | 29 | OneWireThermometer(crcOn, useAddr, parasitic, pin, DS18B20_ID) |
afilipem | 0:c385e589a779 | 30 | { |
afilipem | 0:c385e589a779 | 31 | } |
afilipem | 0:c385e589a779 | 32 | |
afilipem | 0:c385e589a779 | 33 | |
afilipem | 0:c385e589a779 | 34 | void DS18B20::setResolution(eResolution resln) |
afilipem | 0:c385e589a779 | 35 | { |
afilipem | 0:c385e589a779 | 36 | // as the write to the configuration register involves a write to the |
afilipem | 0:c385e589a779 | 37 | // high and low alarm bytes, need to read these registers first |
afilipem | 0:c385e589a779 | 38 | // and copy them back on the write |
afilipem | 0:c385e589a779 | 39 | |
afilipem | 0:c385e589a779 | 40 | BYTE read_data[THERMOM_SCRATCHPAD_SIZE]; |
afilipem | 0:c385e589a779 | 41 | BYTE write_data[ALARM_CONFIG_SIZE]; |
afilipem | 0:c385e589a779 | 42 | |
afilipem | 0:c385e589a779 | 43 | if (readAndValidateData(read_data)) |
afilipem | 0:c385e589a779 | 44 | { |
afilipem | 0:c385e589a779 | 45 | // copy alarm and config data to write data |
afilipem | 0:c385e589a779 | 46 | for (int k = 2; k < 5; k++) |
afilipem | 0:c385e589a779 | 47 | { |
afilipem | 0:c385e589a779 | 48 | write_data[k - 2] = read_data[k]; |
afilipem | 0:c385e589a779 | 49 | } |
afilipem | 0:c385e589a779 | 50 | int config = write_data[2]; |
afilipem | 0:c385e589a779 | 51 | config &= 0x9F; |
afilipem | 0:c385e589a779 | 52 | config ^= (resln << 5); |
afilipem | 0:c385e589a779 | 53 | write_data[2] = config; |
afilipem | 0:c385e589a779 | 54 | |
afilipem | 0:c385e589a779 | 55 | resetAndAddress(); |
afilipem | 0:c385e589a779 | 56 | oneWire.writeByte(WRITESCRATCH); |
afilipem | 0:c385e589a779 | 57 | for (int k = 0; k < 3; k++) |
afilipem | 0:c385e589a779 | 58 | { |
afilipem | 0:c385e589a779 | 59 | oneWire.writeByte(write_data[k]); |
afilipem | 0:c385e589a779 | 60 | } |
afilipem | 0:c385e589a779 | 61 | |
afilipem | 0:c385e589a779 | 62 | // remember it so we can use the correct delay in reading the temperature |
afilipem | 0:c385e589a779 | 63 | // for parasitic power |
afilipem | 0:c385e589a779 | 64 | resolution = resln; |
afilipem | 0:c385e589a779 | 65 | } |
afilipem | 0:c385e589a779 | 66 | } |
afilipem | 0:c385e589a779 | 67 | |
afilipem | 0:c385e589a779 | 68 | float DS18B20::calculateTemperature(BYTE* data) |
afilipem | 0:c385e589a779 | 69 | { |
afilipem | 0:c385e589a779 | 70 | bool signBit = false; |
afilipem | 0:c385e589a779 | 71 | if (data[TEMPERATURE_MSB] & 0x80) signBit = true; |
afilipem | 0:c385e589a779 | 72 | |
afilipem | 0:c385e589a779 | 73 | int read_temp = (data[TEMPERATURE_MSB] << 8) + data[TEMPERATURE_LSB]; |
afilipem | 0:c385e589a779 | 74 | if (signBit) |
afilipem | 0:c385e589a779 | 75 | { |
afilipem | 0:c385e589a779 | 76 | read_temp = (read_temp ^ 0xFFFF) + 1; // two's complement |
afilipem | 0:c385e589a779 | 77 | read_temp *= -1; |
afilipem | 0:c385e589a779 | 78 | } |
afilipem | 0:c385e589a779 | 79 | |
afilipem | 0:c385e589a779 | 80 | //***I have a question. If the data is read earlier why it sets the resolution again now? |
afilipem | 0:c385e589a779 | 81 | int resolution = (data[CONFIG_REG_BYTE] & 0x60) >> 5; // mask off bits 6,5 and move to 1,0 |
afilipem | 0:c385e589a779 | 82 | switch (resolution) |
afilipem | 0:c385e589a779 | 83 | { |
afilipem | 0:c385e589a779 | 84 | case nineBit: // 0.5 deg C increments |
afilipem | 0:c385e589a779 | 85 | read_temp &= 0xFFF8; // bits 2,1,0 are undefined |
afilipem | 0:c385e589a779 | 86 | pc_ds18B20.traceOut("9 bit resolution ...\r\n"); |
afilipem | 0:c385e589a779 | 87 | break; |
afilipem | 0:c385e589a779 | 88 | case tenBit: // 0.25 deg C increments |
afilipem | 0:c385e589a779 | 89 | read_temp &= 0xFFFC; // bits 1,0 are undefined |
afilipem | 0:c385e589a779 | 90 | pc_ds18B20.traceOut("10 bit resolution ...\r\n"); |
afilipem | 0:c385e589a779 | 91 | break; |
afilipem | 0:c385e589a779 | 92 | case elevenBit: // 0.125 deg C increments |
afilipem | 0:c385e589a779 | 93 | read_temp &= 0xFFFE; // bit 0 is undefined, ***bit 0 is 2^ |
afilipem | 0:c385e589a779 | 94 | pc_ds18B20.traceOut("11 bit resolution ...\r\n"); |
afilipem | 0:c385e589a779 | 95 | break; |
afilipem | 0:c385e589a779 | 96 | case twelveBit: // 0.0625 deg C increments |
afilipem | 0:c385e589a779 | 97 | pc_ds18B20.traceOut("12 bit resolution ...\r\n"); |
afilipem | 0:c385e589a779 | 98 | break; |
afilipem | 0:c385e589a779 | 99 | } |
afilipem | 0:c385e589a779 | 100 | float realTemp = (float)read_temp/16 ; |
afilipem | 0:c385e589a779 | 101 | |
afilipem | 0:c385e589a779 | 102 | pc_ds18B20.traceOut("TEMP_READ/REAL TEMP: %f \r\n", realTemp); |
afilipem | 0:c385e589a779 | 103 | |
afilipem | 0:c385e589a779 | 104 | return realTemp; |
afilipem | 0:c385e589a779 | 105 | } |