《DS18B20温度计C程序.docx》由会员分享,可在线阅读,更多相关《DS18B20温度计C程序.docx(5页珍藏版)》请在第一文库网上搜索。
1、温度值精确到0.1度,1cd1602显示仿真电路图如下:C程序如下:Sinc1udeJtdefineucharunsignedchar#defineuintunsignedintsbitDQ=P37ds18b20与单片机连接口sbitRS=P30;sbitRW=P31;sbitEN=P32;unsignedcharcodestr1=temperature:“;“;unsignedcharcodestr2=*uchardatadisdata5;uittva1ue;温度值uchartf1ag;/温度正负标志/*Icd1602程序*/voidde1ay1ms(unsignedintms)延时1亳秒(
2、不够精确的)(unsignedinti,j;for(i=0;ims;i+)for(j=OJ0;i)(DQ=0;给脉冲信号dat=1;I)Q=1;给脉冲信号if(DQ)datI=0x80;de1ay_18B20(10);)return(dat);)voidds1820wr(ucharWdata)*写数据*/(unsignedchari=0;for(i=8;i0;i)DQ=0:DQ=wdata&OxO1;de1ay_18B20(10);DQ=1;wdata=1;)read_temp()*读取温度值并转换*/uchara,b;ds1820rst();ds1820wr(Oxcc);*跳过读序列号*/d
3、s1820wr(0x44);*启动温度转换*/ds1820rst();ds1820wr(Oxcc);*跳过读序列号*/ds1820wr(Oxbe);*读取温度*/a=ds1820rd();b=ds1820rd();tva1ue=b;tva1ue=8;tva1ue=tva1uea;if(tva1uexfff)tfIag=O;e1setva1ue=utva1ue+1;tag=1;)tva1ue=tva1ue*(O.625);温度值扩大10倍,精确到1位小数return(tva1ue);/*/voidds1820disp()温度值显示ucharf1agdat;disdata0=tva1ue/1000
4、+0x30;百位数disdata1=tva1ue%1000100+0x30;十位数disdata2=tva1ue%10010+0x30;/个位数disdata3=tva1ue%10+0x30;小数位if(tfIag=O)f1agdat=0x20;正温度不显示符号e1sef1agdat=0x2d;负温度显示负号:-if(disdata0=0x30)disdata0=0x20;如果百位为0,不显示if(disdata1=0x30)disdata1=0x20;/如果百位为0,卜位为0也不显示)wrcom(OxcO);wrdat(f1agdat);显示符号位wr_com(Oxc1);wr-dat(disdataO);显示百位wr_com(0xc2);WJdat(disdata1);显示H立wr_com(0xc3);wr-dat(disdata2);显示个位wr_com(0xc4);wr_dat(0x2e);显示小数点WjCOnI(OXC5);Wjdat(disdata3);显示小数位)/*主程序*/voidain()init_p1ay();初始化显示whiIe(I)(reademp();读取温度ds1820disp();显示