-
Notifications
You must be signed in to change notification settings - Fork 6
/
OneWire.h
332 lines (245 loc) · 8.09 KB
/
OneWire.h
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
/* ************************************************************************
*
* OneWire bus
*
* (c) 2018-2023 by Markus Reschke
*
* ************************************************************************ */
/* ************************************************************************
* ROM commands (common to all clients)
* ************************************************************************ */
/*
* ROM code
* - byte #0: family code
* bit #0-6: device type
* bit #7: custom flag (customer ID = 12 upper bits of serial number)
* - byte #1-6: serial number (byte #1 LSB)
* - byte #7: CRC (of the first 7 bytes)
*/
/* ROM family codes */
#define FAMILY_CODE_DS18B20 0x28
#define FAMILY_CODE_DS18S20 0x10
#define FAMILY_CODE_DS1822 0x22
/*
* search ROM
* - search for client(s) on the bus
* - tree-like search starting with ROM bit #0
* - master's first read: clients send ROM bit
* - master's second read: clients send inverted ROM bit
* - master sends ROM bit to select a part of the tree
* - this process is repeated for the remaining ROM bits
* and will get the ROM code of one client
* - ROM bit logic (ROM bit, inverted ROM bit)
* - 00 devices with conflicting ROM bits
* - 01 all devices have ROM bit 0
* - 10 all devices have ROM bit 1
* - 11 no devices on bus
*/
#define CMD_SEARCH_ROM 0xF0
/*
* read ROM
* - can be used only when one single client is on the bus
* - client sends its ROM code to the master (byte 0 first)
*/
#define CMD_READ_ROM 0x33
/*
* match ROM
* - address client with specific ROM code
* - master sends client's ROM code (byte 0 first)
*/
#define CMD_MATCH_ROM 0x55
/*
* skip ROM
* - address all clients on the bus
*/
#define CMD_SKIP_ROM 0xCC
/*
* alarm search
* - search for clients with an alarm condition
* - works like the "search ROM" command
*/
#define CMD_ALARM_SEARCH 0xEC
/* ************************************************************************
* DS18B20 function commands
* ************************************************************************ */
/*
* scatchpad memory map
* - byte #0: temperature LSB
* - byte #1: temperature MSB
* - byte #2: T_H register or user byte #1
* - byte #3: T_L register or user byte #2
* - byte #4: configuration register
* - byte #5: reserved (0xFF)
* - byte #6: reserved
* - byte #7: reserved (0x10)
* - byte #8: CRC (of the first 8 bytes)
*/
/*
* temperature register
* - bit 7 6 5 4 3 2 1 0
* t LSB 2^3 2^2 2^1 2^0 2^-1 2^-2 2^-3 2^-4
* t MSB S S S S S 2^6 2^5 2^4
* - format
* - two's complement
* - sign bit S = 2^7 (- 2^11)
* - resolution undefined bits step for decimal places
* 12 bits - 0.0625°C
* 11 bits 2^-4 0.125°C
* 10 bits 2^-4 & 2^-3 0.25°C
* 9 bits 2^-4, 2^-3 & 2^-2 0.5°C
*/
/*
* configuration register
* - bit 7 6 5 4 3 2 1 0
* 0 R1 R0 1 1 1 1 1
*/
/* thermometer resolution */
#define FLAG_DS18B20_R0 0b00100000 /* R0 bit */
#define FLAG_DS18B20_R1 0b01000000 /* R1 bit */
#define FLAG_DS18B20_RES_9 0b00000000 /* 9 bits */
#define FLAG_DS18B20_RES_10 0b00100000 /* 10 bits */
#define FLAG_DS18B20_RES_11 0b01000000 /* 11 bits */
#define FLAG_DS18B20_RES_12 0b01100000 /* 12 bits (default) */
/*
* alarm trigger registers T_H and T_L
* - T_H: alarm when T >= T_H
* - T_L: alarm when T <= T_L
* - bit 7 6 5 4 3 2 1 0
* S #6 #5 #4 #3 #2 #1 #0
* - sign bits S
* - positive temperature: S = 0
* - negative temperature: S = 1
*/
/*
* convert T
* - start conversion of temperature
* - DS18B20 sends conversion status to master
* (not available when parasitic-power mode is used)
*/
#define CMD_DS18B20_CONVERT_T 0x44
/* response */
#define FLAG_CONV_IN_PROGRESS 0 /* conversion (still) in progress */
#define FLAG_CONV_DONE 1 /* conversion finished */
/*
* write scratchpad
* - write data from master to scratchpad
* (T_H, T_L and configuration register)
* - master sends 3 bytes
* - no feedback from DS18B20 to master
*/
#define CMD_DS18B20_WRITE_SCRATCHPAD 0x4E
/*
* read scratchpad
* - read entire scratchpad including CRC
* - DS18B20 sends up to 9 bytes to master (byte 0 first)
*/
#define CMD_DS18B20_READ_SCRATCHPAD 0xBE
/*
* copy scratchpad
* - copy T_H, T_L and configuration register
* from scatchpad to EEPROM
* - no feedback from DS18B20 to master
*/
#define CMD_DS18B20_COPY_SCRATCHPAD 0x48
/*
* recall E² (EEPROM)
* - copy T_H, T_L and configuration register
* from EEPROM to scatchpad
* - DS18B20 sends recall status to master
*/
#define CMD_DS18B20_RECALL_E2 0xB8
/* response */
#define FLAG_RECALL_IN_PROGRESS 0 /* recall (still) in progress */
#define FLAG_RECALL_DONE 1 /* recall finished */
/*
* read power supply
* - DS18B20 sends supply status to master
*/
#define CMD_DS18B20_READ_POWER_SUPPLY 0xB4
/* response */
#define FLAG_PWR_PARASITIC 0 /* parsitic-powered */
#define FLAG_PWR_EXTERNAL 1 /* powered externally */
/* ************************************************************************
* DS18S20 function commands
* ************************************************************************ */
/*
* scatchpad memory map
* - byte #0: temperature LSB
* - byte #1: temperature MSB
* - byte #2: T_H register or user byte #1
* - byte #3: T_L register or user byte #2
* - byte #4: reserved (0xFF)
* - byte #5: reserved (0xFF)
* - byte #6: count remain (<=0x0C)
* - byte #7: cound per °C (0x10)
* - byte #8: CRC (of the first 8 bytes)
*/
/*
* temperature register
* - bit 7 6 5 4 3 2 1 0
* t LSB 2^6 2^5 2^4 2^3 2^2 2^1 2^0 2^-1
* t MSB S S S S S S S S
* - format
* - two's complement
* - sign bit S
*/
/*
* alarm trigger registers T_H and T_L
* - T_H: alarm when T >= T_H
* - T_L: alarm when T <= T_L
* - bit 7 6 5 4 3 2 1 0
* S #6 #5 #4 #3 #2 #1 #0
* - sign bits S
* - positive temperature: S = 0
* - negative temperature: S = 1
*/
/*
* convert T
* - start conversion of temperature
* - DS18S20 sends conversion status to master
* (not available when parasitic-power mode is used)
*/
#define CMD_DS18S20_CONVERT_T 0x44
/* response (see CMD_DS18B20_CONVERT_T) */
/* FLAG_CONV_IN_PROGRESS 0 conversion (still) in progress */
/* FLAG_CONV_DONE 1 conversion finished */
/*
* read scratchpad
* - read entire scratchpad including CRC
* - DS18S20 sends up to 9 bytes to master (byte 0 first)
*/
#define CMD_DS18S20_READ_SCRATCHPAD 0xBE
/*
* write scratchpad
* - write data from master to scratchpad
* (T_H and T_L)
* - master sends 2 bytes
* - no feedback from DS18S20 to master
*/
#define CMD_DS18S20_WRITE_SCRATCHPAD 0x4E
/*
* copy scratchpad
* - copy T_H and T_L from scatchpad to EEPROM
* - no feedback from DS18S20 to master
*/
#define CMD_DS18S20_COPY_SCRATCHPAD 0x48
/*
* recall E² (EEPROM)
* - copy T_H and T_L from EEPROM to scatchpad
* - DS18S20 sends recall status to master
*/
#define CMD_DS18S20_RECALL_E2 0xB8
/* response (see CMD_DS18B20_RECALL_E2) */
/* FLAG_RECALL_IN_PROGRESS 0 recall (still) in progress */
/* FLAG_RECALL_DONE 1 recall finished */
/*
* read power supply
* - DS18S20 sends supply status to master
*/
#define CMD_DS18S20_READ_POWER_SUPPLY 0xB4
/* response (see CMD_DS18B20_READ_POWER_SUPPLY) */
/* FLAG_PWR_PARASITIC 0 parsitic-powered */
/* FLAG_PWR_EXTERNAL 1 powered externally */
/* ************************************************************************
* EOF
* ************************************************************************ */