-
Notifications
You must be signed in to change notification settings - Fork 0
/
DHT20.c
280 lines (232 loc) · 5.31 KB
/
DHT20.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
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
/*
Aosong AHT20/DHT20 sensor library for Raspberry Pi Pico.
*/
#include <string.h>
#include "DHT20.h"
uint8_t readStatus(DHT20 *sens)
{
uint8_t ret;
i2c_read_blocking(I2C_INST, DHT20_ADDRESS, &ret, 1, false);
return ret;
}
static bool needsReset(DHT20 *sens)
{
return ((readStatus(sens) & 0x18) != 0x18);
}
int DHT20_init(DHT20 *sens)
{
sens->temperature = 0;
sens->humidity = 0;
sens->humOffset = 0;
sens->tempOffset = 0;
sens->status = 0;
sens->lastRequest = 0;
sens->lastRead = 0;
sens->updateInterval=1500;
sens->crc = 0;
memset(sens->bytes, 0, 7);
// Ensure 100ms wait after powerup as per sensor datasheet
sleep_ms(100);
for (int i = 0; i < 3; i++)
{
resetSensor(sens);
sleep_ms(100);
if (!needsReset(sens))
{
return DHT20_OK;
}
}
return DHT20_ERROR_NORESET;
}
static void resetSensor(DHT20 *sens)
{
if (needsReset(sens))
{
i2c_write_blocking(i2c0, DHT20_ADDRESS, rst_msg_1, 3, false);
i2c_write_blocking(i2c0, DHT20_ADDRESS, rst_msg_2, 3, false);
i2c_write_blocking(i2c0, DHT20_ADDRESS, rst_msg_3, 3, false);
}
}
int startMeasurement(DHT20 *sens)
{
if (i2c_write_blocking(I2C_INST, DHT20_ADDRESS, trigger_measurement, 3, false) == PICO_ERROR_GENERIC)
{
return DHT20_ERROR_CONNECT;
}
return DHT20_OK;
}
int readMeasurement(DHT20 *sens)
{
if (i2c_read_blocking(I2C_INST, DHT20_ADDRESS, sens->bytes, 7, false) == PICO_ERROR_GENERIC)
{
return DHT20_ERROR_CONNECT;
}
// Return busy if the measurements weren't ready yet
if ((sens->bytes[0] & 0x80) == 0x80)
{
return DHT20_ERROR_BUSY;
}
bool allZero = true;
for (int i = 0; i < 7; i++)
{
if (sens->bytes[i] > 0)
{
allZero = false;
break;
}
}
if (allZero)
{
return DHT20_ERROR_BYTES_ALL_ZERO;
}
sens->lastRead = to_ms_since_boot(get_absolute_time());
return DHT20_OK;
}
int convert(DHT20 *sens)
{
// CONVERT AND STORE
sens->status = sens->bytes[0];
uint32_t raw = sens->bytes[1];
raw <<= 8;
raw += sens->bytes[2];
raw <<= 4;
raw += (sens->bytes[3] >> 4);
sens->humidity = raw * 9.5367431640625e-5; // ==> / 1048576.0 * 100%;
raw = (sens->bytes[3] & 0x0F);
raw <<= 8;
raw += sens->bytes[4];
raw <<= 8;
raw += sens->bytes[5];
sens->temperature = raw * 1.9073486328125e-4 - 50; // ==> / 1048576.0 * 200 - 50;
// TEST CHECKSUM
sens->crc = _crc8(sens->bytes, 6);
if (sens->crc != sens->bytes[6])
{
return DHT20_ERROR_CHECKSUM;
}
return DHT20_OK;
}
// function that can be called every cycle in the loop. function does not sleep
int updateMeasurement(DHT20 *sens){
int status;
if (to_ms_since_boot(get_absolute_time()) - sens->lastRequest > sens->updateInterval) {
status=startMeasurement(sens);
sens->lastRequest=to_ms_since_boot(get_absolute_time());
}
if (status != DHT20_OK)
{
return status;
}
if (to_ms_since_boot(get_absolute_time()) - sens->lastRequest > 100){
if (to_ms_since_boot(get_absolute_time()) - sens->lastRead > 1000){
status=readMeasurement(sens);
convert(sens);
}
}
if (status != DHT20_OK)
{
return status;
}
return DHT20_OK;
}
int getMeasurement(DHT20 *sens)
{
int status;
if (to_ms_since_boot(get_absolute_time()) - sens->lastRead < 1000)
{
return DHT20_ERROR_LASTREAD;
}
status = startMeasurement(sens);
if (status != DHT20_OK)
{
return status;
}
sleep_ms(50);
for (int i = 5; i > 0; i--)
{
status = readMeasurement(sens);
if (status == DHT20_ERROR_BUSY)
{
sleep_ms(10);
continue;
}
}
if (status != DHT20_OK)
{
return status;
}
return convert(sens);
}
float getHumidity(DHT20 *sens)
{
return sens->humidity + sens->humOffset;
}
float getTemperature(DHT20 *sens)
{
return sens->temperature + sens->tempOffset;
}
void setHumOffset(struct DHT20 *sens, float offset)
{
sens->humOffset = offset;
};
void setTempOffset(struct DHT20 *sens, float offset)
{
sens->tempOffset = offset;
};
void setUpdateInterval(struct DHT20 *sens, uint8_t time)
{
sens->updateInterval=time;
};
float getHumOffset(DHT20 *sens)
{
return sens->humOffset;
};
float getTempOffset(DHT20 *sens)
{
return sens->tempOffset;
};
bool isCalibrated(DHT20 *sens)
{
return (readStatus(sens) & 0x08) == 0x08;
}
bool isMeasuring(DHT20 *sens)
{
return (readStatus(sens) & 0x80) == 0x80;
}
bool isIdle(DHT20 *sens)
{
return (readStatus(sens) & 0x80) == 0x00;
}
int internalStatus(DHT20 *sens)
{
return sens->status;
};
uint32_t lastRead(DHT20 *sens)
{
return sens->lastRead;
};
uint32_t lastRequest(DHT20 *sens)
{
return sens->lastRequest;
};
static uint8_t _crc8(uint8_t *ptr, uint8_t len)
{
uint8_t crc = 0xFF;
while (len--)
{
crc ^= *ptr++;
for (uint8_t i = 0; i < 8; i++)
{
if (crc & 0x80)
{
crc <<= 1;
crc ^= 0x31;
}
else
{
crc <<= 1;
}
}
}
return crc;
}