Sensor with Web Server
Dependencies: EthernetInterface mbed-rpc mbed-rtos mbed
OneWireThermometer.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 | * OneWireThermometer. Base class for Maxim One-Wire Thermometers. |
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 "OneWireThermometer.h" |
afilipem | 0:c385e589a779 | 24 | #include "OneWireDefs.h" |
afilipem | 0:c385e589a779 | 25 | #include "DebugTrace.h" |
afilipem | 0:c385e589a779 | 26 | |
afilipem | 0:c385e589a779 | 27 | DebugTrace pc(ON, TO_SERIAL); |
afilipem | 0:c385e589a779 | 28 | |
afilipem | 0:c385e589a779 | 29 | // constructor specifies standard speed for the 1-Wire comms |
afilipem | 0:c385e589a779 | 30 | OneWireThermometer::OneWireThermometer(bool crcOn, bool useAddr, bool parasitic, PinName pin, int device_id) : |
afilipem | 0:c385e589a779 | 31 | useCRC(crcOn), useAddress(useAddr), useParasiticPower(parasitic), |
afilipem | 0:c385e589a779 | 32 | oneWire(pin, STANDARD), deviceId(device_id), resolution(twelveBit) |
afilipem | 0:c385e589a779 | 33 | { |
afilipem | 0:c385e589a779 | 34 | // NOTE: the power-up resolution of a DS18B20 is 12 bits. The DS18S20's resolution is always |
afilipem | 0:c385e589a779 | 35 | // 9 bits + enhancement, but we treat the DS18S20 as fixed to 12 bits for calculating the |
afilipem | 0:c385e589a779 | 36 | // conversion time Tconv. |
afilipem | 0:c385e589a779 | 37 | } |
afilipem | 0:c385e589a779 | 38 | |
afilipem | 0:c385e589a779 | 39 | bool OneWireThermometer::initialize() |
afilipem | 0:c385e589a779 | 40 | { |
afilipem | 0:c385e589a779 | 41 | // get the device address for use in selectROM() when reading the temperature |
afilipem | 0:c385e589a779 | 42 | // - not really needed except for device validation if using skipROM() |
afilipem | 0:c385e589a779 | 43 | if (useAddress) |
afilipem | 0:c385e589a779 | 44 | { |
afilipem | 0:c385e589a779 | 45 | pc.traceOut("\r\n"); |
afilipem | 0:c385e589a779 | 46 | pc.traceOut("New Scan\r\n"); |
afilipem | 0:c385e589a779 | 47 | |
afilipem | 0:c385e589a779 | 48 | oneWire.resetSearch(); |
afilipem | 0:c385e589a779 | 49 | if (!oneWire.search(address)) // search for 1-wire device address |
afilipem | 0:c385e589a779 | 50 | { |
afilipem | 0:c385e589a779 | 51 | pc.traceOut("No more addresses.\r\n"); |
afilipem | 0:c385e589a779 | 52 | wait(2); |
afilipem | 0:c385e589a779 | 53 | return false; |
afilipem | 0:c385e589a779 | 54 | } |
afilipem | 0:c385e589a779 | 55 | |
afilipem | 0:c385e589a779 | 56 | pc.traceOut("Address = "); |
afilipem | 0:c385e589a779 | 57 | for (int i = 0; i < ADDRESS_SIZE; i++) |
afilipem | 0:c385e589a779 | 58 | { |
afilipem | 0:c385e589a779 | 59 | pc.traceOut("%x ", (int)address[i]); |
afilipem | 0:c385e589a779 | 60 | } |
afilipem | 0:c385e589a779 | 61 | pc.traceOut("\r\n"); |
afilipem | 0:c385e589a779 | 62 | |
afilipem | 0:c385e589a779 | 63 | if (OneWireCRC::crc8(address, ADDRESS_CRC_BYTE) != address[ADDRESS_CRC_BYTE]) // check address CRC is valid |
afilipem | 0:c385e589a779 | 64 | { |
afilipem | 0:c385e589a779 | 65 | pc.traceOut("CRC is not valid!\r\n"); |
afilipem | 0:c385e589a779 | 66 | wait(2); |
afilipem | 0:c385e589a779 | 67 | return false; |
afilipem | 0:c385e589a779 | 68 | } |
afilipem | 0:c385e589a779 | 69 | |
afilipem | 0:c385e589a779 | 70 | if (address[0] != deviceId) |
afilipem | 0:c385e589a779 | 71 | { |
afilipem | 0:c385e589a779 | 72 | // Make sure it is a one-wire thermometer device |
afilipem | 0:c385e589a779 | 73 | if (DS18B20_ID == deviceId) |
afilipem | 0:c385e589a779 | 74 | pc.traceOut("You need to use a DS1820 or DS18S20 for correct results.\r\n"); |
afilipem | 0:c385e589a779 | 75 | else if (DS18S20_ID == deviceId) |
afilipem | 0:c385e589a779 | 76 | pc.traceOut("You need to use a DS18B20 for correct results.\r\n"); |
afilipem | 0:c385e589a779 | 77 | else |
afilipem | 0:c385e589a779 | 78 | pc.traceOut("Device is not a DS18B20/DS1820/DS18S20 device.\r\n"); |
afilipem | 0:c385e589a779 | 79 | |
afilipem | 0:c385e589a779 | 80 | wait(2); |
afilipem | 0:c385e589a779 | 81 | return false; |
afilipem | 0:c385e589a779 | 82 | } |
afilipem | 0:c385e589a779 | 83 | else |
afilipem | 0:c385e589a779 | 84 | { |
afilipem | 0:c385e589a779 | 85 | if (DS18B20_ID == deviceId) pc.traceOut("DS18B20 present and correct.\r\n"); |
afilipem | 0:c385e589a779 | 86 | if (DS18S20_ID == deviceId) pc.traceOut("DS1820/DS18S20 present and correct.\r\n"); |
afilipem | 0:c385e589a779 | 87 | } |
afilipem | 0:c385e589a779 | 88 | } |
afilipem | 0:c385e589a779 | 89 | |
afilipem | 0:c385e589a779 | 90 | return true; |
afilipem | 0:c385e589a779 | 91 | } |
afilipem | 0:c385e589a779 | 92 | |
afilipem | 0:c385e589a779 | 93 | // NOTE ON USING SKIP ROM: ok to use before a Convert command to get all |
afilipem | 0:c385e589a779 | 94 | // devices on the bus to do simultaneous temperature conversions. BUT can |
afilipem | 0:c385e589a779 | 95 | // only use before a Read Scratchpad command if there is only one device on the |
afilipem | 0:c385e589a779 | 96 | // bus. For purpose of this library it is assumed there is only one device |
afilipem | 0:c385e589a779 | 97 | // on the bus. |
afilipem | 0:c385e589a779 | 98 | void OneWireThermometer::resetAndAddress() |
afilipem | 0:c385e589a779 | 99 | { |
afilipem | 0:c385e589a779 | 100 | oneWire.reset(); // reset device |
afilipem | 0:c385e589a779 | 101 | if (useAddress) |
afilipem | 0:c385e589a779 | 102 | { |
afilipem | 0:c385e589a779 | 103 | oneWire.matchROM(address); // select which device to talk to |
afilipem | 0:c385e589a779 | 104 | } |
afilipem | 0:c385e589a779 | 105 | else |
afilipem | 0:c385e589a779 | 106 | { |
afilipem | 0:c385e589a779 | 107 | oneWire.skipROM(); // broadcast |
afilipem | 0:c385e589a779 | 108 | } |
afilipem | 0:c385e589a779 | 109 | } |
afilipem | 0:c385e589a779 | 110 | |
afilipem | 0:c385e589a779 | 111 | bool OneWireThermometer::readAndValidateData(BYTE* data) |
afilipem | 0:c385e589a779 | 112 | { |
afilipem | 0:c385e589a779 | 113 | bool dataOk = true; |
afilipem | 0:c385e589a779 | 114 | |
afilipem | 0:c385e589a779 | 115 | resetAndAddress(); |
afilipem | 0:c385e589a779 | 116 | oneWire.writeByte(READSCRATCH); // read Scratchpad |
afilipem | 0:c385e589a779 | 117 | |
afilipem | 0:c385e589a779 | 118 | pc.traceOut("read = "); |
afilipem | 0:c385e589a779 | 119 | for (int i = 0; i < THERMOM_SCRATCHPAD_SIZE; i++) |
afilipem | 0:c385e589a779 | 120 | { |
afilipem | 0:c385e589a779 | 121 | // we need all bytes which includes CRC check byte |
afilipem | 0:c385e589a779 | 122 | data[i] = oneWire.readByte(); |
afilipem | 0:c385e589a779 | 123 | pc.traceOut("%x ", (int)data[i]); |
afilipem | 0:c385e589a779 | 124 | } |
afilipem | 0:c385e589a779 | 125 | pc.traceOut("\r\n"); |
afilipem | 0:c385e589a779 | 126 | |
afilipem | 0:c385e589a779 | 127 | // Check CRC is valid if you want to |
afilipem | 0:c385e589a779 | 128 | if (useCRC && !(OneWireCRC::crc8(data, THERMOM_CRC_BYTE) == data[THERMOM_CRC_BYTE])) |
afilipem | 0:c385e589a779 | 129 | { |
afilipem | 0:c385e589a779 | 130 | // CRC failed |
afilipem | 0:c385e589a779 | 131 | pc.traceOut("CRC FAILED... \r\n"); |
afilipem | 0:c385e589a779 | 132 | dataOk = false; |
afilipem | 0:c385e589a779 | 133 | } |
afilipem | 0:c385e589a779 | 134 | |
afilipem | 0:c385e589a779 | 135 | return dataOk; |
afilipem | 0:c385e589a779 | 136 | } |
afilipem | 0:c385e589a779 | 137 | |
afilipem | 0:c385e589a779 | 138 | float OneWireThermometer::readTemperature() |
afilipem | 0:c385e589a779 | 139 | { |
afilipem | 0:c385e589a779 | 140 | BYTE data[THERMOM_SCRATCHPAD_SIZE]; |
afilipem | 0:c385e589a779 | 141 | float realTemp = -999; |
afilipem | 0:c385e589a779 | 142 | |
afilipem | 0:c385e589a779 | 143 | resetAndAddress(); |
afilipem | 0:c385e589a779 | 144 | oneWire.writeByte(CONVERT); // issue Convert command |
afilipem | 0:c385e589a779 | 145 | |
afilipem | 0:c385e589a779 | 146 | if (useParasiticPower) |
afilipem | 0:c385e589a779 | 147 | { |
afilipem | 0:c385e589a779 | 148 | // wait while converting - Tconv (according to resolution of reading) |
afilipem | 0:c385e589a779 | 149 | wait_ms(CONVERSION_TIME[resolution]); |
afilipem | 0:c385e589a779 | 150 | } |
afilipem | 0:c385e589a779 | 151 | else |
afilipem | 0:c385e589a779 | 152 | { |
afilipem | 0:c385e589a779 | 153 | // TODO |
afilipem | 0:c385e589a779 | 154 | // after the Convert command, the device should respond by transmitting 0 |
afilipem | 0:c385e589a779 | 155 | // while the temperature conversion is in progress and 1 when the conversion is done |
afilipem | 0:c385e589a779 | 156 | // - as were are not checking this (TODO), we use Tconv, as we would do for |
afilipem | 0:c385e589a779 | 157 | // parasitic power |
afilipem | 0:c385e589a779 | 158 | wait_ms(CONVERSION_TIME[resolution]); |
afilipem | 0:c385e589a779 | 159 | } |
afilipem | 0:c385e589a779 | 160 | |
afilipem | 0:c385e589a779 | 161 | if (readAndValidateData(data)) // issue Read Scratchpad commmand and get data |
afilipem | 0:c385e589a779 | 162 | { |
afilipem | 0:c385e589a779 | 163 | realTemp = calculateTemperature(data); |
afilipem | 0:c385e589a779 | 164 | } |
afilipem | 0:c385e589a779 | 165 | |
afilipem | 0:c385e589a779 | 166 | return realTemp; |
afilipem | 0:c385e589a779 | 167 | } |