-
Notifications
You must be signed in to change notification settings - Fork 5
/
Sixfab_HDC1080.cpp
122 lines (96 loc) · 2.7 KB
/
Sixfab_HDC1080.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
#include <Wire.h>
#include "Sixfab_HDC1080.h"
Sixfab_HDC1080::Sixfab_HDC1080(){
}
void Sixfab_HDC1080::begin(uint8_t address){
_address = address;
Wire.begin();
setResolution(SIXFAB_HDC1080_RESOLUTION_14BIT, SIXFAB_HDC1080_RESOLUTION_14BIT);
}
void Sixfab_HDC1080::setResolution(HDC1080_MeasurementResolution humidity, HDC1080_MeasurementResolution temperature) {
HDC1080_Registers reg;
reg.HumidityMeasurementResolution = 0;
reg.TemperatureMeasurementResolution = 0;
if (temperature == SIXFAB_HDC1080_RESOLUTION_11BIT)
reg.TemperatureMeasurementResolution = 0x01;
switch (humidity)
{
case SIXFAB_HDC1080_RESOLUTION_8BIT:
reg.HumidityMeasurementResolution = 0x02;
break;
case SIXFAB_HDC1080_RESOLUTION_11BIT:
reg.HumidityMeasurementResolution = 0x01;
break;
default:
break;
}
writeRegister(reg);
}
HDC1080_SerialNumber Sixfab_HDC1080::readSerialNumber() {
HDC1080_SerialNumber sernum;
sernum.serialFirst = readData(HDC1080_SERIAL_ID_FIRST);
sernum.serialMid = readData(HDC1080_SERIAL_ID_MID);
sernum.serialLast = readData(HDC1080_SERIAL_ID_LAST);
return sernum;
}
HDC1080_Registers Sixfab_HDC1080::readRegister() {
HDC1080_Registers reg;
reg.rawData = (readData(HDC1080_CONFIGURATION) >> 8);
return reg;
}
void Sixfab_HDC1080::writeRegister(HDC1080_Registers reg) {
Wire.beginTransmission(_address);
Wire.write(HDC1080_CONFIGURATION);
Wire.write(reg.rawData);
Wire.write(0x00);
Wire.endTransmission();
delay(10);
}
void Sixfab_HDC1080::heatUp(uint8_t seconds) {
HDC1080_Registers reg = readRegister();
reg.Heater = 1;
reg.ModeOfAcquisition = 1;
writeRegister(reg);
uint8_t buf[4];
for (int i = 1; i < (seconds*66); i++) {
Wire.beginTransmission(_address);
Wire.write(0x00);
Wire.endTransmission();
delay(20);
Wire.requestFrom(_address, (uint8_t)4);
Wire.readBytes(buf, (size_t)4);
}
reg.Heater = 0;
reg.ModeOfAcquisition = 0;
writeRegister(reg);
}
double Sixfab_HDC1080::readT() {
return readTemperature();
}
double Sixfab_HDC1080::readTemperature() {
uint16_t rawT = readData(HDC1080_TEMPERATURE);
return (rawT / pow(2, 16)) * 165.0 - 40.0;
}
double Sixfab_HDC1080::readH() {
return readHumidity();
}
double Sixfab_HDC1080::readHumidity() {
uint16_t rawH = readData(HDC1080_HUMIDITY);
return (rawH / pow(2, 16)) * 100.0;
}
uint16_t Sixfab_HDC1080::readManufacturerId() {
return readData(HDC1080_MANUFACTURER_ID);
}
uint16_t Sixfab_HDC1080::readDeviceId() {
return readData(HDC1080_DEVICE_ID);
}
uint16_t Sixfab_HDC1080::readData(uint8_t pointer) {
Wire.beginTransmission(_address);
Wire.write(pointer);
Wire.endTransmission();
delay(9);
Wire.requestFrom(_address, (uint8_t)2);
byte msb = Wire.read();
byte lsb = Wire.read();
return msb << 8 | lsb;
}