-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_dht11.c
100 lines (87 loc) · 2.35 KB
/
my_dht11.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
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
void d_while(int pinNumber){
int e_count=1000;
while(digitalRead(pinNumber)&&e_count){
e_count--;
}
}
void nd_while(int pinNumber){
int e_count=1000;
while(!digitalRead(pinNumber)&&e_count){
e_count--;
}
}
void hello(int a){
printf("%d",a);
}
unsigned long init_dht11(int pinNumber){ // 初始化dht11
if (-1 == wiringPiSetup()) {
printf("Setup wiringPi failed!");
return 1;
}
pinMode(pinNumber, OUTPUT); // set mode to output
digitalWrite(pinNumber, 1); // output a high level
return 0;
}
unsigned long readSensorData(int pinNumber)
{
unsigned long databuf;
unsigned char crc;
unsigned char i;
int HIGH_TIME = 32;
pinMode(pinNumber, OUTPUT); // set mode to output
digitalWrite(pinNumber, 0); // output a high level
delay(25);
digitalWrite(pinNumber, 1); // output a low level
pinMode(pinNumber, INPUT); // set mode to input
pullUpDnControl(pinNumber, PUD_UP);
delayMicroseconds(27);
if (digitalRead(pinNumber) == 0) //SENSOR ANS
{
// while (!digitalRead(pinNumber))
// ; //wait to high
nd_while(pinNumber);
for (i = 0; i < 32; i++)
{
// while (digitalRead(pinNumber))
// ; //data clock start
// while (!digitalRead(pinNumber))
// ; //data start
d_while(pinNumber);
nd_while(pinNumber);
delayMicroseconds(HIGH_TIME);
databuf = databuf * 2;
if (digitalRead(pinNumber) == 1) //1
{
databuf++;
}
}
for (i = 0; i < 8; i++)
{
// while (digitalRead(pinNumber))
// ; //data clock start
// while (!digitalRead(pinNumber))
// ; //data start
d_while(pinNumber);
nd_while(pinNumber);
delayMicroseconds(HIGH_TIME);
crc *= 2;
if (digitalRead(pinNumber) == 1) //1
{
crc++;
}
}
return databuf;
}
else
{
return 0;
}
}
void preRead(int pinNumber){
pinMode(pinNumber, OUTPUT); // set mode to output
digitalWrite(pinNumber, 1); // output a high level
// delay(3000);
}