-
Notifications
You must be signed in to change notification settings - Fork 3
/
SparkFunMLX90614.cpp
444 lines (391 loc) · 9.5 KB
/
SparkFunMLX90614.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/******************************************************************************
SparkFunMLX90614.cpp
Source file for the SparkFun IR Thermometer Library
Jim Lindblom @ SparkFun Electronics
October 23, 2015
https://github.com/sparkfun/SparkFun_MLX90614_Arduino_Library
This file defines the SMBus hardware interface(s) for the MLX90614 IR thermometer
and abstracts temperature measurments and other features of the MLX90614
Development environment specifics:
Arduino 1.6.5
SparkFun IR Thermometer Evaluation Board - MLX90614
******************************************************************************/
#include <SparkFunMLX90614.h>
IRTherm::IRTherm()
{
// Set initial values for all private member variables
_deviceAddress = 0;
_defaultUnit = TEMP_C;
_rawObject = 0;
_rawAmbient = 0;
_rawObject2 = 0;
_rawMax = 0;
_rawMin = 0;
}
uint8_t IRTherm::begin(uint8_t address)
{
_deviceAddress = address; // Store the address in a private member
Wire.begin(); // Initialize I2C
//! TODO: read a register, return success only if the register
//! produced a known-good value.
return 1; // Return success
}
void IRTherm::setUnit(temperature_units unit)
{
_defaultUnit = unit; // Store the unit into a private member
}
uint8_t IRTherm::read()
{
// read both the object and ambient temperature values
if (readObject() && readAmbient())
{
// If the reads succeeded, return success
return 1;
}
return 0; // Else return fail
}
uint8_t IRTherm::readRange()
{
// Read both minimum and maximum values from EEPROM
if (readMin() && readMax())
{
// If the read succeeded, return success
return 1;
}
return 0; // Else return fail
}
float IRTherm::ambient(void)
{
// Return the calculated ambient temperature
return calcTemperature(_rawAmbient);
}
float IRTherm::object(void)
{
// Return the calculated object temperature
return calcTemperature(_rawObject);
}
float IRTherm::minimum(void)
{
// Return the calculated minimum temperature
return calcTemperature(_rawMin);
}
float IRTherm::maximum(void)
{
// Return the calculated maximum temperature
return calcTemperature(_rawMax);
}
uint8_t IRTherm::readObject()
{
int16_t rawObj;
// Read from the TOBJ1 register, store into the rawObj variable
if (I2CReadWord(MLX90614_REGISTER_TOBJ1, &rawObj))
{
// If the read succeeded
if (rawObj & 0x8000) // If there was a flag error
{
return 0; // Return fail
}
// Store the object temperature into the class variable
_rawObject = rawObj;
return 1;
}
return 0;
}
uint8_t IRTherm::readObject2()
{
int16_t rawObj;
// Read from the TOBJ2 register, store into the rawObj variable
if (I2CReadWord(MLX90614_REGISTER_TOBJ2, &rawObj))
{
// If the read succeeded
if (rawObj & 0x8000) // If there was a flag error
{
return 0; // Return fail
}
// Store the object2 temperature into the class variable
_rawObject2 = rawObj;
return 1;
}
return 0;
}
uint8_t IRTherm::readAmbient()
{
int16_t rawAmb;
// Read from the TA register, store value in rawAmb
if (I2CReadWord(MLX90614_REGISTER_TA, &rawAmb))
{
// If the read succeeds, store the read value
_rawAmbient = rawAmb; // return success
return 1;
}
return 0; // else return fail
}
uint8_t IRTherm::readMax()
{
int16_t toMax;
// Read from the TOMax EEPROM address, store value in toMax
if (I2CReadWord(MLX90614_REGISTER_TOMAX, &toMax))
{
_rawMax = toMax;
return 1;
}
return 0;
}
uint8_t IRTherm::readMin()
{
int16_t toMin;
// Read from the TOMin EEPROM address, store value in toMax
if (I2CReadWord(MLX90614_REGISTER_TOMIN, &toMin))
{
_rawMin = toMin;
return 1;
}
return 0;
}
uint8_t IRTherm::setMax(float maxTemp)
{
// Convert the unit-ed value to a raw ADC value:
int16_t rawMax = calcRawTemp(maxTemp);
// Write that value to the TOMAX EEPROM address:
return writeEEPROM(MLX90614_REGISTER_TOMAX, rawMax);
}
uint8_t IRTherm::setMin(float minTemp)
{
// Convert the unit-ed value to a raw ADC value:
int16_t rawMin = calcRawTemp(minTemp);
// Write that value to the TOMIN EEPROM address:
return writeEEPROM(MLX90614_REGISTER_TOMIN, rawMin);
}
uint8_t IRTherm::setEmissivity(float emis)
{
// Make sure emissivity is between 0.1 and 1.0
if ((emis > 1.0) || (emis < 0.1))
return 0; // Return fail if not
// Calculate the raw 16-bit value:
uint16_t ke = uint16_t(65535.0 * emis);
ke = constrain(ke, 0x2000, 0xFFFF);
// Write that value to the ke register
return writeEEPROM(MLX90614_REGISTER_KE, (int16_t)ke);
}
float IRTherm::readEmissivity(void)
{
int16_t ke;
if (I2CReadWord(MLX90614_REGISTER_KE, &ke))
{
// If we successfully read from the ke register
// calculate the emissivity between 0.1 and 1.0:
return (((float)((uint16_t)ke)) / 65535.0);
}
return 0; // Else return fail
}
uint8_t IRTherm::readAddress(void)
{
int16_t tempAdd;
// Read from the 7-bit I2C address EEPROM storage address:
if (I2CReadWord(MLX90614_REGISTER_ADDRESS, &tempAdd))
{
// If read succeeded, return the address:
return (uint8_t) tempAdd;
}
return 0; // Else return fail
}
uint8_t IRTherm::setAddress(uint8_t newAdd)
{
int16_t tempAdd;
// Make sure the address is within the proper range:
if ((newAdd >= 0x80) || (newAdd == 0x00))
return 0; // Return fail if out of range
// Read from the I2C address address first:
if (I2CReadWord(MLX90614_REGISTER_ADDRESS, &tempAdd))
{
tempAdd &= 0xFF00; // Mask out the address (MSB is junk?)
tempAdd |= newAdd; // Add the new address
// Write the new addres back to EEPROM:
return writeEEPROM(MLX90614_REGISTER_ADDRESS, tempAdd);
}
return 0;
}
uint8_t IRTherm::readID(void)
{
for (int i=0; i<4; i++)
{
int16_t temp = 0;
// Read from all four ID registers, beginning at the first:
if (!I2CReadWord(MLX90614_REGISTER_ID0 + i, &temp))
return 0;
// If the read succeeded, store the ID into the id array:
id[i] = (uint16_t)temp;
}
return 1;
}
uint32_t IRTherm::getIDH(void)
{
// Return the upper 32 bits of the ID
return ((uint32_t)id[3] << 16) | id[2];
}
uint32_t IRTherm::getIDL(void)
{
// Return the lower 32 bits of the ID
return ((uint32_t)id[1] << 16) | id[0];
}
uint8_t IRTherm::sleep(void)
{
// Calculate a crc8 value.
// Bits sent: _deviceAddress (shifted left 1) + 0xFF
uint8_t crc = crc8(0, (_deviceAddress << 1));
crc = crc8(crc, MLX90614_REGISTER_SLEEP);
// Manually send the sleep command:
Wire.beginTransmission(_deviceAddress);
Wire.write(MLX90614_REGISTER_SLEEP);
Wire.write(crc);
Wire.endTransmission(true);
// Set the SCL pin LOW, and SDA pin HIGH (should be pulled up)
pinMode(SCL, OUTPUT);
digitalWrite(SCL, LOW);
pinMode(SDA, INPUT);
}
uint8_t IRTherm::wake(void)
{
// Wake operation from datasheet. (Doesn't seem to be working.)
pinMode(SCL, INPUT); // SCL high
pinMode(SDA, OUTPUT);
digitalWrite(SDA, LOW); // SDA low
delay(50); // delay at least 33ms
pinMode(SDA, INPUT); // SDA high
delay(250);
// PWM to SMBus mode:
pinMode(SCL, OUTPUT);
digitalWrite(SCL, LOW); // SCL low
delay(10); // Delay at least 1.44ms
pinMode(SCL, INPUT); // SCL high
Wire.begin();
}
int16_t IRTherm::calcRawTemp(float calcTemp)
{
int16_t rawTemp; // Value to eventually be returned
if (_defaultUnit == TEMP_RAW)
{
// If unit is set to raw, just return that:
rawTemp = (int16_t) calcTemp;
}
else
{
float tempFloat;
// First convert each temperature to Kelvin:
if (_defaultUnit == TEMP_F)
{
// Convert from farenheit to Kelvin
tempFloat = (calcTemp - 32.0) * 5.0 / 9.0 + 273.15;
}
else if (_defaultUnit == TEMP_C)
{
tempFloat = calcTemp + 273.15;
}
else if (_defaultUnit == TEMP_K)
{
tempFloat = calcTemp;
}
// Then multiply by 0.02 degK / bit
tempFloat *= 50;
rawTemp = (int16_t) tempFloat;
}
return rawTemp;
}
float IRTherm::calcTemperature(int16_t rawTemp)
{
float retTemp;
if (_defaultUnit == TEMP_RAW)
{
retTemp = (float) rawTemp;
}
else
{
retTemp = float(rawTemp) * 0.02;
if (_defaultUnit != TEMP_K)
{
retTemp -= 273.15;
if (_defaultUnit == TEMP_F)
{
retTemp = retTemp * 9.0 / 5.0 + 32;
}
}
}
return retTemp;
}
uint8_t IRTherm::I2CReadWord(byte reg, int16_t * dest)
{
int timeout = I2C_READ_TIMEOUT;
Wire.beginTransmission(_deviceAddress);
Wire.write(reg);
Wire.endTransmission(false); // Send restart
Wire.requestFrom(_deviceAddress, (uint8_t) 3);
while ((Wire.available() < 3) && (timeout-- > 0))
delay(1);
if (timeout <= 0)
return 0;
uint8_t lsb = Wire.read();
uint8_t msb = Wire.read();
uint8_t pec = Wire.read();
uint8_t crc = crc8(0, (_deviceAddress << 1));
crc = crc8(crc, reg);
crc = crc8(crc, (_deviceAddress << 1) + 1);
crc = crc8(crc, lsb);
crc = crc8(crc, msb);
if (crc == pec)
{
*dest = (msb << 8) | lsb;
return 1;
}
else
{
return 0;
}
}
uint8_t IRTherm::writeEEPROM(byte reg, int16_t data)
{
// Clear out EEPROM first:
if (I2CWriteWord(reg, 0) != 0)
return 0; // If the write failed, return 0
delay(5); // Delay tErase
uint8_t i2cRet = I2CWriteWord(reg, data);
delay(5); // Delay tWrite
if (i2cRet == 0)
return 1;
else
return 0;
}
uint8_t IRTherm::I2CWriteWord(byte reg, int16_t data)
{
uint8_t crc;
uint8_t lsb = data & 0x00FF;
uint8_t msb = (data >> 8);
crc = crc8(0, (_deviceAddress << 1));
crc = crc8(crc, reg);
crc = crc8(crc, lsb);
crc = crc8(crc, msb);
Wire.beginTransmission(_deviceAddress);
Wire.write(reg);
Wire.write(lsb);
Wire.write(msb);
Wire.write(crc);
return Wire.endTransmission(true);
}
uint8_t IRTherm::crc8 (uint8_t inCrc, uint8_t inData)
{
uint8_t i;
uint8_t data;
data = inCrc ^ inData;
for ( i = 0; i < 8; i++ )
{
if (( data & 0x80 ) != 0 )
{
data <<= 1;
data ^= 0x07;
}
else
{
data <<= 1;
}
}
return data;
}