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