sht11.c
8.21 KB
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
/*
* Copyright 2009, Freie Universitaet Berlin (FUB). All rights reserved.
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup drivers_sht11
* @brief Driver for the Sensirion SHT11 humidity and temperature sensor
* @{
*
* @file
* @brief SHT11 Device Driver
*
* @version $Revision: 2396 $
*
* @note $Id: sht11.c 2396 2010-07-06 15:12:35Z ziegert $
* @}
*/
#include <stdio.h>
#include <stdint.h>
#include "xtimer.h"
#include "mutex.h"
#include "sht11.h"
#include "sht11-board.h"
#include "bitarithm.h"
float sht11_temperature_offset;
/**
* @brief Perform measurement
*
* @param p_value Measured value (14 or 12 bit -> 2 bytes)
* @param p_checksum Checksum of measurement
* @param mode The requestested measurement mode: temperature or humidity
*
* @return 1 on success, 0 otherwise
*/
static uint8_t measure(uint8_t *p_value, uint8_t *p_checksum, uint8_t mode);
/**
* @brief Write one byte
*
* @param value The value to write
*
* @return 1 for acknowledged write, 0 otherwise
*/
static uint8_t write_byte(uint8_t value);
/**
* @brief Read ony byte
*
* @param ack Set if the data read should be acknowledged
*
* @return The read byte
*/
static uint8_t read_byte(uint8_t ack);
/**
* @brief Communication reset
*/
static void connection_reset(void);
/**
* @brief Send start of transmision sequence
*/
static void transmission_start(void);
/**
* @brief Toggle the clock line
*/
static inline void clk_signal(void);
/* mutex for exclusive measurement operation */
mutex_t sht11_mutex = MUTEX_INIT;
/*---------------------------------------------------------------------------*/
static inline void clk_signal(void)
{
SHT11_SCK_HIGH;
xtimer_usleep(SHT11_CLK_WAIT);
SHT11_SCK_LOW;
xtimer_usleep(SHT11_CLK_WAIT);
}
/*---------------------------------------------------------------------------*/
static uint8_t write_byte(uint8_t value)
{
uint8_t i;
uint8_t ack;
SHT11_DATA_OUT;
/* send value bit by bit to sht11 */
for (i = 0; i < 8; i++) {
if (value & BIT7) {
SHT11_DATA_HIGH;
xtimer_usleep(SHT11_DATA_WAIT);
}
else {
SHT11_DATA_LOW;
xtimer_usleep(SHT11_DATA_WAIT);
}
/* trigger clock signal */
clk_signal();
/* shift value to write next bit */
value = value << 1;
}
/* wait for ack */
SHT11_DATA_IN;
xtimer_usleep(SHT11_CLK_WAIT);
ack = SHT11_DATA;
clk_signal();
return ack;
}
/*---------------------------------------------------------------------------*/
static uint8_t read_byte(uint8_t ack)
{
uint8_t i;
uint8_t value = 0;
SHT11_DATA_IN;
xtimer_usleep(SHT11_DATA_WAIT);
/* read value bit by bit */
for (i = 0; i < 8; i++) {
value = value << 1;
SHT11_SCK_HIGH;
xtimer_usleep(SHT11_CLK_WAIT);
if (SHT11_DATA) {
/* increase data by one when DATA is high */
value++;
}
SHT11_SCK_LOW;
xtimer_usleep(SHT11_CLK_WAIT);
}
/* send ack if necessary */
SHT11_DATA_OUT;
if (ack) {
SHT11_DATA_LOW;
xtimer_usleep(SHT11_DATA_WAIT);
}
else {
SHT11_DATA_HIGH;
xtimer_usleep(SHT11_DATA_WAIT);
}
clk_signal();
/* release data line */
SHT11_DATA_IN;
return value;
}
/*---------------------------------------------------------------------------*/
static void transmission_start(void)
{
/* _____ ________
DATA: |_______|
___ ___
SCK : ___| |___| |______
*/
SHT11_DATA_OUT;
/* set initial state */
SHT11_DATA_HIGH;
xtimer_usleep(SHT11_DATA_WAIT);
SHT11_SCK_LOW;
xtimer_usleep(SHT11_CLK_WAIT);
SHT11_SCK_HIGH;
xtimer_usleep(SHT11_CLK_WAIT);
SHT11_DATA_LOW;
xtimer_usleep(SHT11_DATA_WAIT);
SHT11_SCK_LOW;
xtimer_usleep(SHT11_CLK_WAIT);
SHT11_SCK_HIGH;
xtimer_usleep(SHT11_CLK_WAIT);
SHT11_DATA_HIGH;
xtimer_usleep(SHT11_DATA_WAIT);
SHT11_SCK_LOW;
xtimer_usleep(SHT11_CLK_WAIT);
}
/*---------------------------------------------------------------------------*/
static void connection_reset(void)
{
/* _____________________________________________________ ____
DATA: |_______|
_ _ _ _ _ _ _ _ _ ___ ___
SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |__
*/
uint8_t i;
SHT11_DATA_HIGH;
xtimer_usleep(SHT11_DATA_WAIT);
SHT11_SCK_LOW;
xtimer_usleep(SHT11_CLK_WAIT);
for (i = 0; i < 9; i++) {
clk_signal();
}
transmission_start();
}
/*---------------------------------------------------------------------------*/
static uint8_t measure(uint8_t *p_value, uint8_t *p_checksum, uint8_t mode)
{
uint8_t error = 0;
uint8_t ack = 1;
uint16_t i;
transmission_start();
error = write_byte(mode);
xtimer_usleep(1000);
/* wait untile sensor has finished measurement or timeout */
for (i = 0; (i < SHT11_MEASURE_TIMEOUT) && (!error); i++) {
ack = SHT11_DATA;
if (!ack) {
break;
}
xtimer_usleep(1000);
}
error += ack;
/* read MSB */
*(p_value + 1) = read_byte(SHT11_ACK);
/* read LSB */
*(p_value) = read_byte(SHT11_ACK);
/* read checksum */
*p_checksum = read_byte(SHT11_NO_ACK);
return (!error);
}
/*---------------------------------------------------------------------------*/
void sht11_init(void)
{
sht11_temperature_offset = 0;
SHT11_INIT;
xtimer_usleep(11 * 1000);
}
/*---------------------------------------------------------------------------*/
uint8_t sht11_read_status(uint8_t *p_value, uint8_t *p_checksum)
{
uint8_t error = 0;
transmission_start();
error |= write_byte(SHT11_STATUS_REG_R);
*p_value = read_byte(SHT11_ACK);
*p_checksum = read_byte(SHT11_NO_ACK);
return (!error);
}
/*---------------------------------------------------------------------------*/
uint8_t sht11_write_status(uint8_t *p_value)
{
uint8_t error = 0;
transmission_start();
error += write_byte(SHT11_STATUS_REG_W);
error += write_byte(*p_value);
return (!error);
}
/*---------------------------------------------------------------------------*/
uint8_t sht11_read_sensor(sht11_val_t *value, sht11_mode_t mode)
{
uint8_t error = 0;
uint8_t checksum;
uint16_t humi_int, temp_int;
/* Temperature arithmetic where S0(T) is read value
* T = D1 + D2 * S0(T) */
const float D1 = -39.6;
const float D2 = 0.01;
/* Arithmetic for linear humdity where S0(RH) is read value
* HL = C1 + C2 * S0(RH) + C3 * SO(RH)^2 */
const float C1 = -4.0;
const float C2 = +0.0405;
const float C3 = -0.0000028;
/* Arithmetic for temperature compesated relative humdity
* HT = (T-25) * ( T1 + T2 * SO(RH) ) + HL */
const float T1 = +0.01;
const float T2 = +0.00008;
/* check for valid buffer */
if (value == NULL) {
return 0;
}
value->temperature = 0;
value->relhum = 0;
value->relhum_temp = 0;
mutex_lock(&sht11_mutex);
connection_reset();
/* measure humidity */
if (mode & HUMIDITY) {
error += (!measure((uint8_t *) &humi_int, &checksum, SHT11_MEASURE_HUMI));
}
/* measure temperature */
if (mode & TEMPERATURE) {
error += (!measure((uint8_t *) &temp_int, &checksum, SHT11_MEASURE_TEMP));
}
/* break on error */
if (error != 0) {
connection_reset();
mutex_unlock(&sht11_mutex);
return 0;
}
if (mode & TEMPERATURE) {
value->temperature = D1 + (D2 * ((float) temp_int)) + sht11_temperature_offset;
}
if (mode & HUMIDITY) {
value->relhum = C1 + (C2 * ((float) humi_int)) + (C3 * ((float) humi_int) * ((float) humi_int));
if (mode & TEMPERATURE) {
value->relhum_temp = (value->temperature - 25) * (T1 + (T2 * (float) humi_int)) + value->relhum;
}
}
mutex_unlock(&sht11_mutex);
return 1;
}