-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGFDS18B20.cpp
More file actions
426 lines (371 loc) · 11.3 KB
/
GFDS18B20.cpp
File metadata and controls
426 lines (371 loc) · 11.3 KB
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#include "Energia.h"
#include "GFDS18B20.h"
// list of commands DS18B20:
#define DS1820_WRITE_SCRATCHPAD 0x4E
#define DS1820_READ_SCRATCHPAD 0xBE
#define DS1820_COPY_SCRATCHPAD 0x48
#define DS1820_READ_EEPROM 0xB8
#define DS1820_READ_PWRSUPPLY 0xB4
#define DS1820_SEARCHROM 0xF0
#define DS1820_SKIP_ROM 0xCC
#define DS1820_READROM 0x33
#define DS1820_MATCHROM 0x55
#define DS1820_ALARMSEARCH 0xEC
#define DS1820_CONVERT_T 0x44
extern const uint16_t port_to_dir[]; // fix
extern const uint16_t port_to_ren[]; // fix
extern const uint16_t port_to_input[];
extern const uint16_t port_to_output[];
#define portDirRegister(P) ( (volatile uint8_t *)( port_to_dir[P]) )
#define portRenRegister(P) ( (volatile uint8_t *)( port_to_ren[P]) )
#define portOutputRegister(P) ( (volatile uint8_t *)( port_to_output[P]) )
#define portInputRegister(P) ( (volatile uint8_t *)( port_to_input[P]) )
#define OW_LO { *_OWPORTDIR |= _OWPORTPIN; *_OWPORTREN &= ~_OWPORTPIN; *_OWPORTOUT &= ~_OWPORTPIN; }
#define OW_HI { *_OWPORTDIR |= _OWPORTPIN; *_OWPORTREN &= ~_OWPORTPIN; *_OWPORTOUT |= _OWPORTPIN; }
#define OW_RLS { *_OWPORTDIR &= ~_OWPORTPIN; *_OWPORTREN |= _OWPORTPIN; *_OWPORTOUT |= _OWPORTPIN; }
#define OW_IN (*_OWPORTIN & _OWPORTPIN)
DS18B20::DS18B20(uint8_t OWPIN)
{
uint8_t OWPORT = digitalPinToPort(OWPIN);
_OWPORTPIN = digitalPinToBitMask(OWPIN);
_OWPORTDIR = portDirRegister(OWPORT);
_OWPORTREN = portRenRegister(OWPORT);
_OWPORTIN = portInputRegister(OWPORT);
_OWPORTOUT = portOutputRegister(OWPORT);
}
/***************************************************************/
int32_t DS18B20::GetData(void)
{
uint16_t temp;
reset();
write_byte(0xcc); // skip ROM command
write_byte(0x44); // convert T command
OW_HI
delay(750); // had incorrectly used delayMicroseconds()
reset();
write_byte(0xcc); // skip ROM command
write_byte(0xbe); // read scratchpad command
temp = ReadDS1820();
int16_t stemp = (int16_t)temp;
return((int32_t)stemp*625/10000);
}
int32_t DS18B20::GetData10(void)
{
uint16_t temp;
reset();
write_byte(0xcc); // skip ROM command
write_byte(0x44); // convert T command
OW_HI
delay(750); // had incorrectly used delayMicroseconds()
reset();
write_byte(0xcc); // skip ROM command
write_byte(0xbe); // read scratchpad command
temp = ReadDS1820();
int16_t stemp = (int16_t)temp;
return((int32_t)stemp*6250/10000);
}
//*****************
float DS18B20::GetDataf(void)
{
unsigned int temp;
reset();
write_byte(0xcc); // skip ROM command
write_byte(0x44); // convert T command
OW_HI
delay(750); // had incorrectly used delayMicroseconds()
reset();
write_byte(0xcc); // skip ROM command
write_byte(0xbe); // read scratchpad command
temp = ReadDS1820();
if(temp<0x8000)
{
return(temp*0.0625);
}
else
{
temp=(~temp)+1;
return(temp*0.0625);
}
}
//***************
uint16_t DS18B20::ReadDS1820 ( void )
{
unsigned int i;
uint16_t byte = 0;
for(i = 16; i > 0; i--){
byte >>= 1;
if (read_bit()) {
byte |= 0x8000;
}
}
return byte;
}
int DS18B20::reset(void)
{
OW_LO
delayMicroseconds(500); // 480us minimum // try 500
OW_RLS
delayMicroseconds(80); // slave waits 15-60us // try 80 or 40
if (OW_IN) return 1; // line should be pulled down by slave
delayMicroseconds(300); // slave TX presence pulse 60-240us
if (!OW_IN) return 2; // line should be "released" by slave
return 0;
}
void DS18B20::write_bit(int bit)
{
delayMicroseconds(1); // recovery, min 1us
OW_HI
if (bit) {
OW_LO
delayMicroseconds(5); // max 15us
OW_RLS // input
delayMicroseconds(56);
}
else {
OW_LO
delayMicroseconds(60); // min 60us
OW_RLS // input
delayMicroseconds(1);
}
}
//#####################################################################
int DS18B20::read_bit()
{
int bit=0;
delayMicroseconds(1);
OW_LO
delayMicroseconds(5); // hold min 1us
OW_RLS
delayMicroseconds(10); // 15us window
if (OW_IN) {
bit = 1;
}
delayMicroseconds(46); // rest of the read slot
return bit;
}
//#####################################################################
void DS18B20::write_byte(uint8_t byte)
{
int i;
for(i = 0; i < 8; i++)
{
write_bit(byte & 1);
byte >>= 1;
}
}
//#####################################################################
void DS18B20::resolution(uint8_t byte)
{
reset();
write_byte(0xCC); // skip ROM command
write_byte(0x4E); // write to eeprom
write_byte(0x00); // write to eeprom
write_byte(0x00); // write to eeprom
switch (byte) {
case 9:
write_byte(0x1F); // 0x1F - 9bit; 0x3F - 10 bit; 0x5F - 11 bit; 0x7F - 12bit
break;
case 10:
write_byte(0x3F); // 0x1F - 9bit; 0x3F - 10 bit; 0x5F - 11 bit; 0x7F - 12bit
break;
case 11:
write_byte(0x5F); // 0x1F - 9bit; 0x3F - 10 bit; 0x5F - 11 bit; 0x7F - 12bit
break;
case 12:
write_byte(0x7F); // 0x1F - 9bit; 0x3F - 10 bit; 0x5F - 11 bit; 0x7F - 12bit
break;
default:
write_byte(0x1F); // 0x1F - 9bit; 0x3F - 10 bit; 0x5F - 11 bit; 0x7F - 12bit
}
reset();
}
//#####################################################################
uint8_t DS18B20::read_byte()
{
unsigned int i;
uint8_t byte = 0;
for(i = 0; i < 8; i++)
{
byte >>= 1;
if (read_bit()) byte |= 0x80;
}
return byte;
}
void DS18B20::reset_search()
{
// reset the search state
LastDiscrepancy = 0;
LastDeviceFlag = FALSE;
LastFamilyDiscrepancy = 0;
for(int i = 7; ; i--)
{
ROM_NO[i] = 0;
if ( i == 0) break;
}
}
//
// Perform a search. If this function returns a '1' then it has
// enumerated the next device and you may retrieve the ROM from the
// OneWire::address variable. If there are no devices, no further
// devices, or something horrible happens in the middle of the
// enumeration then a 0 is returned. If a new device is found then
// its address is copied to newAddr. Use OneWire::reset_search() to
// start over.
//
// --- Replaced by the one from the Dallas Semiconductor web site ---
//--------------------------------------------------------------------------
// Perform the 1-Wire Search Algorithm on the 1-Wire bus using the existing
// search state.
// Return TRUE : device found, ROM number in ROM_NO buffer
// FALSE : device not found, end of search
//
uint8_t DS18B20::search(uint8_t *newAddr)
{
uint8_t id_bit_number;
uint8_t last_zero, rom_byte_number, search_result;
uint8_t id_bit, cmp_id_bit;
uint8_t ii=0;
unsigned char rom_byte_mask, search_direction;
// initialize for search
id_bit_number = 1;
last_zero = 0;
rom_byte_number = 0;
rom_byte_mask = 1;
search_result = 0;
// if the last call was not the last one
if (!LastDeviceFlag)
{
// 1-Wire reset
ii=reset();
if (ii) // ii>0
{
// reset the search
LastDiscrepancy = 0;
LastDeviceFlag = FALSE;
LastFamilyDiscrepancy = 0;
return ii; // Pass back the reset error status gf***
}
// issue the search command
write_byte(0xF0);
// loop to do the search
do
{
// read a bit and its complement
id_bit = read_bit();
cmp_id_bit = read_bit();
// check for no devices on 1-wire
if ((id_bit == 1) && (cmp_id_bit == 1))
break;
else
{
// all devices coupled have 0 or 1
if (id_bit != cmp_id_bit)
search_direction = id_bit; // bit write value for search
else
{
// if this discrepancy if before the Last Discrepancy
// on a previous next then pick the same as last time
if (id_bit_number < LastDiscrepancy)
search_direction = ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0);
else
// if equal to last pick 1, if not then pick 0
search_direction = (id_bit_number == LastDiscrepancy);
// if 0 was picked then record its position in LastZero
if (search_direction == 0)
{
last_zero = id_bit_number;
// check for Last discrepancy in family
if (last_zero < 9)
LastFamilyDiscrepancy = last_zero;
}
}
// set or clear the bit in the ROM byte rom_byte_number
// with mask rom_byte_mask
if (search_direction == 1)
ROM_NO[rom_byte_number] |= rom_byte_mask;
else
ROM_NO[rom_byte_number] &= ~rom_byte_mask;
// serial number search direction write bit
write_bit(search_direction);
// increment the byte counter id_bit_number
// and shift the mask rom_byte_mask
id_bit_number++;
rom_byte_mask <<= 1;
// if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask
if (rom_byte_mask == 0)
{
rom_byte_number++;
rom_byte_mask = 1;
}
}
}
while(rom_byte_number < 8); // loop until through all ROM bytes 0-7
// if the search was successful then
if (!(id_bit_number < 65))
{
// search successful so set LastDiscrepancy,LastDeviceFlag,search_result
LastDiscrepancy = last_zero;
// check for last device
if (LastDiscrepancy == 0)
LastDeviceFlag = TRUE;
//search_result = 0; // All OK status GF***
search_result =30; // All OK status GF***
}
}
// if no device found then reset counters so next 'search' will be like a first
//if (search_result || !ROM_NO[0])
//if (!ROM_NO[0])
if (!search_result || !ROM_NO[0])
{
LastDiscrepancy = 0;
LastDeviceFlag = FALSE;
LastFamilyDiscrepancy = 0;
search_result = 3; //FALSE; //GF***
}
for (int i = 0; i < 8; i++) newAddr[i] = ROM_NO[i];
return search_result;
}
//
// Compute a Dallas Semiconductor 8 bit CRC directly.
//
/*
uint8_t DS18B20::crc8( uint8_t *addr, uint8_t len)
{
uint8_t crc = 0;
while (len--) {
uint8_t inbyte = *addr++;
for (uint8_t i = 8; i; i--) {
uint8_t mix = (crc ^ inbyte) & 0x01;
crc >>= 1;
if (mix) crc ^= 0x8C;
inbyte >>= 1;
}
}
return crc;
}
*/
void DS18B20::select( uint8_t rom[8])
{
int i;
write_byte(0x55); // Choose ROM
for( i = 0; i < 8; i++) write_byte(rom[i]);
}
//
// Write a byte. The writing code uses the active drivers to raise the
// pin high, if you need power after the write (e.g. DS18S20 in
// parasite power mode) then set 'power' to 1, otherwise the pin will
// go tri-state at the end of the write to avoid heating in a short or
// other mishap.
//void DS18B20::write(uint8_t v, uint8_t power /* = 0 */) {
/* uint8_t bitMask;
for (bitMask = 0x01; bitMask; bitMask <<= 1) {
OneWire::write_bit( (bitMask & v)?1:0);
}
if ( !power) {
noInterrupts(); // cli();
DIRECT_MODE_INPUT(baseReg, bitmask);
DIRECT_WRITE_LOW(baseReg, bitmask);
interrupts(); // sei();
}
}
*/