Rev 2779 Rev 2836
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 // //
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 // 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
30 delay_us(2); 35 delay_us(2);
31 output_low(sht_data_pin); //data low 36 output_low(sht_data_pin); //data low
32 delay_us(2); 37 delay_us(2);
33 output_low(sht_clk_pin); //clk low 38 output_low(sht_clk_pin); //clk low
34 delay_us(5); 39 delay_us(5);
35 output_high(sht_clk_pin); //clk high 40 output_high(sht_clk_pin); //clk high
36 delay_us(2); 41 delay_us(2);
37 output_float(sht_data_pin); //data high 42 output_float(sht_data_pin); //data high
38 delay_us(2); 43 delay_us(2);
39 output_low(sht_clk_pin); //clk low 44 output_low(sht_clk_pin); //clk low
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
51 delay_us(5); 56 delay_us(5);
52 for(i=0; i<8; i++) 57 for(i=0; i<8; i++)
53 { 58 {
54 output_low(sht_clk_pin); //clk low 59 output_low(sht_clk_pin); //clk low
55 if((iobyte & mask) > 0) output_float(sht_data_pin); //data high if MSB high 60 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 61 else output_low(sht_data_pin); //data low if MSB low
57 delay_us(2); 62 delay_us(2);
58 output_high(sht_clk_pin); //clk high 63 output_high(sht_clk_pin); //clk high
59 delay_us(2); 64 delay_us(2);
60 mask = mask >> 1; //shift to next bit 65 mask = mask >> 1; //shift to next bit
61 } 66 }
62   67  
63 //Shift in ack 68 //Shift in ack
64 output_low(sht_clk_pin); //clk low 69 output_low(sht_clk_pin); //clk low
65 delay_us(2); 70 delay_us(2);
66 ack = input(sht_data_pin); //get ack bit 71 ack = input(sht_data_pin); //get ack bit
67 output_high(sht_clk_pin); //clk high 72 output_high(sht_clk_pin); //clk high
68 delay_us(2); 73 delay_us(2);
69 output_low(sht_clk_pin); //clk low 74 output_low(sht_clk_pin); //clk low
70 return(ack); 75 return(ack);
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;
82   87  
83 //shift in MSB data 88 //shift in MSB data
84 for(i=0; i<8; i++) 89 for(i=0; i<8; i++)
85 { 90 {
86 iobyte = iobyte << 1; 91 iobyte = iobyte << 1;
87 output_high(sht_clk_pin); //clk high 92 output_high(sht_clk_pin); //clk high
88 delay_us(2); 93 delay_us(2);
89 if (input(sht_data_pin)) iobyte |= mask1; //shift in data bit 94 if (input(sht_data_pin)) iobyte |= mask1; //shift in data bit
90 else iobyte |= mask0; 95 else iobyte |= mask0;
91 output_low(sht_clk_pin); //clk low 96 output_low(sht_clk_pin); //clk low
92 delay_us(2); 97 delay_us(2);
93 } 98 }
94   99  
95 //send ack 0 bit 100 //send ack 0 bit
96 output_low(sht_data_pin); //data low 101 output_low(sht_data_pin); //data low
97 delay_us(2); 102 delay_us(2);
98 output_high(sht_clk_pin); //clk high 103 output_high(sht_clk_pin); //clk high
99 delay_us(5); 104 delay_us(5);
100 output_low(sht_clk_pin); //clk low 105 output_low(sht_clk_pin); //clk low
101 delay_us(2); 106 delay_us(2);
102 output_float(sht_data_pin); //data high 107 output_float(sht_data_pin); //data high
103   108  
104 //shift in LSB data 109 //shift in LSB data
105 for(i=0; i<8; i++) 110 for(i=0; i<8; i++)
106 { 111 {
107 iobyte = iobyte << 1; 112 iobyte = iobyte << 1;
108 output_high(sht_clk_pin); //clk high 113 output_high(sht_clk_pin); //clk high
109 delay_us(2); 114 delay_us(2);
110 if (input(sht_data_pin)) iobyte |= mask1; //shift in data bit 115 if (input(sht_data_pin)) iobyte |= mask1; //shift in data bit
111 else iobyte |= mask0; 116 else iobyte |= mask0;
112 output_low(sht_clk_pin); //clk low 117 output_low(sht_clk_pin); //clk low
113 delay_us(2); 118 delay_us(2);
114 } 119 }
115   120  
116 //send ack 1 bit 121 //send ack 1 bit
117 output_float(sht_data_pin); //data high 122 output_float(sht_data_pin); //data high
118 delay_us(2); 123 delay_us(2);
119 output_high(sht_clk_pin); //clk high 124 output_high(sht_clk_pin); //clk high
120 delay_us(5); 125 delay_us(5);
121 output_low(sht_clk_pin); //clk low 126 output_low(sht_clk_pin); //clk low
122   127  
123 return(iobyte); 128 return(iobyte);
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
152 delay_us(5); 161 delay_us(5);
153 for(i=0; i<9; i++) 162 for(i=0; i<9; i++)
154 { 163 {
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
216 rh = (float) humid; 225 rh = (float) humid;
217   226  
218 rhlin = (rh * 0.0405) - (rh * rh * 0.0000028) - 4.0; 227 rhlin = (rh * 0.0405) - (rh * rh * 0.0000028) - 4.0;
219   228  
220 //calculate True RH reading 229 //calculate True RH reading
221 rhtrue = ((tc - 25.0) * (0.01 + (0.00008 * rh))) + rhlin; 230 rhtrue = ((tc - 25.0) * (0.01 + (0.00008 * rh))) + rhlin;
222 } 231 }
223   232  
224   233  
225 //***** Function to measure & calculate SHT75 temp & RH ***** 234 //***** Function to measure & calculate SHT75 temp & RH *****
226   235  
227 void sht_rd (float & temp, float & truehumid) 236 void sht_rd (float & temp, float & truehumid)
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 }