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

Committer:
tonymudd
Date:
Sun Jul 17 15:56:49 2011 +0000
Revision:
0:fb8b6da96a8b

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tonymudd 0:fb8b6da96a8b 1 /*
tonymudd 0:fb8b6da96a8b 2 * OneWireCRC. This is a port to mbed of Jim Studt's Adruino One Wire
tonymudd 0:fb8b6da96a8b 3 * library. Please see additional copyrights below this one, including
tonymudd 0:fb8b6da96a8b 4 * references to other copyrights.
tonymudd 0:fb8b6da96a8b 5 *
tonymudd 0:fb8b6da96a8b 6 * Copyright (C) <2009> Petras Saduikis <petras@petras.co.uk>
tonymudd 0:fb8b6da96a8b 7 *
tonymudd 0:fb8b6da96a8b 8 * This file is part of OneWireCRC.
tonymudd 0:fb8b6da96a8b 9 *
tonymudd 0:fb8b6da96a8b 10 * OneWireCRC is free software: you can redistribute it and/or modify
tonymudd 0:fb8b6da96a8b 11 * it under the terms of the GNU General Public License as published by
tonymudd 0:fb8b6da96a8b 12 * the Free Software Foundation, either version 3 of the License, or
tonymudd 0:fb8b6da96a8b 13 * (at your option) any later version.
tonymudd 0:fb8b6da96a8b 14 *
tonymudd 0:fb8b6da96a8b 15 * OneWireCRC is distributed in the hope that it will be useful,
tonymudd 0:fb8b6da96a8b 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
tonymudd 0:fb8b6da96a8b 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tonymudd 0:fb8b6da96a8b 18 * GNU General Public License for more details.
tonymudd 0:fb8b6da96a8b 19 *
tonymudd 0:fb8b6da96a8b 20 * You should have received a copy of the GNU General Public License
tonymudd 0:fb8b6da96a8b 21 * along with OneWireCRC. If not, see <http://www.gnu.org/licenses/>.
tonymudd 0:fb8b6da96a8b 22 */
tonymudd 0:fb8b6da96a8b 23 /*
tonymudd 0:fb8b6da96a8b 24 Copyright (c) 2007, Jim Studt
tonymudd 0:fb8b6da96a8b 25
tonymudd 0:fb8b6da96a8b 26 Updated to work with arduino-0008 and to include skip() as of
tonymudd 0:fb8b6da96a8b 27 2007/07/06. --RJL20
tonymudd 0:fb8b6da96a8b 28
tonymudd 0:fb8b6da96a8b 29 Modified to calculate the 8-bit CRC directly, avoiding the need for
tonymudd 0:fb8b6da96a8b 30 the 256-byte lookup table to be loaded in RAM. Tested in arduino-0010
tonymudd 0:fb8b6da96a8b 31 -- Tom Pollard, Jan 23, 2008
tonymudd 0:fb8b6da96a8b 32
tonymudd 0:fb8b6da96a8b 33 Permission is hereby granted, free of charge, to any person obtaining
tonymudd 0:fb8b6da96a8b 34 a copy of this software and associated documentation files (the
tonymudd 0:fb8b6da96a8b 35 "Software"), to deal in the Software without restriction, including
tonymudd 0:fb8b6da96a8b 36 without limitation the rights to use, copy, modify, merge, publish,
tonymudd 0:fb8b6da96a8b 37 distribute, sublicense, and/or sell copies of the Software, and to
tonymudd 0:fb8b6da96a8b 38 permit persons to whom the Software is furnished to do so, subject to
tonymudd 0:fb8b6da96a8b 39 the following conditions:
tonymudd 0:fb8b6da96a8b 40
tonymudd 0:fb8b6da96a8b 41 The above copyright notice and this permission notice shall be
tonymudd 0:fb8b6da96a8b 42 included in all copies or substantial portions of the Software.
tonymudd 0:fb8b6da96a8b 43
tonymudd 0:fb8b6da96a8b 44 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
tonymudd 0:fb8b6da96a8b 45 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
tonymudd 0:fb8b6da96a8b 46 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
tonymudd 0:fb8b6da96a8b 47 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
tonymudd 0:fb8b6da96a8b 48 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
tonymudd 0:fb8b6da96a8b 49 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
tonymudd 0:fb8b6da96a8b 50 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
tonymudd 0:fb8b6da96a8b 51
tonymudd 0:fb8b6da96a8b 52 Much of the code was inspired by Derek Yerger's code, though I don't
tonymudd 0:fb8b6da96a8b 53 think much of that remains. In any event that was..
tonymudd 0:fb8b6da96a8b 54 (copyleft) 2006 by Derek Yerger - Free to distribute freely.
tonymudd 0:fb8b6da96a8b 55
tonymudd 0:fb8b6da96a8b 56 The CRC code was excerpted and inspired by the Dallas Semiconductor
tonymudd 0:fb8b6da96a8b 57 sample code bearing this copyright.
tonymudd 0:fb8b6da96a8b 58 */
tonymudd 0:fb8b6da96a8b 59 //---------------------------------------------------------------------------
tonymudd 0:fb8b6da96a8b 60 // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved.
tonymudd 0:fb8b6da96a8b 61 //
tonymudd 0:fb8b6da96a8b 62 // Permission is hereby granted, free of charge, to any person obtaining a
tonymudd 0:fb8b6da96a8b 63 // copy of this software and associated documentation files (the "Software"),
tonymudd 0:fb8b6da96a8b 64 // to deal in the Software without restriction, including without limitation
tonymudd 0:fb8b6da96a8b 65 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
tonymudd 0:fb8b6da96a8b 66 // and/or sell copies of the Software, and to permit persons to whom the
tonymudd 0:fb8b6da96a8b 67 // Software is furnished to do so, subject to the following conditions:
tonymudd 0:fb8b6da96a8b 68 //
tonymudd 0:fb8b6da96a8b 69 // The above copyright notice and this permission notice shall be included
tonymudd 0:fb8b6da96a8b 70 // in all copies or substantial portions of the Software.
tonymudd 0:fb8b6da96a8b 71 //
tonymudd 0:fb8b6da96a8b 72 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
tonymudd 0:fb8b6da96a8b 73 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
tonymudd 0:fb8b6da96a8b 74 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
tonymudd 0:fb8b6da96a8b 75 // IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
tonymudd 0:fb8b6da96a8b 76 // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
tonymudd 0:fb8b6da96a8b 77 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
tonymudd 0:fb8b6da96a8b 78 // OTHER DEALINGS IN THE SOFTWARE.
tonymudd 0:fb8b6da96a8b 79 //
tonymudd 0:fb8b6da96a8b 80 // Except as contained in this notice, the name of Dallas Semiconductor
tonymudd 0:fb8b6da96a8b 81 // shall not be used except as stated in the Dallas Semiconductor
tonymudd 0:fb8b6da96a8b 82 // Branding Policy.
tonymudd 0:fb8b6da96a8b 83 //--------------------------------------------------------------------------
tonymudd 0:fb8b6da96a8b 84
tonymudd 0:fb8b6da96a8b 85 #ifndef SNATCH59_ONEWIRECRC_H
tonymudd 0:fb8b6da96a8b 86 #define SNATCH59_ONEWIRECRC_H
tonymudd 0:fb8b6da96a8b 87
tonymudd 0:fb8b6da96a8b 88 #include <mbed.h>
tonymudd 0:fb8b6da96a8b 89
tonymudd 0:fb8b6da96a8b 90 // Select the table-lookup method of computing the 8-bit CRC by setting this to 1
tonymudd 0:fb8b6da96a8b 91 #ifndef ONEWIRE_CRC8_TABLE
tonymudd 0:fb8b6da96a8b 92 #define ONEWIRE_CRC8_TABLE 1
tonymudd 0:fb8b6da96a8b 93 #endif
tonymudd 0:fb8b6da96a8b 94
tonymudd 0:fb8b6da96a8b 95 typedef unsigned char BYTE; // used to be uint8_t : something a byte wide, whatever ....
tonymudd 0:fb8b6da96a8b 96
tonymudd 0:fb8b6da96a8b 97 enum eSpeed {OVERDRIVE, STANDARD};
tonymudd 0:fb8b6da96a8b 98
tonymudd 0:fb8b6da96a8b 99 class OneWireCRC
tonymudd 0:fb8b6da96a8b 100 {
tonymudd 0:fb8b6da96a8b 101 public:
tonymudd 0:fb8b6da96a8b 102 OneWireCRC(PinName oneWire, eSpeed);
tonymudd 0:fb8b6da96a8b 103
tonymudd 0:fb8b6da96a8b 104 // reset, read, write functions
tonymudd 0:fb8b6da96a8b 105 int reset();
tonymudd 0:fb8b6da96a8b 106 void writeByte(int data);
tonymudd 0:fb8b6da96a8b 107 int readByte();
tonymudd 0:fb8b6da96a8b 108 int touchByte(int data);
tonymudd 0:fb8b6da96a8b 109 void block(BYTE* data, int data_len);
tonymudd 0:fb8b6da96a8b 110 int overdriveSkip(BYTE* data, int data_len);
tonymudd 0:fb8b6da96a8b 111
tonymudd 0:fb8b6da96a8b 112 // address functions
tonymudd 0:fb8b6da96a8b 113 void matchROM(BYTE rom[8]);
tonymudd 0:fb8b6da96a8b 114 void skipROM();
tonymudd 0:fb8b6da96a8b 115
tonymudd 0:fb8b6da96a8b 116 // address search functions
tonymudd 0:fb8b6da96a8b 117 void resetSearch();
tonymudd 0:fb8b6da96a8b 118 BYTE search(BYTE* newAddr);
tonymudd 0:fb8b6da96a8b 119
tonymudd 0:fb8b6da96a8b 120 // CRC check functions
tonymudd 0:fb8b6da96a8b 121 static BYTE crc8(BYTE* addr, BYTE len);
tonymudd 0:fb8b6da96a8b 122 static unsigned short crc16(unsigned short* data, unsigned short len);
tonymudd 0:fb8b6da96a8b 123
tonymudd 0:fb8b6da96a8b 124 private:
tonymudd 0:fb8b6da96a8b 125 const int* timing;
tonymudd 0:fb8b6da96a8b 126
tonymudd 0:fb8b6da96a8b 127 BYTE address[8];
tonymudd 0:fb8b6da96a8b 128 int searchJunction; // so we can set to it -1 somewhere
tonymudd 0:fb8b6da96a8b 129 bool searchExhausted;
tonymudd 0:fb8b6da96a8b 130
tonymudd 0:fb8b6da96a8b 131 DigitalInOut oneWirePort;
tonymudd 0:fb8b6da96a8b 132
tonymudd 0:fb8b6da96a8b 133 // read/write bit functions
tonymudd 0:fb8b6da96a8b 134 void writeBit(int bit);
tonymudd 0:fb8b6da96a8b 135 int readBit();
tonymudd 0:fb8b6da96a8b 136 };
tonymudd 0:fb8b6da96a8b 137
tonymudd 0:fb8b6da96a8b 138 #endif