-
Notifications
You must be signed in to change notification settings - Fork 5
/
ATH20.cpp
96 lines (73 loc) · 1.73 KB
/
ATH20.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
// AHT20 ARDUINO LIBRARY
#include "AHT20.h"
void AHT20::begin()
{
Wire.begin();
Wire.beginTransmission(0x38); // transmit to device #8
Wire.write(0xBE);
Wire.endTransmission(); // stop transmitting
}
bool AHT20::startSensor()
{
Wire.beginTransmission(0x38); // transmit to device #8
Wire.write(0xac);
Wire.write(0x33);
Wire.write(0x00);
Wire.endTransmission(); // stop transmitting
unsigned long timer_s = millis();
while(1)
{
if(millis()-timer_s > 200) return 0; // time out
Wire.requestFrom(0x38, 1);
while(Wire.available())
{
unsigned char c = Wire.read();
if(c&0x80 != 0)return 1; // busy
}
delay(20);
}
}
bool AHT20::getSensor(float *h, float *t)
{
startSensor();
Wire.requestFrom(0x38, 6);
unsigned char str[6] ={0,};
int index = 0;
while (Wire.available())
{
str[index++] = Wire.read(); // receive a byte as character
}
if(index == 0 )return 0;
if(str[0] & 0x80)return 0;
unsigned long __humi = 0;
unsigned long __temp = 0;
__humi = str[1];
__humi <<= 8;
__humi += str[2];
__humi <<= 4;
__humi += str[3] >> 4;
*h = (float)__humi/1048576.0;
__temp = str[3]&0x0f;
__temp <<=8;
__temp += str[4];
__temp <<=8;
__temp += str[5];
*t = (float)__temp/1048576.0*200.0-50.0;
return 1;
}
bool AHT20::getTemperature(float *t)
{
float __t, __h;
int ret = getSensor(&__h, &__t);
if(0 == ret)return 0;
*t = __t;
return 1;
}
bool AHT20::getHumidity(float *h)
{
float __t, __h;
int ret = getSensor(&__h, &__t);
if(0 == ret)return 0;
*h = __h;
return 1;
}