Skip to content

Commit b71ac85

Browse files
author
K. Townsend
committed
2 parents b1364eb + e1900c2 commit b71ac85

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

Adafruit_DHT_Driver/Adafruit_DHT.c

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// How to access GPIO registers from C-code on the Raspberry-Pi
2+
// Example program
3+
// 15-January-2012
4+
// Dom and Gert
5+
//
6+
7+
8+
// Access from ARM Running Linux
9+
10+
#define BCM2708_PERI_BASE 0x20000000
11+
#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */
12+
13+
14+
#include <stdio.h>
15+
#include <string.h>
16+
#include <stdlib.h>
17+
#include <dirent.h>
18+
#include <fcntl.h>
19+
#include <assert.h>
20+
#include <unistd.h>
21+
#include <sys/mman.h>
22+
#include <sys/types.h>
23+
#include <sys/stat.h>
24+
#include <bcm2835.h>
25+
#include <unistd.h>
26+
27+
#define DHTPIN RPI_GPIO_P1_07
28+
#define MAXTIMINGS 100
29+
30+
#define DHT11 11
31+
#define DHT22 22
32+
#define AM2302 22
33+
34+
int readDHT(int type, int pin);
35+
36+
int main(int argc, char **argv)
37+
{
38+
if (!bcm2835_init())
39+
return 1;
40+
41+
if (argc != 2) {
42+
printf("usage: %s [11|22|2302]\n", argv[0]);
43+
return 2;
44+
}
45+
int type = 0;
46+
if (strcmp(argv[1], "11") == 0) type = DHT11;
47+
if (strcmp(argv[1], "22") == 0) type = DHT22;
48+
if (strcmp(argv[1], "2303") == 0) type = AM2302;
49+
if (type == 0) {
50+
printf("Select 11, 22, 2303 as type!\n");
51+
return 3;
52+
}
53+
54+
readDHT(type, DHTPIN);
55+
return 0;
56+
57+
} // main
58+
59+
60+
int bits[250], data[100];
61+
int bitidx = 0;
62+
63+
int readDHT(int type, int pin) {
64+
int counter = 0;
65+
int laststate = HIGH;
66+
int j=0;
67+
68+
// Set GPIO pin to output
69+
bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_OUTP);
70+
bcm2835_gpio_write(pin, HIGH);
71+
usleep(100);
72+
bcm2835_gpio_write(pin, LOW);
73+
usleep(20000);
74+
bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_INPT);
75+
76+
data[0] = data[1] = data[2] = data[3] = data[4] = 0;
77+
// read data!
78+
for (int i=0; i< MAXTIMINGS; i++) {
79+
counter = 0;
80+
while ( bcm2835_gpio_lev(pin) == laststate) {
81+
counter++;
82+
nanosleep(1); // overclocking might change this?
83+
if (counter == 100)
84+
break;
85+
}
86+
laststate = bcm2835_gpio_lev(pin);
87+
if (counter == 100) break;
88+
bits[bitidx++] = counter;
89+
90+
if ((i>3) && (i%2 == 0)) {
91+
// shove each bit into the storage bytes
92+
data[j/8] <<= 1;
93+
if (counter > 16)
94+
data[j/8] |= 1;
95+
j++;
96+
}
97+
}
98+
99+
100+
#ifdef DEBUG
101+
for (int i=3; i<bitidx; i+=2) {
102+
printf("bit %d: %d\n", i-3, bits[i]);
103+
printf("bit %d: %d (%d)\n", i-2, bits[i+1], bits[i+1] > 15);
104+
}
105+
#endif
106+
107+
printf("Data (%d): 0x%x 0x%x 0x%x 0x%x 0x%x\n", j, data[0], data[1], data[2], data[3], data[4]);
108+
109+
if ((j >= 39) &&
110+
(data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) ) {
111+
// yay!
112+
if (type == DHT11)
113+
printf("Temp = %d *C, Hum = %d \%\n", data[2], data[0]);
114+
if (type == DHT22) {
115+
float f, h;
116+
h = data[0] * 256 + data[1];
117+
h /= 10;
118+
119+
f = (data[2] & 0x7F)* 256; + data[3];
120+
f /= 10.0;
121+
if (data[2] & 0x80) f *= -1;
122+
printf("Temp = %.1f *C, Hum = %.1f \%\n", f, h);
123+
}
124+
return 1;
125+
}
126+
127+
return 0;
128+
}

0 commit comments

Comments
 (0)