Skip to content

Commit 9c13789

Browse files
committed
+ version 0.1.01 (first working DHT22 simulation)
+ improved protocol implementation + refactor to get code in logical place + support for negative temperature + improved comments
1 parent 4f46069 commit 9c13789

File tree

1 file changed

+46
-43
lines changed

1 file changed

+46
-43
lines changed
Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: DHT_simulator.ino
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.00
4+
// VERSION: 0.1.01
55
// PURPOSE:
66
// DATE: 2014-06-14
77
// URL:
@@ -12,15 +12,15 @@
1212
// TODO
1313
// - robustness
1414
// - timeout loops
15-
// - get timing accurate
1615

1716
const int dataPin = 5;
1817
byte b[5];
1918

2019
void setup()
2120
{
2221
Serial.begin(115200);
23-
Serial.println("Start ");
22+
Serial.print("Start ");
23+
Serial.println(__FILE__);
2424

2525
pinMode(dataPin, INPUT_PULLUP);
2626
}
@@ -46,80 +46,80 @@ void loop()
4646
if (digitalRead(dataPin) == LOW)
4747
{
4848
uint32_t start = micros();
49-
// wait until signal goes high // todo timeout onblocking loop?
50-
while (digitalRead(dataPin) == LOW);
49+
// wait until signal goes high
50+
// todo timeout on blocking loop
51+
while (digitalRead(dataPin) == LOW)
52+
{
53+
if (micros() - start > 1500) return;
54+
}
5155
if (micros() - start > 500) // serious request...
5256
{
53-
54-
pinMode(dataPin, OUTPUT);
55-
// send ACK
56-
digitalWrite(dataPin, LOW);
57-
delayMicroseconds(80); // overhead digitalwrite (2..4us)
58-
digitalWrite(dataPin, HIGH);
59-
delayMicroseconds(80);
60-
6157
DHTsend(H, T);
6258

63-
digitalWrite(dataPin, LOW);
64-
delayMicroseconds(50);
65-
pinMode(dataPin, INPUT_PULLUP);
66-
67-
// for (int i = 0; i < 5; i++)
68-
// {
69-
// Serial.print(b[i]);
70-
// Serial.print(" ");
71-
// }
72-
// Serial.println();
73-
7459
Serial.print(H);
7560
Serial.print("\t");
7661
Serial.println(T);
7762
}
7863
}
7964
}
8065

66+
8167
void DHTsend(int H, int T)
8268
{
83-
// prepare data
69+
pinMode(dataPin, OUTPUT);
70+
// SEND ACK
71+
digitalWrite(dataPin, LOW);
72+
delayMicroseconds(80); // 80 us
73+
digitalWrite(dataPin, HIGH);
74+
delayMicroseconds(80); // 80 us
75+
76+
// PREPARE DATA
8477
b[0] = H / 256;
8578
b[1] = H & 255;
8679

87-
uint16_t t;
88-
if (T >= 0)
89-
{
90-
t = T;
91-
b[2] = 0;
92-
}
93-
else
80+
b[2] = 0;
81+
if (T < 0)
9482
{
95-
t = -T;
83+
T = -T;
9684
b[2] = 0x80;
9785
}
98-
Serial.println(t);
9986

100-
b[2] |= (t / 256);
101-
b[3] = (t & 255);
102-
103-
b[4] = b[0] + b[1] + b[2] + b[3]; // CRC
87+
b[2] |= T / 256;
88+
b[3] = T & 255;
89+
// CRC
90+
b[4] = b[0] + b[1] + b[2] + b[3];
10491

92+
// SEND DATA
10593
for (int i = 0; i < 5; i++)
10694
{
10795
DHTsendbyte(b[i]);
10896
}
97+
98+
// END OF TRANSMISSION SIGNAL
99+
digitalWrite(dataPin, LOW);
100+
delayMicroseconds(50); // 50 us
101+
pinMode(dataPin, INPUT_PULLUP);
102+
103+
// DEBUG
104+
// for (int i = 0; i < 5; i++)
105+
// {
106+
// Serial.print(b[i]);
107+
// Serial.print(" ");
108+
// }
109+
// Serial.println();
109110
}
110111

112+
// timing manual tuned
111113
void DHTsendbyte(byte b)
112114
{
113115
byte mask = 128;
114116
for(int i = 0; i < 8; i++)
115117
{
116118
digitalWrite(dataPin, LOW);
117-
delayMicroseconds(45); // overhead digitalwrite (2..4us)
119+
delayMicroseconds(45); // 50 us
118120
digitalWrite(dataPin, HIGH);
119-
120-
if (b & mask) delayMicroseconds(60); // idem
121-
else delayMicroseconds(24); // idem
122-
121+
if (b & mask) delayMicroseconds(60); // 70 us
122+
else delayMicroseconds(24); // 26 us
123123
mask >>= 1;
124124
}
125125
}
@@ -139,3 +139,6 @@ void DHTsendbyte(byte b)
139139

140140

141141

142+
143+
144+

0 commit comments

Comments
 (0)