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