-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
dht.c
156 lines (154 loc) · 4.86 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
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
#include "dht.h"
#include "dhtConf.h"
#include "tim.h"
#if (_DHT_USE_FREERTOS==1)
#include "cmsis_os.h"
#define DHT_delayMs(x) osDelay(x)
#else
#define DHT_delayMs(x) HAL_Delay(x)
#endif
//###############################################################################################################
void DHT_pinChangeCallBack(DHT_t *dht)
{
dht->time = HAL_GetTick();
if(dht->cnt < sizeof(dht->data)-1)
{
dht->data[dht->cnt] = dht->tim->Instance->CNT - dht->lastCNT;
dht->lastCNT = dht->tim->Instance->CNT;
dht->cnt++;
}
}
//###############################################################################################################
void DHT_delayUs(DHT_t *dht, uint16_t DelayUs)
{
dht->tim->Instance->CNT=0;
while(dht->tim->Instance->CNT < DelayUs);
}
//###############################################################################################################
void DHT_output(DHT_t *dht)
{
GPIO_InitTypeDef gpio;
dht->gpio->BSRR = dht->pin;
gpio.Mode = GPIO_MODE_OUTPUT_OD;
gpio.Speed = GPIO_SPEED_FREQ_HIGH;
gpio.Pin = dht->pin;
gpio.Pull = GPIO_NOPULL;
HAL_GPIO_Init(dht->gpio,&gpio);
}
//###############################################################################################################
void DHT_input(DHT_t *dht)
{
GPIO_InitTypeDef gpio;
gpio.Mode = GPIO_MODE_IT_RISING_FALLING;
gpio.Speed = GPIO_SPEED_FREQ_HIGH;
gpio.Pin = dht->pin;
gpio.Pull = GPIO_NOPULL;
HAL_GPIO_Init(dht->gpio,&gpio);
}
//###############################################################################################################
bool DHT_decode(DHT_t *dht,uint8_t *byteArray)
{
int8_t bit;
switch(dht->type)
{
case DHT_Type_DHT21:
case DHT_Type_AM2301:
case DHT_Type_AM2305:
if((dht->data[0] < 60) || (dht->data[0] > 100) || (dht->data[1] < 60) || (dht->data[1] > 100))
return false;
bit = 7;
for(uint8_t i=0 ; i<80 ; i+=2)
{
if((dht->data[i+2] >= 35) && (dht->data[i+2] <= 70))
{
if((dht->data[i+3] >= 10) && (dht->data[i+3] <= 45))
*byteArray &= ~(1<<bit);
else if((dht->data[i+3] >= 55) && (dht->data[i+3] <= 95))
*byteArray |= (1<<bit);
else
return false;
bit--;
if(bit == -1)
{
bit = 7;
byteArray++;
}
}
else
return false;
}
return true;
default:
return false;
}
}
//###############################################################################################################
void DHT_init(DHT_t *dht, DHT_Type_t type, TIM_HandleTypeDef *tim,uint16_t timerBusFrequencyMHz, GPIO_TypeDef *gpio, uint16_t pin)
{
dht->tim = tim;
dht->gpio = gpio;
dht->pin = pin;
dht->type = type;
DHT_output(dht);
dht->tim->Init.Prescaler = timerBusFrequencyMHz - 1;
dht->tim->Init.CounterMode = TIM_COUNTERMODE_UP;
dht->tim->Init.Period = 0xFFFF;
dht->tim->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
HAL_TIM_Base_Init(dht->tim);
HAL_TIM_Base_Start(dht->tim);
while(HAL_GetTick()<2000)
DHT_delayMs(1);
DHT_delayMs(20);
}
//###############################################################################################################
bool DHT_readData(DHT_t *dht, float *Temperature, float *Humidity)
{
uint32_t startTime;
switch(dht->type)
{
case DHT_Type_DHT21:
case DHT_Type_AM2301:
case DHT_Type_AM2305:
DHT_output(dht);
dht->gpio->BSRR = (dht->pin)<<16;
DHT_delayMs(5);
dht->gpio->BSRR = dht->pin;
DHT_delayUs(dht,20);
dht->gpio->BSRR = (dht->pin)<<16;
DHT_delayUs(dht,5);
dht->cnt = 0;
dht->lastCNT = 0;
dht->tim->Instance->CNT = 0;
startTime = HAL_GetTick();
DHT_input(dht);
while(1)
{
if(HAL_GetTick() - startTime > 8)
goto ERROR;
if(HAL_GetTick() - dht->time > 1)
{
uint8_t data[5];
if(DHT_decode(dht,data) == false)
goto ERROR;
if(((data[0] + data[1] + data[2] + data[3]) & 0x00FF) != data[4])
goto ERROR;
dht->temperature = (float)(data[2]*256 + data[3]) / 10.0f;
if(Temperature != NULL)
*Temperature = dht->temperature;
dht->humidity = (float)(data[0]*256 + data[1]) / 10.0f;
if(Humidity != NULL)
*Humidity = dht->humidity;
dht->dataValid = true;
DHT_output(dht);
return true;
}
}
default:
break;
}
ERROR:
dht->dataValid = false;
DHT_output(dht);
return false;
}
//###############################################################################################################