forked from tinkeringtech/HDC2080_breakout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HDC2080.cpp
586 lines (442 loc) · 12.4 KB
/
HDC2080.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/*
HDC2080.cpp
HDC2010.cpp originally created by: Brandon Fisher, August 1st 2017
This code is release AS-IS into the public domain, no guarantee or warranty is given.
Description: This library facilitates communication with, and configuration of,
the HDC2080 Temperature and Humidity Sensor. It makes extensive use of the
Wire.H library, and should be useable with both Arduino and Energia.
*/
#include <HDC2080.h>
#include <Wire.h>
//Define Register Map
#define TEMP_LOW 0x00
#define TEMP_HIGH 0x01
#define HUMID_LOW 0x02
#define HUMID_HIGH 0x03
#define INTERRUPT_DRDY 0x04
#define TEMP_MAX 0x05
#define HUMID_MAX 0x06
#define INTERRUPT_CONFIG 0x07
#define TEMP_OFFSET_ADJUST 0x08
#define HUM_OFFSET_ADJUST 0x09
#define TEMP_THR_L 0x0A
#define TEMP_THR_H 0x0B
#define HUMID_THR_L 0x0C
#define HUMID_THR_H 0x0D
#define CONFIG 0x0E
#define MEASUREMENT_CONFIG 0x0F
#define MID_L 0xFC
#define MID_H 0xFD
#define DEVICE_ID_L 0xFE
#define DEVICE_ID_H 0xFF
HDC2080::HDC2080(uint8_t addr)
{
_addr = addr;
}
void HDC2080::begin(void)
{
Wire.begin();
}
float HDC2080::readTemp(void)
{
uint8_t byte[2];
uint16_t temp;
byte[0] = readReg(TEMP_LOW);
byte[1] = readReg(TEMP_HIGH);
temp = (unsigned int)byte[1] << 8 | byte[0];
return (float)(temp) * 165 / 65536 - 40;
}
float HDC2080::readHumidity(void)
{
uint8_t byte[2];
uint16_t humidity;
byte[0] = readReg(HUMID_LOW);
byte[1] = readReg(HUMID_HIGH);
humidity = (unsigned int)byte[1] << 8 | byte[0];
return (float)(humidity)/( 65536 )* 100;
}
void HDC2080::enableHeater(void)
{
uint8_t configContents; //Stores current contents of config register
configContents = readReg(CONFIG);
//set bit 3 to 1 to enable heater
configContents = (configContents | 0x08);
writeReg(CONFIG, configContents);
}
void HDC2080::disableHeater(void)
{
uint8_t configContents; //Stores current contents of config register
configContents = readReg(CONFIG);
//set bit 3 to 0 to disable heater (all other bits 1)
configContents = (configContents & 0xF7);
writeReg(CONFIG, configContents);
}
void HDC2080::openReg(uint8_t reg)
{
Wire.beginTransmission(_addr); // Connect to HDC2080
Wire.write(reg); // point to specified register
Wire.endTransmission(); // Relinquish bus control
}
uint8_t HDC2080::readReg(uint8_t reg)
{
openReg(reg);
uint8_t reading; // holds byte of read data
Wire.requestFrom(_addr, 1); // Request 1 byte from open register
Wire.endTransmission(); // Relinquish bus control
if (1 <= Wire.available())
{
reading = (Wire.read()); // Read byte
}
return reading;
}
void HDC2080::writeReg(uint8_t reg, uint8_t data)
{
Wire.beginTransmission(_addr); // Open Device
Wire.write(reg); // Point to register
Wire.write(data); // Write data to register
Wire.endTransmission(); // Relinquish bus control
}
void HDC2080::setLowTemp(float temp)
{
uint8_t temp_thresh_low;
// Verify user is not trying to set value outside bounds
if (temp < -40)
{
temp = -40;
}
else if (temp > 125)
{
temp = 125;
}
// Calculate value to load into register
temp_thresh_low = (uint8_t)(256 * (temp + 40)/165);
writeReg(TEMP_THR_L, temp_thresh_low);
}
void HDC2080::setHighTemp(float temp)
{
uint8_t temp_thresh_high;
// Verify user is not trying to set value outside bounds
if (temp < -40)
{
temp = -40;
}
else if (temp > 125)
{
temp = 125;
}
// Calculate value to load into register
temp_thresh_high = (uint8_t)(256 * (temp + 40)/165);
writeReg(TEMP_THR_H, temp_thresh_high);
}
void HDC2080::setHighHumidity(float humid)
{
uint8_t humid_thresh;
// Verify user is not trying to set value outside bounds
if (humid < 0)
{
humid = 0;
}
else if (humid > 100)
{
humid = 100;
}
// Calculate value to load into register
humid_thresh = (uint8_t)(256 * (humid)/100);
writeReg(HUMID_THR_H, humid_thresh);
}
void HDC2080::setLowHumidity(float humid)
{
uint8_t humid_thresh;
// Verify user is not trying to set value outside bounds
if (humid < 0)
{
humid = 0;
}
else if (humid > 100)
{
humid = 100;
}
// Calculate value to load into register
humid_thresh = (uint8_t)(256 * (humid)/100);
writeReg(HUMID_THR_L, humid_thresh);
}
// Return humidity from the low threshold register
float HDC2080::readLowHumidityThreshold(void)
{
uint8_t regContents;
regContents = readReg(HUMID_THR_L);
return (float)regContents * 100/256;
}
// Return humidity from the high threshold register
float HDC2080::readHighHumidityThreshold(void)
{
uint8_t regContents;
regContents = readReg(HUMID_THR_H);
return (float)regContents * 100/256;
}
// Return temperature from the low threshold register
float HDC2080::readLowTempThreshold(void)
{
uint8_t regContents;
regContents = readReg(TEMP_THR_L);
return (float)regContents * 165/256 - 40;
}
// Return temperature from the high threshold register
float HDC2080::readHighTempThreshold(void)
{
uint8_t regContents;
regContents = readReg(TEMP_THR_H);
return (float)regContents * 165/256 - 40;
}
/* Upper two bits of the MEASUREMENT_CONFIG register controls
the temperature resolution*/
void HDC2080::setTempRes(int resolution)
{
uint8_t configContents;
configContents = readReg(MEASUREMENT_CONFIG);
switch(resolution)
{
case FOURTEEN_BIT:
configContents = (configContents & 0x3F);
break;
case ELEVEN_BIT:
configContents = (configContents & 0x7F);
configContents = (configContents | 0x40);
break;
case NINE_BIT:
configContents = (configContents & 0xBF);
configContents = (configContents | 0x80);
break;
default:
configContents = (configContents & 0x3F);
}
writeReg(MEASUREMENT_CONFIG, configContents);
}
/* Bits 5 and 6 of the MEASUREMENT_CONFIG register controls
the humidity resolution*/
void HDC2080::setHumidRes(int resolution)
{
uint8_t configContents;
configContents = readReg(MEASUREMENT_CONFIG);
switch(resolution)
{
case FOURTEEN_BIT:
configContents = (configContents & 0xCF);
break;
case ELEVEN_BIT:
configContents = (configContents & 0xDF);
configContents = (configContents | 0x10);
break;
case NINE_BIT:
configContents = (configContents & 0xEF);
configContents = (configContents | 0x20);
break;
default:
configContents = (configContents & 0xCF);
}
writeReg(MEASUREMENT_CONFIG, configContents);
}
/* Bits 2 and 1 of the MEASUREMENT_CONFIG register controls
the measurement mode */
void HDC2080::setMeasurementMode(int mode)
{
uint8_t configContents;
configContents = readReg(MEASUREMENT_CONFIG);
switch(mode)
{
case TEMP_AND_HUMID:
configContents = (configContents & 0xF9);
break;
case TEMP_ONLY:
configContents = (configContents & 0xFC);
configContents = (configContents | 0x02);
break;
case HUMID_ONLY:
configContents = (configContents & 0xFD);
configContents = (configContents | 0x04);
break;
default:
configContents = (configContents & 0xF9);
}
writeReg(MEASUREMENT_CONFIG, configContents);
}
/* Bit 0 of the MEASUREMENT_CONFIG register can be used
to trigger measurements */
void HDC2080::triggerMeasurement(void)
{
uint8_t configContents;
configContents = readReg(MEASUREMENT_CONFIG);
configContents = (configContents | 0x01);
writeReg(MEASUREMENT_CONFIG, configContents);
}
/* Bit 7 of the CONFIG register can be used to trigger a
soft reset */
void HDC2080::reset(void)
{
uint8_t configContents;
configContents = readReg(CONFIG);
configContents = (configContents | 0x80);
writeReg(CONFIG, configContents);
delay(50);
}
/* Bit 2 of the CONFIG register can be used to enable/disable
the interrupt pin */
void HDC2080::enableInterrupt(void)
{
uint8_t configContents;
configContents = readReg(CONFIG);
configContents = (configContents | 0x04);
writeReg(CONFIG, configContents);
}
/* Bit 2 of the CONFIG register can be used to enable/disable
the interrupt pin */
void HDC2080::disableInterrupt(void)
{
uint8_t configContents;
configContents = readReg(CONFIG);
configContents = (configContents & 0xFB);
writeReg(CONFIG, configContents);
}
/* Bits 6-4 of the CONFIG register controls the measurement
rate */
void HDC2080::setRate(int rate)
{
uint8_t configContents;
configContents = readReg(CONFIG);
switch(rate)
{
case MANUAL:
configContents = (configContents & 0x8F);
break;
case TWO_MINS:
configContents = (configContents & 0x9F);
configContents = (configContents | 0x10);
break;
case ONE_MINS:
configContents = (configContents & 0xAF);
configContents = (configContents | 0x20);
break;
case TEN_SECONDS:
configContents = (configContents & 0xBF);
configContents = (configContents | 0x30);
break;
case FIVE_SECONDS:
configContents = (configContents & 0xCF);
configContents = (configContents | 0x40);
break;
case ONE_HZ:
configContents = (configContents & 0xDF);
configContents = (configContents | 0x50);
break;
case TWO_HZ:
configContents = (configContents & 0xEF);
configContents = (configContents | 0x60);
break;
case FIVE_HZ:
configContents = (configContents | 0x70);
break;
default:
configContents = (configContents & 0x8F);
}
writeReg(CONFIG, configContents);
}
/* Bit 1 of the CONFIG register can be used to control the
the interrupt pins polarity */
void HDC2080::setInterruptPolarity(int polarity)
{
uint8_t configContents;
configContents = readReg(CONFIG);
switch(polarity)
{
case ACTIVE_LOW:
configContents = (configContents & 0xFD);
break;
case ACTIVE_HIGH:
configContents = (configContents | 0x02);
break;
default:
configContents = (configContents & 0xFD);
}
writeReg(CONFIG, configContents);
}
/* Bit 0 of the CONFIG register can be used to control the
the interrupt pin's mode */
void HDC2080::setInterruptMode(int mode)
{
uint8_t configContents;
configContents = readReg(CONFIG);
switch(mode)
{
case LEVEL_MODE:
configContents = (configContents & 0xFE);
break;
case COMPARATOR_MODE:
configContents = (configContents | 0x01);
break;
default:
configContents = (configContents & 0xFE);
}
writeReg(CONFIG, configContents);
}
uint8_t HDC2080::readInterruptStatus(void)
{
uint8_t regContents;
regContents = readReg(INTERRUPT_DRDY);
return regContents;
}
// Clears the maximum temperature register
void HDC2080::clearMaxTemp(void)
{
writeReg(TEMP_MAX, 0x00);
}
// Clears the maximum humidity register
void HDC2080::clearMaxHumidity(void)
{
writeReg(HUMID_MAX, 0x00);
}
// Reads the maximum temperature register
float HDC2080::readMaxTemp(void)
{
uint8_t regContents;
regContents = readReg(TEMP_MAX);
return (float)regContents * 165/256 - 40;
}
// Reads the maximum humidity register
float HDC2080::readMaxHumidity(void)
{
uint8_t regContents;
regContents = readReg(HUMID_MAX);
return (float)regContents /256 * 100;
}
// Enables the interrupt pin for comfort zone operation
void HDC2080::enableThresholdInterrupt(void)
{
uint8_t regContents;
regContents = readReg(INTERRUPT_CONFIG);
regContents = (regContents | 0x78);
writeReg(INTERRUPT_CONFIG, regContents);
}
// Disables the interrupt pin for comfort zone operation
void HDC2080::disableThresholdInterrupt(void)
{
uint8_t regContents;
regContents = readReg(INTERRUPT_CONFIG);
regContents = (regContents & 0x87);
writeReg(INTERRUPT_CONFIG, regContents);
}
// enables the interrupt pin for DRDY operation
void HDC2080::enableDRDYInterrupt(void)
{
uint8_t regContents;
regContents = readReg(INTERRUPT_CONFIG);
regContents = (regContents | 0x80);
writeReg(INTERRUPT_CONFIG, regContents);
}
// disables the interrupt pin for DRDY operation
void HDC2080::disableDRDYInterrupt(void)
{
uint8_t regContents;
regContents = readReg(INTERRUPT_CONFIG);
regContents = (regContents & 0x7F);
writeReg(INTERRUPT_CONFIG, regContents);
}