CLEON  Version 1
Cloud-Offloaded GPS Receiver
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 /*----------------------------------------------------------------------------*/
25 {
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 /*----------------------------------------------------------------------------*/
74 {
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 /*----------------------------------------------------------------------------*/
127 {
131 }
132 
133 /*----------------------------------------------------------------------------*/
143 {
145 }
146 
147 /*----------------------------------------------------------------------------*/
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 /*----------------------------------------------------------------------------*/
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 /*----------------------------------------------------------------------------*/
201 {
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 /*----------------------------------------------------------------------------*/
218 {
222 }
223 
224 /*----------------------------------------------------------------------------*/
233 {
235  SHT11_SCL_LOW();
237  SHT11_SCL_HIGH();
241  SHT11_SCL_LOW();
243  SHT11_SCL_HIGH();
247  SHT11_SCL_LOW();
248 }
249 
250 /*----------------------------------------------------------------------------*/
259 {
261  SHT11_SCL_LOW();
262  for(int i = 0; i < 9 ; i++) {
263  SHT11_SCL_HIGH();
265  SHT11_SCL_LOW();
267  }
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) {
286  }else{
288  }
289  SHT11_SCL_HIGH();
291  SHT11_SCL_LOW();
293  }
294 
296  SHT11_SCL_HIGH();
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 
319 
320  for(int i = 0; i < 8; i++) {
321  ucReceivedData <<= 1;
322  SHT11_SCL_HIGH();
324  if(SHT11_IS_SDA_HIGH) {
325  ucReceivedData |= 0x1;
326  }
327  SHT11_SCL_LOW();
329  }
330 
331  if(bAckRequested) {
333  }
334 
335  SHT11_SCL_HIGH();
337  SHT11_SCL_LOW();
338 
340 
341  return ucReceivedData;
342 }
343 
344 /*----------------------------------------------------------------------------*/
352 unsigned int SYS_SENSORS_SHT11_Command(unsigned char ucCommand)
353 {
354  unsigned long n;
355 
357  if(!SYS_SENSORS_SHT11_Write(ucCommand)) {
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