Subversion Repositories HomeAutomation

Rev

Rev 245 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*
  2.  * Humidity & Temperature sensor SHT7x. (71/75)
  3.  *
  4.  * Sensor: http://www.sensirion.com/en/02_sensors/03_humidity/00_humidity_temperature_sensor/06_humidity_sensor_sht75.htm
  5.  * Origin of code: http://www.avrfreaks.net/index.php?module=PNphpBB2&file=viewtopic&t=31562&highlight=
  6.  *
  7.  * Copied from avrfreaks and rewritten by Erik Larsson.
  8.  * Date: 2007-01-14
  9.  *
  10.  */
  11. #include <avr/io.h>
  12. #include <sht7x.h>
  13.  
  14. /*
  15. Name: void HumidityDelay(void)
  16.  
  17. Description: Delay for SCLK
  18.  
  19. Input: none
  20.  
  21. Output: none
  22.  
  23.  
  24. Misc:
  25. ******************************************************************************/
  26. void HumidityDelay(void)
  27. {
  28. int i;
  29.  
  30. for (i=0;i<40;i++); /* Probably not that important because BIOS will take som time to start */
  31. }
  32.  
  33. /******************************************************************************
  34.  
  35. Name: void HumidityInit(void)
  36.  
  37. Description: Initialise port and constant used by
  38. the Humidity & Temp hardware
  39.  
  40.  
  41. Input: none
  42.  
  43. Output: none
  44.  
  45. Misc:
  46. ******************************************************************************/
  47. void HumidityInit(void)
  48. {
  49. HUMIDITY_DDR |= HUMIDITY_DATA + HUMIDITY_SCK;
  50. HUMIDITY_PORT |= HUMIDITY_DATA + HUMIDITY_SCK;
  51. HumiditySoftReset();
  52. }
  53.  
  54. /******************************************************************************
  55.  
  56. Name: unsigned char HumidityWriteByte(unsigned char value)
  57.  
  58. Description: writes a byte on the Sensibus and
  59. checks the acknowledge
  60.  
  61. Input: unsigned char value -> byte to write
  62.  
  63. Output: none
  64.  
  65.  
  66. Misc: If the device don't ACK set HumiError to TRUE;
  67. ******************************************************************************/
  68. void HumidityWriteByte(unsigned char value)
  69. {
  70. unsigned char i;
  71.  
  72. HUMIDITY_DDR |= HUMIDITY_DATA; //DATA-line in output
  73.  
  74. for (i=0x80;i>0;i/=2) //shift bit for masking
  75. {
  76. HumidityDelay();
  77. if (i & value) HUMIDITY_PORT |= HUMIDITY_DATA;
  78. else HUMIDITY_PORT &= ~HUMIDITY_DATA;
  79. HumidityDelay();
  80. HUMIDITY_PORT |= HUMIDITY_SCK;
  81. HumidityDelay();
  82. HUMIDITY_PORT &= ~HUMIDITY_SCK;
  83. }
  84.  
  85. HUMIDITY_PORT |= HUMIDITY_DATA; //release DATA-line
  86. HUMIDITY_DDR &= ~HUMIDITY_DATA; //DATA-line in input
  87. HumidityDelay();
  88. HUMIDITY_PORT |= HUMIDITY_SCK; //clk #9 for ack
  89. HumidityDelay();
  90. if (HUMIDITY_PIN & HUMIDITY_DATA) HumiError = TRUE;
  91. HUMIDITY_PORT &= ~HUMIDITY_SCK;
  92. HumidityDelay();
  93. }
  94.  
  95. /******************************************************************************
  96.  
  97. Name: unsigned char HumidityReadByte(unsigned char ack)
  98.  
  99. Description: reads a byte form the Sensibus and gives
  100. an acknowledge in case of "ack=1"
  101.  
  102. Input: unsigned char ack -> ack status
  103.  
  104. Output: unsigned char value readed
  105.  
  106. Misc:
  107. ******************************************************************************/
  108. unsigned char HumidityReadByte(unsigned char ack)
  109. {
  110. unsigned char i,val=0;
  111.  
  112. HUMIDITY_PORT |= HUMIDITY_DATA; //release DATA-line
  113. HUMIDITY_DDR &= ~HUMIDITY_DATA; //DATA-line in input
  114.  
  115. for (i=0x80;i>0;i/=2) //shift bit for masking
  116. {
  117. HumidityDelay();
  118. HUMIDITY_PORT |= HUMIDITY_SCK; //clk for SENSI-BUS
  119. HumidityDelay();
  120. if (HUMIDITY_PIN & HUMIDITY_DATA) val=(val | i); //read bit
  121. HUMIDITY_PORT &= ~HUMIDITY_SCK;
  122. }
  123.  
  124. if (ack == 1)
  125. {
  126. HUMIDITY_DDR |= HUMIDITY_DATA; //DATA-line in output
  127. HUMIDITY_PORT &= ~HUMIDITY_DATA; //"ack==1" pull down DATA-Line
  128. HumidityDelay();
  129. HUMIDITY_PORT |= HUMIDITY_SCK;
  130. HumidityDelay();
  131. HUMIDITY_PORT &= ~HUMIDITY_SCK;
  132. }
  133. else
  134. {
  135. HumidityDelay();
  136. HUMIDITY_PORT |= HUMIDITY_SCK;
  137. HumidityDelay();
  138. HUMIDITY_PORT &= ~HUMIDITY_SCK;
  139. }
  140. return val;
  141. }
  142.  
  143. /******************************************************************************
  144.  
  145. Name: unsigned char HumidityTransStart(void)
  146.  
  147. Description: Generates a transmission start
  148.  
  149. Input: none
  150.  
  151. Output: none
  152.  
  153. Misc:
  154. _____ ________
  155. DATA: |_______|
  156. ___ ___
  157. SCK : ___| |___| |______
  158. ******************************************************************************/
  159. void HumidityTransStart(void)
  160. {
  161. HUMIDITY_DDR |= HUMIDITY_DATA; //DATA-line in output
  162.  
  163. HUMIDITY_PORT |= HUMIDITY_DATA; //DATA=1
  164. HUMIDITY_PORT &= ~HUMIDITY_SCK; //SCK=0
  165. HumidityDelay();
  166. HUMIDITY_PORT |= HUMIDITY_SCK; //SCK=1
  167. HumidityDelay();
  168. HUMIDITY_PORT &= ~HUMIDITY_DATA; //DATA=0
  169. HumidityDelay();
  170. HUMIDITY_PORT &= ~HUMIDITY_SCK; //SCK=0
  171. HumidityDelay();
  172. HUMIDITY_PORT |= HUMIDITY_SCK; //SCK=1
  173. HumidityDelay();
  174. HUMIDITY_PORT |= HUMIDITY_DATA; //DATA=1
  175. HumidityDelay();
  176. HUMIDITY_PORT &= ~HUMIDITY_SCK; //SCK=0
  177. HumidityDelay();
  178. }
  179.  
  180. /******************************************************************************
  181.  
  182. Name: void HumidityConnectionReset(void)
  183.  
  184. Description: communication Reset: DATA-line=1 and at
  185. least 9 SCK cycles followed by transstart
  186.  
  187. Input: none
  188.  
  189. Output: none
  190.  
  191. Misc:
  192. _____________________________________________________ ________
  193. DATA: |_______|
  194. _ _ _ _ _ _ _ _ _ ___ ___
  195. SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
  196. ******************************************************************************/
  197. void HumidityConnectionReset(void)
  198. {
  199. unsigned char i;
  200.  
  201. HUMIDITY_DDR |= HUMIDITY_DATA; //DATA-line in output
  202. HUMIDITY_PORT |= HUMIDITY_DATA; //DATA=1
  203. HUMIDITY_PORT &= ~HUMIDITY_SCK; //SCK=0
  204.  
  205. for (i=0;i<9;i++) //9 SCK cycles
  206. {
  207. HumidityDelay();
  208. HUMIDITY_PORT |= HUMIDITY_SCK; //SCK=1
  209. HumidityDelay();
  210. HUMIDITY_PORT &= ~HUMIDITY_SCK; //SCK=0
  211. }
  212. HumidityTransStart(); //transmission start
  213. HumiError = FALSE;
  214. }
  215.  
  216. /******************************************************************************
  217.  
  218. Name: void HumiditySoftReset(void)
  219.  
  220. Description: Resets the sensor by a softReset
  221.  
  222. Input: none
  223.  
  224. Output: none
  225.  
  226. Misc:
  227.  
  228. ******************************************************************************/
  229. void HumiditySoftReset(void)
  230. {
  231. int i;
  232. HumidityConnectionReset(); //Reset communication
  233. HumidityWriteByte(HUMIDITY_RESET); //send Reset-command to sensor
  234. for (i=0;i<100;i++) HumidityDelay(); // 11ms delay
  235. }
  236.  
  237. /******************************************************************************
  238.  
  239. Name: unsigned char HumidityReadStatus(void)
  240.  
  241. Description: reads the status register with checksum (8-bit)
  242.  
  243. Input: none
  244.  
  245. Output: unsigned char status byte
  246.  
  247. Misc:
  248.  
  249. ******************************************************************************/
  250. unsigned char HumidityReadStatus(void)
  251. {
  252. unsigned char value,tmp;
  253.  
  254. HumidityTransStart(); //transmission start
  255. HumidityWriteByte(HUMIDITY_STATUS_REG_R); //send command to sensor
  256. value = HumidityReadByte(ACK); //read status register (8-bit)
  257. tmp=HumidityReadByte(noACK); //dummy read checksum (8-bit)
  258. return value;
  259. }
  260.  
  261. /******************************************************************************
  262.  
  263. Name: void HumidityWriteStatus(unsigned char value)
  264.  
  265. Description: writes the status register with checksum (8-bit)
  266.  
  267. Input: unsigned char value to write in the status register
  268.  
  269. Output: none
  270.  
  271. Misc:
  272.  
  273. ******************************************************************************/
  274. void HumidityWriteStatus(unsigned char value)
  275. {
  276. HumidityTransStart(); //transmission start
  277. HumidityWriteByte(HUMIDITY_STATUS_REG_W); //send command to sensor
  278. HumidityWriteByte(value); //send value of status register
  279. }
  280.  
  281. /******************************************************************************
  282.  
  283. Name: unsigned int HumidityMeasure(unsigned char mode)
  284.  
  285. Description: makes a measurement (humidity/temperature)
  286.  
  287. Input: mode -> HUMI or TEMP
  288.  
  289. Output: unsinged int Value mesured
  290.  
  291. Misc:
  292.  
  293. ******************************************************************************/
  294. unsigned int HumidityMeasure(unsigned char mode)
  295. {
  296. unsigned char msb,lsb,tmp;
  297. //int i,j;
  298. uint16_t i,j;
  299.  
  300. HumidityTransStart(); //transmission start
  301. switch (mode) //send command to sensor
  302. {
  303. case HUMIDITY_MEASURE_TEMP : HumidityWriteByte(HUMIDITY_MEASURE_TEMP);
  304. break;
  305. case HUMIDITY_MEASURE_HUMI : HumidityWriteByte(HUMIDITY_MEASURE_HUMI);
  306. break;
  307. default : break;
  308. }
  309.  
  310. HUMIDITY_PORT |= HUMIDITY_DATA; //release DATA-line
  311. HUMIDITY_DDR &= ~HUMIDITY_DATA; //DATA-line in input
  312.  
  313. for (i=0;i<=65530;i++)
  314. {
  315. for (j=0;j<10;j++); /* Maybe it works */
  316. if((HUMIDITY_PIN & HUMIDITY_DATA) == 0) break; //wait until sensor
  317. } //has finished the measure
  318. if(i > 65530)
  319. {
  320. HumiError=1; // or timeout (~2 sec.) is reached
  321. return 0xffff;
  322. }
  323.  
  324. msb =HumidityReadByte(ACK); //read the first byte (MSB)
  325. lsb=HumidityReadByte(ACK); //read the second byte (LSB)
  326. tmp =HumidityReadByte(noACK); //dummy read checksum
  327. return (msb<<8)+lsb;
  328. }
  329.  
  330. /******************************************************************************
  331.  
  332. Name: void HumidityCalc(float *p_humidity ,float *p_temperature)
  333.  
  334. Description: calculates temperature [°C] and humidity [%RH]
  335.  
  336. Input: humi [Ticks] (12 bit)
  337. temp [Ticks] (14 bit)
  338.  
  339. Output: humi [%RH]
  340. temp [°C]
  341.  
  342. Misc:
  343.  
  344. ******************************************************************************/
  345. void HumidityCalc(float *p_humidity ,float *p_temperature)
  346. {
  347. float C1=-4.0; // for 12 Bit
  348. float C2=+0.0405; // for 12 Bit
  349. float C3=-0.0000028; // for 12 Bit
  350. float T1=+0.01; // for 14 Bit @ 3V
  351. float T2=+0.00008; // for 14 Bit @ 3V
  352.  
  353. float rh=*p_humidity; // rh: Humidity [Ticks] 12 Bit
  354. float t=*p_temperature; // t: Temperature [Ticks] 14 Bit
  355. float rh_lin; // rh_lin: Humidity linear
  356. float rh_true; // rh_true: Temperature compensated humidity
  357. float t_C; // t_C : Temperature [°C]
  358.  
  359. t_C=t*0.01 - 39.6; //calc. temperature from ticks to [°C]
  360. rh_lin=C3*rh*rh + C2*rh + C1; //calc. humidity from ticks to [%RH]
  361. rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. temp compensated humidity [%RH]
  362. if(rh_true>100)rh_true=100; //cut if the value is outside of
  363. if(rh_true<0.1)rh_true=0.1; //the physical possible range
  364.  
  365. *p_temperature=t_C; //return temperature [°C]
  366. *p_humidity=rh_true; //return humidity[%RH]
  367. }
  368.  
  369. /******************************************************************************
  370.  
  371. Name: void HumidityGet(float *p_humidity ,float *p_temperature)
  372.  
  373. Description: measure humidity [ticks](12 bit) and temperature [ticks](14 bit)
  374.  
  375. Input: humi [%RH]
  376. temp [°C]
  377.  
  378. Output: humi [%RH]
  379. temp [°C]
  380.  
  381. Misc:
  382.  
  383. ******************************************************************************/
  384. void HumidityGet(float *p_humidity ,float *p_temperature)
  385. {
  386. unsigned int humi_val,temp_val;
  387.  
  388. humi_val = HumidityMeasure(HUMIDITY_MEASURE_HUMI); //measure humidity
  389. temp_val = HumidityMeasure(HUMIDITY_MEASURE_TEMP); //measure temperature
  390.  
  391. if(HumiError == TRUE) HumidityConnectionReset(); //in case of an error: Reset
  392. else
  393. {
  394. *p_humidity=(float)humi_val; //converts integer to float
  395. *p_temperature=(float)temp_val; //converts integer to float
  396. HumidityCalc(p_humidity,p_temperature); //calculate humidity, temperature
  397. }
  398. }
  399.  
  400. /******************************************************************************
  401.  
  402. Name: void HumidityUpdate(void)
  403.  
  404. Description:
  405.  
  406. Input:
  407.  
  408. Output:
  409. Misc:
  410.  
  411. ******************************************************************************/
  412. void HumidityUpdate(void)
  413. {
  414. if (HumiError == TRUE) HumidityInit();
  415. HumidityGet(&Humidity,&Temperature);
  416. Hum = Humidity * 10;
  417. Temp = Temperature * 10;
  418. }
  419.