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