下面是DS18B20的C51程序:////初始化18B20/////////uchar init18b20(void) { uint i; bit no; DS=0; i=103; while(i>0)i--; DS=1; i=4;while(i>0)i--; no=DS; if(no==0) { DS=1; i=100; while(i>0)i--; no=DS; if(no==1) not[1]=0; else not[1]=1; } else not[1]=1; return (not[1]);}bit tmpreadbit(void) //读一位{ uint i; bit dat; DS=0; i++; DS=1; i++;i++; dat=DS; i=8;while(i>0)i--; return (dat);}uchar tmpread(void) //读一个字节{ uchar i,j,dat; dat=0; for(i=1;i<=8;i++) { j=tmpreadbit(); dat=(j<<7)|(dat>>1); } return(dat);}void tmpwritebyte(uchar dat) //写一个字节到 ds18b20{ uint i; uchar j; bit testb; for(j=1;j<=8;j++) { testb=dat&0x01; dat=dat>>1; if(testb) { DS=0; i++;i++; DS=1; i=8;while(i>0)i--; } else { DS=0; i=8;while(i>0)i--; DS=1; i++;i++; } }}int tmp() //DS18B20温度读取{ float tt; uchar a,b; EA=0; if(init18b20()==0) { delay(1); tmpwritebyte(0xcc); tmpwritebyte(0x44); delay(10); init18b20(); delay(1); tmpwritebyte(0xcc); tmpwritebyte(0xbe); a=tmpread(); b=tmpread(); temp=b; temp<<=8; temp=temp|a; c=b>>4; tt=temp*0.0625; temp=tt*10+0.5; EA=1; return temp; } else not[1]=1; EA=1; }