-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTMP275.cpp
More file actions
177 lines (150 loc) · 4.68 KB
/
Copy pathTMP275.cpp
File metadata and controls
177 lines (150 loc) · 4.68 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
/*
Copyright (c) 2016, Embedded Adventures
All rights reserved.
Contact us at source [at] embeddedadventures.com
www.embeddedadventures.com
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name of Embedded Adventures nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
*/
// TMP275 MOD-1001 RTC and Temperature Sensor Arduino library
// Written originally by Embedded Adventures
#include "TMP275.h"
#include "Wire.h"
static int measurementDelay; //28ms, 55ms, 110ms, 220ms
void TMP275Class::init() {
//Serial.println("init start");
measurementDelay = 28;
Wire.beginTransmission(TMP275_ADDR);
Wire.write(CONF_REG);
Wire.write(0x00);
Wire.endTransmission();
//Serial.println("init end");
}
uns8 TMP275Class::readConfigRegister() {
uns8 config;
Wire.beginTransmission(TMP275_ADDR);
Wire.write(CONF_REG);
Wire.endTransmission();
Wire.requestFrom(TMP275_ADDR, 1);
if (Wire.available())
config = Wire.read();
return config;
}
void TMP275Class::writeConfigRegister(uns8 config) {
Wire.beginTransmission(TMP275_ADDR);
Wire.write(CONF_REG);
Wire.write(config);
Wire.endTransmission();
}
float TMP275Class::getTemperature() {
uns8 hi, lo;
Wire.beginTransmission(TMP275_ADDR);
Wire.write(TEMP_REG);
Wire.endTransmission();
delay(measurementDelay);
Wire.requestFrom(TMP275_ADDR, 2);
if (Wire.available()) {
hi = Wire.read();
lo = Wire.read();
}
sn16 temp = ((hi << 4) | (lo >> 4));
return float(temp/16.0);
}
void TMP275Class::setResolution(int res) {
uns8 config = readConfigRegister() & 0x9F; //Mask out the res bits
config |= uns8(res-1) << 5;
writeConfigRegister(config);
//Change measurement delay
if (res == 1)
measurementDelay = 28;
else if (res == 2)
measurementDelay = 55;
else if (res == 3)
measurementDelay = 110;
else
measurementDelay = 220;
}
void TMP275Class::enableShutdownMode(bool enable) {
uns8 config = readConfigRegister();
config &= 0xFE; //Mask out LSB
if (enable)
config |= 0x01;
writeConfigRegister(config);
}
void TMP275Class::enableOS() {
enableShutdownMode(false);
delay(measurementDelay);
enableShutdownMode(true);
}
void TMP275Class::enableComparatorMode() {
uns8 config = readConfigRegister();
config &= 0xFD;
writeConfigRegister(config);
}
void TMP275Class::setHighTempThreshold(sn16 threshold) {
Wire.beginTransmission(TMP275_ADDR);
Wire.write(THIGH_REG);
Wire.write(threshold >> 8);
Wire.write(threshold & 0xFF);
Wire.endTransmission();
}
void TMP275Class::setLowTempThreshold(sn16 threshold) {
Wire.beginTransmission(TMP275_ADDR);
Wire.write(TLOW_REG);
Wire.write(threshold >> 8);
Wire.write(threshold & 0xFF);
Wire.endTransmission();
}
float TMP275Class::getHighTempThreshold() {
uns8 hi, lo;
Wire.beginTransmission(TMP275_ADDR);
Wire.write(THIGH_REG);
Wire.endTransmission();
delay(measurementDelay);
Wire.requestFrom(TMP275_ADDR, 2);
if (Wire.available()) {
hi = Wire.read();
lo = Wire.read();
}
sn16 temp = ((hi << 4) | (lo >> 4));
return float(temp/16.0);
}
float TMP275Class::getLowTempThreshold() {
uns8 hi, lo;
Wire.beginTransmission(TMP275_ADDR);
Wire.write(TLOW_REG);
Wire.endTransmission();
delay(measurementDelay);
Wire.requestFrom(TMP275_ADDR, 2);
if (Wire.available()) {
hi = Wire.read();
lo = Wire.read();
}
sn16 temp = ((hi << 4) | (lo >> 4));
return float(temp/16.0);
}
int TMP275Class::getFaults() {
uns8 config = readConfigRegister();
config = (config >> 3) & 0x03;
return config;
}
TMP275Class tmp275;