欢迎来到天天文库
浏览记录
ID:39463453
大小:23.50 KB
页数:5页
时间:2019-07-03
《51单片机ds18b20和lcd1602显示温度》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、/*本程序为基于51单片机的DS18b20与lcd1602显示温度程序,1602数据线友P0口提供*/#include#defineucharunsignedchar#defineuintunsignedintsbitDQ=P2^0;//ds18b20sbitlcd_rw=P2^5;sbitlcd_rs=P2^6;sbitlcd_en=P2^7;unsignedcharcodestr1[]={"temperature:"};unsignedcharcodestr2[16]={""};uchardatadisdata[5];uintvalue;//温度值ucharflag
2、;//正负标志/*************************lcd1602程序**************************/voiddelay1ms(unsignedintms)//延时1毫秒(不够精确的){unsignedinti,j;for(i=0;i3、te_dat(unsignedchardat)//写数据//{delay1ms(1);;lcd_rs=1;lcd_rw=0;lcd_en=0;P0=dat;delay1ms(1);lcd_en=1;delay1ms(1);lcd_en=0;}voidlcd_init()//初始化设置//{write_com(0x38);delay1ms(5);write_com(0x08);delay1ms(5);write_com(0x01);delay1ms(5);write_com(0x06);delay1ms(5);write_com(0x0c);delay1ms(5);}voiddisplay4、(unsignedchar*p)//显示//{while(*p!=' '){write_dat(*p);p++;delay1ms(1);}}init_play()//初始化显示{lcd_init();write_com(0x80);display(str1);write_com(0xc0);display(str2);}voiddelay_18B20(unsignedinti)//延时1微秒{while(i--);}voidds1820rst()/*ds1820复位*/{unsignedcharx=0;DQ=1;//DQ复位delay_18B20(4);//延时DQ=0;//DQ拉低d5、elay_18B20(100);//精确延时大于480usDQ=1;//拉高delay_18B20(40);}uchards1820rd()/*读数据*/{unsignedchari=0;unsignedchardat=0;for(i=8;i>0;i--){DQ=0;//给脉冲信号dat>>=1;DQ=1;//给脉冲信号if(DQ)dat6、=0x80;delay_18B20(10);}return(dat);}voidds1820wr(ucharwdata)/*写数据*/{unsignedchari=0;for(i=8;i>0;i--){DQ=0;DQ=wdata&0x01;delay_7、18B20(10);DQ=1;wdata>>=1;}}read_temp()/*读取温度值并转换*/{uchara,b;ds1820rst();ds1820wr(0xcc);//*跳过读序列号*/ds1820wr(0x44);//*启动温度转换*/ds1820rst();ds1820wr(0xcc);//*跳过读序列号*/ds1820wr(0xbe);//*读取温度*/a=ds1820rd();b=ds1820rd();value=b;value<<=8;value=value8、a;if(value<0x0fff)flag=0;else{value=~value+1;flag=1;}va9、lue=value*(0.625);//温度值扩大10倍,精确到1位小数return(value);}/*******************************************************************/voidds1820disp()//温度值显示{ucharflagdat;disdata[0]=value/1000+0x30;//百位数disdata[1]=value%1000/100+0x3
3、te_dat(unsignedchardat)//写数据//{delay1ms(1);;lcd_rs=1;lcd_rw=0;lcd_en=0;P0=dat;delay1ms(1);lcd_en=1;delay1ms(1);lcd_en=0;}voidlcd_init()//初始化设置//{write_com(0x38);delay1ms(5);write_com(0x08);delay1ms(5);write_com(0x01);delay1ms(5);write_com(0x06);delay1ms(5);write_com(0x0c);delay1ms(5);}voiddisplay
4、(unsignedchar*p)//显示//{while(*p!=' '){write_dat(*p);p++;delay1ms(1);}}init_play()//初始化显示{lcd_init();write_com(0x80);display(str1);write_com(0xc0);display(str2);}voiddelay_18B20(unsignedinti)//延时1微秒{while(i--);}voidds1820rst()/*ds1820复位*/{unsignedcharx=0;DQ=1;//DQ复位delay_18B20(4);//延时DQ=0;//DQ拉低d
5、elay_18B20(100);//精确延时大于480usDQ=1;//拉高delay_18B20(40);}uchards1820rd()/*读数据*/{unsignedchari=0;unsignedchardat=0;for(i=8;i>0;i--){DQ=0;//给脉冲信号dat>>=1;DQ=1;//给脉冲信号if(DQ)dat
6、=0x80;delay_18B20(10);}return(dat);}voidds1820wr(ucharwdata)/*写数据*/{unsignedchari=0;for(i=8;i>0;i--){DQ=0;DQ=wdata&0x01;delay_
7、18B20(10);DQ=1;wdata>>=1;}}read_temp()/*读取温度值并转换*/{uchara,b;ds1820rst();ds1820wr(0xcc);//*跳过读序列号*/ds1820wr(0x44);//*启动温度转换*/ds1820rst();ds1820wr(0xcc);//*跳过读序列号*/ds1820wr(0xbe);//*读取温度*/a=ds1820rd();b=ds1820rd();value=b;value<<=8;value=value
8、a;if(value<0x0fff)flag=0;else{value=~value+1;flag=1;}va
9、lue=value*(0.625);//温度值扩大10倍,精确到1位小数return(value);}/*******************************************************************/voidds1820disp()//温度值显示{ucharflagdat;disdata[0]=value/1000+0x30;//百位数disdata[1]=value%1000/100+0x3
此文档下载收益归作者所有