-
Notifications
You must be signed in to change notification settings - Fork 11
/
sht1x.c
390 lines (346 loc) · 9.48 KB
/
sht1x.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// #############################################################################
// # ESP8266 WiFi Weather Sensor #
// #############################################################################
// # sht1x.c - Functions for SHT11/12/15 temperature / humidity sensor #
// #############################################################################
// # Version: 1.1 - Compiler: esp-open-sdk 1.5.2 (Linux) #
// # (c) 2015-2016 by Malte Pöggel - www.MALTEPOEGGEL.de - [email protected] #
// #############################################################################
// # This program is free software; you can redistribute it and/or modify it #
// # under the terms of the GNU General Public License as published by the #
// # Free Software Foundation; either version 3 of the License, #
// # or (at your option) any later version. #
// # #
// # This program is distributed in the hope that it will be useful, #
// # but WITHOUT ANY WARRANTY; without even the implied warranty of #
// # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. #
// # See the GNU General Public License for more details. #
// # #
// # You should have received a copy of the GNU General Public License along #
// # with this program; if not, see <http://www.gnu.org/licenses/>. #
// #############################################################################
#include <esp8266.h>
#include "sht1x.h"
// Initialize sensor
int ICACHE_FLASH_ATTR shtInit( struct shtdata* d )
{
int rtn = -1;
uint16_t status;
uint8_t crc_calc;
// Disable GPIO interrupts
ETS_GPIO_INTR_DISABLE();
// Set pin function to GPIO
PIN_FUNC_SELECT(SHT_DATA_MUX, SHT_DATA_FUNC);
PIN_FUNC_SELECT(SHT_CLK_MUX, SHT_CLK_FUNC);
// Data as open drain
GPIO_REG_WRITE(GPIO_PIN_ADDR(GPIO_ID_PIN(SHT_DATA_PIN)), GPIO_REG_READ(GPIO_PIN_ADDR(GPIO_ID_PIN(SHT_DATA_PIN))) | GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_ENABLE));
GPIO_REG_WRITE(GPIO_ENABLE_ADDRESS, GPIO_REG_READ(GPIO_ENABLE_ADDRESS) | (1 << SHT_DATA_PIN));
// Clock as open drain
GPIO_REG_WRITE(GPIO_PIN_ADDR(GPIO_ID_PIN(SHT_CLK_PIN)), GPIO_REG_READ(GPIO_PIN_ADDR(GPIO_ID_PIN(SHT_CLK_PIN))) | GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_ENABLE));
GPIO_REG_WRITE(GPIO_ENABLE_ADDRESS, GPIO_REG_READ(GPIO_ENABLE_ADDRESS) | (1 << SHT_CLK_PIN));
// Enable GPIO interrupts again
ETS_GPIO_INTR_ENABLE();
// Set ports
SHT_DATA_HIGH();
SHT_CLK_LOW();
// Reset data
d->temperature = 0;
d->humidity = 0;
// Reset sensor
shtReset();
// Check for sensor
shtStart();
shtWrite8(SHT_CMD_READ_STATUS_REG);
if(shtReadAck()!=0) goto endfunction;
shtRead16();
shtSendAck();
rtn = 0;
// Reduce power consumption through pullup - same port as i2c
SHT_CLK_HIGH();
endfunction:
return rtn;
}
// Read temperature & humidity
int ICACHE_FLASH_ATTR shtRead( struct shtdata* d )
{
int rtn = -1;
uint16_t t, h;
uint8_t temp;
#ifdef SHT_USE_CRC
uint8_t crc, crc_calc;
#endif
// Read temperature
shtStart();
shtWrite8(SHT_CMD_MEASURE_TEMPERATURE);
if(shtReadAck()!=0) goto endfunction;
temp=shtWaitForConversion(SHT_MEASUREMENT14_MS);
if(temp!=0) goto endfunction;
t = shtRead16();
#ifndef SHT_USE_CRC
shtSkipAck();
#else
shtSendAck();
crc = shtRead8();
shtSendAck();
crc_calc = shtCalculateCRC(0x00, SHT_CMD_MEASURE_TEMPERATURE, (uint8_t)(t>>8), (uint8_t)t);
if(crc!=crc_calc)
{
os_printf("SHT1x: checksum mismatch\n");
goto endfunction;
}
#endif
// Calculate temperature
d->temperature = shtConvertTemperature(t);
// Read humidity
shtStart();
shtWrite8(SHT_CMD_MEASURE_HUMIDITY);
if(shtReadAck()!=0) goto endfunction;
temp=shtWaitForConversion(SHT_MEASUREMENT12_MS);
if(temp!=0) goto endfunction;
h = shtRead16();
#ifndef SHT_USE_CRC
shtSkipAck();
#else
shtSendAck();
crc = shtRead8();
shtSendAck();
crc_calc = shtCalculateCRC(0x00, SHT_CMD_MEASURE_HUMIDITY, (uint8_t)(h>>8), (uint8_t)h);
if(crc!=crc_calc)
{
os_printf("SHT1x: checksum mismatch\n");
goto endfunction;
}
#endif
// Calculate compensated humidity
d->humidity = shtConvertHumidity(h, d->temperature);
// Done.
//os_printf("SHT1x tRaw=%d, hRaw=%d", t, h);
os_printf("SHT1x: t=%d, h=%d\n", d->temperature, d->humidity);
rtn=0;
endfunction:
// Reset port
SHT_CLK_HIGH();
SHT_DATA_HIGH();
return rtn;
}
// Bus reset
void ICACHE_FLASH_ATTR shtReset( void )
{
uint8_t i;
SHT_DATA_HIGH();
SHT_CLK_LOW();
shtDelay();
// Pulse 9 times to reset sensor interface
for(i=9; i>0; i--)
{
SHT_CLK_HIGH();
shtDelay();
SHT_CLK_LOW();
shtDelay();
}
}
// Start condition
void ICACHE_FLASH_ATTR shtStart( void )
{
SHT_DATA_HIGH();
SHT_CLK_LOW();
shtDelay();
SHT_CLK_HIGH();
shtDelay();
SHT_DATA_LOW();
shtDelay();
SHT_CLK_LOW();
shtDelay();
SHT_CLK_HIGH();
shtDelay();
SHT_DATA_HIGH();
shtDelay();
SHT_CLK_LOW();
shtDelay();
}
// Ack
void ICACHE_FLASH_ATTR shtSendAck( void )
{
SHT_DATA_LOW();
SHT_CLK_HIGH();
shtDelay();
SHT_CLK_LOW();
shtDelay();
SHT_DATA_HIGH();
}
// Skip ack
void ICACHE_FLASH_ATTR shtSkipAck( void )
{
SHT_DATA_HIGH();
SHT_CLK_HIGH();
shtDelay();
SHT_CLK_LOW();
shtDelay();
}
// Wait for ack from sensor
uint8_t ICACHE_FLASH_ATTR shtReadAck( void )
{
uint8_t i = SHT_DATA_VALID_NS;
SHT_DATA_HIGH();
// Wait for low pulse
while(SHT_READ() != 0)
{
if(--i<=0) return 1; // Timeout
}
SHT_CLK_HIGH();
shtDelay();
SHT_CLK_LOW();
shtDelay();
return 0;
}
// Read 8bit value
uint8_t ICACHE_FLASH_ATTR shtRead8( void )
{
uint8_t i;
uint8_t data = 0;
SHT_DATA_HIGH();
SHT_CLK_LOW();
for(i=8; i>0; i--)
{
SHT_CLK_HIGH();
shtDelay();
data<<=1;
data |= SHT_READ();
SHT_CLK_LOW();
shtDelay();
}
return data;
}
// Read 16bit value
uint16_t ICACHE_FLASH_ATTR shtRead16( void )
{
uint16_t data = 0;
data = shtRead8();
data<<=8;
shtSendAck();
data |= shtRead8();
return data;
}
// Write 8bit value
void ICACHE_FLASH_ATTR shtWrite8( uint8_t data )
{
uint8_t i;
SHT_DATA_HIGH();
SHT_CLK_LOW();
for(i=8; i>0; i--)
{
if(data&0x80)
{
SHT_DATA_HIGH();
} else {
SHT_DATA_LOW();
}
SHT_CLK_HIGH();
shtDelay();
SHT_CLK_LOW();
shtDelay();
data<<=1;
}
SHT_DATA_HIGH();
}
// Wait until sensor finishes ADC conversion
uint8_t ICACHE_FLASH_ATTR shtWaitForConversion( uint16_t time )
{
SHT_DATA_HIGH();
SHT_CLK_LOW();
while(SHT_READ() != 0)
{
system_soft_wdt_feed();
os_delay_us(1000);
if(--time<=0) return 1; // Timeout
}
return 0;
}
// Delay between pulses
void ICACHE_FLASH_ATTR shtDelay( void )
{
os_delay_us(SHT_TSCK_NS);
}
// Will return temperature x10
#ifdef USE_FLOAT_CALC
int16_t ICACHE_FLASH_ATTR shtConvertTemperature( uint16_t temperature_raw )
{
float temperature;
temperature = SHT_TEMPERATURE_D1 + SHT_TEMPERATURE_D2 * (float)temperature_raw;
return (uint16_t)(temperature*10);
}
#else
int16_t ICACHE_FLASH_ATTR shtConvertTemperature( uint16_t temperature_raw )
{
int16_t temperature;
temperature = SHT_TEMPERATURE_D1*100;
temperature += SHT_TEMPERATURE_D2*100 * temperature_raw;
temperature = temperature/10; // (/100 *10)
return temperature;
}
#endif
// Will return humidity x10
#ifdef USE_FLOAT_CALC
int16_t ICACHE_FLASH_ATTR shtConvertHumidity( uint16_t humidity_raw, int16_t temperature )
{
int16_t humidity_compensated = 0;
float linear, humidity;
linear = SHT_HUMIDITY_C1;
linear += SHT_HUMIDITY_C2 * humidity_raw;
linear += SHT_HUMIDITY_C3 * humidity_raw * humidity_raw;
humidity = (temperature-25*10)*(SHT_HUMIDITY_T1 + SHT_HUMIDITY_T2 * (float)humidity_raw);
humidity_compensated += (int16_t)humidity + (int16_t)(linear*10);
if(humidity_compensated>1000) humidity_compensated = 1000;
if(humidity_compensated<0) humidity_compensated = 0;
return humidity_compensated;
}
#else
int16_t ICACHE_FLASH_ATTR shtConvertHumidity( uint16_t humidity_raw, int16_t temperature )
{
int32_t humidity;
int32_t linear;
int32_t linear2;
// Calculate linear part x10
linear = SHT_HUMIDITY_C1*10000;
linear += SHT_HUMIDITY_C2*10000 * humidity_raw;
linear2 = SHT_HUMIDITY_C3*10000000 * humidity_raw*humidity_raw;
linear += linear2 / 1000;
linear = linear / 1000;
// Calculate humidity x10
humidity = SHT_HUMIDITY_T1*10000;
humidity += SHT_HUMIDITY_T2*10000 * humidity_raw;
humidity = humidity/10 * (temperature-250);
humidity = humidity / 1000;
// Final calculation
humidity += linear;
if(humidity>1000) humidity = 1000;
if(humidity<0) humidity = 0;
return humidity;
}
#endif
#ifdef SHT_USE_CRC
// Used for CRC check
uint8_t ICACHE_FLASH_ATTR shtReverseByte( uint8_t a )
{
uint8_t i, b = 0;
for(i=8; i>0; i--)
{
b<<=1;
b |= (a&0x01);
a>>=1;
}
return b;
}
// Check CRC of sensor data
uint8_t ICACHE_FLASH_ATTR shtCalculateCRC( uint8_t start, uint8_t byte0, uint8_t byte1, uint8_t byte2 )
{
// Start value: data of status register (reversed!)
uint8_t crc = shtReverseByte(start);
// Now every value is XOR'd with the previous CRC value
crc = sht_crc_table[byte0^crc];
crc = sht_crc_table[byte1^crc];
crc = sht_crc_table[byte2^crc];
// Reverse value again
return shtReverseByte(crc);
}
#endif