2779 |
kaklik |
1 |
/////////////////////////////////////////////////////////////////////////////// |
|
|
2 |
// // |
2836 |
kaklik |
3 |
// Driver file for SHT1x Temperature & Humidity Sensor // |
2779 |
kaklik |
4 |
// // |
2836 |
kaklik |
5 |
// ***** To initialise SHT1x sensor upon power up ***** // |
2779 |
kaklik |
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_D0 |
|
|
19 |
//#define sht_clk_pin PIN_D1 |
|
|
20 |
|
2907 |
kaklik |
21 |
//#include <math.h> |
2779 |
kaklik |
22 |
|
2837 |
kaklik |
23 |
|
|
|
24 |
#define noACK 0 |
|
|
25 |
#define ACK 1 |
|
|
26 |
//adr command r/w |
|
|
27 |
#define STATUS_REG_W 0x06 //000 0011 0 |
|
|
28 |
#define STATUS_REG_R 0x07 //000 0011 1 |
|
|
29 |
#define MEASURE_TEMP 0x03 //000 0001 1 |
|
|
30 |
#define MEASURE_HUMI 0x05 //000 0010 1 |
|
|
31 |
#define RESET 0x1e //000 1111 0 |
|
|
32 |
|
2779 |
kaklik |
33 |
//***** Function to alert SHT75 ***** |
2836 |
kaklik |
34 |
// generates a transmission start |
|
|
35 |
// _____ ________ |
|
|
36 |
// DATA: |_______| |
|
|
37 |
// ___ ___ |
|
|
38 |
// SCK : ___| |___| |______ |
2779 |
kaklik |
39 |
|
2836 |
kaklik |
40 |
void sht_comstart (void) |
2779 |
kaklik |
41 |
{ |
|
|
42 |
output_float(sht_data_pin); //data high |
|
|
43 |
output_low(sht_clk_pin); //clk low |
|
|
44 |
delay_us(2); |
|
|
45 |
output_high(sht_clk_pin); //clk high |
|
|
46 |
delay_us(2); |
|
|
47 |
output_low(sht_data_pin); //data low |
|
|
48 |
delay_us(2); |
|
|
49 |
output_low(sht_clk_pin); //clk low |
|
|
50 |
delay_us(5); |
|
|
51 |
output_high(sht_clk_pin); //clk high |
|
|
52 |
delay_us(2); |
|
|
53 |
output_float(sht_data_pin); //data high |
|
|
54 |
delay_us(2); |
|
|
55 |
output_low(sht_clk_pin); //clk low |
|
|
56 |
} |
|
|
57 |
|
|
|
58 |
|
|
|
59 |
//***** Function to write data to SHT75 ***** |
|
|
60 |
|
2836 |
kaklik |
61 |
int1 sht_comwrite (int8 iobyte) |
2779 |
kaklik |
62 |
{ |
|
|
63 |
int8 i, mask = 0x80; |
|
|
64 |
int1 ack; |
|
|
65 |
|
|
|
66 |
//Shift out command |
|
|
67 |
delay_us(5); |
|
|
68 |
for(i=0; i<8; i++) |
|
|
69 |
{ |
|
|
70 |
output_low(sht_clk_pin); //clk low |
|
|
71 |
if((iobyte & mask) > 0) output_float(sht_data_pin); //data high if MSB high |
|
|
72 |
else output_low(sht_data_pin); //data low if MSB low |
|
|
73 |
delay_us(2); |
|
|
74 |
output_high(sht_clk_pin); //clk high |
|
|
75 |
delay_us(2); |
|
|
76 |
mask = mask >> 1; //shift to next bit |
|
|
77 |
} |
|
|
78 |
|
|
|
79 |
//Shift in ack |
|
|
80 |
output_low(sht_clk_pin); //clk low |
|
|
81 |
delay_us(2); |
|
|
82 |
ack = input(sht_data_pin); //get ack bit |
|
|
83 |
output_high(sht_clk_pin); //clk high |
|
|
84 |
delay_us(2); |
|
|
85 |
output_low(sht_clk_pin); //clk low |
|
|
86 |
return(ack); |
|
|
87 |
} |
|
|
88 |
|
|
|
89 |
|
|
|
90 |
//***** Function to read data from SHT75 ***** |
|
|
91 |
|
2836 |
kaklik |
92 |
int16 sht_comread (void) |
2779 |
kaklik |
93 |
{ |
|
|
94 |
int8 i; |
|
|
95 |
int16 iobyte = 0; |
|
|
96 |
const int16 mask0 = 0x0000; |
|
|
97 |
const int16 mask1 = 0x0001; |
|
|
98 |
|
|
|
99 |
//shift in MSB data |
|
|
100 |
for(i=0; i<8; i++) |
|
|
101 |
{ |
|
|
102 |
iobyte = iobyte << 1; |
|
|
103 |
output_high(sht_clk_pin); //clk high |
|
|
104 |
delay_us(2); |
|
|
105 |
if (input(sht_data_pin)) iobyte |= mask1; //shift in data bit |
|
|
106 |
else iobyte |= mask0; |
|
|
107 |
output_low(sht_clk_pin); //clk low |
|
|
108 |
delay_us(2); |
|
|
109 |
} |
|
|
110 |
|
|
|
111 |
//send ack 0 bit |
|
|
112 |
output_low(sht_data_pin); //data low |
|
|
113 |
delay_us(2); |
|
|
114 |
output_high(sht_clk_pin); //clk high |
|
|
115 |
delay_us(5); |
|
|
116 |
output_low(sht_clk_pin); //clk low |
|
|
117 |
delay_us(2); |
|
|
118 |
output_float(sht_data_pin); //data high |
|
|
119 |
|
|
|
120 |
//shift in LSB data |
|
|
121 |
for(i=0; i<8; i++) |
|
|
122 |
{ |
|
|
123 |
iobyte = iobyte << 1; |
|
|
124 |
output_high(sht_clk_pin); //clk high |
|
|
125 |
delay_us(2); |
|
|
126 |
if (input(sht_data_pin)) iobyte |= mask1; //shift in data bit |
|
|
127 |
else iobyte |= mask0; |
|
|
128 |
output_low(sht_clk_pin); //clk low |
|
|
129 |
delay_us(2); |
|
|
130 |
} |
|
|
131 |
|
|
|
132 |
//send ack 1 bit |
|
|
133 |
output_float(sht_data_pin); //data high |
|
|
134 |
delay_us(2); |
|
|
135 |
output_high(sht_clk_pin); //clk high |
|
|
136 |
delay_us(5); |
|
|
137 |
output_low(sht_clk_pin); //clk low |
|
|
138 |
|
|
|
139 |
return(iobyte); |
|
|
140 |
} |
|
|
141 |
|
|
|
142 |
|
|
|
143 |
//***** Function to wait for SHT75 reading ***** |
|
|
144 |
|
2836 |
kaklik |
145 |
void sht_comwait (void) |
2779 |
kaklik |
146 |
{ |
|
|
147 |
int16 sht_delay; |
|
|
148 |
|
|
|
149 |
output_float(sht_data_pin); //data high |
|
|
150 |
output_low(sht_clk_pin); //clk low |
|
|
151 |
delay_us(2); |
2836 |
kaklik |
152 |
for(sht_delay=0; sht_delay<350; sht_delay++) // wait for max 350ms (for 14bit measurement) |
2779 |
kaklik |
153 |
{ |
|
|
154 |
if (!input(sht_data_pin)) break; //if sht_data_pin low, SHT75 ready |
2836 |
kaklik |
155 |
delay_ms(1); |
2779 |
kaklik |
156 |
} |
|
|
157 |
} |
|
|
158 |
|
|
|
159 |
|
|
|
160 |
//***** Function to reset SHT75 communication ***** |
2836 |
kaklik |
161 |
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart |
|
|
162 |
// _____________________________________________________ ________ |
|
|
163 |
// DATA: |_______| |
|
|
164 |
// _ _ _ _ _ _ _ _ _ ___ ___ |
|
|
165 |
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______ |
2837 |
kaklik |
166 |
|
2836 |
kaklik |
167 |
void sht_comreset (void) |
2779 |
kaklik |
168 |
{ |
|
|
169 |
int8 i; |
|
|
170 |
|
|
|
171 |
output_float(sht_data_pin); //data high |
|
|
172 |
output_low(sht_clk_pin); //clk low |
|
|
173 |
delay_us(5); |
|
|
174 |
for(i=0; i<9; i++) |
|
|
175 |
{ |
|
|
176 |
output_high(sht_clk_pin); //toggle clk 9 times |
|
|
177 |
delay_us(5); |
|
|
178 |
output_low(sht_clk_pin); |
|
|
179 |
delay_us(5); |
|
|
180 |
} |
2836 |
kaklik |
181 |
sht_comstart(); |
2779 |
kaklik |
182 |
} |
|
|
183 |
|
|
|
184 |
|
|
|
185 |
//***** Function to soft reset SHT75 ***** |
|
|
186 |
|
|
|
187 |
void sht_soft_reset (void) |
|
|
188 |
{ |
2836 |
kaklik |
189 |
sht_comreset(); //SHT75 communication reset |
|
|
190 |
sht_comwrite(0x1e); //send SHT75 reset command |
2779 |
kaklik |
191 |
delay_ms(15); //pause 15 ms |
|
|
192 |
} |
|
|
193 |
|
2837 |
kaklik |
194 |
//---------------------------------------------------------------------------------- |
|
|
195 |
char sht_write_statusreg(unsigned char *p_value) |
|
|
196 |
//---------------------------------------------------------------------------------- |
|
|
197 |
// writes the status register with checksum (8-bit) |
|
|
198 |
{ |
|
|
199 |
unsigned char error=0; |
|
|
200 |
sht_comstart(); //transmission start |
|
|
201 |
error+=sht_comwrite(STATUS_REG_W);//send command to sensor |
|
|
202 |
error+=sht_comwrite(*p_value); //send value of status register |
|
|
203 |
return error; //error>=1 in case of no response form the sensor |
|
|
204 |
} |
2779 |
kaklik |
205 |
|
|
|
206 |
//***** Function to measure SHT75 temperature ***** |
|
|
207 |
|
2907 |
kaklik |
208 |
int16 sht_measuretemp () |
2779 |
kaklik |
209 |
{ |
|
|
210 |
int1 ack; |
|
|
211 |
int16 iobyte; |
|
|
212 |
|
2836 |
kaklik |
213 |
sht_comstart(); //alert SHT75 |
|
|
214 |
ack = sht_comwrite(0x03); //send measure temp command and read ack status |
2779 |
kaklik |
215 |
if(ack == 1) return; |
2836 |
kaklik |
216 |
sht_comwait(); //wait for SHT75 measurement to complete |
|
|
217 |
iobyte = sht_comread(); //read SHT75 temp data |
2779 |
kaklik |
218 |
return(iobyte); |
|
|
219 |
} |
|
|
220 |
|
|
|
221 |
|
|
|
222 |
//***** Function to measure SHT75 RH ***** |
|
|
223 |
|
2907 |
kaklik |
224 |
int16 sht_measurehumid () |
2779 |
kaklik |
225 |
{ |
|
|
226 |
int1 ack; |
|
|
227 |
int16 iobyte; |
|
|
228 |
|
2836 |
kaklik |
229 |
sht_comstart(); //alert SHT75 |
|
|
230 |
ack = sht_comwrite(0x05); //send measure RH command and read ack status |
2779 |
kaklik |
231 |
if(ack == 1) return; |
2836 |
kaklik |
232 |
sht_comwait(); //wait for SHT75 measurement to complete |
|
|
233 |
iobyte = sht_comread(); //read SHT75 temp data |
2779 |
kaklik |
234 |
return(iobyte); |
|
|
235 |
} |
|
|
236 |
|
|
|
237 |
|
|
|
238 |
//***** Function to calculate SHT75 temp & RH ***** |
|
|
239 |
|
|
|
240 |
void calculate_data (int16 temp, int16 humid, float & tc, float & rhlin, float & rhtrue) |
|
|
241 |
{ |
2837 |
kaklik |
242 |
const float C1=-2.0468; // for 12 Bit RH |
|
|
243 |
const float C2=+0.0367; // for 12 Bit RH |
|
|
244 |
const float C3=-0.0000015955; // for 12 Bit RH |
2907 |
kaklik |
245 |
// const float T1=+0.01; // for 12 Bit RH |
|
|
246 |
// const float T2=+0.00008; // for 12 Bit RH |
2836 |
kaklik |
247 |
float rh; |
2779 |
kaklik |
248 |
|
|
|
249 |
//calculate temperature reading |
|
|
250 |
tc = ((float) temp * 0.01) - 40.0; |
|
|
251 |
|
|
|
252 |
//calculate Real RH reading |
|
|
253 |
rh = (float) humid; |
|
|
254 |
|
2837 |
kaklik |
255 |
rhlin = C1 + (rh * C2) + (rh * rh * C3); |
2779 |
kaklik |
256 |
|
|
|
257 |
//calculate True RH reading |
|
|
258 |
rhtrue = ((tc - 25.0) * (0.01 + (0.00008 * rh))) + rhlin; |
2837 |
kaklik |
259 |
|
|
|
260 |
if(rhtrue>99)rhtrue=100; //cut if the value is outside of |
|
|
261 |
if(rhtrue<0.1)rhtrue=0.1; //the physical possible range |
2779 |
kaklik |
262 |
} |
|
|
263 |
|
|
|
264 |
|
|
|
265 |
//***** Function to measure & calculate SHT75 temp & RH ***** |
|
|
266 |
|
|
|
267 |
void sht_rd (float & temp, float & truehumid) |
|
|
268 |
{ |
|
|
269 |
int16 restemp, reshumid; |
|
|
270 |
float realhumid; |
|
|
271 |
restemp = 0; truehumid = 0; |
|
|
272 |
|
2836 |
kaklik |
273 |
restemp = sht_measuretemp(); //measure temp |
|
|
274 |
reshumid = sht_measurehumid(); //measure RH |
2779 |
kaklik |
275 |
calculate_data (restemp, reshumid, temp, realhumid, truehumid); //calculate temp & RH |
|
|
276 |
} |
|
|
277 |
|
|
|
278 |
|
|
|
279 |
//***** Function to initialise SHT75 on power-up ***** |
|
|
280 |
|
|
|
281 |
void sht_init (void) |
|
|
282 |
{ |
2836 |
kaklik |
283 |
sht_comreset(); //reset SHT75 |
2779 |
kaklik |
284 |
delay_ms(20); //delay for power-up |
|
|
285 |
} |
2837 |
kaklik |
286 |
|
|
|
287 |
//-------------------------------------------------------------------- |
2907 |
kaklik |
288 |
//float calc_dewpoint(float h,float t) |
2837 |
kaklik |
289 |
//-------------------------------------------------------------------- |
|
|
290 |
// calculates dew point |
|
|
291 |
// input: humidity [%RH], temperature [K] |
|
|
292 |
// output: dew point [K] |
2907 |
kaklik |
293 |
//{ float k,dew_point ; |
|
|
294 |
// |
|
|
295 |
// k = (log10(h)-2)/0.4343 + (17.62*t)/(243.12+t); |
|
|
296 |
// dew_point = 243.12*k/(17.62-k); |
|
|
297 |
// return dew_point; |
|
|
298 |
//} |
2837 |
kaklik |
299 |
|