-
Notifications
You must be signed in to change notification settings - Fork 5
/
dht11.c
127 lines (101 loc) · 3.08 KB
/
dht11.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
#include "dht11.h"
int DHT11_init(struct DHT11_Dev* dev, GPIO_TypeDef* port, uint16_t pin) {
TIM_TimeBaseInitTypeDef TIM_TimBaseStructure;
GPIO_InitTypeDef GPIO_InitStructure;
dev->port = port;
dev->pin = pin;
//Initialise TIMER2
TIM_TimBaseStructure.TIM_Period = 84000000 - 1;
TIM_TimBaseStructure.TIM_Prescaler = 84;
TIM_TimBaseStructure.TIM_ClockDivision = 0;
TIM_TimBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimBaseStructure);
TIM_Cmd(TIM2, ENABLE);
//Initialise GPIO DHT11
GPIO_InitStructure.GPIO_Pin = dev->pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(dev->port, &GPIO_InitStructure);
return 0;
}
int DHT11_read(struct DHT11_Dev* dev) {
//Initialisation
uint8_t i, j, temp;
uint8_t data[5] = {0x00, 0x00, 0x00, 0x00, 0x00};
GPIO_InitTypeDef GPIO_InitStructure;
//Generate START condition
//o
GPIO_InitStructure.GPIO_Pin = dev->pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(dev->port, &GPIO_InitStructure);
//dev->port->MODER |= GPIO_MODER_MODER6_0;
//Put LOW for at least 18ms
GPIO_ResetBits(dev->port, dev->pin);
//wait 18ms
TIM2->CNT = 0;
while((TIM2->CNT) <= 18000);
//Put HIGH for 20-40us
GPIO_SetBits(dev->port, dev->pin);
//wait 40us
TIM2->CNT = 0;
while((TIM2->CNT) <= 40);
//End start condition
//io();
//Input mode to receive data
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_Init(dev->port, &GPIO_InitStructure);
//DHT11 ACK
//should be LOW for at least 80us
//while(!GPIO_ReadInputDataBit(dev->port, dev->pin));
TIM2->CNT = 0;
while(!GPIO_ReadInputDataBit(dev->port, dev->pin)) {
if(TIM2->CNT > 100)
return DHT11_ERROR_TIMEOUT;
}
//should be HIGH for at least 80us
//while(GPIO_ReadInputDataBit(dev->port, dev->pin));
TIM2->CNT = 0;
while(GPIO_ReadInputDataBit(dev->port, dev->pin)) {
if(TIM2->CNT > 100)
return DHT11_ERROR_TIMEOUT;
}
//Read 40 bits (8*5)
for(j = 0; j < 5; ++j) {
for(i = 0; i < 8; ++i) {
//LOW for 50us
while(!GPIO_ReadInputDataBit(dev->port, dev->pin));
/*TIM2->CNT = 0;
while(!GPIO_ReadInputDataBit(dev->port, dev->pin)) {
if(TIM2->CNT > 60)
return DHT11_ERROR_TIMEOUT;
}*/
//Start counter
TIM_SetCounter(TIM2, 0);
//HIGH for 26-28us = 0 / 70us = 1
while(GPIO_ReadInputDataBit(dev->port, dev->pin));
/*while(!GPIO_ReadInputDataBit(dev->port, dev->pin)) {
if(TIM2->CNT > 100)
return DHT11_ERROR_TIMEOUT;
}*/
//Calc amount of time passed
temp = TIM_GetCounter(TIM2);
//shift 0
data[j] = data[j] << 1;
//if > 30us it's 1
if(temp > 40)
data[j] = data[j]+1;
}
}
//verify the Checksum
if(data[4] != (data[0] + data[2]))
return DHT11_ERROR_CHECKSUM;
//set data
dev->temparature = data[2];
dev->humidity = data[0];
return DHT11_SUCCESS;
}