-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathHTS.cpp
206 lines (160 loc) · 5.38 KB
/
HTS.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
/*
This file is part of the Arduino_HTS221 library.
Copyright (c) 2019 Arduino SA. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <Wire.h>
#include "HTS.h"
#define HTS221_ADDRESS 0x5F
#define HTS221_WHO_AM_I_REG 0x0f
#define HTS221_CTRL1_REG 0x20
#define HTS221_CTRL2_REG 0x21
#define HTS221_CTRL3_REG 0x22 //Dara Ready (b7 0 active high)(open drain, b6 1)(b2, 1 enable data ready)
#define HTS221_STATUS_REG 0x27
#define HTS221_HUMIDITY_OUT_L_REG 0x28
#define HTS221_TEMP_OUT_L_REG 0x2a
#define HTS221_H0_rH_x2_REG 0x30
#define HTS221_H1_rH_x2_REG 0x31
#define HTS221_T0_degC_x8_REG 0x32
#define HTS221_T1_degC_x8_REG 0x33
#define HTS221_T1_T0_MSB_REG 0x35
#define HTS221_H0_T0_OUT_REG 0x36
#define HTS221_H1_T0_OUT_REG 0x3a
#define HTS221_T0_OUT_REG 0x3c
#define HTS221_T1_OUT_REG 0x3e
HTS221Class::HTS221Class(TwoWire& wire) :
_wire(&wire)
{
}
int HTS221Class::begin()
{
_wire->begin();
if (i2cRead(HTS221_WHO_AM_I_REG) != 0xbc) {
end();
return 0;
}
readHTS221Calibration();
// enable HTS221
i2cWrite(HTS221_CTRL1_REG, 0x80);
// Default DREADY configuration
disableDataReady();
setPushPull();
return 1;
}
void HTS221Class::end()
{
// disable HTS221
i2cWrite(HTS221_CTRL1_REG, 0x00);
_wire->end();
}
void HTS221Class::enableDataReady(){
uint8_t data = i2cRead(HTS221_CTRL3_REG) & 0b11111000;
i2cWrite(HTS221_CTRL3_REG, data | 0b1 << 2);
}
void HTS221Class::disableDataReady(){
uint8_t data = i2cRead(HTS221_CTRL3_REG) & 0b11111000;
i2cWrite(HTS221_CTRL3_REG, data | 0b0 << 2);
}
void HTS221Class::setOpenDrain(){
uint8_t data = i2cRead(HTS221_CTRL3_REG) & 0b10111100;
i2cWrite(HTS221_CTRL3_REG, data | 0b1 << 6);
}
void HTS221Class::setPushPull(){
uint8_t data = i2cRead(HTS221_CTRL3_REG) & 0b10111100;
i2cWrite(HTS221_CTRL3_REG, data);
}
void HTS221Class::setActiveHigh(){
uint8_t data = i2cRead(HTS221_CTRL3_REG) & 0b01111111;
i2cWrite(HTS221_CTRL3_REG, data);
}
void HTS221Class::setActiveLow(){
uint8_t data = i2cRead(HTS221_CTRL3_REG) & 0b01111111;
i2cWrite(HTS221_CTRL3_REG, data | 0b1 << 7);
}
float HTS221Class::readTemperature(int units)
{
// Wait for ONE_SHOT bit to be cleared by the hardware
while (i2cRead(HTS221_CTRL2_REG) & 0x01);
// trigger one shot
i2cWrite(HTS221_CTRL2_REG, 0x01);
// wait for completion
while ((i2cRead(HTS221_STATUS_REG) & 0x01) == 0) {
yield();
}
// read value and convert
int16_t tout = i2cRead16(HTS221_TEMP_OUT_L_REG);
float reading = (tout * _hts221TemperatureSlope + _hts221TemperatureZero);
if (units == FAHRENHEIT) { // Fahrenheit = (Celsius * 9 / 5) + 32
return (reading * 9.0 / 5.0) + 32.0;
} else {
return reading;
}
}
float HTS221Class::readHumidity()
{
// Wait for ONE_SHOT bit to be cleared by the hardware
while (i2cRead(HTS221_CTRL2_REG) & 0x01);
// trigger one shot
i2cWrite(HTS221_CTRL2_REG, 0x01);
// wait for completion
while ((i2cRead(HTS221_STATUS_REG) & 0x02) == 0) {
yield();
}
// read value and convert
int16_t hout = i2cRead16(HTS221_HUMIDITY_OUT_L_REG);
return (hout * _hts221HumiditySlope + _hts221HumidityZero);
}
int HTS221Class::i2cRead(uint8_t reg)
{
_wire->beginTransmission(HTS221_ADDRESS);
_wire->write(reg);
if (_wire->endTransmission(false) != 0) {
return -1;
}
if (_wire->requestFrom(HTS221_ADDRESS, 1) != 1) {
return -1;
}
return _wire->read();
}
int HTS221Class::i2cWrite(uint8_t reg, uint8_t val)
{
_wire->beginTransmission(HTS221_ADDRESS);
_wire->write(reg);
_wire->write(val);
if (_wire->endTransmission() != 0) {
return 0;
}
return 1;
}
void HTS221Class::readHTS221Calibration()
{
uint8_t h0rH = i2cRead(HTS221_H0_rH_x2_REG);
uint8_t h1rH = i2cRead(HTS221_H1_rH_x2_REG);
uint16_t t0degC = i2cRead(HTS221_T0_degC_x8_REG) | ((i2cRead(HTS221_T1_T0_MSB_REG) & 0x03) << 8);
uint16_t t1degC = i2cRead(HTS221_T1_degC_x8_REG) | ((i2cRead(HTS221_T1_T0_MSB_REG) & 0x0c) << 6);
int16_t h0t0Out = i2cRead16(HTS221_H0_T0_OUT_REG);
int16_t h1t0Out = i2cRead16(HTS221_H1_T0_OUT_REG);
int16_t t0Out = i2cRead16(HTS221_T0_OUT_REG);
int16_t t1Out = i2cRead16(HTS221_T1_OUT_REG);
// calculate slopes and 0 offset from calibration values,
// for future calculations: value = a * X + b
_hts221HumiditySlope = (h1rH - h0rH) / (2.0 * (h1t0Out - h0t0Out));
_hts221HumidityZero = (h0rH / 2.0) - _hts221HumiditySlope * h0t0Out;
_hts221TemperatureSlope = (t1degC - t0degC) / (8.0 * (t1Out - t0Out));
_hts221TemperatureZero = (t0degC / 8.0) - _hts221TemperatureSlope * t0Out;
}
#ifdef ARDUINO_ARDUINO_NANO33BLE
HTS221Class HTS(Wire1);
#else
HTS221Class HTS(Wire);
#endif