-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathds18b20.ino
61 lines (56 loc) · 2.18 KB
/
ds18b20.ino
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
void initDS() {//init temp sensors
uint8_t i = 0;
uint8_t resolution = 0;
while (ds.search(DSaddr[i])) {
if (i > 1) {
PRINTLNF("more than 2 devices, unable to load them all");
break;
}
if (OneWire::crc8(DSaddr[i], 7) != DSaddr[i][7]) {
PRINTLNF("CRC of temp sensor is not valid!");
break;
}
// PRINTLNF("read resolution");
ds.reset(); //read resolution
ds.select(DSaddr[i]);
ds.write(0xBE); // Read Scratchpad
for (uint8_t j = 0; j < 5; j++) {
resolution = ds.read(); // we need fifth byte, (resolution) 7F=12bits 5F=11bits 3F=10bits 1F=9bits
}
PRINTLNHEX("DS18B20 resolution (0x7F max)=", resolution);
if (resolution != 0x7f) {
ds.reset();
ds.select(DSaddr[i]);
ds.write(0x4E); // Write scratchpad command
ds.write(0); // TL data
ds.write(0); // TH data
ds.write(0x7F); // Configuration Register (resolution) 7F=12bits 5F=11bits 3F=10bits 1F=9bits
ds.reset(); // This "reset" sequence is mandatory
ds.select(DSaddr[i]); // it allows the DS18B20 to understand the copy scratchpad to EEPROM command
ds.write(0x48); // Copy Scratchpad command
}
i++;
}
}
void requestTempUpdateScreen() {//send request to temp sensors
ds.reset();
ds.write(0xCC, 0); // skip address (broadcast to all devices)
ds.write(0x44, 0); // start conversion (start temp measurement)
timer.setTimeout(200L, readDSresponse);
}
void readDSresponse() {//read response from sensor
static uint8_t buf[9];
for (uint8_t i = 0; i < 2; i++) {
ds.reset();
ds.select(DSaddr[i]);
ds.write(0xBE, 0); // read data from DS
ds.read_bytes(buf, 9);
if (OneWire::crc8(buf, 8) == buf[8] ) // check CRC
temp[i] = ((int)buf[0] | (((int)buf[1]) << 8)) * 0.0625 + 0.03125; //first and second byte read, convert to int
else
temp[i] = -99; // CRC is not valid
//temp[i] = ((int)ds.read() | (((int)ds.read()) << 8)) * 0.0625 + 0.03125; //first and second byte read, convert to int, TODO add CRC check
}
if (currentMenu == 0 || currentMenu >= 4) //if main screen or set params is displayed,
updateMainScreenFlag = true;
}