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 |
|
2911 |
kaklik |
11 |
#define SHT25_HEATER_ON 0x04 |
|
|
12 |
#define SHT25_HEATER_OFF 0x00 |
|
|
13 |
#define SHT25_OTP_reload_off 0x02 |
|
|
14 |
#define SHT25_RH12_T14 0x00 |
|
|
15 |
#define SHT25_RH8_T12 0x01 |
|
|
16 |
#define SHT25_RH10_T13 0x80 |
|
|
17 |
#define SHT25_RH11_T11 0x81 |
2910 |
kaklik |
18 |
|
2911 |
kaklik |
19 |
#define SHT25_ADDR 0x80 |
|
|
20 |
|
|
|
21 |
unsigned int8 SHT25_setup(unsigned int8 setup_reg ) // writes to status register and returns its value |
2909 |
kaklik |
22 |
{ |
|
|
23 |
unsigned int8 reg; |
2906 |
jacho |
24 |
|
2909 |
kaklik |
25 |
i2c_start(); // Start condition |
2911 |
kaklik |
26 |
i2c_write(SHT25_ADDR); // Device address |
2909 |
kaklik |
27 |
i2c_write(0xE7); // Device command |
2906 |
jacho |
28 |
|
2909 |
kaklik |
29 |
i2c_start(); // Start condition |
2911 |
kaklik |
30 |
i2c_write(SHT25_ADDR+1); // Device address |
|
|
31 |
reg=i2c_read(0); // Read status actual status register |
|
|
32 |
|
|
|
33 |
reg = (reg & 0x3A) | setup_reg; |
|
|
34 |
|
|
|
35 |
i2c_start(); // Start condition |
|
|
36 |
i2c_write(SHT25_ADDR); // Device address |
|
|
37 |
i2c_write(0xE7); // Write to status register |
|
|
38 |
i2c_write(reg); // Device command |
2909 |
kaklik |
39 |
i2c_stop(); // Stop condition |
2911 |
kaklik |
40 |
|
|
|
41 |
return (reg); |
2909 |
kaklik |
42 |
} |
2906 |
jacho |
43 |
|
2911 |
kaklik |
44 |
|
2909 |
kaklik |
45 |
float SHT25_get_temp() |
|
|
46 |
{ |
|
|
47 |
unsigned int8 MSB, LSB, Check; |
|
|
48 |
unsigned int16 data; |
2906 |
jacho |
49 |
|
2909 |
kaklik |
50 |
i2c_start(); |
2911 |
kaklik |
51 |
I2C_Write(SHT25_ADDR); |
2909 |
kaklik |
52 |
I2C_write(0xE3); |
2906 |
jacho |
53 |
i2c_stop(); |
2909 |
kaklik |
54 |
|
|
|
55 |
delay_ms(100); |
2906 |
jacho |
56 |
|
|
|
57 |
i2c_start(); |
2911 |
kaklik |
58 |
I2C_Write(SHT25_ADDR+1); |
2909 |
kaklik |
59 |
MSB=i2c_read(1); |
|
|
60 |
LSB=i2c_read(1); |
|
|
61 |
Check=i2c_read(0); |
|
|
62 |
i2c_stop(); |
|
|
63 |
|
|
|
64 |
LSB = LSB >> 2; // trow out status bits |
2906 |
jacho |
65 |
|
2909 |
kaklik |
66 |
data = (((unsigned int16) MSB << 8) + (LSB << 4)); |
|
|
67 |
return(-46.85 + 175.72*((float)data/0xFFFF)); |
|
|
68 |
} |
|
|
69 |
|
|
|
70 |
float SHT25_get_hum() |
|
|
71 |
{ |
|
|
72 |
unsigned int8 MSB, LSB, Check; |
|
|
73 |
unsigned int16 data; |
|
|
74 |
|
|
|
75 |
i2c_start(); //RH |
2911 |
kaklik |
76 |
I2C_Write(SHT25_ADDR); |
2909 |
kaklik |
77 |
I2C_write(0xE5); |
|
|
78 |
// i2c_stop(); |
|
|
79 |
|
|
|
80 |
delay_ms(100); |
|
|
81 |
|
2906 |
jacho |
82 |
i2c_start(); |
2911 |
kaklik |
83 |
I2C_Write(SHT25_ADDR+1); |
2909 |
kaklik |
84 |
MSB=i2c_read(1); |
|
|
85 |
LSB=i2c_read(1); |
|
|
86 |
Check=i2c_read(0); |
|
|
87 |
i2c_stop(); |
2906 |
jacho |
88 |
|
2909 |
kaklik |
89 |
// printf("%X %X %X\r\n",MSB, LSB, Check); |
2906 |
jacho |
90 |
|
2909 |
kaklik |
91 |
LSB = LSB >> 2; // trow out status bits |
2906 |
jacho |
92 |
|
2909 |
kaklik |
93 |
data = (((unsigned int16) MSB << 8) + (LSB << 4) ); |
|
|
94 |
return( -6.0 + 125.0*((float)data/0xFFFF)); |
|
|
95 |
} |
2906 |
jacho |
96 |
|
|
|
97 |
|
2909 |
kaklik |
98 |
void main() |
|
|
99 |
{ |
2911 |
kaklik |
100 |
unsigned int8 i=0; |
2906 |
jacho |
101 |
|
2909 |
kaklik |
102 |
setup_adc_ports(NO_ANALOGS|VSS_VDD); |
|
|
103 |
setup_adc(ADC_CLOCK_DIV_2); |
|
|
104 |
setup_spi(SPI_SS_DISABLED); |
|
|
105 |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); |
|
|
106 |
setup_timer_1(T1_DISABLED); |
|
|
107 |
setup_timer_2(T2_DISABLED,0,1); |
|
|
108 |
setup_ccp1(CCP_OFF); |
|
|
109 |
setup_comparator(NC_NC_NC_NC); |
|
|
110 |
|
2911 |
kaklik |
111 |
SHT25_soft_reset(); |
2909 |
kaklik |
112 |
printf("SHT25 humidity and temperature sensor example \r\n",); |
|
|
113 |
delay_ms (500); |
|
|
114 |
|
|
|
115 |
while(TRUE) |
|
|
116 |
{ |
2911 |
kaklik |
117 |
if (i<10) |
|
|
118 |
printf("setup: %X \r\n",SHT25_setup(SHT25_RH12_T14 | SHT25_HEATER_OFF)); |
|
|
119 |
else |
|
|
120 |
{ |
|
|
121 |
printf("setup: %X \r\n",SHT25_setup(SHT25_RH12_T14 | SHT25_HEATER_ON)); |
|
|
122 |
i = 0; |
|
|
123 |
} |
2909 |
kaklik |
124 |
delay_ms (500); |
|
|
125 |
printf("Temp: %f \r\n",SHT25_get_temp()); |
|
|
126 |
delay_ms (500); |
|
|
127 |
printf("Hum: %f \r\n",SHT25_get_hum()); |
|
|
128 |
delay_ms (1000); |
2911 |
kaklik |
129 |
i++; |
2909 |
kaklik |
130 |
} |
2906 |
jacho |
131 |
} |
|
|
132 |
|