-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDHT.c
108 lines (92 loc) · 2.36 KB
/
DHT.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
#include <stdio.h>
#include <string.h>
#include <avr/io.h>
#ifndef F_CPU
#define F_CPU 1000000
#endif
#include <util/delay.h>
#include "DHT.h"
//main function that communicates with DHT sensor
#if DHT_TYPE == 1
int8_t dht_GetTemp(uint16_t *temperature, uint16_t *humidity) {
#elif DHT_TYPE == 0
int8_t dht_GetTemp(int8_t *temperature, int8_t *humidity) {
#endif
uint8_t bits[5];
uint8_t i,j = 0;
memset(bits, 0, sizeof(bits));
//prepare correct port and pin of DHT sensor
DHT_DDR |= (1 << DHT_INPUTPIN); //output
DHT_PORT |= (1 << DHT_INPUTPIN); //high
_delay_ms(100);
//begin send request
DHT_PORT &= ~(1 << DHT_INPUTPIN); //low
#if DHT_TYPE == DHT_DHT11
_delay_ms(18);
#elif DHT_TYPE == DHT_DHT22
_delay_us(500);
#endif
DHT_PORT |= (1 << DHT_INPUTPIN); //high
DHT_DDR &= ~(1 << DHT_INPUTPIN); //input
_delay_us(40);
//check first start condition
if((DHT_PIN & (1<<DHT_INPUTPIN))) {
return -1;
}
_delay_us(80);
//check second start condition
if(!(DHT_PIN & (1<<DHT_INPUTPIN))) {
return -1;
}
_delay_us(80);
//read-in data
uint16_t timeoutcounter = 0;
for (j=0; j<5; j++) { //for each byte (5 total)
uint8_t result = 0;
for(i=0; i<8; i++) {//for each bit in each byte (8 total)
timeoutcounter = 0;
while(!(DHT_PIN & (1<<DHT_INPUTPIN))) { //wait for an high input (non blocking)
timeoutcounter++;
if(timeoutcounter > DHT_TIMEOUT) {
return -1;
}
}
_delay_us(30);
if(DHT_PIN & (1<<DHT_INPUTPIN))
result |= (1<<(7-i));
timeoutcounter = 0;
while(DHT_PIN & (1<<DHT_INPUTPIN)) {
timeoutcounter++;
if(timeoutcounter > DHT_TIMEOUT) {
return -1;
}
}
}
bits[j] = result;
}
//reset port
DHT_DDR |= (1<<DHT_INPUTPIN); //output
DHT_PORT |= (1<<DHT_INPUTPIN); //low
_delay_ms(100);
//compare checksum
if ((uint8_t)(bits[0] + bits[1] + bits[2] + bits[3]) == bits[4]) {
//return temperature and humidity
#if DHT_TYPE == DHT_DHT22
*temperature = bits[2]<<8 | bits[3];
*humidity = bits[0]<<8 | bits[1];
#elif DHT_TYPE == DHT_DHT11
*temperature = bits[2];
*humidity = bits[0];
#endif
return 0;
}
return -1;
}
//function that calls data function read-in
#if DHT_TYPE == 1
int8_t dht_GetTempUtil(uint16_t *temperature, uint16_t *humidity) {
#elif DHT_TYPE == 0
int8_t dht_GetTempUtil(int8_t *temperature, int8_t *humidity) {
#endif
return dht_GetTemp(temperature, humidity);
}