2754 |
kaklik |
1 |
/////////////////////////////////////////////////////////////////////////////// |
|
|
2 |
// // |
|
|
3 |
// Driver file for SHT75 Temperature & Humidity Sensor // |
|
|
4 |
// // |
|
|
5 |
// ***** To initialise SHT75 sensor upon power up ***** // |
|
|
6 |
// // |
|
|
7 |
// Function : sht_init() // |
|
|
8 |
// Return : none // |
|
|
9 |
// // |
|
|
10 |
// // |
|
|
11 |
// ***** To measure and caluculate SHT75 temp & real RH ***** // |
|
|
12 |
// // |
|
|
13 |
// Function : sht_rd (temp, truehumid) // |
|
|
14 |
// Return : temperature & true humidity in float values // |
|
|
15 |
// // |
|
|
16 |
/////////////////////////////////////////////////////////////////////////////// |
|
|
17 |
|
|
|
18 |
#define sht_data_pin PIN_D3 //(I use A1 as data input) |
|
|
19 |
#define sht_clk_pin PIN_C4 |
|
|
20 |
|
|
|
21 |
|
|
|
22 |
|
|
|
23 |
|
|
|
24 |
//***** Function to alert SHT75 ***** |
|
|
25 |
|
|
|
26 |
void comstart (void) |
|
|
27 |
{ |
|
|
28 |
output_float(sht_data_pin); //data high |
|
|
29 |
output_bit(sht_clk_pin, 0); //clk low |
|
|
30 |
delay_us(1); |
|
|
31 |
output_bit(sht_clk_pin, 1); //clk high |
|
|
32 |
delay_us(1); |
|
|
33 |
output_bit(sht_data_pin, 0); //data low |
|
|
34 |
delay_us(1); |
|
|
35 |
output_bit(sht_clk_pin, 0); //clk low |
|
|
36 |
delay_us(2); |
|
|
37 |
output_bit(sht_clk_pin, 1); //clk high |
|
|
38 |
delay_us(1); |
|
|
39 |
output_float(sht_data_pin); //data high |
|
|
40 |
delay_us(1); |
|
|
41 |
output_bit(sht_clk_pin, 0); //clk low |
|
|
42 |
} |
|
|
43 |
|
|
|
44 |
|
|
|
45 |
//***** Function to write data to SHT75 ***** |
|
|
46 |
|
|
|
47 |
int1 comwrite (int8 iobyte) |
|
|
48 |
{ |
|
|
49 |
int8 i, mask = 0x80; |
|
|
50 |
int1 ack; |
|
|
51 |
|
|
|
52 |
//Shift out command |
|
|
53 |
delay_us(4); |
|
|
54 |
for(i=0; i<8> 0) output_float(sht_data_pin); //data high if MSB high |
|
|
55 |
else output_bit(sht_data_pin, 0); //data low if MSB low |
|
|
56 |
delay_us(1); |
|
|
57 |
output_bit(sht_clk_pin, 1); //clk high |
|
|
58 |
delay_us(1); |
|
|
59 |
mask = mask >> 1; //shift to next bit |
|
|
60 |
} |
|
|
61 |
|
|
|
62 |
//Shift in ack |
|
|
63 |
output_bit(sht_clk_pin, 0); //clk low |
|
|
64 |
delay_us(1); |
|
|
65 |
ack = input(sht_data_pin); //get ack bit |
|
|
66 |
output_bit(sht_clk_pin, 1); //clk high |
|
|
67 |
delay_us(1); |
|
|
68 |
output_bit(sht_clk_pin, 0); //clk low |
|
|
69 |
return(ack); |
|
|
70 |
} |
|
|
71 |
|
|
|
72 |
|
|
|
73 |
//***** Function to read data from SHT75 ***** |
|
|
74 |
|
|
|
75 |
int16 comread (void) |
|
|
76 |
{ |
|
|
77 |
int8 i; |
|
|
78 |
int16 iobyte = 0; |
|
|
79 |
const int16 mask0 = 0x0000; |
|
|
80 |
const int16 mask1 = 0x0001; |
|
|
81 |
|
|
|
82 |
//shift in MSB data |
|
|
83 |
for(i=0; i<8; i++) |
|
|
84 |
{ |
|
|
85 |
iobyte = iobyte << 1; |
|
|
86 |
output_bit(sht_clk_pin, 1); //clk high |
|
|
87 |
delay_us(1); |
|
|
88 |
if (input(sht_data_pin)) iobyte |= mask1; //shift in data bit |
|
|
89 |
else iobyte |= mask0; |
|
|
90 |
output_bit(sht_clk_pin, 0); //clk low |
|
|
91 |
delay_us(1); |
|
|
92 |
} |
|
|
93 |
|
|
|
94 |
//send ack 0 bit |
|
|
95 |
output_bit(sht_data_pin, 0); //data low |
|
|
96 |
delay_us(1); |
|
|
97 |
output_bit(sht_clk_pin, 1); //clk high |
|
|
98 |
delay_us(2); |
|
|
99 |
output_bit(sht_clk_pin, 0); //clk low |
|
|
100 |
delay_us(1); |
|
|
101 |
output_float(sht_data_pin); //data high |
|
|
102 |
|
|
|
103 |
//shift in LSB data |
|
|
104 |
for(i=0; i<8; i++) |
|
|
105 |
{ |
|
|
106 |
iobyte = iobyte << 1; |
|
|
107 |
output_bit(sht_clk_pin, 1); //clk high |
|
|
108 |
delay_us(1); |
|
|
109 |
if (input(sht_data_pin)) iobyte |= mask1; //shift in data bit |
|
|
110 |
else iobyte |= mask0; |
|
|
111 |
output_bit(sht_clk_pin, 0); //clk low |
|
|
112 |
delay_us(1); |
|
|
113 |
} |
|
|
114 |
|
|
|
115 |
//send ack 1 bit |
|
|
116 |
output_float(sht_data_pin); //data high |
|
|
117 |
delay_us(1); |
|
|
118 |
output_bit(sht_clk_pin, 1); //clk high |
|
|
119 |
delay_us(2); |
|
|
120 |
output_bit(sht_clk_pin, 0); //clk low |
|
|
121 |
|
|
|
122 |
return(iobyte); |
|
|
123 |
} |
|
|
124 |
|
|
|
125 |
|
|
|
126 |
//***** Function to wait for SHT75 reading ***** |
|
|
127 |
|
|
|
128 |
void comwait (void) |
|
|
129 |
{ |
|
|
130 |
int16 sht_delay; |
|
|
131 |
|
|
|
132 |
output_float(sht_data_pin); //data high |
|
|
133 |
output_bit(sht_clk_pin, 0); //clk low |
|
|
134 |
delay_us(1); |
|
|
135 |
for(sht_delay=0; sht_delay<30000; sht_delay++) // wait for max 300ms |
|
|
136 |
{ |
|
|
137 |
if (!input(sht_data_pin)) break; //if sht_data_pin low, SHT75 ready |
|
|
138 |
delay_us(10); |
|
|
139 |
} |
|
|
140 |
} |
|
|
141 |
|
|
|
142 |
|
|
|
143 |
//***** Function to reset SHT75 communication ***** |
|
|
144 |
|
|
|
145 |
void comreset (void) |
|
|
146 |
{ |
|
|
147 |
int8 i; |
|
|
148 |
|
|
|
149 |
output_float(sht_data_pin); //data high |
|
|
150 |
output_bit(sht_clk_pin, 0); //clk low |
|
|
151 |
delay_us(2); |
|
|
152 |
for(i=0; i<9; i++) |
|
|
153 |
{ |
|
|
154 |
output_bit(sht_clk_pin, 1); //toggle clk 9 times |
|
|
155 |
delay_us(2); |
|
|
156 |
output_bit(sht_clk_pin, 0); |
|
|
157 |
delay_us(2); |
|
|
158 |
} |
|
|
159 |
comstart(); |
|
|
160 |
} |
|
|
161 |
|
|
|
162 |
|
|
|
163 |
//***** Function to soft reset SHT75 ***** |
|
|
164 |
|
|
|
165 |
void sht_soft_reset (void) |
|
|
166 |
{ |
|
|
167 |
comreset(); //SHT75 communication reset |
|
|
168 |
comwrite(0x1e); //send SHT75 reset command |
|
|
169 |
delay_ms(15); //pause 15 ms |
|
|
170 |
} |
|
|
171 |
|
|
|
172 |
|
|
|
173 |
//***** Function to measure SHT75 temperature ***** |
|
|
174 |
|
|
|
175 |
int16 measuretemp (void) |
|
|
176 |
{ |
|
|
177 |
int1 ack; |
|
|
178 |
int16 iobyte; |
|
|
179 |
|
|
|
180 |
comstart(); //alert SHT75 |
|
|
181 |
ack = comwrite(0x03); //send measure temp command and read ack status |
|
|
182 |
if(ack == 1) return; |
|
|
183 |
comwait(); //wait for SHT75 measurement to complete |
|
|
184 |
iobyte = comread(); //read SHT75 temp data |
|
|
185 |
return(iobyte); |
|
|
186 |
} |
|
|
187 |
|
|
|
188 |
|
|
|
189 |
//***** Function to measure SHT75 RH ***** |
|
|
190 |
|
|
|
191 |
int16 measurehumid (void) |
|
|
192 |
{ |
|
|
193 |
int1 ack; |
|
|
194 |
int16 iobyte; |
|
|
195 |
|
|
|
196 |
comstart(); //alert SHT75 |
|
|
197 |
ack = comwrite(0x05); //send measure RH command and read ack status |
|
|
198 |
if(ack == 1) return; |
|
|
199 |
comwait(); //wait for SHT75 measurement to complete |
|
|
200 |
iobyte = comread(); //read SHT75 temp data |
|
|
201 |
return(iobyte); |
|
|
202 |
} |
|
|
203 |
|
|
|
204 |
|
|
|
205 |
//***** Function to calculate SHT75 temp & RH ***** |
|
|
206 |
|
|
|
207 |
void calculate_data (int16 temp, int16 humid, float & tc, float & rhlin, float & rhtrue) |
|
|
208 |
{ |
|
|
209 |
float truehumid1, rh; |
|
|
210 |
|
|
|
211 |
//calculate temperature reading |
|
|
212 |
tc = ((float) temp * 0.01) - 40.0; |
|
|
213 |
|
|
|
214 |
//calculate Real RH reading |
|
|
215 |
rh = (float) humid; |
|
|
216 |
|
|
|
217 |
rhlin = (rh * 0.0405) - (rh * rh * 0.0000028) - 4.0; |
|
|
218 |
|
|
|
219 |
//calculate True RH reading |
|
|
220 |
rhtrue = ((tc - 25.0) * (0.01 + (0.00008 * rh))) + rhlin; |
|
|
221 |
} |
|
|
222 |
|
|
|
223 |
|
|
|
224 |
//***** Function to measure & calculate SHT75 temp & RH ***** |
|
|
225 |
|
|
|
226 |
void sht_rd (float & temp, float & truehumid) |
|
|
227 |
{ |
|
|
228 |
int16 restemp, reshumid; |
|
|
229 |
float realhumid; |
|
|
230 |
restemp = 0; truehumid = 0; |
|
|
231 |
|
|
|
232 |
restemp = measuretemp(); //measure temp |
|
|
233 |
reshumid = measurehumid(); //measure RH |
|
|
234 |
calculate_data (restemp, reshumid, temp, realhumid, truehumid); //calculate temp & RH |
|
|
235 |
} |
|
|
236 |
|
|
|
237 |
|
|
|
238 |
//***** Function to initialise SHT75 on power-up ***** |
|
|
239 |
|
|
|
240 |
void sht_init (void) |
|
|
241 |
{ |
|
|
242 |
comreset(); //reset SHT75 |
|
|
243 |
delay_ms(20); //delay for power-up |
|
|
244 |
} |
|
|
245 |
|