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