CLEON
Version 1
Cloud-Offloaded GPS Receiver
Main Page
Related Pages
Data Structures
Files
File List
Globals
sys_sensors.c
Go to the documentation of this file.
1
7
#include "
cleon_conf.h
"
8
#include "
app_define.h
"
9
#include "
sys_define.h
"
10
#include "
hal_define.h
"
11
#include "
fs_define.h
"
12
16
/*----------------------------------------------------------------------------*/
24
void
SYS_SENSORS_S1087_Init
(
void
)
25
{
26
SENSOR_S1087_PORT
(
SEL
) |=
BV
(
SENSOR_S1087_PIN
);
27
SENSOR_S1087_PORT
(
DIR
) |=
BV
(
SENSOR_S1087_PIN
);
28
SENSOR_S1087_PORT
(OUT) &= ~
BV
(
SENSOR_S1087_PIN
);
29
}
30
31
/*----------------------------------------------------------------------------*/
39
unsigned
int
SYS_SENSORS_S1087_Read
(
void
)
40
{
41
double
dLight = 0;
42
43
// Sensor value (in 12-bit ADC-representation)
44
dLight = (double) ADC12MEM0;
45
46
// Voltage difference (in 12-bit ADC-representation)
47
dLight = 4096 - dLight;
48
49
// For S1087, it gives 1nA upon sensing 1 lux
50
// It means that 0.5mV appears across 500k resistor
51
// Therefore, light intensity (lux) = Vsense (V) * 1 (lux) / 0.5m (V)
52
// That is, Vsense * 2000;
53
dLight = dLight / 4096 * 2.88 * 2000;
54
55
#if (DEBUG_MODE) && (DUMP_SENSOR_VALUE_VIA_USB)
56
SYS_USB_Printf
(
"Light1(PAR): %.4d \r\n"
, (
unsigned
int
) dLight);
57
#endif
58
59
return
(
unsigned
int
) dLight;
60
}
61
65
/*----------------------------------------------------------------------------*/
73
void
SYS_SENSORS_S108701_Init
(
void
)
74
{
75
SENSOR_S108701_PORT
(
SEL
) |=
BV
(
SENSOR_S108701_PIN
);
76
SENSOR_S108701_PORT
(
DIR
) |=
BV
(
SENSOR_S108701_PIN
);
77
SENSOR_S108701_PORT
(OUT) &= ~
BV
(
SENSOR_S108701_PIN
);
78
}
79
80
/*----------------------------------------------------------------------------*/
88
unsigned
int
SYS_SENSORS_S108701_Read
(
void
)
89
{
90
double
dLight = 0;
91
92
// Sensor value (in 12-bit ADC-representation)
93
dLight = (double) ADC12MEM1;
94
95
// Voltage difference (in 12-bit ADC-representation) 3890
96
dLight = 4096 - dLight;
97
98
// For S1087-01, it gives 10nA upon sensing 1 lux
99
// It means that 5mV appears across 500k resistor
100
// Therefore, light intensity (lux) = Vsense (V) * 1 (lux) / 5m (V)
101
// That is, Vsense * 200;
102
dLight = dLight / 4096 * 2.88 * 200;
103
104
// Manually found calibration factor
105
dLight = dLight * 8;
106
107
#if (DEBUG_MODE) && (DUMP_SENSOR_VALUE_VIA_USB)
108
SYS_USB_Printf
(
"Light2(TSR): %.4d \r\n"
, (
unsigned
int
)dLight);
109
#endif
110
111
return
(
unsigned
int
) dLight;
112
}
113
114
118
/*----------------------------------------------------------------------------*/
126
void
SYS_SENSORS_Init
(
void
)
127
{
128
SYS_SENSORS_SHT11_Init
();
129
SYS_SENSORS_S1087_Init
();
130
SYS_SENSORS_S108701_Init
();
131
}
132
133
/*----------------------------------------------------------------------------*/
142
void
SYS_SENSORS_SHT11_Init
(
void
)
143
{
144
SYS_SENSORS_SHT11_Off
();
145
}
146
147
/*----------------------------------------------------------------------------*/
155
unsigned
int
SYS_SENSORS_SHT11_ReadTemperature
(
void
)
156
{
157
unsigned
int
uiTemp = 0;
158
159
// Read temperature in Fahrenheit
160
uiTemp = (
unsigned
int) (-40.00 + 0.018 *
SYS_SENSORS_SHT11_Command
(
MEASURE_TEMP
));
161
162
#if (DEBUG_MODE) && (DUMP_SENSOR_VALUE_VIA_USB)
163
SYS_USB_Printf
(
"Temp(F) : %.4d \r\n"
, (
unsigned
int
) uiTemp);
164
#endif
165
166
return
uiTemp;
167
}
168
169
/*----------------------------------------------------------------------------*/
177
unsigned
int
SYS_SENSORS_SHT11_ReadHumidity
(
void
)
178
{
179
unsigned
int
uiRelativeHumidity = 0;
180
unsigned
int
uiHumidity = 0;
181
182
uiRelativeHumidity =
SYS_SENSORS_SHT11_Command
(
MEASURE_HUMI
);
183
uiHumidity = (
unsigned
int)( -4 + 0.0405 * uiRelativeHumidity - 2.8e-6 * (uiRelativeHumidity * uiRelativeHumidity));
184
185
#if (DEBUG_MODE) && (DUMP_SENSOR_VALUE_VIA_USB)
186
SYS_USB_Printf
(
"Humidity : %.4d \r\n"
, (
unsigned
int
) uiHumidity);
187
#endif
188
189
return
uiHumidity;
190
}
191
192
/*----------------------------------------------------------------------------*/
200
void
SYS_SENSORS_SHT11_On
(
void
)
201
{
202
SENSOR_SHT11_PORT
(OUT) |=
BV
(
SENSOR_SHT11_PWR_PIN
);
203
SENSOR_SHT11_PORT
(OUT) &= ~(
BV
(
SENSOR_SHT11_SDA_PIN
) |
BV
(
SENSOR_SHT11_SCL_PIN
));
// Set pull-up register for DATA and SCK pins
204
SENSOR_SHT11_PORT
(
DIR
) |=
BV
(
SENSOR_SHT11_PWR_PIN
) |
BV
(
SENSOR_SHT11_SCL_PIN
);
// SHT11 power on & Set SCK pin as output
205
206
SHT11_TIMING_DELAY
(20);
// wait about 20ms for SHT11 stabilization
207
}
208
209
/*----------------------------------------------------------------------------*/
217
void
SYS_SENSORS_SHT11_Off
(
void
)
218
{
219
SENSOR_SHT11_PORT
(OUT) &= ~
BV
(
SENSOR_SHT11_PWR_PIN
);
220
SENSOR_SHT11_PORT
(OUT) &= ~(
BV
(
SENSOR_SHT11_SDA_PIN
) |
BV
(
SENSOR_SHT11_SCL_PIN
));
221
SENSOR_SHT11_PORT
(
DIR
) |=
BV
(
SENSOR_SHT11_PWR_PIN
) |
BV
(
SENSOR_SHT11_SCL_PIN
);
222
}
223
224
/*----------------------------------------------------------------------------*/
232
void
SYS_SENSORS_SHT11_Start
(
void
)
233
{
234
SHT11_SDA_SET_TO_INPUT_PIN
();
235
SHT11_SCL_LOW
();
236
SHT11_TIMING_DELAY
(1);
237
SHT11_SCL_HIGH
();
238
SHT11_TIMING_DELAY
(1);
239
SHT11_SDA_SET_TO_OUTPUT_PIN
();
240
SHT11_TIMING_DELAY
(1);
241
SHT11_SCL_LOW
();
242
SHT11_TIMING_DELAY
(1);
243
SHT11_SCL_HIGH
();
244
SHT11_TIMING_DELAY
(1);
245
SHT11_SDA_SET_TO_INPUT_PIN
();
246
SHT11_TIMING_DELAY
(1);
247
SHT11_SCL_LOW
();
248
}
249
250
/*----------------------------------------------------------------------------*/
258
void
SYS_SENSORS_SHT11_Reset
(
void
)
259
{
260
SHT11_SDA_SET_TO_INPUT_PIN
();
261
SHT11_SCL_LOW
();
262
for
(
int
i = 0; i < 9 ; i++) {
263
SHT11_SCL_HIGH
();
264
SHT11_TIMING_DELAY
(1);
265
SHT11_SCL_LOW
();
266
SHT11_TIMING_DELAY
(1);
267
}
268
SYS_SENSORS_SHT11_Start
();
269
}
270
271
/*----------------------------------------------------------------------------*/
279
bool
SYS_SENSORS_SHT11_Write
(
unsigned
char
ucCommand)
280
{
281
bool
bAck;
282
283
for
(
int
i = 0; i < 8; i++, ucCommand <<= 1) {
284
if
(ucCommand & 0x80) {
285
SHT11_SDA_SET_TO_INPUT_PIN
();
286
}
else
{
287
SHT11_SDA_SET_TO_OUTPUT_PIN
();
288
}
289
SHT11_SCL_HIGH
();
290
SHT11_TIMING_DELAY
(1);
291
SHT11_SCL_LOW
();
292
SHT11_TIMING_DELAY
(1);
293
}
294
295
SHT11_SDA_SET_TO_INPUT_PIN
();
296
SHT11_SCL_HIGH
();
297
SHT11_TIMING_DELAY
(1);
298
299
bAck = !
SHT11_IS_SDA_HIGH
;
300
301
SHT11_SCL_LOW
();
302
303
return
bAck;
304
}
305
306
/*----------------------------------------------------------------------------*/
314
unsigned
char
SYS_SENSORS_SHT11_Read
(
bool
bAckRequested)
315
{
316
unsigned
char
ucReceivedData = 0x00;
317
318
SHT11_SDA_SET_TO_INPUT_PIN
();
319
320
for
(
int
i = 0; i < 8; i++) {
321
ucReceivedData <<= 1;
322
SHT11_SCL_HIGH
();
323
SHT11_TIMING_DELAY
(1);
324
if
(
SHT11_IS_SDA_HIGH
) {
325
ucReceivedData |= 0x1;
326
}
327
SHT11_SCL_LOW
();
328
SHT11_TIMING_DELAY
(1);
329
}
330
331
if
(bAckRequested) {
332
SHT11_SDA_SET_TO_OUTPUT_PIN
();
333
}
334
335
SHT11_SCL_HIGH
();
336
SHT11_TIMING_DELAY
(1);
337
SHT11_SCL_LOW
();
338
339
SHT11_SDA_SET_TO_INPUT_PIN
();
340
341
return
ucReceivedData;
342
}
343
344
/*----------------------------------------------------------------------------*/
352
unsigned
int
SYS_SENSORS_SHT11_Command
(
unsigned
char
ucCommand)
353
{
354
unsigned
long
n;
355
356
SYS_SENSORS_SHT11_Start
();
357
if
(!
SYS_SENSORS_SHT11_Write
(ucCommand)) {
358
SYS_SENSORS_SHT11_Reset
();
359
return
_FAIL_
;
360
}
else
{
361
for
(n = 0; n < 250000; n++) {
362
if
(!
SHT11_IS_SDA_HIGH
) {
363
#pragma diag_suppress = Pe550
364
unsigned
char
t0, t1, rcrc;
365
t0 =
SYS_SENSORS_SHT11_Read
(1);
366
t1 =
SYS_SENSORS_SHT11_Read
(1);
367
rcrc =
SYS_SENSORS_SHT11_Read
(0);
368
369
return
(t0 << 8) | t1;
370
}
371
}
372
}
373
#pragma diag_suppress = Pe940
374
}
375
sys
sys_sensors.c
Generated on Tue May 28 2013 15:17:19 for CLEON by
1.8.2