欢迎来到天天文库
浏览记录
ID:57395042
大小:42.50 KB
页数:5页
时间:2020-08-15
《DS18B20温度读取及显示.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、DS18B20温度读取及显示#include#defineucharunsignedchar#defineuintunsignedint#definewelaP2#definedulaP0ucharcodetable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};sbitDS=P3^7;voiddelay6us(ucharz){while(z--);}voiddelayms(ucharz){uchari,j;for(i=0;i2、r(j=0;j<160;j++);}voidinit(){ucharpresence=1;while(presence){DS=0;delay6us(80);//延时480us以上DS=1;delay6us(15);if(DS==0){presence=0;while(DS==0);}elsepresence=1;}}uchards_read(){ucharbyt,bi;uchari;for(i=0;i<8;i++){DS=0;delay6us(1);DS=1;delay6us(1);bi=DS;byt=(byt>>1)3、4、(bi<<7);delay6us(11);}returnbyt;}voidds_write(ucharch){uchari;for(i=0;i<8;i++){DS=0;delay6us(1);DS=ch&0x01;delay6us(11);DS=1;delay6us(1);ch>>=1;}}voidDSchange(){DS=1;init();ds_write(0xcc);ds_write(0x44);}voidDSreadtempcom(){DS=1;init();ds_write(0xcc);ds_write(0xbe5、);}uintDSreadtemp(){inttemp=0;uchartmh,tml;DSchange();delayms(1);DSreadtempcom();tml=ds_read();tmh=ds_read();DS=1;temp=tmh;temp<<=8;temp6、=tml;returntemp;}voiddisplay(uintwendu){ucharbai,shi,ge,sfen,bfen,qfen;uchars,tp,th,tl;uintxs,flag;flag=wendu&0x8000;if(flag!=07、)wendu=~(wendu-1);th=wendu/256;tl=wendu%256;s=tl&0x0f;xs=0.0625*s*1000;th<<=4;tl>>=4;tp=th8、tl;bai=tp/100;shi=(tp%100)/10;ge=tp%10;sfen=xs/100;bfen=(xs%100)/10;qfen=xs%10;//显示的百位十位个位以及小数点后三位wela=0;if(flag!=0){dula=0x40;//显示负号}elsedula=0x00;delayms(10);wela=1;if(bai9、!=0){dula=table[bai];}elsedula=0x00;delayms(10);wela=2;dula=table[shi];delayms(10);wela=3;dula=table[ge]10、0x80;//加小数点delayms(10);wela=4;dula=table[sfen];delayms(10);wela=5;dula=table[bfen];delayms(10);wela=6;dula=table[qfen];delayms(10);}voidmain(){uinttemp;while(111、){temp=DSreadtemp();display(temp);}}
2、r(j=0;j<160;j++);}voidinit(){ucharpresence=1;while(presence){DS=0;delay6us(80);//延时480us以上DS=1;delay6us(15);if(DS==0){presence=0;while(DS==0);}elsepresence=1;}}uchards_read(){ucharbyt,bi;uchari;for(i=0;i<8;i++){DS=0;delay6us(1);DS=1;delay6us(1);bi=DS;byt=(byt>>1)
3、
4、(bi<<7);delay6us(11);}returnbyt;}voidds_write(ucharch){uchari;for(i=0;i<8;i++){DS=0;delay6us(1);DS=ch&0x01;delay6us(11);DS=1;delay6us(1);ch>>=1;}}voidDSchange(){DS=1;init();ds_write(0xcc);ds_write(0x44);}voidDSreadtempcom(){DS=1;init();ds_write(0xcc);ds_write(0xbe
5、);}uintDSreadtemp(){inttemp=0;uchartmh,tml;DSchange();delayms(1);DSreadtempcom();tml=ds_read();tmh=ds_read();DS=1;temp=tmh;temp<<=8;temp
6、=tml;returntemp;}voiddisplay(uintwendu){ucharbai,shi,ge,sfen,bfen,qfen;uchars,tp,th,tl;uintxs,flag;flag=wendu&0x8000;if(flag!=0
7、)wendu=~(wendu-1);th=wendu/256;tl=wendu%256;s=tl&0x0f;xs=0.0625*s*1000;th<<=4;tl>>=4;tp=th
8、tl;bai=tp/100;shi=(tp%100)/10;ge=tp%10;sfen=xs/100;bfen=(xs%100)/10;qfen=xs%10;//显示的百位十位个位以及小数点后三位wela=0;if(flag!=0){dula=0x40;//显示负号}elsedula=0x00;delayms(10);wela=1;if(bai
9、!=0){dula=table[bai];}elsedula=0x00;delayms(10);wela=2;dula=table[shi];delayms(10);wela=3;dula=table[ge]
10、0x80;//加小数点delayms(10);wela=4;dula=table[sfen];delayms(10);wela=5;dula=table[bfen];delayms(10);wela=6;dula=table[qfen];delayms(10);}voidmain(){uinttemp;while(1
11、){temp=DSreadtemp();display(temp);}}
此文档下载收益归作者所有