-
Notifications
You must be signed in to change notification settings - Fork 0
/
DHT22.c
216 lines (198 loc) · 4.88 KB
/
DHT22.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
//==============================================================================
//
// main.h
//
//
//==============================================================================
// FILE INFORMATION
//==============================================================================
//
// Source:
//
// Project: Humidity and temperature monitoring
//
// Author: M.Shahzad
//
// Date: 20/03/2023
//
// Revision: 1.0
//
//==============================================================================
// FILE DESCRIPTION
//==============================================================================
//
//! \file
//! This module takes care of the
//
//==============================================================================
// REVISION HISTORY
//==============================================================================
// Revision: 1.0
//
//
//==============================================================================
// INCLUDES
//==============================================================================
#include "DHT22.h"
//==============================================================================
//
// void floattostr_(float x, unsigned char *str, char precision)
//
// Author: M. Shahzad
// Date: 06/08/2021
//
//! This function used to convert float data to string
//
//==============================================================================
void floattostr_(float x, unsigned char *str, char precision)
{
// convert float to string data
int ie, i, k, ndig;
double y;
ndig = (precision <= 0) ? 7 : (precision > 22 ? 23 : precision + 1);
ie = 0;
/* if x negative, write minus and reverse */
if (x < 0)
{
*str++ = '-';
x = -x;
}
/* put x in range 1 <= x < 10 */
if (x > 0.0)
while (x < 1.0)
{
x *= 10.0; // a la place de =*
ie--;
}
while (x >= 10.0)
{
x = x / 10.0;
ie++;
}
ndig += ie; // a la place de =+
// round. x is between 1 and 10 and ndig will be printed to
// right of decimal point so rounding is ...
for (y = i = 1; i < ndig; i++)
y = y / 10.;
x += y / 2.;
if (x >= 10.0)
{
x = 1.0;
ie++;
}
if (ie < 0)
{
*str++ = '0';
*str++ = '.';
if (ndig < 0)
ie = ie - ndig;
for (i = -1; i > ie; i--)
*str++ = '0';
}
for (i = 0; i < ndig; i++)
{
k = x;
*str++ = k + '0';
if (i == ie)
*str++ = '.';
x -= (y = k);
x *= 10.0;
}
*str = '\0';
}
//==============================================================================
//
// char dht22_get_byte(char s_number)
//
// Author: M. Shahzad
// Date: 06/08/2021
//
//! Get the values of DHT11
//
//==============================================================================
char dht22_get_byte(char s_number)
{
char dht22_data, count_for, delaycnt = 0;
for (count_for = 0; count_for < 8; count_for++)
{
while (!dht22_S1_pin)
{
Delay_us(1);
delaycnt++;
if (delaycnt > 250)
break;
}
delay_us(30);
delaycnt = 0;
if (dht22_S1_pin == 0)
dht22_data &= ~(1 << (7 - count_for));
else
{
dht22_data |= (1 << (7 - count_for));
while (dht22_S1_pin)
{
Delay_us(1);
delaycnt++;
if (delaycnt > 250)
break;
}
}
}
return dht22_data;
}
//==============================================================================
//
// char dht22_S1_function(char value)
//
// Author: M. Shahzad
// Date: 06/08/2021
//
//! This function reads temperatue and humidity values from the sensors
//
//==============================================================================
char dht22_S1_function(char value)
{
char res_ = 0;
dht22_S1_pin_direction = 0;
dht22_S1_pin = 0;
delay_ms(18);
dht22_S1_pin = 1;
delay_us(30);
dht22_S1_pin_direction = 1; // configura entrada pin del sensor
delay_us(40);
if (dht22_S1_pin == 0)
{
delay_us(80);
if (dht22_S1_pin == 1)
res_ = 1;
delay_us(40);
}
if (res_ == dht22_noresponse)
return (res_);
res_ = 0; // inicializa cero para procesar lectura
dht22_S1_object.hum1_ = dht22_get_byte(1);
dht22_S1_object.hum2_ = dht22_get_byte(1);
dht22_S1_object.temp1_ = dht22_get_byte(1);
dht22_S1_object.temp2_ = dht22_get_byte(1);
dht22_S1_object.checksum_ = dht22_get_byte(1);
if (dht22_S1_object.checksum_ == ((dht22_S1_object.hum1_ + dht22_S1_object.hum2_ + dht22_S1_object.temp1_ + dht22_S1_object.temp2_) & 0XFF))
{
// Calcula Temperatura
dht22_S1_object.Temp_ = dht22_S1_object.temp1_;
dht22_S1_object.Temp_ = (dht22_S1_object.Temp_ << 8) | dht22_S1_object.Temp2_;
if (dht22_S1_object.Temp_ > 0X8000)
{
dht22_S1_object.Temp_ = dht22_S1_object.Temp_ & 0X7FFF;
}
dht22_S1_object.temperature_ = dht22_S1_object.Temp_ / 10.0;
// calcula Humedad
dht22_S1_object.hume_ = dht22_S1_object.hum1_;
dht22_S1_object.hume_ = (dht22_S1_object.hume_ << 8) | dht22_S1_object.hum2_;
dht22_S1_object.humedad_ = dht22_S1_object.hume_ / 10.0;
res_ = 1;
}
if (res_ = dht22_noresponse)
return (dht22_errorcheck_sum);
else
return (dht22_ok);
}