forked from WiseLord/ampcontrol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathds18x20.c
260 lines (202 loc) · 4.94 KB
/
ds18x20.c
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
#include "ds18x20.h"
#include <util/delay.h>
#include <util/crc16.h>
static ds18x20Dev devs[DS18X20_MAX_DEV];
static uint8_t devCount = 0;
static uint8_t ds18x20IsOnBus(void)
{
uint8_t ret;
DDR(ONE_WIRE) |= ONE_WIRE_LINE; // Pin as output (0)
PORT(ONE_WIRE) &= ~ONE_WIRE_LINE; // Set active 0
_delay_us(480); // Reset
DDR(ONE_WIRE) &= ~ONE_WIRE_LINE; // Pin as input (1)
PORT(ONE_WIRE) |= ONE_WIRE_LINE; // Enable pull-up resitor
_delay_us(70); // Wait for response
ret = !(PIN(ONE_WIRE) & ONE_WIRE_LINE);
_delay_us(410);
return ret;
}
static void ds18x20SendBit(uint8_t bit)
{
DDR(ONE_WIRE) |= ONE_WIRE_LINE; // Pin as output (0)
PORT(ONE_WIRE) &= ~ONE_WIRE_LINE; // Set active 0
_delay_us(5);
if (!bit)
_delay_us(50);
DDR(ONE_WIRE) &= ~ONE_WIRE_LINE; // Pin as input (1)
PORT(ONE_WIRE) |= ONE_WIRE_LINE; // Enable pull-up resitor
_delay_us(5);
if (bit)
_delay_us(50);
return;
}
static uint8_t ds18x20GetBit(void)
{
uint8_t ret;
DDR(ONE_WIRE) |= ONE_WIRE_LINE; // Pin as output (0)
PORT(ONE_WIRE) &= ~ONE_WIRE_LINE; // Set active 0
_delay_us(5); // Strob
DDR(ONE_WIRE) &= ~ONE_WIRE_LINE; // Pin as input (1)
PORT(ONE_WIRE) |= ONE_WIRE_LINE; // Enable pull-up resitor
_delay_us(5);
ret = PIN(ONE_WIRE) & ONE_WIRE_LINE;
_delay_us(50);
return ret;
}
static void ds18x20SendByte(uint8_t byte)
{
uint8_t i;
for (i = 0; i < 8; i++) {
ds18x20SendBit(byte & 0x01);
byte >>= 1;
}
return;
}
static uint8_t ds18x20GetByte(void)
{
uint8_t i, ret;
ret = 0;
for (i = 0; i < 8; i++) {
ret >>= 1;
if (ds18x20GetBit())
ret |= 0x80;
}
return ret;
}
static void ds18x20Select(ds18x20Dev *dev)
{
uint8_t i;
ds18x20SendByte(DS18X20_CMD_MATCH_ROM);
for (i = 0; i < 8; i++)
ds18x20SendByte(dev->id[i]);
return;
}
static void ds18x20GetAllTemps()
{
uint8_t i, j;
uint8_t crc;
uint8_t arr[DS18X20_SCRATCH_LEN];
for (i = 0; i < devCount; i++) {
if (ds18x20IsOnBus()) {
ds18x20Select(&devs[i]);
ds18x20SendByte(DS18X20_CMD_READ_SCRATCH);
// Control scratchpad checksum
crc = 0;
for (j = 0; j < DS18X20_SCRATCH_LEN; j++) {
arr[j] = ds18x20GetByte();
crc = _crc_ibutton_update(crc, arr[j]);
}
if (crc == 0) {
// Save first 2 bytes (temperature) of scratchpad
for (j = 0; j < DS18X20_SCRATCH_TEMP_LEN; j++)
devs[i].sp[j] = arr[j];
}
}
}
return;
}
static void ds18x20ConvertTemp(void)
{
ds18x20SendByte(DS18X20_CMD_SKIP_ROM);
ds18x20SendByte(DS18X20_CMD_CONVERT);
#ifdef DS18X20_PARASITE_POWER
// Set active 1 on port for at least 750ms as parasitic power
PORT(ONE_WIRE) |= ONE_WIRE_LINE;
DDR(ONE_WIRE) |= ONE_WIRE_LINE;
#endif
return;
}
static uint8_t ds18x20SearchRom(uint8_t *bitPattern, uint8_t lastDeviation)
{
uint8_t currBit;
uint8_t newDeviation = 0;
uint8_t bitMask = 0x01;
uint8_t bitA;
uint8_t bitB;
// Send SEARCH ROM command on the bus
ds18x20SendByte(DS18X20_CMD_SEARCH_ROM);
// Walk through all 64 bits
for (currBit = 0; currBit < DS18X20_ID_LEN * 8; currBit++)
{
// Read bit from bus twice.
bitA = ds18x20GetBit();
bitB = ds18x20GetBit();
if (bitA && bitB) { // Both bits 1 = ERROR
return 0xFF;
} else if (!(bitA || bitB)) { // Both bits 0
if (currBit == lastDeviation) { // Select 1 if device has been selected
*bitPattern |= bitMask;
} else if (currBit > lastDeviation) { // Select 0 if no, and remember device
(*bitPattern) &= ~bitMask;
newDeviation = currBit;
} else if (!(*bitPattern & bitMask)) { // Otherwise just remember device
newDeviation = currBit;
}
} else { // Bits differ
if (bitA)
*bitPattern |= bitMask;
else
*bitPattern &= ~bitMask;
}
// Send the selected bit to the bus.
ds18x20SendBit(*bitPattern & bitMask);
// Adjust bitMask and bitPattern pointer.
bitMask <<= 1;
if (!bitMask)
{
bitMask = 0x01;
bitPattern++;
}
}
return newDeviation;
}
void ds18x20SearchDevices(void)
{
uint8_t i, j;
uint8_t *newID;
uint8_t *currentID;
uint8_t lastDeviation;
uint8_t count = 0;
// Reset addresses
for (i = 0; i < DS18X20_MAX_DEV; i++)
for (j = 0; j < DS18X20_ID_LEN; j++)
devs[i].id[j] = 0x00;
// Search all sensors
newID = devs[0].id;
lastDeviation = 0;
currentID = newID;
do {
for (j = 0; j < DS18X20_ID_LEN; j++)
newID[j] = currentID[j];
if (!ds18x20IsOnBus()) {
devCount = 0;
return;
}
lastDeviation = ds18x20SearchRom(newID, lastDeviation);
currentID = newID;
count++;
newID=devs[count].id;
} while (lastDeviation != 0);
devCount = count;
return;
}
uint8_t ds18x20Process(void)
{
ds18x20GetAllTemps();
// Convert temperature
if (ds18x20IsOnBus())
ds18x20ConvertTemp();
return devCount;
}
int16_t ds18x20GetTemp(uint8_t num)
{
int16_t ret = devs[num].temp * 5;
if (devs[num].id[0] == 0x28) // DS18B20 has 8X better resolution
ret /= 8;
// Return value is in 0.1°C units
return ret;
}
uint8_t ds18x20GetDevCount(void)
{
return devCount;
}