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