-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdht11.cpp
51 lines (42 loc) · 1.36 KB
/
dht11.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
/*
* dht11.c
*
* Created: 26.07.2018 19:47:49
* Author: rahm
*/
#include "dht11.h"
uint8_t dht11_read(uint8_t *data)
{
volatile uint8_t temp;
volatile uint8_t byte, bit;
volatile uint8_t counter;
data[0] = data[1] = data[2] = data[3] = data[4] = 0;
// DHT11 Lesen initialisieren
bit_init(DHT11_PORT,DHT11_BIT,OUT); // DHT11pin als Ausgang
bit_write(DHT11_PORT,DHT11_BIT,0); // Low ausgeben
delay_ms( 18 ); // 18ms warten
bit_init(DHT11_PORT,DHT11_BIT,IN); // DHT11 pin als Eingang (mit Pullup)
while (bit_read(DHT11_PORT,DHT11_BIT)==1); // Dauer 20-40us
while (bit_read(DHT11_PORT,DHT11_BIT)==0); // Dauer ~80us
while (bit_read(DHT11_PORT,DHT11_BIT)==1); // Dauer ~80us
// Starte Transmission
// 5 Byte Daten lesen
for (byte = 0; byte < 5; byte++)
{
temp = 0;
for (bit = 0; bit < 8; bit++)
{
while (bit_read(DHT11_PORT,DHT11_BIT)==0); // Dauer ~50us
counter = 0;
while ( bit_read(DHT11_PORT, DHT11_BIT ) == 1) // Dauer: 0 ca. 26-28us
{ // 1 ca. 70us
_delay_us( 10 ); counter++;
if ( counter == 255 ) return 1; // Error
}
temp <<= 1;
if (counter > 3) temp |= 0x01; // >30us
}
data[byte] = temp;
}
return 0; // OK
}