Now supports DS18B20 and DS18S20 Maxim/Dallas one-wire thermometer devices. Also supports DS18S20 in 9, 10, 11, and 12 bit resolution modes. 'Use Address' mode now checks if the correct device type is present, and informs the user which device to use. Correct temperature conversion times now used in non-parasitic mode. The device should be placed at least 6 inches (15 cm) from the mbed board in order to accurately read ambient temperature.

Dependencies:   mbed

Committer:
snatch59
Date:
Sun Jan 03 11:57:31 2010 +0000
Revision:
0:01a6a40578c9

        

Who changed what in which revision?

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