Netcruzer Library API  V2.03
 All Data Structures Files Functions Variables Typedefs Enumerations Macros Groups Pages
nz_sensDht.h
Go to the documentation of this file.
1 
94 #ifndef NZ_SENS_DHT_H
95 #define NZ_SENS_DHT_H
96 
97 // Number of Sensors, default is 1
98 #if !defined(SENS_DHT_NUMBER_OF_SENSORS)
99 #define SENS_DHT_NUMBER_OF_SENSORS ( 0 )
100 #endif
101 
102 //Disable all code if SENS_DHT_NUMBER_OF_SENSORS = 0
103 #if (SENS_DHT_NUMBER_OF_SENSORS > 0)
104 
106 #if !defined(SENS_DHT_PIN_ARRAY_SIZE)
107 #define SENS_DHT_PIN_ARRAY_SIZE ( 256 )
108 #endif
109 
110 // Sensor Type, 1=DHT11, 2=DHT22
111 #if !defined(SENS_DHT_TYPE)
112 #define SENS_DHT_TYPE ( 2 )
113 #endif
114 
115 #if (SENS_DHT_PIN_ARRAY_SIZE!=4) && (SENS_DHT_PIN_ARRAY_SIZE!=8) && (SENS_DHT_PIN_ARRAY_SIZE!=16) && (SENS_DHT_PIN_ARRAY_SIZE!=32) && (SENS_DHT_PIN_ARRAY_SIZE!=64) & (SENS_DHT_PIN_ARRAY_SIZE!=128) && (SENS_DHT_PIN_ARRAY_SIZE!=256)
116 #error "SENS_DHT_PIN_ARRAY_SIZE must be 4,8,16,32,64,128 or 256!"
117 #endif
118 
119 #if (SENS_DHT_NUMBER_OF_SENSORS >= 40)
120 #error "SENS_DHT_NUMBER_OF_SENSORS must be less than 40!"
121 #endif
122 
124 
125 typedef struct __attribute__((__packed__)) SENS_DHT_
126 {
127  BYTE data[4];
128  BYTE portID;
129 
130  //Flags, ensure 0 is default value! Are reset to 0 at new cycle
131  union {
132  struct
133  {
134  //Bottom 4 bits are cleared each cycle
135  unsigned char error : 1;
136  unsigned char newTemperature : 1;
137  unsigned char newHumidity : 1;
138  unsigned char fill3 : 1;
139  //Initialized during initialization
140  unsigned char fill4to6 : 3;
141  unsigned char initialized : 1;
142  } flags;
143  struct
144  {
145  unsigned char clearEachCycle : 4;
146  unsigned char fill4to7 : 4;
147  } flagsCycle;
148  BYTE flagsVal;
149  };
150 } DHT_SENSOR;
151 
152 
153 //typedef struct SENS_DHT_INFO_
154 typedef struct __attribute__((aligned(2), packed)) SENS_DHT_INFO_
155 //typedef struct __attribute__((__packed__)) SENS_DHT_INFO_
156 {
157  WORD timeout;
158  WORD tmrDelay; //Timer for read
159  WORD tickLastPinChange;
160  WORD bitadrPin; //bitadr for pin of current DHT sensor
161  WORD pinArr[SENS_DHT_PIN_ARRAY_SIZE];
162 
163  WORD pinPut;
164  WORD pinGet;
165  WORD bitadrPwrSwt; //bitadr for power switch LAT register, or BITADR_NA if not used
166  BYTE data[5]; //Data for current pin. Is copied to sens[].data once done
167  BYTE cnt; //Temporary counters
168  BYTE sm;
169  BYTE currSens; //Index of current sensor, is a value from 0 to (SENS_DHT_NUMBER_OF_SENSORS-1)
170  union {
171  struct
172  {
173  unsigned char pwrSwtInverted : 1; //Power switch is inverted
174  unsigned char fill1to7 : 7;
175  } flags;
176  BYTE flagsVal;
177  };
178  BYTE fill; //Fill required to get on WORD boundary
179 
180  //A DHT_SENSOR structure for each sensor
181  DHT_SENSOR sens[SENS_DHT_NUMBER_OF_SENSORS];
182 
183 } SENS_DHT_INFO;
184 
185 extern SENS_DHT_INFO dhtInfo;
186 //extern SENS_DHT_INFO* pObj;
187 
189 
217 BYTE sensDht_init(BYTE portIdSens, WORD startupDelay, BYTE portPowerSwitch, BOOL powerSwitchInverted);
218 
219 
223 void sensDht_task();
224 
225 
244 #define sensDht_isr_MACRO(tick15bit_8us) \
245  tick15bit_8us = tick15bit_8us & 0x7fff; \
246  /* Save value to be read later. Time is stored in lower 15-bits, and pin value in bit 15 */ \
247  if ((portReadBitadr_MACRO(dhtInfo.bitadrPin))!=0) \
248  tick15bit_8us |= 0x8000; \
249  dhtInfo.pinArr[dhtInfo.pinPut] = tick15bit_8us; \
250  /* Increment, and clear first bit outside of range. Not required for SENS_DHT_PIN_ARRAY_SIZE=256! */ \
251  if (SENS_DHT_PIN_ARRAY_SIZE==256) \
252  dhtInfo.pinPut++; \
253  else \
254  dhtInfo.pinPut = (dhtInfo.pinPut+1) & (~(SENS_DHT_PIN_ARRAY_SIZE)); \
255  /* ALTENATIVE METHOD - might use more code because not sigle clear bit instruction like above! */ \
256  /* dhtInfo.pinPut = (dhtInfo.pinPut + 1) & (SENS_DHT_PIN_ARRAY_SIZE-1); */ \
257  /* ALTERNATIVE single line for all lines above */ \
258  /* dhtInfo.pinArr[dhtInfo.pinPut&(SENS_DHT_PIN_ARRAY_SIZE-1)] = (pin==0)?utick15bit:(utick15bit|0x8000); */ \
259 
260 
265 void sensDht_fbrTask();
266 
267 
278 void sensDht_addSensor(BYTE idxSensor, BYTE portIdSens);
279 
280 
284 void sensDht_startRead();
285 
292 BOOL __INLINE_FUNCTION__ sensDht_hasNewHumidity(BYTE idxSensor) {
293  BOOL ret;
294  ret = dhtInfo.sens[idxSensor].flags.newHumidity;
295  dhtInfo.sens[idxSensor].flags.newHumidity = FALSE;
296  return ret;
297 }
298 
305 BOOL __INLINE_FUNCTION__ sensDht_hasNewTemperature(BYTE idxSensor) {
306  BOOL ret;
307  ret = dhtInfo.sens[idxSensor].flags.newTemperature;
308  dhtInfo.sens[idxSensor].flags.newTemperature = FALSE;
309  return ret;
310 }
311 
312 
321 #if (SENS_DHT_TYPE == 2) //DHT22
322 #define sensDht_getHumidityX10(idxSensor) ((WORD)*((WORD*)&(dhtInfo.sens[idxSensor].data[0])))
323 #elif (SENS_DHT_TYPE == 1) //DHT11
324 #define sensDht_getHumidityX10(idxSensor) (((WORD*)&(dhtInfo.sens[].data[0]))[0] * 10)
325 #endif
326 
327 
336 #if (SENS_DHT_TYPE == 2) //DHT22
337 #define sensDht_getHumidityInteger(idxSensor) (((WORD)*((WORD*)&(dhtInfo.sens[idxSensor].data[0])))/10)
338 #elif (SENS_DHT_TYPE == 1) //DHT11
339 #define sensDht_getHumidityInteger(idxSensor) (dhtInfo.sens[idxSensor].data[0])
340 #endif
341 
342 
351 #if (SENS_DHT_TYPE == 2) //DHT22
352 #define sensDht_getHumidityDecimal(idxSensor) (((WORD)*((WORD*)&(dhtInfo.sens[idxSensor].data[0])))%10)
353 #elif (SENS_DHT_TYPE == 1) //DHT11
354 #define sensDht_getHumidityDecimal(idxSensor) (dhtInfo.sens[idxSensor].data[1])
355 #endif
356 
357 
366 #if (SENS_DHT_TYPE == 2) //DHT22
367 #define sensDht_getTemperatureX10(idxSensor) ((WORD)*((WORD*)&(dhtInfo.sens[idxSensor].data[2])))
368 #elif (SENS_DHT_TYPE == 1) //DHT11
369 #define sensDht_getTemperatureX10(idxSensor) (((WORD*)&(dhtInfo.sens[idxSensor].data[2]))[0] * 10)
370 #endif
371 
372 
381 #if (SENS_DHT_TYPE == 2) //DHT22
382 #define sensDht_getTemperatureInteger(idxSensor) (((WORD)*((WORD*)&(dhtInfo.sens[idxSensor].data[2])))/10)
383 #elif (SENS_DHT_TYPE == 1) //DHT11
384 #define sensDht_getTemperatureInteger(idxSensor) (dhtInfo.sens[idxSensor].data[2])
385 #endif
386 
387 
396 #if (SENS_DHT_TYPE == 2) //DHT22
397 #define sensDht_getTemperatureDecimal(idxSensor) (((WORD)*((WORD*)&(dhtInfo.sens[idxSensor].data[2])))%10)
398 #elif (SENS_DHT_TYPE == 1) //DHT11
399 #define sensDht_getTemperatureDecimal(idxSensor) (dhtInfo.sens[idxSensor].data[3])
400 #endif
401 
402 
412 BOOL sensDht_isIdle();
413 
418 #define sensDht_getStatus() (dhtInfo.status)
419 
420 
421 #endif //#if (SENS_DHT_NUMBER_OF_SENSORS > 0)
422 
423 #endif //#ifndef NZ_SENS_DHT_H