add SHT3X support / new smaller SHT2X (V2)
[owSlave2.git] / common / I2C / SHT3x.c
diff --git a/common/I2C/SHT3x.c b/common/I2C/SHT3x.c
new file mode 100644 (file)
index 0000000..b20de79
--- /dev/null
@@ -0,0 +1,71 @@
+//==============================================================================\r
+// S E N S I R I O N AG, Laubisruetistr. 50, CH-8712 Staefa, Switzerland\r
+//==============================================================================\r
+// Project : SHT2x Sample Code (V1.2)\r
+// File : SHT2x.c\r
+// Author : MST\r
+// Controller: NEC V850/SG3 (uPD70F3740)\r
+// Compiler : IAR compiler for V850 (3.50A)\r
+// Brief : Sensor layer. Functions for sensor access\r
+//==============================================================================\r
+//---------- Includes ----------------------------------------------------------\r
+#define F_CPU 8000000UL\r
+#include <avr/io.h>\r
+#include <util/delay.h>\r
+#include "USI_TWI_Master.h"\r
+#include "SHT3x.h"\r
+\r
+uint8_t initSHT3x(uint8_t adrline){\r
+       /*I2c_StartCondition();\r
+       I2c_WriteByte(0b10001000|(adrline<<1));\r
+       I2c_WriteByte(0x27);\r
+       I2c_WriteByte(0x37);\r
+       I2c_StopCondition();*/\r
+       return 1;\r
+}\r
+\r
+uint8_t calcCRCSHT3x(uint8_t b1, uint8_t b2) {\r
+       uint8_t bit;\r
+       uint8_t crc=0xFF;\r
+       crc^=b1;\r
+       for(bit=8;bit>0;--bit) {\r
+               if (crc&0x80) crc=(crc<<1)^  0x131;\r
+               else crc=(crc<<1);\r
+       }\r
+       crc^=b2;\r
+       for(bit=8;bit>0;--bit) {\r
+               if (crc&0x80) crc=(crc<<1)^  0x131;\r
+               else crc=(crc<<1);\r
+       }\r
+       return crc;\r
+       \r
+}\r
+\r
+\r
+\r
+uint8_t getSHT3xHumTemp(uint8_t adrline,double *temp,double *hum) {\r
+       uint8_t ret=1;\r
+       I2c_StartCondition();\r
+       I2c_WriteByte(0b10001000|(adrline<<1));\r
+       I2c_WriteByte(0x24);\r
+       I2c_WriteByte(0x00); //NotClock Scr\r
+       I2c_StopCondition();\r
+       _delay_ms(16);\r
+       \r
+       I2c_StartCondition();\r
+       I2c_WriteByte (0b10001001|(adrline<<1));\r
+       volatile uint8_t t1 =I2c_ReadByte(ACK);\r
+       volatile uint8_t t2 =I2c_ReadByte(ACK);\r
+       volatile uint8_t tc =I2c_ReadByte(ACK);\r
+       volatile uint8_t f1 =I2c_ReadByte(ACK);\r
+       volatile uint8_t f2 =I2c_ReadByte(ACK);\r
+       volatile uint8_t fc =I2c_ReadByte(NO_ACK);\r
+       I2c_StopCondition();\r
+       if (calcCRCSHT3x(t1,t2)==tc)\r
+               *temp=175.0f*(double)(((uint16_t)t1<<8)|t2)/65535.0f-45.0f;\r
+       else ret=0;\r
+       if (calcCRCSHT3x(f1,f2)==fc)\r
+               *hum=100.0f*(double)(((uint16_t)f1<<8)|f2)/65535.0f;\r
+       else ret=0;\r
+       return ret;\r
+}\r