1
+ #include " DFRobot_SHT20.h"
2
+
3
+ void DFRobot_SHT20::initSHT20 (TwoWire &wirePort)
4
+ {
5
+ i2cPort = &wirePort;
6
+ i2cPort->begin ();
7
+ }
8
+
9
+ uint16_t DFRobot_SHT20::readValue (byte cmd)
10
+ {
11
+ i2cPort->beginTransmission (SLAVE_ADDRESS);
12
+ i2cPort->write (cmd);
13
+ i2cPort->endTransmission ();
14
+ byte toRead;
15
+ byte counter;
16
+ for (counter = 0 , toRead = 0 ; counter < MAX_COUNTER && toRead != 3 ; counter++){
17
+ delay (DELAY_INTERVAL);
18
+ toRead = i2cPort->requestFrom (SLAVE_ADDRESS, 3 );
19
+ }
20
+ if (counter == MAX_COUNTER){
21
+ return (ERROR_I2C_TIMEOUT);
22
+ }
23
+ byte msb, lsb, checksum;
24
+ msb = i2cPort->read ();
25
+ lsb = i2cPort->read ();
26
+ checksum = i2cPort->read ();
27
+ uint16_t rawValue = ((uint16_t ) msb << 8 ) | (uint16_t ) lsb;
28
+ if (checkCRC (rawValue, checksum) != 0 ){
29
+ return (ERROR_BAD_CRC);
30
+ }
31
+ return rawValue & 0xFFFC ;
32
+ }
33
+
34
+ float DFRobot_SHT20::readHumidity (void )
35
+ {
36
+ uint16_t rawHumidity = readValue (TRIGGER_HUMD_MEASURE_NOHOLD);
37
+ if (rawHumidity == ERROR_I2C_TIMEOUT || rawHumidity == ERROR_BAD_CRC){
38
+ return (rawHumidity);
39
+ }
40
+ float tempRH = rawHumidity * (125.0 / 65536.0 );
41
+ float rh = tempRH - 6.0 ;
42
+ return (rh);
43
+ }
44
+
45
+ float DFRobot_SHT20::readTemperature (void )
46
+ {
47
+ uint16_t rawTemperature = readValue (TRIGGER_TEMP_MEASURE_NOHOLD);
48
+ if (rawTemperature == ERROR_I2C_TIMEOUT || rawTemperature == ERROR_BAD_CRC){
49
+ return (rawTemperature);
50
+ }
51
+ float tempTemperature = rawTemperature * (175.72 / 65536.0 );
52
+ float realTemperature = tempTemperature - 46.85 ;
53
+ return (realTemperature);
54
+ }
55
+
56
+ void DFRobot_SHT20::setResolution (byte resolution)
57
+ {
58
+ byte userRegister = readUserRegister ();
59
+ userRegister &= B01111110;
60
+ resolution &= B10000001;
61
+ userRegister |= resolution;
62
+ writeUserRegister (userRegister);
63
+ }
64
+
65
+ byte DFRobot_SHT20::readUserRegister (void )
66
+ {
67
+ byte userRegister;
68
+ i2cPort->beginTransmission (SLAVE_ADDRESS);
69
+ i2cPort->write (READ_USER_REG);
70
+ i2cPort->endTransmission ();
71
+ i2cPort->requestFrom (SLAVE_ADDRESS, 1 );
72
+ userRegister = i2cPort->read ();
73
+ return (userRegister);
74
+ }
75
+
76
+ void DFRobot_SHT20::writeUserRegister (byte val)
77
+ {
78
+ i2cPort->beginTransmission (SLAVE_ADDRESS);
79
+ i2cPort->write (WRITE_USER_REG);
80
+ i2cPort->write (val);
81
+ i2cPort->endTransmission ();
82
+ }
83
+
84
+ byte DFRobot_SHT20::checkCRC (uint16_t message_from_sensor, uint8_t check_value_from_sensor)
85
+ {
86
+ uint32_t remainder = (uint32_t )message_from_sensor << 8 ;
87
+ remainder |= check_value_from_sensor;
88
+ uint32_t divsor = (uint32_t )SHIFTED_DIVISOR;
89
+ for (int i = 0 ; i < 16 ; i++){
90
+ if (remainder & (uint32_t )1 << (23 - i)){
91
+ remainder ^= divsor;
92
+ }
93
+ divsor >>= 1 ;
94
+ }
95
+ return (byte)remainder;
96
+ }
97
+
98
+ void DFRobot_SHT20::showReslut (const char *prefix, int val)
99
+ {
100
+ Serial.print (prefix);
101
+ if (val){
102
+ Serial.println (" yes" );
103
+ }else {
104
+ Serial.println (" no" );
105
+ }
106
+ }
107
+
108
+ void DFRobot_SHT20::checkSHT20 (void )
109
+ {
110
+ byte reg = readUserRegister ();
111
+ showReslut (" End of battery: " , reg & USER_REGISTER_END_OF_BATTERY);
112
+ showReslut (" Heater enabled: " , reg & USER_REGISTER_HEATER_ENABLED);
113
+ showReslut (" Disable OTP reload: " , reg & USER_REGISTER_DISABLE_OTP_RELOAD);
114
+ }
0 commit comments