Rev Author Line No. Line
2909 kaklik 1 #include "main.h"
2906 jacho 2  
2909 kaklik 3 void SHT25_soft_reset()
4 {
5 i2c_start(); // Start condition
6 i2c_write(0x80); // Device address
7 i2c_write(0xFE); // Device command
8 i2c_stop(); // Stop condition
9 }
2906 jacho 10  
2909 kaklik 11 unsigned int8 SHT25_setup()
12 {
13 unsigned int8 reg;
2906 jacho 14  
2909 kaklik 15 i2c_start(); // Start condition
16 i2c_write(0x80); // Device address
17 i2c_write(0xE7); // Device command
18 // i2c_stop(); // Stop condition
19  
20 i2c_start(); // Start condition
21 i2c_write(0x81); // Device address
22 reg=i2c_read(0); // Device command
23 i2c_stop(); // Stop condition
24  
25 return (reg);
26 }
27  
28 float SHT25_get_temp()
29 {
30 unsigned int8 MSB, LSB, Check;
31 unsigned int16 data;
32  
33 i2c_start();
34 I2C_Write(0x80);
35 I2C_write(0xE3);
36 i2c_stop();
37  
38 delay_ms(100);
39  
40 i2c_start();
41 I2C_Write(0x81);
42 MSB=i2c_read(1);
43 LSB=i2c_read(1);
44 Check=i2c_read(0);
45 i2c_stop();
46  
47 printf("%X %X %X\r\n",MSB, LSB, Check);
48  
49 LSB = LSB >> 2; // trow out status bits
50  
51 data = (((unsigned int16) MSB << 8) + (LSB << 4));
52 return(-46.85 + 175.72*((float)data/0xFFFF));
53 }
54  
55 float SHT25_get_hum()
56 {
57 unsigned int8 MSB, LSB, Check;
58 unsigned int16 data;
59  
60 i2c_start(); //RH
61 I2C_Write(0x80);
62 I2C_write(0xE5);
63 // i2c_stop();
64  
65 delay_ms(100);
66  
67 i2c_start();
68 I2C_Write(0x81);
69 MSB=i2c_read(1);
70 LSB=i2c_read(1);
71 Check=i2c_read(0);
72 i2c_stop();
73  
74 printf("%X %X %X\r\n",MSB, LSB, Check);
75  
76 LSB = LSB >> 2; // trow out status bits
77  
78 data = (((unsigned int16) MSB << 8) + (LSB << 4) );
79 return( -6.0 + 125.0*((float)data/0xFFFF));
80 }
81  
82  
83 void main()
84 {
85  
86 setup_adc_ports(NO_ANALOGS|VSS_VDD);
87 setup_adc(ADC_CLOCK_DIV_2);
88 setup_spi(SPI_SS_DISABLED);
89 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
90 setup_timer_1(T1_DISABLED);
91 setup_timer_2(T2_DISABLED,0,1);
92 setup_ccp1(CCP_OFF);
93 setup_comparator(NC_NC_NC_NC);
94  
95 void SHT25_soft_reset();
96 printf("SHT25 humidity and temperature sensor example \r\n",);
97 delay_ms (500);
98  
99 while(TRUE)
100 {
101 printf("setup: %X \r\n",SHT25_setup());
102 delay_ms (500);
103 printf("Temp: %f \r\n",SHT25_get_temp());
104 delay_ms (500);
105 printf("Hum: %f \r\n",SHT25_get_hum());
106 delay_ms (1000);
107 }
108 }
109