📄 3_ds.lst
字号:
209 2 default : break;
210 2 }
211 1 for (i=0;i<65535;i++) if(DATA==0) break; //wait until sensor has finished the measurement
212 1 if(DATA) error+=1; // or timeout (~2 sec.) is reached
213 1 *(p_value) =s_read_byte(ACK); //read the first byte (MSB)
214 1 *(p_value+1)=s_read_byte(ACK); //read the second byte (LSB)
215 1 *p_checksum =s_read_byte(noACK); //read checksum
216 1 return error;
217 1 }
218
219 //----------------------------------------------------------------------------------
220 /*void init_uart()
221 //----------------------------------------------------------------------------------
222 //9600 bps @ 11.059 MHz
223 {SCON = 0x52;
224 TMOD = 0x20;
225 TCON = 0x69;
226 TH1 = 0xfd;
227 }
228 */
229 //----------------------------------------------------------------------------------------
230 void calc_sth11(float *p_humidity ,float *p_temperature)
231 //----------------------------------------------------------------------------------------
232 // calculates temperature [癈] and humidity [%RH]
233 // input : humi [Ticks] (12 bit)
234 // temp [Ticks] (14 bit)
235 // output: humi [%RH]
236 // temp [癈]
237 { const float C1=-4.0; // for 12 Bit
238 1 const float C2=+0.0405; // for 12 Bit
239 1 const float C3=-0.0000028; // for 12 Bit
240 1 const float T1=+0.01; // for 14 Bit @ 5V
241 1 const float T2=+0.00008; // for 14 Bit @ 5V
C51 COMPILER V8.09 3_DS 01/20/2009 09:40:18 PAGE 5
242 1
243 1 float rh=*p_humidity; // rh: Humidity [Ticks] 12 Bit
244 1 float t=*p_temperature; // t: Temperature [Ticks] 14 Bit
245 1 float rh_lin; // rh_lin: Humidity linear
246 1 float rh_true; // rh_true: Temperature compensated humidity
247 1 float t_C; // t_C : Temperature [癈]
248 1
249 1 t_C=t*0.01 - 40; //calc. temperature from ticks to [癈]
250 1 rh_lin=C3*rh*rh + C2*rh + C1; //calc. humidity from ticks to [%RH]
251 1 rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. temperature compensated humidity [%RH]
252 1 if(rh_true>100)rh_true=100; //cut if the value is outside of
253 1 if(rh_true<0.1)rh_true=0.1; //the physical possible range
254 1
255 1 *p_temperature=t_C; //return temperature [癈]
256 1 *p_humidity=rh_true; //return humidity[%RH]
257 1 }
258
259 //--------------------------------------------------------------------
260 /*float calc_dewpoint(float h,float t)
261 //--------------------------------------------------------------------
262 // calculates dew point
263 // input: humidity [%RH], temperature [癈]
264 // output: dew point [癈]
265 { float logEx,dew_point;
266 logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2);
267 dew_point = (logEx - 0.66077)*237.3/(0.66077+7.5-logEx);
268 return dew_point;
269 }
270 */
271
272 /*****************************************/
273 /* Copyright (c) 2005, 通信工程学院 */
274 /* All rights reserved. */
275 /* 作 者:戴 佳 */
276 /*****************************************/
277
278 //#include "DigThermo.h"
279
280 /* 延时t毫秒 */
281 void delay(uint t)
282 {
283 1 uint i;
284 1 while(t--)
285 1 {
286 2 /* 对于11.0592M时钟,约延时1ms */
287 2 for (i=0;i<125;i++)
288 2 {}
289 2 }
290 1 }
291
292 /* 产生复位脉冲初始化DS18B20 */
293 void TxReset(void)
294 {
295 1 uint i;
296 1 P0 = 0x00;
297 1
298 1 /* 拉低约900us */
299 1 i = 100;
300 1 while (i>0) i--;
301 1
302 1 P0 = ds_num; // 产生上升沿
303 1 i = 4;
C51 COMPILER V8.09 3_DS 01/20/2009 09:40:18 PAGE 6
304 1 while (i>0) i--;
305 1 }
306
307 /* 等待应答脉冲 */
308 void RxWait(void)
309 {
310 1 uint i;
311 1 while(P0);
312 1 while(P0==ds_num); // 检测到应答脉冲
313 1 i = 4;
314 1 while (i>0) i--;
315 1 }
316
317 /* 读取数据的一位,满足读时隙要求 */
318 uchar RdBit(void)
319 {
320 1 uint i;
321 1 uchar b;
322 1 P0 = 0x00;
323 1 i++;
324 1 P0 = ds_num;
325 1 i++;i++; // 延时15us以上,读时隙下降沿后15us,DS18B20输出数据才有效
326 1 b = P0;
327 1 i = 8;
328 1 while(i>0) i--;
329 1 return (b);
330 1 }
331
332 /* 读取数据的一个字节 */
333 uchar RdByte(void)
334 {
335 1 //float k;
336 1 unsigned int xdata uiData[8];
337 1 unsigned char xdata Mask; //OS the resoult of Temperature
338 1 unsigned char xdata p_tem[16];
339 1 uint i,j,b;
340 1 //uchar i,j,b;
341 1 //b = 0;
342 1 for (i=0;i<=15;i++)
343 1 {
344 2 p_tem[i] = RdBit();
345 2 //b = (j<<7)|(b>>1);
346 2 }
347 1 //return(b);
348 1
349 1 for(i=16;i>0;--i)
350 1 {
351 2 b=i-1;
352 2 Mask = 0x01;
353 2 for(j=0;j<8;j++)
354 2 {
355 3 uiData[j] = uiData[j]<<1;
356 3 if(p_tem[b]&Mask) uiData[j]++;
357 3 Mask = Mask<<1;
358 3 }
359 2 }
360 1 //k=uiData[0]*0.0625;
361 1 ds1.ds1_temp=uiData[0]*6.25;
362 1 ds2.ds2_temp=uiData[1]*6.25;
363 1 ds3.ds3_temp=uiData[2]*6.25;
364 1 }
*** WARNING C173 IN LINE 364 OF 3_DS.C: missing return-expression
C51 COMPILER V8.09 3_DS 01/20/2009 09:40:18 PAGE 7
365
366 /* 写数据的一个字节,满足写1和写0的时隙要求 */
367 void WrByte(uchar b)
368 {
369 1 uint i;
370 1 uchar j;
371 1 bit btmp;
372 1 for(j=1;j<=8;j++)
373 1 {
374 2 btmp = b&0x01;
375 2 b = b>>1; // 取下一位(由低位向高位)
376 2 if (btmp)
377 2 {
378 3 /* 写1 */
379 3 P0 = 0;
380 3 i++;i++; // 延时,使得15us以内拉高
381 3 P0 = ds_num;
382 3 i = 8;
383 3 while(i>0) i--; // 整个写1时隙不低于60us
384 3 }
385 2 else
386 2 {
387 3 /* 写0 */
388 3 P0 = 0;
389 3 i = 8;
390 3 while(i>0) i--; // 保持低在60us到120us之间
391 3 P0 = ds_num;
392 3 i++;
393 3 i++;
394 3 }
395 2 }
396 1 }
397
398 /* 启动温度转换 */
399 void convert(void)
400 {
401 1 TxReset(); // 产生复位脉冲,初始化DS18B20
402 1 RxWait(); // 等待DS18B20给出应答脉冲
403 1 delay(1); // 延时
404 1 WrByte(0xcc); // skip rom 命令
405 1 WrByte(0x44); // convert T 命令
406 1 }
407
408 /* 读取温度值 */
409 void RdTemp(void)
410 {
411 1 TxReset(); // 产生复位脉冲,初始化DS18B20
412 1 RxWait(); // 等待DS18B20给出应答脉冲
413 1 delay(1); // 延时
414 1 WrByte(0xcc); // skip rom 命令
415 1 WrByte(0xbe); // read scratchpad 命令
416 1 //tplsb = RdByte(); // 温度值低位字节(其中低4位为二进制的“小数”部分)
417 1 //tpmsb = RdByte(); // 高位值高位字节(其中高5位为符号位)
418 1 RdByte();
419 1 }
420
421 /* 主程序,读取的温度值最终存放在tplsb和tpmsb变量中。
422 tplsb其中低4位为二进制的“小数”部分;tpmsb其中高
423 5位为符号位。真正通过数码管输出时,需要进行到十进
424 制有符号实数(包括小数部分)的转换。 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -