📄 18b20_1602.lst
字号:
220 1
221 1 b = DO; //读取数据
222 1 Delayus(40); //每个读时隙至少持续60us
223 1
224 1 return(b);
225 1 }
226
227 /*读取一个字节*/
228 uchar Readbyte()
229 {
230 1 uchar byte_read = 0;
231 1 uchar i, j;
232 1
233 1 for(i=0; i<8; i++)
234 1 {
235 2 j = Readbit();
236 2 byte_read = (j<<i) | byte_read; //低位读起
237 2 }
238 1
239 1 return(byte_read);
240 1 }
241
C51 COMPILER V8.02 18B20_1602 06/16/2007 18:49:50 PAGE 5
242 /*写一个字节*/
243 void Writebyte(uchar byte_to_write)
244 {
245 1 uchar i = 0;
246 1 uchar j = 0;
247 1 bit write_bit = 0;
248 1
249 1 for(j=0; j<8; j++)
250 1 {
251 2 write_bit = (byte_to_write & 0x01);
252 2 if(write_bit == 1) //写1
253 2 {
254 3 DO = 0; //产生写时隙
255 3 Delayus(3); //延时15us
256 3
257 3 DO = 1; //写1
258 3 Delayus(40); //延时,写时隙不得低于60us
259 3 }
260 2 else
261 2 {
262 3 DO = 0; //产生写时隙
263 3 Delayus(50); //延时,保持低约60us~120us
264 3 DO = 1;
265 3 i++;
266 3 }
267 2 byte_to_write = byte_to_write >> 1;
268 2 }
269 1 }
270
271
272 /*启动温度转换*/
273 void StartConvert()
274 {
275 1 Resetpaulse(); // 发出复位脉冲,每次操作都从复位开始
276 1 Delay(1);
277 1 EA = 0;
278 1 Writebyte(0xcc); //skip room命令
279 1 Writebyte(0x44); //启动温度转换命令
280 1 EA = 1;
281 1 }
282
283 /*读取温度值*/
284 void ReadTempreture()
285 {
286 1 EA = 0;
287 1 Resetpaulse(); // 发出复位脉冲,每次操作都从复位开始
288 1 Delay(1);
289 1 Writebyte(0xcc); //skip room命令
290 1 Writebyte(0xbe); //读取暂存器命令
291 1 temp_l = Readbyte(); //存储温度低字节值 (整数部分低四位和小数部分)
292 1 temp_h = Readbyte(); //存储温度高字节值 (其中高五位为符号位)
293 1 EA = 1;
294 1 }
295
296 /*数据处理程序*/
297 void Digital_process()
298 {
299 1 uchar total = 0;
300 1 uchar low = 0;
301 1 uint dicimal = 0;
302 1
303 1 tempsign = (temp_h >> 7) & 0x01; //得出符号位
C51 COMPILER V8.02 18B20_1602 06/16/2007 18:49:50 PAGE 6
304 1 total = ((temp_h << 4)&0xf0) | ((temp_l >> 4)&0x0f); //取整数位
305 1 low = temp_l & 0x0f; //取小数位
306 1
307 1 if(tempsign == 0)
308 1 {
309 2 temp_integer[0] = total / 100 + '0'; //计算百、十、个位
310 2 temp_integer[1] = (total%100)/10 + '0';
311 2 temp_integer[2] = (total%100)%10 + '0';
312 2 temp_integer[3] = '\0';
313 2 if(temp_integer[0] == '0')
314 2 {
315 3 if(temp_integer[1] != '0')
316 3 {
317 4 temp_integer[0] = '\0'; //百位零消隐
318 4
319 4 }
320 3 else if(temp_integer[1] == '0')
321 3 {
322 4 temp_integer[0] = '\0'; //百位,十位零都消隐
323 4 temp_integer[1] = '\0';
324 4 }
325 3 }
326 2 dicimal = low * 625; //计算小数
327 2 temp_dicimal[0] = dicimal / 1000 + '0'; //十分位
328 2 temp_dicimal[1] = dicimal % 1000 /100 + '0'; //百分位
329 2 temp_dicimal[2] = dicimal % 100 / 10 + '0'; //千分位
330 2 temp_dicimal[3] = dicimal % 10 + '0'; //万分位
331 2 temp_dicimal[4] = '\0'; //数组加一个空字符(好像系统也会自动加上的?)
332 2 }
333 1
334 1 else if(tempsign == 1) //负数处理
335 1 {
336 2 if(low == 0x00) //负数要取反加一再乘以0.0625就是实际温度值了,我这里没有设那么多int型变量,
337 2 {
338 3 total = ~total + 1; //所以就用了这么一个计算方法
339 3 low &= 0x0f;
340 3 } /*具体一点讲,小树低四位为全零时取反加一要有进位,此时只要整数位取反加一即可,
341 2 小数位不用理会,其余情况整数位取反,小数位取反加一*/
342 2 else
343 2 {
344 3 total = ~total ;
345 3 low = (~low) + 1;
346 3 low &= 0x0f; //注意高四位要变成零
347 3 }
348 2 temp_integer[1] = (total%100)/10 + '0'; //计算十、个位
349 2 temp_integer[2] = (total%100)%10 + '0';
350 2 temp_integer[3] = '\0';
351 2
352 2
353 2 if(temp_integer[1] == '0')
354 2 {
355 3 temp_integer[1] = '\0';
356 3 }
357 2 dicimal = low * 625;
358 2 temp_dicimal[0] = dicimal / 1000 + '0';
359 2 temp_dicimal[1] = dicimal % 1000 /100 + '0';
360 2 temp_dicimal[2] = dicimal % 100 / 10 + '0';
361 2 temp_dicimal[3] = dicimal % 10 + '0';
362 2 temp_dicimal[4] = '\0';
363 2 }
364 1
365 1
C51 COMPILER V8.02 18B20_1602 06/16/2007 18:49:50 PAGE 7
366 1 }
367
368
369
370 void main()
371 {
372 1 bit palse = 0;
373 1 Initial_LCD();
374 1
375 1 GotoXY(0,0);
376 1 Print("CHECKING...",12);
377 1 Delay(3000);
378 1
379 1
380 1 palse = Resetpaulse(); //检测DS18B20是否响应
381 1 if(palse)
382 1 {
383 2 Initial_LCD();
384 2 GotoXY(0,0);
385 2 Print("DS18B20 OK",11);
386 2 }
387 1 else
388 1 {
389 2 Initial_LCD();
390 2 GotoXY(0,0);
391 2 Print("DS18B20 ERROR",13);
392 2 while(1);
393 2
394 2 }
395 1
396 1 do{
397 2 Delay(1);
398 2 StartConvert();
399 2 Delay(1020);
400 2 ReadTempreture();
401 2 Digital_process();
402 2
403 2 if(tempsign == 0) //显示正值温度
404 2 {
405 3 GotoXY(0,1);
406 3 Print("TEMP:",5);
407 3 GotoXY(5,1);
408 3 Print(temp_integer,3);
409 3 GotoXY(8,1);
410 3 Print(".",1);
411 3 GotoXY(9,1);
412 3 Print(temp_dicimal,4);
413 3 }
414 2 else //显示负值温度
415 2 {
416 3 GotoXY(0,1);
417 3 Print("TEMP:",5);
418 3 GotoXY(5,1);
419 3 Print("-",1);
420 3 GotoXY(6,1);
421 3 Print(temp_integer + 1,2);
422 3 GotoXY(8,1);
423 3 Print(".",1);
424 3 GotoXY(9,1);
425 3 Print(temp_dicimal,4);
426 3 }
427 2 }
C51 COMPILER V8.02 18B20_1602 06/16/2007 18:49:50 PAGE 8
428 1 while(1);
429 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 980 ----
CONSTANT SIZE = 47 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 12 3
IDATA SIZE = ---- ----
BIT SIZE = ---- 4
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -