Line 1... |
Line 1... |
1 |
/////////////////////////////////////////////////////////////////////////////// |
1 |
/////////////////////////////////////////////////////////////////////////////// |
2 |
// // |
2 |
// // |
3 |
// Driver file for SHT75 Temperature & Humidity Sensor // |
3 |
// Driver file for SHT1x Temperature & Humidity Sensor // |
4 |
// // |
4 |
// // |
5 |
// ***** To initialise SHT75 sensor upon power up ***** // |
5 |
// ***** To initialise SHT1x sensor upon power up ***** // |
6 |
// // |
6 |
// // |
7 |
// Function : sht_init() // |
7 |
// Function : sht_init() // |
8 |
// Return : none // |
8 |
// Return : none // |
9 |
// // |
9 |
// // |
10 |
// // |
10 |
// // |
Line 18... |
Line 18... |
18 |
//#define sht_data_pin PIN_D0 |
18 |
//#define sht_data_pin PIN_D0 |
19 |
//#define sht_clk_pin PIN_D1 |
19 |
//#define sht_clk_pin PIN_D1 |
20 |
|
20 |
|
21 |
|
21 |
|
22 |
//***** Function to alert SHT75 ***** |
22 |
//***** Function to alert SHT75 ***** |
- |
|
23 |
// generates a transmission start |
- |
|
24 |
// _____ ________ |
- |
|
25 |
// DATA: |_______| |
- |
|
26 |
// ___ ___ |
- |
|
27 |
// SCK : ___| |___| |______ |
23 |
|
28 |
|
24 |
void comstart (void) |
29 |
void sht_comstart (void) |
25 |
{ |
30 |
{ |
26 |
output_float(sht_data_pin); //data high |
31 |
output_float(sht_data_pin); //data high |
27 |
output_low(sht_clk_pin); //clk low |
32 |
output_low(sht_clk_pin); //clk low |
28 |
delay_us(2); |
33 |
delay_us(2); |
29 |
output_high(sht_clk_pin); //clk high |
34 |
output_high(sht_clk_pin); //clk high |
Line 40... |
Line 45... |
40 |
} |
45 |
} |
41 |
|
46 |
|
42 |
|
47 |
|
43 |
//***** Function to write data to SHT75 ***** |
48 |
//***** Function to write data to SHT75 ***** |
44 |
|
49 |
|
45 |
int1 comwrite (int8 iobyte) |
50 |
int1 sht_comwrite (int8 iobyte) |
46 |
{ |
51 |
{ |
47 |
int8 i, mask = 0x80; |
52 |
int8 i, mask = 0x80; |
48 |
int1 ack; |
53 |
int1 ack; |
49 |
|
54 |
|
50 |
//Shift out command |
55 |
//Shift out command |
Line 71... |
Line 76... |
71 |
} |
76 |
} |
72 |
|
77 |
|
73 |
|
78 |
|
74 |
//***** Function to read data from SHT75 ***** |
79 |
//***** Function to read data from SHT75 ***** |
75 |
|
80 |
|
76 |
int16 comread (void) |
81 |
int16 sht_comread (void) |
77 |
{ |
82 |
{ |
78 |
int8 i; |
83 |
int8 i; |
79 |
int16 iobyte = 0; |
84 |
int16 iobyte = 0; |
80 |
const int16 mask0 = 0x0000; |
85 |
const int16 mask0 = 0x0000; |
81 |
const int16 mask1 = 0x0001; |
86 |
const int16 mask1 = 0x0001; |
Line 124... |
Line 129... |
124 |
} |
129 |
} |
125 |
|
130 |
|
126 |
|
131 |
|
127 |
//***** Function to wait for SHT75 reading ***** |
132 |
//***** Function to wait for SHT75 reading ***** |
128 |
|
133 |
|
129 |
void comwait (void) |
134 |
void sht_comwait (void) |
130 |
{ |
135 |
{ |
131 |
int16 sht_delay; |
136 |
int16 sht_delay; |
132 |
|
137 |
|
133 |
output_float(sht_data_pin); //data high |
138 |
output_float(sht_data_pin); //data high |
134 |
output_low(sht_clk_pin); //clk low |
139 |
output_low(sht_clk_pin); //clk low |
135 |
delay_us(2); |
140 |
delay_us(2); |
136 |
for(sht_delay=0; sht_delay<30000; sht_delay++) // wait for max 300ms |
141 |
for(sht_delay=0; sht_delay<350; sht_delay++) // wait for max 350ms (for 14bit measurement) |
137 |
{ |
142 |
{ |
138 |
if (!input(sht_data_pin)) break; //if sht_data_pin low, SHT75 ready |
143 |
if (!input(sht_data_pin)) break; //if sht_data_pin low, SHT75 ready |
139 |
delay_us(10); |
144 |
delay_ms(1); |
140 |
} |
145 |
} |
141 |
} |
146 |
} |
142 |
|
147 |
|
143 |
|
148 |
|
144 |
//***** Function to reset SHT75 communication ***** |
149 |
//***** Function to reset SHT75 communication ***** |
145 |
|
- |
|
- |
|
150 |
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart |
- |
|
151 |
// _____________________________________________________ ________ |
- |
|
152 |
// DATA: |_______| |
- |
|
153 |
// _ _ _ _ _ _ _ _ _ ___ ___ |
- |
|
154 |
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______ |
146 |
void comreset (void) |
155 |
void sht_comreset (void) |
147 |
{ |
156 |
{ |
148 |
int8 i; |
157 |
int8 i; |
149 |
|
158 |
|
150 |
output_float(sht_data_pin); //data high |
159 |
output_float(sht_data_pin); //data high |
151 |
output_low(sht_clk_pin); //clk low |
160 |
output_low(sht_clk_pin); //clk low |
Line 155... |
Line 164... |
155 |
output_high(sht_clk_pin); //toggle clk 9 times |
164 |
output_high(sht_clk_pin); //toggle clk 9 times |
156 |
delay_us(5); |
165 |
delay_us(5); |
157 |
output_low(sht_clk_pin); |
166 |
output_low(sht_clk_pin); |
158 |
delay_us(5); |
167 |
delay_us(5); |
159 |
} |
168 |
} |
160 |
comstart(); |
169 |
sht_comstart(); |
161 |
} |
170 |
} |
162 |
|
171 |
|
163 |
|
172 |
|
164 |
//***** Function to soft reset SHT75 ***** |
173 |
//***** Function to soft reset SHT75 ***** |
165 |
|
174 |
|
166 |
void sht_soft_reset (void) |
175 |
void sht_soft_reset (void) |
167 |
{ |
176 |
{ |
168 |
comreset(); //SHT75 communication reset |
177 |
sht_comreset(); //SHT75 communication reset |
169 |
comwrite(0x1e); //send SHT75 reset command |
178 |
sht_comwrite(0x1e); //send SHT75 reset command |
170 |
delay_ms(15); //pause 15 ms |
179 |
delay_ms(15); //pause 15 ms |
171 |
} |
180 |
} |
172 |
|
181 |
|
173 |
|
182 |
|
174 |
//***** Function to measure SHT75 temperature ***** |
183 |
//***** Function to measure SHT75 temperature ***** |
175 |
|
184 |
|
176 |
int16 measuretemp (void) |
185 |
int16 sht_measuretemp (void) |
177 |
{ |
186 |
{ |
178 |
int1 ack; |
187 |
int1 ack; |
179 |
int16 iobyte; |
188 |
int16 iobyte; |
180 |
|
189 |
|
181 |
comstart(); //alert SHT75 |
190 |
sht_comstart(); //alert SHT75 |
182 |
ack = comwrite(0x03); //send measure temp command and read ack status |
191 |
ack = sht_comwrite(0x03); //send measure temp command and read ack status |
183 |
if(ack == 1) return; |
192 |
if(ack == 1) return; |
184 |
comwait(); //wait for SHT75 measurement to complete |
193 |
sht_comwait(); //wait for SHT75 measurement to complete |
185 |
iobyte = comread(); //read SHT75 temp data |
194 |
iobyte = sht_comread(); //read SHT75 temp data |
186 |
return(iobyte); |
195 |
return(iobyte); |
187 |
} |
196 |
} |
188 |
|
197 |
|
189 |
|
198 |
|
190 |
//***** Function to measure SHT75 RH ***** |
199 |
//***** Function to measure SHT75 RH ***** |
191 |
|
200 |
|
192 |
int16 measurehumid (void) |
201 |
int16 sht_measurehumid (void) |
193 |
{ |
202 |
{ |
194 |
int1 ack; |
203 |
int1 ack; |
195 |
int16 iobyte; |
204 |
int16 iobyte; |
196 |
|
205 |
|
197 |
comstart(); //alert SHT75 |
206 |
sht_comstart(); //alert SHT75 |
198 |
ack = comwrite(0x05); //send measure RH command and read ack status |
207 |
ack = sht_comwrite(0x05); //send measure RH command and read ack status |
199 |
if(ack == 1) return; |
208 |
if(ack == 1) return; |
200 |
comwait(); //wait for SHT75 measurement to complete |
209 |
sht_comwait(); //wait for SHT75 measurement to complete |
201 |
iobyte = comread(); //read SHT75 temp data |
210 |
iobyte = sht_comread(); //read SHT75 temp data |
202 |
return(iobyte); |
211 |
return(iobyte); |
203 |
} |
212 |
} |
204 |
|
213 |
|
205 |
|
214 |
|
206 |
//***** Function to calculate SHT75 temp & RH ***** |
215 |
//***** Function to calculate SHT75 temp & RH ***** |
207 |
|
216 |
|
208 |
void calculate_data (int16 temp, int16 humid, float & tc, float & rhlin, float & rhtrue) |
217 |
void calculate_data (int16 temp, int16 humid, float & tc, float & rhlin, float & rhtrue) |
209 |
{ |
218 |
{ |
210 |
float truehumid1, rh; |
219 |
float rh; |
211 |
|
220 |
|
212 |
//calculate temperature reading |
221 |
//calculate temperature reading |
213 |
tc = ((float) temp * 0.01) - 40.0; |
222 |
tc = ((float) temp * 0.01) - 40.0; |
214 |
|
223 |
|
215 |
//calculate Real RH reading |
224 |
//calculate Real RH reading |
Line 228... |
Line 237... |
228 |
{ |
237 |
{ |
229 |
int16 restemp, reshumid; |
238 |
int16 restemp, reshumid; |
230 |
float realhumid; |
239 |
float realhumid; |
231 |
restemp = 0; truehumid = 0; |
240 |
restemp = 0; truehumid = 0; |
232 |
|
241 |
|
233 |
restemp = measuretemp(); //measure temp |
242 |
restemp = sht_measuretemp(); //measure temp |
234 |
reshumid = measurehumid(); //measure RH |
243 |
reshumid = sht_measurehumid(); //measure RH |
235 |
calculate_data (restemp, reshumid, temp, realhumid, truehumid); //calculate temp & RH |
244 |
calculate_data (restemp, reshumid, temp, realhumid, truehumid); //calculate temp & RH |
236 |
} |
245 |
} |
237 |
|
246 |
|
238 |
|
247 |
|
239 |
//***** Function to initialise SHT75 on power-up ***** |
248 |
//***** Function to initialise SHT75 on power-up ***** |
240 |
|
249 |
|
241 |
void sht_init (void) |
250 |
void sht_init (void) |
242 |
{ |
251 |
{ |
243 |
comreset(); //reset SHT75 |
252 |
sht_comreset(); //reset SHT75 |
244 |
delay_ms(20); //delay for power-up |
253 |
delay_ms(20); //delay for power-up |
245 |
} |
254 |
} |