-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemp.cpp
203 lines (173 loc) · 4.77 KB
/
Temp.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
#include <Arduino.h>
#include <OneWire.h>
// https://lowpowerlab.com/forum/moteino/improvedoptimized-ds18b201wire-read/
#include "Temp.h"
// http://www.pjrc.com/teensy/td_libs_OneWire.html
Temp::Temp(int power, int pin) : myds(pin) {
// Constructor
// Takes an int as pin to power up, and data pin
_powerPin = power;
pinMode(_powerPin, OUTPUT);
_dataPin = pin;
pinMode(_dataPin, INPUT);
OneWire myds(_dataPin);
}
void Temp::init(void) {
// turn device on
// find ds18B20 address
//DEBUGln("Temp: init");
on();
getFirstDsAdd(myds, _dsAddr);
//DEBUGln("_dsAddr is: ");
//byte i;
//for( i = 0; i < 8; i++) {
// Serial.print("0x");
// if (_dsAddr[i] < 16) {
// Serial.print('0');
// }
// Serial.print(_dsAddr[i], HEX);
// if (i < 7) {
// Serial.print(", ");
// }
//}
off();
}
float Temp::read() {
// should be called before Temp::startConvert
// block whilst we await a ready DS18B20
// hard coded to 9bit sho should take less than 100ms
//DEBUGln("Temp: read");
while (!myds.read()) {
// block (do nothing)
}
_present = 0;
//byte data[12];
//byte type_s;
//type_s = 0;
// get ready to read Sratchpad
_present = myds.reset();
myds.select(_dsAddr);
myds.write(0xBE); // Read Scratchpad
//Serial.print(" Data = ");
//Serial.print(present,HEX);
// Serial.println("Raw Scratchpad Data: ");
// read data
for ( int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = myds.read();
// Serial.print(data[i], HEX);
// Serial.print(" ");
}
//Serial.print(" CRC=");
//Serial.print(OneWire::crc8(data, 8), HEX);
// Serial.println();
// convert the data to actual temperature
//unsigned int raw = (data[1] << 8) | data[0];
// if (type_s) {
// DEBUGln("type S!");
// raw = raw << 3; // 9 bit resolution default
// if (data[7] == 0x10) {
// DEBUGln("data[7] == 0x10");
// count remain gives full 12 bit resolution
// raw = (raw & 0xFFF0) + 12 - data[6];
// } else {
// byte cfg = (data[4] & 0x60);
// DEBUGln("byte cfg = (data[4] & 0x60);");
// if (cfg == 0x00) {
// DEBUGln("cfg == 0x00");
// raw = raw << 3;
// } // 9 bit resolution, 93.75 ms
// else if (cfg == 0x20) {
// DEBUGln("cfg == 0x20");
// raw = raw << 2; // 10 bit res, 187.5 ms
// }
// else if (cfg == 0x40) {
// DEBUGln("cfg == 0x40");
// raw = raw << 1; // 11 bit res, 375 ms
// }
// default is 12 bit resolution, 750 ms conversion time
// }
// }
//raw = raw << 3;
_TReading = (data[1] << 8) + data[0];
_SignBit = _TReading & 0x8000; // test most sig bit
if (_SignBit) // negative
{
_TReading = (_TReading ^ 0xffff) + 1; // 2's comp
}
_celsius = float(_TReading)/16;
if (_SignBit){
_celsius = _celsius * -1;
}
elapsedtime = millis() - starttime;
DEBUG("temp time: ");
DEBUGln(elapsedtime);
off();
return _celsius;
}
void Temp::startConvert(void) {
// switch DS18B20 on
// set resolution
// call temp convert command
//DEBUGln("Temp: startConvert");
on();
dsSetResolution(myds, _dsAddr);
starttime = millis();
dsConvertCommand(myds, _dsAddr);
}
void Temp::dsConvertCommand(OneWire myds, byte addr[8]) {
//DEBUGln("Temp: dsConvertCommand");
myds.reset();
myds.select(addr);
// start conversion, with parasite power on at the end
myds.write(0x44,1);
}
void Temp::dsSetResolution(OneWire myds, byte addr[8]) {
//DEBUGln("Temp: dsSetResolution");
// Set 9 bit config
myds.reset();
myds.select(addr);
myds.write(0x4E); // Write scratchpad
myds.write(0); // TL
myds.write(0); // TH
myds.write(0x1F); // Configuration Register
myds.write(0x48); // Copy Scratchpad
}
void Temp::getFirstDsAdd(OneWire myds, byte firstadd[]){
// find the first DS18B20 address and save it in _dsAddr
byte i;
byte addr[8];
//DEBUGln("Looking for 1-Wire devices...");
while(myds.search(addr)) {
//DEBUGln("Found \'1-Wire\' device with address: ");
for( i = 0; i < 8; i++) {
firstadd[i]=addr[i];
DEBUG("0x");
if (addr[i] < 16) {
DEBUG('0');
}
Serial.print(addr[i], HEX);
if (i < 7) {
DEBUG(", ");
}
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
//DEBUGln("CRC is not valid!");
return;
}
// the first ROM byte indicates which chip
//DEBUGln("");
//DEBUG("address:");
//DEBUGln(addr[0]);
return;
}
}
void Temp::on(void) {
// Switch device on by putting pin HIGH
digitalWrite(_powerPin, HIGH);
//DEBUGln("Temp device on!");
}
void Temp::off(void) {
// turn off the Device by putting pin LOW
digitalWrite(_powerPin, LOW);
//DEBUGln("Temp device off!");
}