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