-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdht.cpp
197 lines (177 loc) · 3.95 KB
/
dht.cpp
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
/*
* Based on https://github.com/bafeigum/DHT11-Library-for-MSP430/tree/master from Bryce Feigum
*/
#include "dht.h"
#include "timer.h"
#include "msp430g2553.h"
dht::dht()
{
SET (P2OUT, BIT_DHT_VCC); // VCC to dht (pin output)
humiDigits[0] = '0';
humiDigits[1] = '0';
tempDigits[0] = '0';
tempDigits[1] = '0';
tempDigits[2] = '0';
pT = new timer1;
}
dht::~dht()
{
delete pT;
}
bool dht::readDht()
{
static unsigned counter = 0;
unsigned char tmpData[5];
counter++;
switch (counter)
{
case 1:
powerOn();
break;
case 5:
readData(tmpData);
powerOff();
return analyzeResponse(tmpData);
case 20:
counter = 0;
}
return false;
}
bool dht::analyzeResponse(unsigned char * tmpData)
{
if (checkChecksum(tmpData))
{
unsigned char * digits;
digits = getDigits(tmpData);
if (!checkDigits(digits))
{
delete digits;
return false;
}
saveDigits(digits);
delete digits;
return true;
}
return false;
}
unsigned char * dht::getDigits(unsigned char * tmpData)
{
unsigned char * digits;
digits = new unsigned char [5];
digits[0] = tmpData[0] / 10 + '0';
digits[1] = tmpData[0] % 10 + '0';
digits[2] = tmpData[2] / 10 + '0';
digits[3] = tmpData[2] % 10 + '0';
digits[4] = tmpData[3] % 10 + '0';
return digits;
}
bool dht::checkDigits(const unsigned char * digits)
{
for (int i=0; i<5; i++)
{
if ( digits[0] >= '0' && digits[0] <= '9')
{
continue;
}
else
{
return false;
}
}
return true;
}
void dht::saveDigits(const unsigned char * digits)
{
humiDigits[0] = digits[0];
humiDigits[1] = digits[1];
tempDigits[0] = digits[2];
tempDigits[1] = digits[3];
tempDigits[2] = digits[4];
}
const char *dht::getTemperature()
{
char *str;
str = new char[6]{"00.0C"};
str[0] = tempDigits[0];
str[1] = tempDigits[1];
str[3] = tempDigits[2];
return str;
}
const char *dht::getHumidity()
{
char *str;
str = new char[6]{"HR00%"};
str[2] = humiDigits[0];
str[3] = humiDigits[1];
return str;
}
void dht::readData(unsigned char *data)
{
startSignal();
if (checkResponse())
{
unsigned char i, j, num;
unsigned interruptIn = 90;
for (j = 0; j<5; j++)
{
num = 0;
for (i = 8; i > 0; i--)
{
while (!(TST(P2IN, DHT_PIN)));//Wait for signal to go high
pT->usInitTimer(&interruptIn);
while (TST(P2IN, DHT_PIN)) //Wait for signal to go low
{
if (pT->timeOut()) break;
}
CLR(TA1CTL, 0x30); //Halt Timer
if (TA1R > 44) //40 @ 1x divider
{
num |= 1 << (i - 1);
}
}
data[j] = num;
}
}
}
void dht::startSignal()
{
unsigned wait1 = 20;
unsigned wait2 = 30;
SET(P2DIR, DHT_PIN); // Set Data pin to output direction
CLR(P2OUT, DHT_PIN); // Low for at least 18ms
pT->msWait(&wait1);
SET(P2OUT, DHT_PIN); // High for at 20us-40us
pT->usWait(&wait2);
CLR(P2DIR, DHT_PIN); // Set data pin to input direction
}
bool dht::checkResponse()
{
unsigned wait = 95;
pT->usInitTimer(&wait);
while (!(TST(P2IN, DHT_PIN)))
{
if (pT->timeOut()) return false;
}
pT->usInitTimer(&wait);
while (TST(P2IN, DHT_PIN))
{
if (pT->timeOut()) return false;
}
return true;
}
bool dht::checkChecksum(unsigned char * tmp)
{
if (tmp[4] != (tmp[0] + tmp[1] + tmp[2] + tmp[3]))
{
return false;
}
return true;
}
void dht::powerOn()
{
SET (P2DIR, BIT_DHT_VCC);
}
void dht::powerOff()
{
CLR(P2DIR, BIT_DHT_VCC);
}