📄 ds18b20-lcd1602-c51.lst
字号:
229 2 dat >>= 1;
230 2 DQ = 1; // 给脉冲信号
231 2
232 2 if(DQ)
233 2 dat |= 0x80;
234 2 Delay(4);
235 2 }
236 1 return (dat);
237 1 }
238
239 /*******************************************************************/
240 /* */
241 /* 写一个字节 */
C51 COMPILER V8.08 DS18B20_LCD1602_C51 03/21/2009 19:07:17 PAGE 5
242 /* */
243 /*******************************************************************/
244 WriteOneChar(unsigned char dat)
245 {
246 1 unsigned char i = 0;
247 1 for (i = 8; i > 0; i--)
248 1 {
249 2 DQ = 0;
250 2 DQ = dat&0x01;
251 2 Delay(5);
252 2
253 2 DQ = 1;
254 2 dat>>=1;
255 2 }
256 1 }
257
258 /*******************************************************************/
259 /* */
260 /* 温度报警值写入DS18B20 */
261 /* */
262 /*******************************************************************/
263 Write_Temperature_alarm(void)
264 {
265 1 Init_DS18B20();
266 1
267 1 WriteOneChar(0xCC); //跳过读序号列号的操作
268 1 WriteOneChar(0x4e); //将设定的温度报警值写入 DS18B20
269 1 WriteOneChar(temp_alarm[0]); //写TH
270 1 WriteOneChar(temp_alarm[1]); //写TL
271 1 WriteOneChar(0x7f); //12位精确度
272 1
273 1 Init_DS18B20();
274 1 WriteOneChar(0xCC); //跳过读序号列号的操作
275 1 WriteOneChar(0x48); //把暂存器里的温度报警值拷贝到EEROM
276 1 }
277
278 /*******************************************************************/
279 /* */
280 /* 读取64位序列码 */
281 /* */
282 /*******************************************************************/
283 Read_RomCord(void)
284 {
285 1 unsigned char j;
286 1 Init_DS18B20();
287 1
288 1 WriteOneChar(0x33); // 读序列码的操作
289 1 for (j = 0; j < 8; j++)
290 1 {
291 2 RomCode[j] = ReadOneChar() ;
292 2 }
293 1 }
294
295 /*******************************************************************/
296 /* */
297 /*DS18B20的CRC8校验程序 */
298 /* */
299 /*******************************************************************/
300 uchar CRC8()
301 {
302 1 uchar i,x; uchar crcbuff;
303 1
C51 COMPILER V8.08 DS18B20_LCD1602_C51 03/21/2009 19:07:17 PAGE 6
304 1 crc=0;
305 1 for(x = 0; x <8; x++)
306 1 {
307 2 crcbuff=RomCode[x];
308 2 for(i = 0; i < 8; i++)
309 2 {
310 3 if(((crc ^ crcbuff)&0x01)==0)
311 3 crc >>= 1;
312 3 else {
313 4 crc ^= 0x18; //CRC=X8+X5+X4+1
314 4 crc >>= 1;
315 4 crc |= 0x80;
316 4 }
317 3 crcbuff >>= 1;
318 3 }
319 2 }
320 1 return crc;
321 1 }
322 /*******************************************************************/
323 /* */
324 /* 数据转换与显示 */
325 /* */
326 /*******************************************************************/
327
328 Disp_RomCode(uchar H_num)
329 {
330 1 uchar j;
331 1
332 1 if(H_num==1)
333 1 lcd_pos(0x00);
334 1 if(H_num==2)
335 1 lcd_pos(0x40);
336 1
337 1 for(j=0;j<8;j++)
338 1 {
339 2 Temp = RomCode[j];
340 2
341 2 display[0]=((Temp&0xf0)>>4);
342 2 if(display[0]>9)
343 2 { display[0]=display[0]+0x37;}
344 2 else{display[0]=display[0]+0x30;}
345 2 lcd_wdat(display[0]); //高位数显示
346 2
347 2 display[1]=(Temp&0x0f);
348 2 if(display[1]>9)
349 2 {display[1]=display[1]+0x37;}
350 2 else {display[1]=display[1]+0x30;}
351 2 lcd_wdat(display[1]); //低位数显示
352 2 }
353 1 }
354
355 /*******************************************************************/
356 /* */
357 /* 读取温度 */
358 /* */
359 /*******************************************************************/
360 Read_Temperature(void)
361 {
362 1 uchar i;
363 1 TR0=0; //关中断,防止读数错误
364 1 Init_DS18B20();
365 1
C51 COMPILER V8.08 DS18B20_LCD1602_C51 03/21/2009 19:07:17 PAGE 7
366 1 WriteOneChar(0xCC); //跳过读序号列号的操作
367 1 WriteOneChar(0x44); //启动温度转换
368 1
369 1 Init_DS18B20();
370 1
371 1 WriteOneChar(0x55); //匹配ROM命令
372 1 for(i=0;i<8;i++)
373 1 WriteOneChar(RomCode[i]);
374 1
375 1 WriteOneChar(0xBE); //读取温度寄存器
376 1
377 1 temp_data[0] = ReadOneChar(); //温度低8位
378 1 temp_data[1] = ReadOneChar(); //温度高8位
379 1 temp_alarm[0] = ReadOneChar(); //温度报警TH
380 1 temp_alarm[1] = ReadOneChar(); //温度报警TL
381 1
382 1 temp_comp=((temp_data[0]&0xf0)>>4)|((temp_data[1]&0x0f)<<4);
383 1 //取温度整数值
384 1 TR0=1; //开中断
385 1 }
386 /*******************************************************************/
387 /* */
388 /* 数据转换与温度显示 */
389 /* */
390 /*******************************************************************/
391 Disp_Temperature()
392 {
393 1 uchar s=0;
394 1 if(temp_data[1]>127) //温度为负值
395 1 {
396 2 temp_data[0]=(~temp_data[0])+1; //取反加一,将补码变成原码
397 2 if((~temp_data[0])>=0xff)
398 2 temp_data[1]=(~temp_data[1])+1;
399 2 else temp_data[1]=~temp_data[1];
400 2 s=1;
401 2 }
402 1
403 1 display[4]=temp_data[0]&0x0f;
404 1 display[0]=(display[4]*10/16)+0x30;
405 1
406 1 display[4]=((temp_data[0]&0xf0)>>4)|((temp_data[1]&0x0f)<<4);
407 1 display[3]=display[4]/100+0x30;
408 1 display[1]=display[4]%100;
409 1 display[2]=display[1]/10+0x30;
410 1 display[1]=display[1]%10+0x30;
411 1
412 1 if(display[3]==0x30) //高位为0,不显示
413 1 {
414 2 display[3]=0x20;
415 2 if(display[2]==0x30) //次高位为0,不显示
416 2 display[2]=0x20;
417 2 }
418 1 if(s)
419 1 display[3]=0x2d; //显示负号
420 1
421 1 lcd_pos(0x07);
422 1 lcd_wdat(display[3]); //百位数显示
423 1 lcd_wdat(display[2]); //十位数显示
424 1 lcd_wdat(display[1]); //个位数显示
425 1 lcd_wdat('.');
426 1 lcd_wdat(display[0]); //小数位数显示
427 1 lcd_wdat(0x00); //显示自定义字符
C51 COMPILER V8.08 DS18B20_LCD1602_C51 03/21/2009 19:07:17 PAGE 8
428 1 lcd_wdat('C'); //显示C
429 1 }
430
431 /*******************************************************************/
432 /* */
433 /* 蜂鸣器响一声 */
434 /* */
435 /*******************************************************************/
436 void beep()
437 {
438 1 unsigned char y;
439 1 for (y=0;y<100;y++)
440 1 {
441 2 Delay(70);
442 2 BEEP=!BEEP; //BEEP取反
443 2 }
444 1 BEEP=1; //关闭蜂鸣器
445 1 Delay(25000);
446 1 }
447
448 /*******************************************************************/
449 /* */
450 /* ROMCORD 显示菜单 */
451 /* */
452 /*******************************************************************/
453 void RomCode_Menu ()
454 {
455 1 uchar m;
456 1 lcd_init(); //初始化LCD
457 1
458 1 lcd_pos(0); //设置显示位置为第一行
459 1 for(m=0;m<16;m++) //显示字符
460 1 lcd_wdat(cdis1[m]);
461 1
462 1 Read_RomCord(); //读取64位序列码
463 1 CRC8(); //CRC效验
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -