-
Notifications
You must be signed in to change notification settings - Fork 2
/
MAX6626.cpp
262 lines (224 loc) · 4.89 KB
/
MAX6626.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
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
/*
@file MAX6266.cpp
@author Sandeepan Sengupta, Tamojit Saha
@website https://www.sandeepan.info,
https://www.tamojitsaha.info
@Library https://github.com/TamojitSaha/MAX6626
@license CC-BY-SA 4.0
I2C Driver for Maxim's MAX6626 12Bit Temperature Sensor
Version 1.0.1 -First Patch for v1.0
*/
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#if defined (__AVR_ATtiny85__) ||defined (__AVR_ATtiny45__)
#include "TinyWireM.h"
#else
#include <Wire.h>
#endif
#include "MAX6626.h"
MAX6626::MAX6626() {
}
boolean MAX6626::begin(address addr)
{
Wire.begin();
i2caddr = addr;
conf_register = readConfig();
Wire.beginTransmission(i2caddr);
uint8_t err = Wire.endTransmission();
if (err != 0)
{
return false;
}
else
{
return true;
}
}
float MAX6626::readTemp(void)
{
uint16_t _temp = read16(TEMP_REG);
float temperature = rawToTemperature(_temp);
/*if (round(temperature) < -124.0 || round(temperature) > 124.0)
{
shutdown();
return 0;
}*/
return temperature;
}
void MAX6626::sleep() {
shutdown_wake(1);
}
void MAX6626::wake() {
shutdown_wake(false);
delay(wakeDelay);
}
void MAX6626::setWakeDelay(uint32_t wkDelay) {
wakeDelay = wkDelay;
}
void MAX6626::setInterruptMode(bool sw)
{
volatile uint8_t reg = (uint8_t)readConfig();
if (sw == true)
{
reg |= CONF_REG_COMP_INT;
conf_register = reg;
write8(CONF_REG, conf_register);
}
if (sw == false)
{
reg &= ~CONF_REG_COMP_INT;
conf_register = reg;
write8(CONF_REG, conf_register);
}
}
void MAX6626::setOTpolarity(bool sw)
{
volatile uint8_t reg = (uint8_t)readConfig();
if (sw == true)
{
reg |= CONF_REG_OT_POLARITY;
conf_register = reg;
write8(CONF_REG, conf_register);
}
if (sw == false)
{
reg &= ~CONF_REG_OT_POLARITY;
conf_register = reg;
write8(CONF_REG, conf_register);
}
}
void MAX6626::setFaultQueueDepth(depth _depth)
{
volatile uint8_t reg = (uint8_t)readConfig();
switch (_depth)
{
case 0:
reg &= FQ_BITS_CLEAR_MASK;
conf_register = reg;
write8(CONF_REG, conf_register);
break;
case 1:
reg &= FQ_BITS_CLEAR_MASK;
reg |= FAULT_DEPTH_1;
conf_register = reg;
write8(CONF_REG, conf_register);
break;
case 2:
reg &= FQ_BITS_CLEAR_MASK;
reg |= FAULT_DEPTH_2;
conf_register = reg;
write8(CONF_REG, conf_register);
break;
case 3:
reg &= FQ_BITS_CLEAR_MASK;
reg |= FAULT_DEPTH_3;
conf_register = reg;
write8(CONF_REG, conf_register);
break;
}
}
void MAX6626::setTHigh(int8_t t_high)
{
write16(T_HIGH_REG, temperatureToRaw(t_high));
}
void MAX6626::setTLow(int8_t t_low)
{
write16(T_LOW_REG, temperatureToRaw(t_low));
}
uint8_t MAX6626::readConfig()
{
Wire.beginTransmission(i2caddr);
Wire.write(CONF_REG);
Wire.endTransmission();
Wire.requestFrom(i2caddr, 1);
uint8_t config_reg = Wire.read();
Wire.endTransmission();
return config_reg;
}
float MAX6626::readTHigh()
{
uint16_t _th = read16(T_HIGH_REG);
float t_high = rawToTemperature(_th);
return t_high;
}
float MAX6626::readTLow()
{
uint16_t _tl = read16(T_LOW_REG);
float t_low = rawToTemperature(_tl);
return t_low;
}
void MAX6626::shutdown_wake(uint8_t sw)
{
uint8_t conf_shutdown ;
if (sw == 1)
{
conf_shutdown = conf_register | CONF_REG_SHUTDOWN;
write8(CONF_REG, conf_shutdown);
}
if (sw == 0)
{
conf_shutdown = conf_register & ~CONF_REG_SHUTDOWN;
write8(CONF_REG, conf_shutdown);
}
}
uint16_t MAX6626::read16(uint8_t reg)
{
uint16_t val;
Wire.beginTransmission(i2caddr);
Wire.write(uint8_t(reg));
Wire.endTransmission();
Wire.requestFrom(i2caddr, uint8_t(2));
val = Wire.read();
val <<= 8;
val |= Wire.read();
Wire.endTransmission();
return val;
}
void MAX6626::write16(uint8_t reg, uint16_t val)
{
Wire.beginTransmission(i2caddr);
Wire.write(uint8_t(reg));
Wire.write(val >> 8);
Wire.write(val & 0xff);
Wire.endTransmission();
}
void MAX6626:: write8(uint8_t reg, uint8_t val)
{
Wire.beginTransmission(i2caddr);
Wire.write(reg);
Wire.write(val);
Wire.endTransmission();
}
float MAX6626:: rawToTemperature(uint16_t rawValue)
{
float actual_temperature = rawValue / 256.0 ;
if (rawValue & 0x8000)
{
actual_temperature -= 256.0;
}
return actual_temperature;
}
uint16_t MAX6626:: temperatureToRaw(int8_t temp)
{
uint16_t val;
if (temp < 0)
{
if (temp < -125)
{
temp = -125;
}
val = temp + 256;
}
else {
if (temp > 125)
{
temp = 125;
}
val = temp;
}
val <<= 8;
return val;
}