Program to read the temperature from multiple DS18B20 sensors on the same pin. The original library/code came from Petras Saduikis (see http://mbed.org/users/snatch59/programs/OneWireCRC/gpdz56), but I've modified it to remember the address of multiple sensors all connected to the same data pin. My sample program displays the temperature of each device in turn. If you want to see more of what's going on behind the scenes turn on the debug by setting DebugTrace pc(OFF, TO_SERIAL); to ON (instead of OFF) in the couple of places it's used - it will log the device addresses as they are found etc. The addresses are set in the devices at the factory - I don't think they can be changed, the search always seems to find them in the same order, but this won't be anything to do with the way you've plugged them in. I've had a play with up to 7 sensors (the code has a limit of 10 hardwired in it, but this would be easy to change)

Dependencies:   mbed

Revision:
0:fb8b6da96a8b
diff -r 000000000000 -r fb8b6da96a8b OneWireCRC.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/OneWireCRC.h	Sun Jul 17 15:56:49 2011 +0000
@@ -0,0 +1,138 @@
+/*
+* OneWireCRC. This is a port to mbed of Jim Studt's Adruino One Wire
+* library. Please see additional copyrights below this one, including
+* references to other copyrights.
+*
+* Copyright (C) <2009> Petras Saduikis <petras@petras.co.uk>
+*
+* This file is part of OneWireCRC.
+*
+* OneWireCRC is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+* 
+* OneWireCRC is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with OneWireCRC.  If not, see <http://www.gnu.org/licenses/>.
+*/
+/*
+Copyright (c) 2007, Jim Studt
+
+Updated to work with arduino-0008 and to include skip() as of
+2007/07/06. --RJL20
+
+Modified to calculate the 8-bit CRC directly, avoiding the need for
+the 256-byte lookup table to be loaded in RAM.  Tested in arduino-0010
+-- Tom Pollard, Jan 23, 2008
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+Much of the code was inspired by Derek Yerger's code, though I don't
+think much of that remains.  In any event that was..
+    (copyleft) 2006 by Derek Yerger - Free to distribute freely.
+
+The CRC code was excerpted and inspired by the Dallas Semiconductor 
+sample code bearing this copyright.
+*/
+//---------------------------------------------------------------------------
+// Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
+// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+// OTHER DEALINGS IN THE SOFTWARE.
+//
+// Except as contained in this notice, the name of Dallas Semiconductor
+// shall not be used except as stated in the Dallas Semiconductor
+// Branding Policy.
+//--------------------------------------------------------------------------
+
+#ifndef SNATCH59_ONEWIRECRC_H
+#define SNATCH59_ONEWIRECRC_H
+
+#include <mbed.h>
+
+// Select the table-lookup method of computing the 8-bit CRC by setting this to 1
+#ifndef ONEWIRE_CRC8_TABLE
+#define ONEWIRE_CRC8_TABLE 1
+#endif
+
+typedef unsigned char BYTE;    // used to be uint8_t : something a byte wide, whatever ....
+
+enum eSpeed {OVERDRIVE, STANDARD};
+
+class OneWireCRC
+{
+public:
+    OneWireCRC(PinName oneWire, eSpeed);
+    
+    // reset, read, write functions
+    int reset();
+    void writeByte(int data);
+    int readByte();
+    int touchByte(int data);
+    void block(BYTE* data, int data_len);
+    int overdriveSkip(BYTE* data, int data_len);
+    
+    // address functions
+    void matchROM(BYTE rom[8]);
+    void skipROM();
+
+    // address search functions
+    void resetSearch();
+    BYTE search(BYTE* newAddr);
+
+    // CRC check functions
+    static BYTE crc8(BYTE* addr, BYTE len);
+    static unsigned short crc16(unsigned short* data, unsigned short len);
+
+private:
+    const int* timing;
+    
+    BYTE address[8];
+    int searchJunction;        // so we can set to it -1 somewhere
+    bool searchExhausted;
+    
+    DigitalInOut oneWirePort;
+    
+    // read/write bit functions
+    void writeBit(int bit);
+    int readBit();
+};
+
+#endif