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