-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhts221.c
executable file
·198 lines (160 loc) · 6.34 KB
/
hts221.c
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
/** @brief Modified from https://github.com/davebm1/c-sense-hat
* @file hts221.c
* @since 2024-03-01
* C code to read humidity and temperature from the
* Raspberry Pi Sense HAT add-on board (HTS221 sensor)
*/
#include "hts221.h"
/*compile with gcc hts221.c -li2c, run with ./a.out
int main(void)
{
// Output
printf("Temp (from hts221) = %.1f°C\n", ShGetTemperatureAlt()); //From humidity sensor
printf("Humidity (from hts221) = %.0f%% rH\n", ShGetHumidity());
return (0);
}*/
double ShGetTemperatureAlt() //From humidity sensor
{
int fd = 0;
uint8_t status = 0;
/* open i2c comms */
if ((fd = open(DEV_PATH, O_RDWR)) < 0) {
perror("Unable to open i2c device");
exit(1);
}
/* configure i2c slave */
if (ioctl(fd, I2C_SLAVE, HTS221_DEV_ID) < 0) {
perror("Unable to configure i2c slave device");
close(fd);
exit(1);
}
/* check we are who we should be */
if (i2c_smbus_read_byte_data(fd, WHO_AM_I) != 0xBC) {
printf("%s\n", "who_am_i error");
close(fd);
exit(1);
}
/* Power down the device (clean start) */
i2c_smbus_write_byte_data(fd, CTRL_REG1, 0x00);
/* Turn on the humidity sensor analog front end in single shot mode */
i2c_smbus_write_byte_data(fd, CTRL_REG1, 0x84);
/* Run one-shot measurement (temperature and humidity). The set bit will be reset by the
* sensor itself after execution (self-clearing bit) */
i2c_smbus_write_byte_data(fd, CTRL_REG2, 0x01);
/* Wait until the measurement is completed */
do {
usleep(25*1000); /* 25 milliseconds */
status = i2c_smbus_read_byte_data(fd, CTRL_REG2);
} while (status != 0);
/* Read calibration temperature LSB (ADC) data
* (temperature calibration x-data for two points)
*/
uint8_t t0_out_l = i2c_smbus_read_byte_data(fd, T0_OUT_L);
uint8_t t0_out_h = i2c_smbus_read_byte_data(fd, T0_OUT_H);
uint8_t t1_out_l = i2c_smbus_read_byte_data(fd, T1_OUT_L);
uint8_t t1_out_h = i2c_smbus_read_byte_data(fd, T1_OUT_H);
/* Read calibration temperature (°C) data
* (temperature calibration y-data for two points)
*/
uint8_t t0_degC_x8 = i2c_smbus_read_byte_data(fd, T0_degC_x8);
uint8_t t1_degC_x8 = i2c_smbus_read_byte_data(fd, T1_degC_x8);
uint8_t t1_t0_msb = i2c_smbus_read_byte_data(fd, T1_T0_MSB);
/* make 16 bit values (bit shift)
* (temperature calibration x-values)
*/
int16_t T0_OUT = t0_out_h << 8 | t0_out_l;
int16_t T1_OUT = t1_out_h << 8 | t1_out_l;
/* make 16 and 10 bit values (bit mask and bit shift) */
uint16_t T0_DegC_x8 = (t1_t0_msb & 3) << 8 | t0_degC_x8;
uint16_t T1_DegC_x8 = ((t1_t0_msb & 12) >> 2) << 8 | t1_degC_x8;
/* Calculate calibration values
* (temperature calibration y-values)
*/
double T0_DegC = T0_DegC_x8 / 8.0;
double T1_DegC = T1_DegC_x8 / 8.0;
/* Read the ambient temperature measurement (2 bytes to read) */
uint8_t t_out_l = i2c_smbus_read_byte_data(fd, TMP_OUT_L);
uint8_t t_out_h = i2c_smbus_read_byte_data(fd, TMP_OUT_H);
/* make 16 bit value */
int16_t T_OUT = t_out_h << 8 | t_out_l;
/* Calculate ambient temperature */
double T_DegC=((float)T1_DegC_x8/8-(float)T0_DegC_x8/8)/(T1_OUT-T0_OUT)*(T_OUT-T0_OUT)+(float)T0_DegC_x8/8;
/* Power down the device */
i2c_smbus_write_byte_data(fd, CTRL_REG1, 0x00);
close(fd);
/* Output */
return T_DegC; //"Temp (from hts221) = %.1f°C\n"
}
double ShGetHumidity()
{
int fd = 0;
uint8_t status = 0;
/* open i2c comms */
if ((fd = open(DEV_PATH, O_RDWR)) < 0) {
perror("Unable to open i2c device");
exit(1);
}
/* configure i2c slave */
if (ioctl(fd, I2C_SLAVE, HTS221_DEV_ID) < 0) {
perror("Unable to configure i2c slave device");
close(fd);
exit(1);
}
/* check we are who we should be */
if (i2c_smbus_read_byte_data(fd, WHO_AM_I) != 0xBC) {
printf("%s\n", "who_am_i error");
close(fd);
exit(1);
}
/* Power down the device (clean start) */
i2c_smbus_write_byte_data(fd, CTRL_REG1, 0x00);
/* Turn on the humidity sensor analog front end in single shot mode */
i2c_smbus_write_byte_data(fd, CTRL_REG1, 0x84);
/* Run one-shot measurement (temperature and humidity). The set bit will be reset by the
* sensor itself after execution (self-clearing bit) */
i2c_smbus_write_byte_data(fd, CTRL_REG2, 0x01);
/* Wait until the measurement is completed */
do {
usleep(25*1000); /* 25 milliseconds */
status = i2c_smbus_read_byte_data(fd, CTRL_REG2);
} while (status != 0);
/* Read calibration relative humidity LSB (ADC) data
* (humidity calibration x-data for two points)
*/
uint8_t h0_out_l = i2c_smbus_read_byte_data(fd, H0_T0_OUT_L);
uint8_t h0_out_h = i2c_smbus_read_byte_data(fd, H0_T0_OUT_H);
uint8_t h1_out_l = i2c_smbus_read_byte_data(fd, H1_T0_OUT_L);
uint8_t h1_out_h = i2c_smbus_read_byte_data(fd, H1_T0_OUT_H);
/* Read relative humidity (% rH) data
* (humidity calibration y-data for two points)
*/
uint8_t h0_rh_x2 = i2c_smbus_read_byte_data(fd, H0_rH_x2);
uint8_t h1_rh_x2 = i2c_smbus_read_byte_data(fd, H1_rH_x2);
/* make 16 bit values (bit shift)
* (humidity calibration x-values)
*/
int16_t H0_T0_OUT = h0_out_h << 8 | h0_out_l;
int16_t H1_T0_OUT = h1_out_h << 8 | h1_out_l;
/* Humidity calibration values
* (humidity calibration y-values)
*/
double H0_rH = h0_rh_x2 / 2.0;
double H1_rH = h1_rh_x2 / 2.0;
/* Solve the linear equasions 'y = mx + c' to give the
* calibration straight line graphs for humidity
*/
double h_gradient_m = (H1_rH - H0_rH) / (H1_T0_OUT - H0_T0_OUT);
double h_intercept_c = H1_rH - (h_gradient_m * H1_T0_OUT);
/* Read the ambient humidity measurement (2 bytes to read) */
uint8_t h_t_out_l = i2c_smbus_read_byte_data(fd, H_T_OUT_L);
uint8_t h_t_out_h = i2c_smbus_read_byte_data(fd, H_T_OUT_H);
/* make 16 bit value */
int16_t H_T_OUT = h_t_out_h << 8 | h_t_out_l;
/* Calculate ambient humidity */
double H_rH = (h_gradient_m * H_T_OUT) + h_intercept_c;
/* Power down the device */
i2c_smbus_write_byte_data(fd, CTRL_REG1, 0x00);
close(fd);
/* Output */
return H_rH; //"Humidity (from hts221) = %.0f%% rH\n"
}