📄 sht10.lst
字号:
189 1 else DATA=1; //如果是校验(ack==0),读取完后结束通讯
190 1 _nop_();_nop_();_nop_(); //pulswith approx. 3 us
191 1 SCK=1; //clk #9 for ack
192 1 _nop_();_nop_();_nop_(); //pulswith approx. 3 us
193 1 SCK=0;
194 1 _nop_();_nop_();_nop_(); //pulswith approx. 3 us
195 1 DATA=1; //release DATA-line
196 1 return val;
197 1 }
198
199 /*--------------------------------------
200 ;模块名称:s_measure();
201 ;功 能:测量温湿度函数
202 ;占用资源:--
203 ;参数说明:--
204 ;创建日期:2008.08.15
205 ;版 本:FV1.0(函数版本Function Version)
206 ;修改日期:--
207 ;修改说明:--
208 ;-------------------------------------*/
209 char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
210 // makes a measurement (humidity/temperature) with checksum
211 {
212 1 unsigned error=0;
213 1 unsigned int i;
214 1
215 1 s_transstart(); //transmission start
216 1 switch(mode){ //send command to sensor
217 2 case TEMP : error+=s_write_byte(MEASURE_TEMP); break;
218 2 case HUMI : error+=s_write_byte(MEASURE_HUMI); break;
219 2 default : break;
220 2 }
221 1 for (i=0;i<65535;i++) if(DATA==0) break; //wait until sensor has finished the measurement
222 1 if(DATA) error+=1; // or timeout (~2 sec.) is reached
223 1 *(p_value) =s_read_byte(ACK); //read the first byte (MSB)
224 1 *(p_value+1)=s_read_byte(ACK); //read the second byte (LSB)
225 1 *p_checksum =s_read_byte(noACK); //read checksum
226 1 return error;
227 1 }
228
229 /*--------------------------------------
230 ;模块名称:calc_dht90();
231 ;功 能:温湿度补偿函数
232 ;占用资源:--
233 ;参数说明:--
234 ;创建日期:2008.08.15
235 ;版 本:FV1.0(函数版本Function Version)
236 ;修改日期:--
237 ;修改说明:--
238 ;-------------------------------------*/
239 void calc_dht90(float *p_humidity ,float *p_temperature)
240 // calculates temperature [C] and humidity [%RH]
241 // input : humi [Ticks] (12 bit)
C51 COMPILER V8.15 SHT10 08/06/2009 13:49:09 PAGE 5
242 // temp [Ticks] (14 bit)
243 // output: humi [%RH]
244 // temp [C]
245 { const float C1=-4.0; // for 12 Bit
246 1 const float C2=+0.0405; // for 12 Bit
247 1 const float C3=-0.0000028; // for 12 Bit
248 1 const float T1=+0.01; // for 14 Bit @ 5V
249 1 const float T2=+0.00008; // for 14 Bit @ 5V
250 1
251 1 float rh=*p_humidity; // rh: Humidity [Ticks] 12 Bit
252 1 float t=*p_temperature; // t: Temperature [Ticks] 14 Bit
253 1 float rh_lin; // rh_lin: Humidity linear
254 1 float rh_true; // rh_true: Temperature compensated humidity
255 1 float t_C; // t_C : Temperature [C]
256 1
257 1 t_C=t*0.01 - 40; //calc. temperature from ticks to [C]
258 1 rh_lin=C3*rh*rh + C2*rh + C1; //calc. humidity from ticks to [%RH]
259 1 rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. temperature compensated humidity [%RH]
260 1 if(rh_true>100)rh_true=100; //cut if the value is outside of
261 1 if(rh_true<0.1)rh_true=0.1; //the physical possible range
262 1
263 1 *p_temperature=t_C; //return temperature [C]
264 1 *p_humidity=rh_true; //return humidity[%RH]
265 1 }
266
267 //*********************第二部分DHT90设置 END****************************************
268
269 void Display1(tempvalue) //在数码管上显示温度
270 {
271 1 uint i,temp;
272 1 temp = tempvalue;
273 1 sel1= temp%10;
274 1 temp= temp/10;
275 1 sel2= temp%10;
276 1 temp= temp/10;
277 1 sel3= temp%10;
278 1 temp= temp/10;
279 1 sel4= temp%10;
280 1 sel8= 10;
281 1 while(i<=100)
282 1 {
283 2 led_showdian();
284 2 i++;
285 2 }
286 1
287 1 }
288
289 void Display2(tempvalue2) //在数码管上显示湿度
290 {
291 1 uint i,temp;
292 1 temp = tempvalue2;
293 1 sel1= temp%10;
294 1 temp= temp/10;
295 1 sel2= temp%10;
296 1 temp= temp/10;
297 1 sel3= temp%10;
298 1 temp= temp/10;
299 1 sel4= temp%10;
300 1 sel8= 11;
301 1 while(i<=100)
302 1 {
303 2 led_showdian();
C51 COMPILER V8.15 SHT10 08/06/2009 13:49:09 PAGE 6
304 2 i++;
305 2 }
306 1
307 1 }
308 //*********主函数*****************
309 void SHT10()
310 {
311 1 value humi_val,temp_val;
312 1 unsigned char error,checksum;
313 1
314 1
315 1 s_connectionreset();
316 1
317 1 delay_n10us(20000); //延时0.2s
318 1
319 1 while(1)
320 1 { error=0;
321 2 error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI); //measure humidity
322 2 error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP); //measure temperature
323 2 if(error!=0) s_connectionreset(); //in case of an error: connection
324 2 else
325 2 { humi_val.f=(float)humi_val.i; //converts integer to float
326 3 temp_val.f=(float)temp_val.i; //converts integer to float
327 3 calc_dht90(&humi_val.f,&temp_val.f); //calculate humidity, temperature
328 3 //wenduf=temp_val.f;
329 3 //shiduf=humi_val.f;
330 3
331 3 wendu=10*temp_val.f;
332 3 shidu=10*humi_val.f;
333 3
334 3 if(flag==1) //如果测量的是温度
335 3 {
336 4 Display1(wendu);
337 4 }
338 3 else if(flag==2) //如果测量的是湿度
339 3 {
340 4 Display2(shidu);
341 4 }
342 3 else if(flag==3) //如果上位机测量
343 3 {
344 4 //wenduf=temp_val.f;
345 4 //shiduf=humi_val.f;
346 4 break;
347 4 }
348 3
349 3 KeyScan();
350 3 KeyFree();
351 3 if(key==1) //如果要退出
352 3 {
353 4 mDelay(100);
354 4 if(key==1)
355 4 {
356 5 key=0;
357 5 P0=0xff;
358 5 wendu = 0;
359 5 shidu = 0;
360 5 P0=0xff;
361 5 flag=0;
362 5 //wenduf=temp_val.f;
363 5 //shiduf=humi_val.f;
364 5 break;
365 5 }
C51 COMPILER V8.15 SHT10 08/06/2009 13:49:09 PAGE 7
366 4 }
367 3 delay_n10us(80000); //延时约0.8s
368 3 }
369 2 }
370 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 1235 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 18 69
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -