-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTU20D.cpp
328 lines (283 loc) · 9.38 KB
/
HTU20D.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
/**************************************************************************/
/*
Distributed with a free-will license.
Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
HTU20D
This code is designed to work with the HTU20D_I2CS I2C Mini Module available from ControlEverything.com.
https://www.controleverything.com/content/Temperature?sku=HTU20D_I2CS#tabs-0-product_tabset-2
*/
/**************************************************************************/
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Wire.h>
#include "HTU20D.h"
/**************************************************************************/
/*
Abstract away platform differences in Arduino wire library
*/
/**************************************************************************/
static uint8_t i2cread(void)
{
#if ARDUINO >= 100
return Wire.read();
#else
return Wire.receive();
#endif
}
/**************************************************************************/
/*
Abstract away platform differences in Arduino wire library
*/
/**************************************************************************/
static void i2cwrite(uint8_t x)
{
#if ARDUINO >= 100
Wire.write((uint8_t)x);
#else
Wire.send(x);
#endif
}
/**************************************************************************/
/*
Writes 8-bits to the destination Register
*/
/**************************************************************************/
static void writeRegister8(uint8_t i2cAddress, uint8_t reg)
{
Wire.beginTransmission(i2cAddress);
i2cwrite((uint8_t)reg);
Wire.endTransmission();
}
/**************************************************************************/
/*
Writes 8-bits to the specified destination Register
*/
/**************************************************************************/
static void writeRegister(uint8_t i2cAddress, uint8_t reg, uint8_t value)
{
Wire.beginTransmission(i2cAddress);
i2cwrite((uint8_t)reg);
i2cwrite((uint8_t)value);
Wire.endTransmission();
}
/**************************************************************************/
/*
Reads 16-bits from the specified destination Register
*/
/**************************************************************************/
static uint16_t readRegister(uint8_t i2cAddress)
{
Wire.beginTransmission(i2cAddress);
Wire.endTransmission();
Wire.requestFrom(i2cAddress, (uint8_t)2);
return (uint16_t)((i2cread() << 8) | i2cread());
}
/**************************************************************************/
/*
Instantiates a new HTU20D class with appropriate properties
*/
/**************************************************************************/
void HTU20D::getAddr_HTU20D(uint8_t i2cAddress)
{
htu_i2cAddress = i2cAddress;
htu_conversionDelay = HTU20D_CONVERSIONDELAY;
}
/**************************************************************************/
/*
Sets up the Hardware
*/
/**************************************************************************/
bool HTU20D::begin()
{
Wire.begin();
// Soft Reset up the Sensor for Temperature and Humidity Measurement
softReset();
return true;
}
/**************************************************************************/
/*
Resets up the Hardware
*/
/**************************************************************************/
void HTU20D::softReset(void)
{
// Resets the HTU20D with the RESET command
writeRegister8(htu_i2cAddress, HTU20D_CMD_SOFT_RESET);
}
/**************************************************************************/
/*
Sets the Temperature Measurement Mode
*/
/**************************************************************************/
void HTU20D::setTempMode(htuTempMode_t tempmode)
{
htu_tempmode = tempmode;
}
/**************************************************************************/
/*
Gets the Temperature Measurement Mode
*/
/**************************************************************************/
htuTempMode_t HTU20D::getTempMode()
{
return htu_tempmode;
}
/**************************************************************************/
/*
Sets the Humidity Measurement Mode
*/
/**************************************************************************/
void HTU20D::setHumidityMode(htuHumidityMode_t humiditymode)
{
htu_humiditymode = humiditymode;
}
/**************************************************************************/
/*
Gets the Humidity Measurement Mode
*/
/**************************************************************************/
htuHumidityMode_t HTU20D::getHumidityMode()
{
return htu_humiditymode;
}
/**************************************************************************/
/*
Sets the Measurement Resolution
*/
/**************************************************************************/
void HTU20D::setResolution(htuResolution_t resolution)
{
htu_resolution = resolution;
}
/**************************************************************************/
/*
Gets the Measurement Resolution
*/
/**************************************************************************/
htuResolution_t HTU20D::getResolution()
{
return htu_resolution;
}
/**************************************************************************/
/*
Sets the VDD Status
*/
/**************************************************************************/
void HTU20D::setVoltage(htuVoltage_t voltage)
{
htu_voltage = voltage;
}
/**************************************************************************/
/*
Gets the VDD Status
*/
/**************************************************************************/
htuVoltage_t HTU20D::getVoltage()
{
return htu_voltage;
}
/**************************************************************************/
/*
Sets the Heater Status
*/
/**************************************************************************/
void HTU20D::setHeaterStatus(htuHeaterStatus_t heaterstatus)
{
htu_heaterstatus = heaterstatus;
}
/**************************************************************************/
/*
Gets the Heater Status
*/
/**************************************************************************/
htuHeaterStatus_t HTU20D::getHeaterStatus()
{
return htu_heaterstatus;
}
/**************************************************************************/
/*
Sets the OTP Reload
*/
/**************************************************************************/
void HTU20D::setOTPStatus(htuOTPStatus_t otpstatus)
{
htu_otpstatus = otpstatus;
}
/**************************************************************************/
/*
Gets the OTP Reload
*/
/**************************************************************************/
htuOTPStatus_t HTU20D::getOTPStatus()
{
return htu_otpstatus;
}
/**************************************************************************/
/*
Reads the Results by Sending Temperature Command
Reads Latest Temperature Value
Measuring the 16-bit Temperature Register
*/
/**************************************************************************/
float HTU20D::Measure_Temperature()
{
// Set Measurement Resolution
uint8_t tempControl = htu_resolution;
// Set VDD (Voltage Status)
tempControl |= htu_voltage;
// Set On-Chip Heater Status
tempControl |= htu_heaterstatus;
// Set OTP Reload
tempControl |= htu_otpstatus;
// Sending Command to the User Register 1 for HTU20D
writeRegister(htu_i2cAddress, HTU20D_CMD_WRITE_USER, tempControl);
// Wait for the conversion to complete
delay(htu_conversionDelay);
// Set the Temperature Measurement Mode
uint8_t tempMode = htu_tempmode;
// Sending Command for Temperature Measurement Mode
uint16_t rawTemperature;
float temperature;
writeRegister8(htu_i2cAddress, tempMode);
delay(htu_conversionDelay);
// Read the Conversion Results
// 16-bit unsigned results for the HTU20D
rawTemperature = readRegister(htu_i2cAddress);
temperature = (172.72 * rawTemperature)/65536.0 - 46.85;
return (float)temperature;
}
/**************************************************************************/
/*
Reads the Results by Sending Humidity Command
Reads Latest Humidity Value
Measuring the 16-bit Humidity Register
*/
/**************************************************************************/
float HTU20D::Measure_Humidity()
{
// Set Measurement Resolution
uint8_t tempControl = htu_resolution;
// Set VDD (Voltage Status)
tempControl |= htu_voltage;
// Set On-Chip Heater Status
tempControl |= htu_heaterstatus;
// Sending command to the User Register 1 for HTU20D
writeRegister(htu_i2cAddress, HTU20D_CMD_WRITE_USER, tempControl);
// Wait for the conversion to complete
delay(htu_conversionDelay);
// Set the Humidity Measurement Mode
uint8_t humidityMode = htu_humiditymode;
// Sending command for Humidity Measurement Mode
uint16_t rawHumidity;
float humidity;
writeRegister8(htu_i2cAddress, humidityMode);
delay(htu_conversionDelay);
// Read the conversion results
// 16-bit unsigned results for the HTU20D
rawHumidity = readRegister(htu_i2cAddress);
humidity = (125.0 * rawHumidity)/65536.0 - 6;
return (float)humidity;
}