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