Plamen Totev
/
peltier
peltier with 2 fans
DS18B20Multiple/OneWireThermometer.cpp@4:5213bee8158e, 2014-04-14 (annotated)
- Committer:
- redplam
- Date:
- Mon Apr 14 02:15:32 2014 +0000
- Revision:
- 4:5213bee8158e
myproject;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
redplam | 4:5213bee8158e | 1 | /* |
redplam | 4:5213bee8158e | 2 | * OneWireThermometer. Base class for Maxim One-Wire Thermometers. |
redplam | 4:5213bee8158e | 3 | * Uses the OneWireCRC library. |
redplam | 4:5213bee8158e | 4 | * |
redplam | 4:5213bee8158e | 5 | * Copyright (C) <2010> Petras Saduikis <petras@petras.co.uk> |
redplam | 4:5213bee8158e | 6 | * |
redplam | 4:5213bee8158e | 7 | * This file is part of OneWireThermometer. |
redplam | 4:5213bee8158e | 8 | * |
redplam | 4:5213bee8158e | 9 | * OneWireThermometer is free software: you can redistribute it and/or modify |
redplam | 4:5213bee8158e | 10 | * it under the terms of the GNU General Public License as published by |
redplam | 4:5213bee8158e | 11 | * the Free Software Foundation, either version 3 of the License, or |
redplam | 4:5213bee8158e | 12 | * (at your option) any later version. |
redplam | 4:5213bee8158e | 13 | * |
redplam | 4:5213bee8158e | 14 | * OneWireThermometer is distributed in the hope that it will be useful, |
redplam | 4:5213bee8158e | 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
redplam | 4:5213bee8158e | 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
redplam | 4:5213bee8158e | 17 | * GNU General Public License for more details. |
redplam | 4:5213bee8158e | 18 | * |
redplam | 4:5213bee8158e | 19 | * You should have received a copy of the GNU General Public License |
redplam | 4:5213bee8158e | 20 | * along with OneWireThermometer. If not, see <http://www.gnu.org/licenses/>. |
redplam | 4:5213bee8158e | 21 | */ |
redplam | 4:5213bee8158e | 22 | |
redplam | 4:5213bee8158e | 23 | #include "OneWireThermometer.h" |
redplam | 4:5213bee8158e | 24 | #include "OneWireDefs.h" |
redplam | 4:5213bee8158e | 25 | //#include "DebugTrace.h" |
redplam | 4:5213bee8158e | 26 | |
redplam | 4:5213bee8158e | 27 | //DebugTrace pc(OFF, TO_SERIAL); |
redplam | 4:5213bee8158e | 28 | |
redplam | 4:5213bee8158e | 29 | // constructor specifies standard speed for the 1-Wire comms |
redplam | 4:5213bee8158e | 30 | // Used to have params for CRCon and useAddress, can't see why you wouldn't use CRC and you have to use address since there could be more than one. |
redplam | 4:5213bee8158e | 31 | OneWireThermometer::OneWireThermometer(PinName pin, int device_id) : |
redplam | 4:5213bee8158e | 32 | oneWire(pin, STANDARD), deviceId(device_id), resolution(twelveBit) |
redplam | 4:5213bee8158e | 33 | { |
redplam | 4:5213bee8158e | 34 | // NOTE: the power-up resolution of a DS18B20 is 12 bits. The DS18S20's resolution is always |
redplam | 4:5213bee8158e | 35 | // 9 bits + enhancement, but we treat the DS18S20 as fixed to 12 bits for calculating the |
redplam | 4:5213bee8158e | 36 | // conversion time Tconv. |
redplam | 4:5213bee8158e | 37 | deviceCount = 0; |
redplam | 4:5213bee8158e | 38 | } |
redplam | 4:5213bee8158e | 39 | |
redplam | 4:5213bee8158e | 40 | bool OneWireThermometer::initialize() |
redplam | 4:5213bee8158e | 41 | { |
redplam | 4:5213bee8158e | 42 | // get all the device addresses for use in selectROM() when reading the temperature |
redplam | 4:5213bee8158e | 43 | // pc.traceOut("\r\n"); |
redplam | 4:5213bee8158e | 44 | //pc.traceOut("New Scan\r\n"); |
redplam | 4:5213bee8158e | 45 | |
redplam | 4:5213bee8158e | 46 | oneWire.resetSearch(); |
redplam | 4:5213bee8158e | 47 | while (oneWire.search(address)) // search for 1-wire device address |
redplam | 4:5213bee8158e | 48 | { |
redplam | 4:5213bee8158e | 49 | //pc.traceOut("Address = "); |
redplam | 4:5213bee8158e | 50 | for (int i = 0; i < ADDRESS_SIZE; i++) |
redplam | 4:5213bee8158e | 51 | { |
redplam | 4:5213bee8158e | 52 | //pc.traceOut("%x ", (int)address[i]); |
redplam | 4:5213bee8158e | 53 | devices[deviceCount][i] = address[i]; |
redplam | 4:5213bee8158e | 54 | } |
redplam | 4:5213bee8158e | 55 | //pc.traceOut("\r\n"); |
redplam | 4:5213bee8158e | 56 | deviceCount++; |
redplam | 4:5213bee8158e | 57 | } |
redplam | 4:5213bee8158e | 58 | |
redplam | 4:5213bee8158e | 59 | if (OneWireCRC::crc8(address, ADDRESS_CRC_BYTE) != address[ADDRESS_CRC_BYTE]) // check address CRC is valid |
redplam | 4:5213bee8158e | 60 | { |
redplam | 4:5213bee8158e | 61 | //pc.traceOut("CRC is not valid!\r\n"); |
redplam | 4:5213bee8158e | 62 | wait(2); |
redplam | 4:5213bee8158e | 63 | return false; |
redplam | 4:5213bee8158e | 64 | } |
redplam | 4:5213bee8158e | 65 | |
redplam | 4:5213bee8158e | 66 | if (address[0] != deviceId) |
redplam | 4:5213bee8158e | 67 | { |
redplam | 4:5213bee8158e | 68 | // Make sure it is a one-wire thermometer device |
redplam | 4:5213bee8158e | 69 | if (DS18B20_ID == deviceId){} |
redplam | 4:5213bee8158e | 70 | //pc.traceOut("You need to use a DS1820 or DS18S20 for correct results.\r\n"); |
redplam | 4:5213bee8158e | 71 | else if (DS18S20_ID == deviceId){} |
redplam | 4:5213bee8158e | 72 | //pc.traceOut("You need to use a DS18B20 for correct results.\r\n"); |
redplam | 4:5213bee8158e | 73 | else{} |
redplam | 4:5213bee8158e | 74 | // pc.traceOut("Device is not a DS18B20/DS1820/DS18S20 device.\r\n"); |
redplam | 4:5213bee8158e | 75 | |
redplam | 4:5213bee8158e | 76 | wait(2); |
redplam | 4:5213bee8158e | 77 | return false; |
redplam | 4:5213bee8158e | 78 | } |
redplam | 4:5213bee8158e | 79 | else |
redplam | 4:5213bee8158e | 80 | { |
redplam | 4:5213bee8158e | 81 | if (DS18B20_ID == deviceId){} //pc.traceOut("DS18B20 present and correct.\r\n"); |
redplam | 4:5213bee8158e | 82 | if (DS18S20_ID == deviceId){} //pc.traceOut("DS1820/DS18S20 present and correct.\r\n"); |
redplam | 4:5213bee8158e | 83 | } |
redplam | 4:5213bee8158e | 84 | |
redplam | 4:5213bee8158e | 85 | return true; |
redplam | 4:5213bee8158e | 86 | } |
redplam | 4:5213bee8158e | 87 | |
redplam | 4:5213bee8158e | 88 | // NOTE ON USING SKIP ROM: ok to use before a Convert command to get all |
redplam | 4:5213bee8158e | 89 | // devices on the bus to do simultaneous temperature conversions. BUT can |
redplam | 4:5213bee8158e | 90 | // only use before a Read Scratchpad command if there is only one device on the |
redplam | 4:5213bee8158e | 91 | // bus. For purpose of this library it is assumed there is only one device |
redplam | 4:5213bee8158e | 92 | // on the bus. |
redplam | 4:5213bee8158e | 93 | void OneWireThermometer::resetAndAddress() |
redplam | 4:5213bee8158e | 94 | { |
redplam | 4:5213bee8158e | 95 | oneWire.reset(); // reset device |
redplam | 4:5213bee8158e | 96 | oneWire.matchROM(address); // select which device to talk to |
redplam | 4:5213bee8158e | 97 | } |
redplam | 4:5213bee8158e | 98 | |
redplam | 4:5213bee8158e | 99 | bool OneWireThermometer::readAndValidateData(BYTE* data) |
redplam | 4:5213bee8158e | 100 | { |
redplam | 4:5213bee8158e | 101 | bool dataOk = true; |
redplam | 4:5213bee8158e | 102 | |
redplam | 4:5213bee8158e | 103 | resetAndAddress(); |
redplam | 4:5213bee8158e | 104 | oneWire.writeByte(READSCRATCH); // read Scratchpad |
redplam | 4:5213bee8158e | 105 | |
redplam | 4:5213bee8158e | 106 | // pc.traceOut("read = "); |
redplam | 4:5213bee8158e | 107 | for (int i = 0; i < THERMOM_SCRATCHPAD_SIZE; i++) |
redplam | 4:5213bee8158e | 108 | { |
redplam | 4:5213bee8158e | 109 | // we need all bytes which includes CRC check byte |
redplam | 4:5213bee8158e | 110 | data[i] = oneWire.readByte(); |
redplam | 4:5213bee8158e | 111 | // pc.traceOut("%x ", (int)data[i]); |
redplam | 4:5213bee8158e | 112 | } |
redplam | 4:5213bee8158e | 113 | //pc.traceOut("\r\n"); |
redplam | 4:5213bee8158e | 114 | |
redplam | 4:5213bee8158e | 115 | // Check CRC is valid |
redplam | 4:5213bee8158e | 116 | if (!(OneWireCRC::crc8(data, THERMOM_CRC_BYTE) == data[THERMOM_CRC_BYTE])) |
redplam | 4:5213bee8158e | 117 | { |
redplam | 4:5213bee8158e | 118 | // CRC failed |
redplam | 4:5213bee8158e | 119 | // pc.traceOut("CRC FAILED... \r\n"); |
redplam | 4:5213bee8158e | 120 | dataOk = false; |
redplam | 4:5213bee8158e | 121 | } |
redplam | 4:5213bee8158e | 122 | |
redplam | 4:5213bee8158e | 123 | return dataOk; |
redplam | 4:5213bee8158e | 124 | } |
redplam | 4:5213bee8158e | 125 | |
redplam | 4:5213bee8158e | 126 | float OneWireThermometer::readTemperature(int device) |
redplam | 4:5213bee8158e | 127 | { |
redplam | 4:5213bee8158e | 128 | BYTE data[THERMOM_SCRATCHPAD_SIZE]; |
redplam | 4:5213bee8158e | 129 | float realTemp = -999; |
redplam | 4:5213bee8158e | 130 | |
redplam | 4:5213bee8158e | 131 | // need to select which device here. |
redplam | 4:5213bee8158e | 132 | for (int i = 0; i < ADDRESS_SIZE; i++) |
redplam | 4:5213bee8158e | 133 | { |
redplam | 4:5213bee8158e | 134 | address[i] = devices[device][i]; |
redplam | 4:5213bee8158e | 135 | } |
redplam | 4:5213bee8158e | 136 | |
redplam | 4:5213bee8158e | 137 | resetAndAddress(); |
redplam | 4:5213bee8158e | 138 | oneWire.writeByte(CONVERT); // issue Convert command |
redplam | 4:5213bee8158e | 139 | |
redplam | 4:5213bee8158e | 140 | // Removed the check for parasitic power, since we waited the same time anyway. |
redplam | 4:5213bee8158e | 141 | wait_ms(CONVERSION_TIME[resolution]); |
redplam | 4:5213bee8158e | 142 | |
redplam | 4:5213bee8158e | 143 | if (readAndValidateData(data)) // issue Read Scratchpad commmand and get data |
redplam | 4:5213bee8158e | 144 | { |
redplam | 4:5213bee8158e | 145 | realTemp = calculateTemperature(data); |
redplam | 4:5213bee8158e | 146 | } |
redplam | 4:5213bee8158e | 147 | |
redplam | 4:5213bee8158e | 148 | return realTemp; |
redplam | 4:5213bee8158e | 149 | } |
redplam | 4:5213bee8158e | 150 | |
redplam | 4:5213bee8158e | 151 | int OneWireThermometer::getDeviceCount() |
redplam | 4:5213bee8158e | 152 | { |
redplam | 4:5213bee8158e | 153 | return deviceCount; |
redplam | 4:5213bee8158e | 154 | } |