Auto search for All DS18B20 sensors on data bus

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers OneWireThermometer.cpp Source File

OneWireThermometer.cpp

00001 /*
00002 * OneWireThermometer. Base class for Maxim One-Wire Thermometers. 
00003 * Uses the OneWireCRC library.
00004 *
00005 * Copyright (C) <2010> Petras Saduikis <petras@petras.co.uk>
00006 *
00007 * This file is part of OneWireThermometer.
00008 *
00009 * OneWireThermometer is free software: you can redistribute it and/or modify
00010 * it under the terms of the GNU General Public License as published by
00011 * the Free Software Foundation, either version 3 of the License, or
00012 * (at your option) any later version.
00013 * 
00014 * OneWireThermometer is distributed in the hope that it will be useful,
00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 * GNU General Public License for more details.
00018 *
00019 * You should have received a copy of the GNU General Public License
00020 * along with OneWireThermometer.  If not, see <http://www.gnu.org/licenses/>.
00021 */
00022 
00023 #include "OneWireThermometer.h"
00024 #include "OneWireDefs.h"
00025 //#include "DebugTrace.h"
00026 
00027 //DebugTrace pc(OFF, TO_SERIAL);
00028 
00029 // constructor specifies standard speed for the 1-Wire comms
00030 // 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.
00031 OneWireThermometer::OneWireThermometer(PinName pin, int device_id) :
00032     oneWire(pin, STANDARD), deviceId(device_id), resolution(twelveBit)
00033 {
00034     // NOTE: the power-up resolution of a DS18B20 is 12 bits. The DS18S20's resolution is always
00035     // 9 bits + enhancement, but we treat the DS18S20 as fixed to 12 bits for calculating the
00036     // conversion time Tconv.
00037     deviceCount = 0;
00038 }
00039 
00040 bool OneWireThermometer::initialize()
00041 {
00042     // get all the device addresses for use in selectROM() when reading the temperature
00043    // pc.traceOut("\r\n");
00044     //pc.traceOut("New Scan\r\n");
00045 
00046     oneWire.resetSearch();    
00047     while (oneWire.search(address))   // search for 1-wire device address
00048     {
00049         //pc.traceOut("Address = ");
00050         for (int i = 0; i < ADDRESS_SIZE; i++) 
00051         {
00052             //pc.traceOut("%x ", (int)address[i]);
00053             devices[deviceCount][i] = address[i];
00054         }
00055         //pc.traceOut("\r\n");
00056         deviceCount++;
00057     }
00058     
00059     if (OneWireCRC::crc8(address, ADDRESS_CRC_BYTE) != address[ADDRESS_CRC_BYTE])   // check address CRC is valid
00060     {
00061         //pc.traceOut("CRC is not valid!\r\n");
00062         wait(2);
00063         return false;
00064     }
00065 
00066     if (address[0] != deviceId)
00067     {                    
00068         // Make sure it is a one-wire thermometer device
00069         if (DS18B20_ID == deviceId){}
00070             //pc.traceOut("You need to use a DS1820 or DS18S20 for correct results.\r\n");
00071         else if (DS18S20_ID == deviceId){}
00072             //pc.traceOut("You need to use a DS18B20 for correct results.\r\n");
00073         else{}
00074          // pc.traceOut("Device is not a DS18B20/DS1820/DS18S20 device.\r\n");
00075         
00076         wait(2);
00077         return false;   
00078     }
00079     else
00080     {
00081         if (DS18B20_ID == deviceId){} //pc.traceOut("DS18B20 present and correct.\r\n");
00082         if (DS18S20_ID == deviceId){} //pc.traceOut("DS1820/DS18S20 present and correct.\r\n");            
00083     }
00084     
00085     return true;
00086 }
00087 
00088 // NOTE ON USING SKIP ROM: ok to use before a Convert command to get all
00089 // devices on the bus to do simultaneous temperature conversions. BUT can 
00090 // only use before a Read Scratchpad command if there is only one device on the
00091 // bus. For purpose of this library it is assumed there is only one device
00092 // on the bus.
00093 void OneWireThermometer::resetAndAddress()
00094 {
00095     oneWire.reset();                // reset device 
00096     oneWire.matchROM(address);  // select which device to talk to
00097 }
00098 
00099 bool OneWireThermometer::readAndValidateData(BYTE* data)
00100 {
00101     bool dataOk = true;
00102     
00103     resetAndAddress();
00104     oneWire.writeByte(READSCRATCH);    // read Scratchpad
00105 
00106    // pc.traceOut("read = ");
00107     for (int i = 0; i < THERMOM_SCRATCHPAD_SIZE; i++) 
00108     {               
00109         // we need all bytes which includes CRC check byte
00110         data[i] = oneWire.readByte();
00111        // pc.traceOut("%x ", (int)data[i]);
00112     }
00113     //pc.traceOut("\r\n");
00114 
00115     // Check CRC is valid
00116     if (!(OneWireCRC::crc8(data, THERMOM_CRC_BYTE) == data[THERMOM_CRC_BYTE]))  
00117     {  
00118         // CRC failed
00119        // pc.traceOut("CRC FAILED... \r\n");
00120         dataOk = false;
00121     }
00122     
00123     return dataOk;
00124 }
00125 
00126 float OneWireThermometer::readTemperature(int device)
00127 {
00128     BYTE data[THERMOM_SCRATCHPAD_SIZE];
00129     float realTemp = -999;
00130 
00131     // need to select which device here.
00132     for (int i = 0; i < ADDRESS_SIZE; i++) 
00133     {
00134         address[i] = devices[device][i];
00135     }
00136 
00137     resetAndAddress();
00138     oneWire.writeByte(CONVERT);     // issue Convert command
00139     
00140     // Removed the check for parasitic power, since we waited the same time anyway.
00141     wait_ms(CONVERSION_TIME[resolution]);
00142 
00143     if (readAndValidateData(data))    // issue Read Scratchpad commmand and get data
00144     {
00145         realTemp = calculateTemperature(data);
00146     }
00147     
00148     return realTemp; 
00149 }
00150 
00151 int OneWireThermometer::getDeviceCount()
00152 {
00153     return deviceCount;
00154 }