LibDriver DHT11
Loading...
Searching...
No Matches
driver_dht11.c
Go to the documentation of this file.
1
37
38#include "driver_dht11.h"
39
43#define CHIP_NAME "ASAIR DHT11"
44#define MANUFACTURER_NAME "ASAIR"
45#define SUPPLY_VOLTAGE_MIN 3.3f
46#define SUPPLY_VOLTAGE_MAX 5.5f
47#define MAX_CURRENT 1.0f
48#define TEMPERATURE_MIN -20.0f
49#define TEMPERATURE_MAX 60.0f
50#define DRIVER_VERSION 2000
51
60static uint8_t a_dht11_reset(dht11_handle_t *handle)
61{
62 uint8_t retry = 0;
63 uint8_t res;
64 uint8_t value;
65
66 res = handle->bus_write(0); /* set low */
67 if (res != 0) /* check result */
68 {
69 handle->debug_print("dht11: bus write 0 failed.\n"); /* write failed */
70
71 return 1; /* return error */
72 }
73 handle->delay_ms(20); /* wait 20ms */
74 handle->disable_irq(); /* disable interrupt */
75 res = handle->bus_write(1); /* set high */
76 if (res != 0) /* check result */
77 {
78 handle->enable_irq(); /* enable interrupt */
79 handle->debug_print("dht11: bus write 1 failed.\n"); /* write failed */
80
81 return 1; /* return error */
82 }
83 handle->delay_us(30); /* wait 20-40us */
84 res = handle->bus_read((uint8_t *)&value); /* read 1 bit */
85 if (res != 0) /* check result */
86 {
87 handle->enable_irq(); /* enable interrupt */
88 handle->debug_print("dht11: bus read failed.\n"); /* read failed */
89
90 return 1; /* return error */
91 }
92 while ((value != 0) && (retry < 100)) /* wait 40-80us */
93 {
94 res = handle->bus_read((uint8_t *)&value); /* read 1 bit */
95 if (res != 0) /* check result */
96 {
97 handle->enable_irq(); /* enable interrupt */
98 handle->debug_print("dht11: bus read failed.\n"); /* read failed */
99
100 return 1; /* return error */
101 }
102 retry++; /* retry times++ */
103 handle->delay_us(1); /* delay 1us */
104 }
105 if (retry >= 100) /* if retry times is over 100 times */
106 {
107 handle->enable_irq(); /* enable interrupt */
108 handle->debug_print("dht11: bus no response.\n"); /* no response */
109
110 return 1; /* return error */
111 }
112 else
113 {
114 retry = 0; /* reset retry times */
115 }
116 res = handle->bus_read((uint8_t *)&value); /* read 1 bit */
117 if (res != 0) /* check result */
118 {
119 handle->enable_irq(); /* enable interrupt */
120 handle->debug_print("dht11: bus read failed.\n"); /* read failed */
121
122 return 1; /* return error */
123 }
124 while ((!value) && (retry < 100)) /* wait for 40-80us */
125 {
126 res = handle->bus_read((uint8_t *)&value); /* read 1 bit */
127 if (res != 0) /* check result */
128 {
129 handle->enable_irq(); /* enable interrupt */
130 handle->debug_print("dht11: bus read failed.\n"); /* read failed */
131
132 return 1; /* return error */
133 }
134 retry++; /* retry times++ */
135 handle->delay_us(1); /* delay 1 us */
136 }
137 if (retry >= 100) /* if retry times is over 100 times */
138 {
139 handle->enable_irq(); /* enable interrupt */
140 handle->debug_print("dht11: bus no response.\n"); /* no response */
141
142 return 1; /* return error */
143 }
144 handle->enable_irq(); /* enable interrupt */
145
146 return 0; /* success return 0 */
147}
148
158static uint8_t a_dht11_read_bit(dht11_handle_t *handle, uint8_t *value)
159{
160 uint8_t retry = 0;
161 uint8_t res;
162
163 res = handle->bus_read((uint8_t *)value); /* read 1 bit */
164 if (res != 0) /* check result */
165 {
166 handle->debug_print("dht11: bus read failed.\n"); /* read failed */
167
168 return 1; /* return error */
169 }
170 while (((*value) != 0) && (retry < 100)) /* wait 100us */
171 {
172 res = handle->bus_read((uint8_t *)value); /* read 1 bit */
173 if (res != 0) /* check result */
174 {
175 handle->debug_print("dht11: bus read failed.\n"); /* read failed */
176
177 return 1; /* return error */
178 }
179 retry++; /* retry times++ */
180 handle->delay_us(1); /* delay 1 us */
181 }
182 retry = 0; /* reset retry times */
183 res = handle->bus_read((uint8_t *)value); /* read 1 bit */
184 if (res != 0) /* check result */
185 {
186 handle->debug_print("dht11: bus read failed.\n"); /* read failed */
187
188 return 1; /* return error */
189 }
190 while ((!(*value)) && (retry < 100)) /* wait 100us */
191 {
192 res = handle->bus_read((uint8_t *)value); /* read 1 bit */
193 if (res != 0) /* check result */
194 {
195 handle->debug_print("dht11: bus read failed.\n"); /* read failed */
196
197 return 1; /* return error */
198 }
199 retry++; /* retry times++ */
200 handle->delay_us(1); /* wait 1 us */
201 }
202 handle->delay_us(40); /* wait 40us */
203 res = handle->bus_read((uint8_t *)value); /* read 1 bit */
204 if (res != 0) /* check result */
205 {
206 handle->debug_print("dht11: bus read failed.\n"); /* read failed */
207
208 return 1; /* return error */
209 }
210 else
211 {
212 return 0; /* success return 0 */
213 }
214}
215
225static uint8_t a_dht11_read_byte(dht11_handle_t *handle, uint8_t *byte)
226{
227 uint8_t i;
228 uint8_t res;
229 uint8_t value;
230
231 *byte = 0; /* set byte 0 */
232 for (i = 0; i < 8; i++) /* read 8 bits */
233 {
234 *byte <<= 1; /* left shift 1 bit */
235 res = a_dht11_read_bit(handle, (uint8_t *)&value); /* read 1 bit */
236 if (res != 0) /* check result */
237 {
238 handle->debug_print("dht11: bus read failed.\n"); /* read failed */
239
240 return 1; /* return error */
241 }
242 *byte |= value; /* set LSB */
243 }
244
245 return 0; /* success return 0 */
246}
247
260uint8_t dht11_read_humidity(dht11_handle_t *handle, uint16_t *raw, uint8_t *s)
261{
262 uint8_t buf[5];
263 uint8_t i;
264
265 if (handle == NULL) /* check handle */
266 {
267 return 2; /* return error */
268 }
269 if (handle->inited != 1) /* check handle initialization */
270 {
271 return 3; /* return error */
272 }
273
274 if (a_dht11_reset(handle) == 0) /* reset the chip */
275 {
276 handle->disable_irq(); /* disable interrupt */
277 for (i = 0; i < 5; i++) /* read 5 bytes */
278 {
279 if (a_dht11_read_byte(handle, (uint8_t *)&buf[i]) != 0) /* read each byte */
280 {
281 handle->enable_irq(); /* enable interrupt */
282 handle->debug_print("dht11: read byte failed.\n"); /* read failed */
283
284 return 1; /* return error */
285 }
286 }
287 handle->enable_irq(); /* enable interrupt */
288 if ((buf[0] + buf[1] + buf[2] + buf[3]) == buf[4]) /* calculate checksum */
289 {
290 *raw = (uint16_t)buf[0] << 8 | buf[1]; /* get raw data */
291 *s = buf[0]; /* convert raw data to real data */
292
293 return 0; /* success return 0 */
294 }
295 else
296 {
297 handle->debug_print("dht11: data check failed.\n"); /* checksum error */
298
299 return 1; /* return error */
300 }
301 }
302 else
303 {
304 handle->debug_print("dht11: reset failed.\n"); /* reset failed */
305
306 return 1; /* return error */
307 }
308}
309
324uint8_t dht11_read_temperature_humidity(dht11_handle_t *handle, uint16_t *temperature_raw, float *temperature_s,
325 uint16_t *humidity_raw, uint8_t *humidity_s)
326{
327 uint8_t buf[5];
328 uint8_t i;
329
330 if (handle == NULL) /* check handle */
331 {
332 return 2; /* return error */
333 }
334 if (handle->inited != 1) /* check handle initialization */
335 {
336 return 3; /* return error */
337 }
338
339 if (a_dht11_reset(handle) == 0) /* reset the chip */
340 {
341 handle->disable_irq(); /* disable interrupt */
342 for (i = 0; i < 5; i++) /* read 5 bytes */
343 {
344 if (a_dht11_read_byte(handle, (uint8_t *)&buf[i]) != 0) /* read each byte */
345 {
346 handle->enable_irq(); /* enable interrupt */
347 handle->debug_print("dht11: read byte failed.\n"); /* read failed */
348
349 return 1; /* return error */
350 }
351 }
352 handle->enable_irq(); /* enable interrupt */
353 if ((buf[0] + buf[1] + buf[2] + buf[3]) == buf[4]) /* calculate checksum */
354 {
355 if (buf[3] > 127) /* if temperature is below zero */
356 {
357 *temperature_raw = (uint16_t)buf[2] << 8 | buf[3]; /* get temperature raw data */
358 *temperature_s= (float)(-(buf[2] * 10 +
359 (buf[3] & ~(1<<7)))) / 10.0f; /* convert temperature raw data to temperature real data */
360 }
361 else
362 {
363 *temperature_raw = (uint16_t)buf[2] << 8 | buf[3]; /* get temperature raw data */
364 *temperature_s= (float)(buf[2]*10 + buf[3]) / 10.0f; /* convert temperature raw data to temperature real data */
365 }
366 *humidity_raw = (uint16_t)buf[0] << 8 | buf[1]; /* get humidity raw */
367 *humidity_s = buf[0]; /* convert humidity raw data to real data */
368
369 return 0; /* success return 0 */
370 }
371 else
372 {
373 handle->debug_print("dht11: data check failed.\n"); /* checksum error */
374
375 return 1; /* return error */
376 }
377 }
378 else
379 {
380 handle->debug_print("dht11: reset failed.\n"); /* reset failed */
381
382 return 1; /* return error */
383 }
384}
385
398uint8_t dht11_read_temperature(dht11_handle_t *handle, uint16_t *raw, float *s)
399{
400 uint8_t buf[5];
401 uint8_t i;
402
403 if (handle == NULL) /* check handle */
404 {
405 return 2; /* return error */
406 }
407 if (handle->inited != 1) /* check handle initialization */
408 {
409 return 3; /* return error */
410 }
411
412 if (a_dht11_reset(handle) == 0) /* reset the chip */
413 {
414 handle->disable_irq(); /* disable interrupt */
415 for (i = 0; i < 5; i++) /* read 5 bytes */
416 {
417 if (a_dht11_read_byte(handle, (uint8_t *)&buf[i]) != 0) /* read each byte */
418 {
419 handle->enable_irq(); /* enable interrupt */
420 handle->debug_print("dht11: read byte failed.\n"); /* read failed */
421
422 return 1; /* return error */
423 }
424 }
425 handle->enable_irq(); /* enable interrupt */
426 if ((buf[0] + buf[1] + buf[2] + buf[3]) == buf[4]) /* calculate checksum */
427 {
428 if (buf[3] > 127) /* if temperature is below zero */
429 {
430 *raw = (uint16_t)buf[2] << 8 | buf[3]; /* get temperature raw data */
431 *s= (float)(-(buf[2] * 10 + (buf[3] & ~(1<<7)))) / 10.0f; /* convert temperature raw data to temperature real data */
432 }
433 else
434 {
435 *raw = (uint16_t)buf[2] << 8 | buf[3]; /* get temperature raw data */
436 *s= (float)(buf[2] * 10 + buf[3]) / 10.0f; /* convert temperature raw data to temperature real data */
437 }
438
439 return 0; /* success return 0 */
440 }
441 else
442 {
443 handle->debug_print("dht11: data check failed.\n"); /* checksum error */
444
445 return 1; /* return error */
446 }
447 }
448 else
449 {
450 handle->debug_print("dht11: reset failed.\n"); /* reset failed */
451
452 return 1; /* return error */
453 }
454}
455
468{
469 if (handle == NULL) /* check handle */
470 {
471 return 2; /* return error */
472 }
473 if (handle->debug_print == NULL) /* check debug_print */
474 {
475 return 3; /* return error */
476 }
477 if (handle->bus_init == NULL) /* check bus_init */
478 {
479 handle->debug_print("dht11: bus_init is null.\n"); /* bus_init is null */
480
481 return 3; /* return error */
482 }
483 if (handle->bus_deinit == NULL) /* check bus_deinit */
484 {
485 handle->debug_print("dht11: bus_deinit is null.\n"); /* bus_deinit is null */
486
487 return 3; /* return error */
488 }
489 if (handle->bus_read == NULL) /* check bus_read */
490 {
491 handle->debug_print("dht11: bus_read is null.\n"); /* bus_read is null */
492
493 return 3; /* return error */
494 }
495 if (handle->bus_write == NULL) /* check bus_write */
496 {
497 handle->debug_print("dht11: bus_write is null.\n"); /* bus_write is null */
498
499 return 3; /* return error */
500 }
501 if (handle->delay_ms == NULL) /* check delay_ms */
502 {
503 handle->debug_print("dht11: delay_ms is null.\n"); /* delay_ms is null */
504
505 return 3; /* return error */
506 }
507 if (handle->delay_us == NULL) /* check delay_us */
508 {
509 handle->debug_print("dht11: delay_us is null.\n"); /* delay_us is null */
510
511 return 3; /* return error */
512 }
513 if (handle->enable_irq == NULL) /* check enable_irq */
514 {
515 handle->debug_print("dht11: enable_irq is null.\n"); /* enable_irq is null */
516
517 return 3; /* return error */
518 }
519 if (handle->disable_irq == NULL) /* check disable_irq */
520 {
521 handle->debug_print("dht11: disable_irq is null.\n"); /* disable_irq is null */
522
523 return 3; /* return error */
524 }
525
526 if (handle->bus_init() != 0) /* initialize bus */
527 {
528 handle->debug_print("dht11: bus init failed.\n"); /* bus init failed */
529
530 return 1; /* return error */
531 }
532 if (a_dht11_reset(handle) != 0) /* reset the chip */
533 {
534 handle->debug_print("dht11: reset failed.\n"); /* reset failed */
535 (void)handle->bus_deinit(); /* close bus */
536
537 return 4; /* return error */
538 }
539 handle->inited = 1; /* flag finish initialization */
540
541 return 0; /* success return 0 */
542}
543
555{
556 if (handle == NULL) /* check handle */
557 {
558 return 2; /* return error */
559 }
560 if (handle->inited != 1) /* check handle initialization */
561 {
562 return 3; /* return error */
563 }
564
565 if (handle->bus_deinit() != 0) /* close bus */
566 {
567 handle->debug_print("dht11: deinit failed.\n"); /* deinit failed */
568
569 return 1; /* return error */
570 }
571 handle->inited = 0; /* flag close */
572
573 return 0; /* success return 0 */
574}
575
585{
586 if (info == NULL) /* check handle */
587 {
588 return 2; /* return error */
589 }
590
591 memset(info, 0, sizeof(dht11_info_t)); /* initialize dht11 info structure */
592 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
593 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
594 strncpy(info->interface, "GPIO", 8); /* copy interface name */
595 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
596 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
597 info->max_current_ma = MAX_CURRENT; /* set maximum current */
598 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
599 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
600 info->driver_version = DRIVER_VERSION; /* set driver version */
601
602 return 0; /* success return 0 */
603}
#define MAX_CURRENT
#define SUPPLY_VOLTAGE_MAX
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
driver dht11 header file
uint8_t dht11_read_temperature_humidity(dht11_handle_t *handle, uint16_t *temperature_raw, float *temperature_s, uint16_t *humidity_raw, uint8_t *humidity_s)
read the temperature and humidity data
struct dht11_info_s dht11_info_t
dht11 info structure definition
uint8_t dht11_info(dht11_info_t *info)
get chip's information
struct dht11_handle_s dht11_handle_t
dht11 handle structure definition
uint8_t dht11_init(dht11_handle_t *handle)
initialize the chip
uint8_t dht11_read_humidity(dht11_handle_t *handle, uint16_t *raw, uint8_t *s)
read the humidity data
uint8_t dht11_deinit(dht11_handle_t *handle)
close the chip
uint8_t dht11_read_temperature(dht11_handle_t *handle, uint16_t *raw, float *s)
read the temperature data
void(* enable_irq)(void)
uint8_t(* bus_deinit)(void)
void(* delay_ms)(uint32_t ms)
void(* debug_print)(const char *const fmt,...)
void(* delay_us)(uint32_t us)
void(* disable_irq)(void)
uint8_t(* bus_read)(uint8_t *value)
uint8_t(* bus_write)(uint8_t value)
uint8_t(* bus_init)(void)
float temperature_max
float supply_voltage_max_v
uint32_t driver_version
float temperature_min
float max_current_ma
char manufacturer_name[32]
float supply_voltage_min_v
char interface[8]
char chip_name[32]