您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > DS18B20温度读取及显示
DS18B20温度读取及显示#includereg51.h#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;iz;i++)for(j=0;j160;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;i8;i++){DS=0;delay6us(1);DS=1;delay6us(1);bi=DS;byt=(byt1)|(bi7);delay6us(11);}returnbyt;}voidds_write(ucharch){uchari;for(i=0;i8;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);}uintDSreadtemp(){inttemp=0;uchartmh,tml;DSchange();delayms(1);DSreadtempcom();tml=ds_read();tmh=ds_read();DS=1;temp=tmh;temp=8;temp|=tml;returntemp;}voiddisplay(uintwendu){ucharbai,shi,ge,sfen,bfen,qfen;uchars,tp,th,tl;uintxs,flag;flag=wendu&0x8000;if(flag!=0)wendu=~(wendu-1);th=wendu/256;tl=wendu%256;s=tl&0x0f;xs=0.0625*s*1000;th=4;tl=4;tp=th|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!=0){dula=table[bai];}elsedula=0x00;delayms(10);wela=2;dula=table[shi];delayms(10);wela=3;dula=table[ge]|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){temp=DSreadtemp();display(temp);}}
本文标题:DS18B20温度读取及显示
链接地址:https://www.777doc.com/doc-5459351 .html