forked from Pfannex/DS18B20_DS2482
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DS2482.cpp
382 lines (314 loc) · 6.5 KB
/
DS2482.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
DS2482 library for Arduino
Copyright (C) 2009-2010 Paeae Technologies
This program 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.
This program 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 readd a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
crc code is from OneWire library
-Updates:
* fixed wireReadByte busyWait (thanks Mike Jackson)
* Modified search function (thanks Gary Fariss)
*/
#include "Arduino.h" // according http://blog.makezine.com/2011/12/01/arduino-1-0-is-out-heres-what-you-need-to-know/
#include "DS2482.h"
#include "Wire.h"
#define PTR_STATUS 0xf0
#define PTR_READ 0xe1
#define PTR_CONFIG 0xc3
enum {
DS18S20 = (byte)0x10
, DS18B20 = (byte)0x28
, DS1822 = (byte)0x22
, DS1825 = (byte)0x3B
} TemperatureSensors;
enum {
DS2406 = (byte)0x12
} SwitchSensors;
DS2482::DS2482(uint8_t addr)
{
mAddress = 0x18 | addr;
}
//-------helpers
void DS2482::begin()
{
Wire.beginTransmission(mAddress);
}
void DS2482::end()
{
Wire.endTransmission();
}
void DS2482::setReadPtr(uint8_t readPtr)
{
begin();
Wire.write(0xe1); // changed from 'send' to 'write' according http://blog.makezine.com/2011/12/01/arduino-1-0-is-out-heres-what-you-need-to-know/'
Wire.write(readPtr);
end();
}
uint8_t DS2482::readByte()
{
Wire.requestFrom(mAddress,(uint8_t)1);
return Wire.read();
}
uint8_t DS2482::wireReadStatus(bool setPtr)
{
if (setPtr)
setReadPtr(PTR_STATUS);
return readByte();
}
uint8_t DS2482::busyWait(bool setReadPtr)
{
uint8_t status;
int loopCount = 1000;
while((status = wireReadStatus(setReadPtr)) & DS2482_STATUS_BUSY)
{
if (--loopCount <= 0)
{
mTimeout = 1;
break;
}
delayMicroseconds(20);
}
return status;
}
//----------interface
uint8_t DS2482::reset()
{
return wireReset();
}
bool DS2482::configure(uint8_t config)
{
busyWait(true);
begin();
Wire.write(0xd2);
Wire.write(config | (~config)<<4);
return readByte() == config;
}
bool DS2482::selectChannel(uint8_t channel)
{
uint8_t ch, ch_read;
switch (channel)
{
case 0:
default:
ch = 0xf0;
ch_read = 0xb8;
break;
case 1:
ch = 0xe1;
ch_read = 0xb1;
break;
case 2:
ch = 0xd2;
ch_read = 0xaa;
break;
case 3:
ch = 0xc3;
ch_read = 0xa3;
break;
case 4:
ch = 0xb4;
ch_read = 0x9c;
break;
case 5:
ch = 0xa5;
ch_read = 0x95;
break;
case 6:
ch = 0x96;
ch_read = 0x8e;
break;
case 7:
ch = 0x87;
ch_read = 0x87;
break;
};
busyWait(true);
begin();
Wire.write(0xc3);
Wire.write(ch);
end();
busyWait();
uint8_t check = readByte();
return check == ch_read;
}
bool DS2482::wireReset()
{
busyWait(true);
begin();
Wire.write(0xb4);
end();
uint8_t status = busyWait();
return status & DS2482_STATUS_PPD ? true : false;
}
void DS2482::wireWriteByte(uint8_t b)
{
busyWait(true);
begin();
Wire.write(0xa5);
Wire.write(b);
end();
}
uint8_t DS2482::wireReadByte()
{
busyWait(true);
begin();
Wire.write(0x96);
end();
busyWait();
setReadPtr(PTR_READ);
return readByte();
}
void DS2482::wireWriteBit(uint8_t bit)
{
busyWait(true);
begin();
Wire.write(0x87);
Wire.write(bit ? 0x80 : 0);
end();
}
uint8_t DS2482::wireReadBit()
{
wireWriteBit(1);
uint8_t status = busyWait(true);
return status & DS2482_STATUS_SBR ? 1 : 0;
}
void DS2482::wireSkip()
{
wireWriteByte(0xcc);
}
void DS2482::wireSelect(uint8_t rom[8])
{
wireWriteByte(0x55);
for (int i=0;i<8;i++)
wireWriteByte(rom[i]);
}
#if ONEWIRE_SEARCH
void DS2482::wireResetSearch()
{
searchExhausted = 0;
searchLastDisrepancy = 0;
for(uint8_t i = 0; i<8; i++)
searchAddress[i] = 0;
}
uint8_t DS2482::wireSearch(uint8_t *newAddr)
{
uint8_t i;
uint8_t direction;
uint8_t last_zero=0;
if (searchExhausted)
return 0;
if (!wireReset())
return 0;
busyWait(true);
wireWriteByte(0xf0);
for(i=1;i<65;i++)
{
int romByte = (i-1)>>3;
int romBit = 1<<((i-1)&7);
if (i < searchLastDisrepancy)
direction = searchAddress[romByte] & romBit;
else
direction = i == searchLastDisrepancy;
busyWait();
begin();
Wire.write(0x78);
Wire.write(direction ? 0x80 : 0);
end();
uint8_t status = busyWait();
uint8_t id = status & DS2482_STATUS_SBR;
uint8_t comp_id = status & DS2482_STATUS_TSB;
direction = status & DS2482_STATUS_DIR;
if (id && comp_id)
return 0;
else
{
if (!id && !comp_id && !direction)
last_zero = i;
}
if (direction)
searchAddress[romByte] |= romBit;
else
searchAddress[romByte] &= (uint8_t)~romBit;
}
searchLastDisrepancy = last_zero;
if (last_zero == 0)
searchExhausted = 1;
for (i=0;i<8;i++)
newAddr[i] = searchAddress[i];
return 1;
}
#endif
uint8_t DS2482::devicesCount(bool printAddress){
uint8_t address[8];
uint8_t count = 0;
String SerialNumber = "";
wireResetSearch();
while (wireSearch(address)){
count++;
SerialNumber = "";
for (uint8_t i = 0; i < 8; i++){
if (address[i] < 0x10) SerialNumber += "0";
SerialNumber += String(address[i], HEX);
if (i < 7) SerialNumber += "-";
}
if (printAddress){
Serial.print(SerialNumber);
deviceName(address[0]);
Serial.println();
}
}
return count;
}
#if ONEWIRE_CRC
// The 1-Wire CRC scheme is described in Maxim Application Note 27:
// "Understanding and Using Cyclic Redundancy Checks with Maxim iButton Products"
//
uint8_t DS2482::crc8( uint8_t *addr, uint8_t len)
{
uint8_t crc=0;
for (uint8_t i=0; i<len;i++)
{
uint8_t inbyte = addr[i];
for (uint8_t j=0;j<8;j++)
{
uint8_t mix = (crc ^ inbyte) & 0x01;
crc >>= 1;
if (mix)
crc ^= 0x8C;
inbyte >>= 1;
}
}
return crc;
}
// tools
#define getString(type) (String)#type
void DS2482::deviceName(uint8_t device) {
String result = "";
switch(device) {
case DS18S20:
result = getString(DS18S20);
break;
case DS18B20:
result = getString(DS18B20);
break;
case DS1822:
result = getString(DS1822);
break;
case DS1825:
result = getString(DS1825);
break;
case DS2406:
result = getString(DS2406);
break;
}
if (result != "")
Serial.print(" - " + result);
}
#endif