Auto search for All DS18B20 sensors on data bus

Dependencies:   mbed

Committer:
umairaftab
Date:
Sun Apr 13 10:06:52 2014 +0000
Revision:
0:a2a81f538133
Multiple Sensors Functionality Added to Peter Sudensikis Library

Who changed what in which revision?

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