这是我自己用DS18B20做的温度检测程序,复制给你看看,我这是通过串口可以在电脑上的串口助手上显示出实时的温度:#include<>#include<>#define uint unsigned int#define uchar unsigned charsbit ds=P1^0;bit flag;uchar count_t0;float f_temp;void delay(uint z){uint x,y;for(x=z;x>0;x--)for(y=122;y>0;y--);}void init() // 串口初始化{TMOD=0x21;SCON=0x50;TH0=0x4c;TL0=0x00;TH1=0xf3;TL1=0xf3;EA=1;ET0=1;TR0=1;TR1=1;}void timer0() interrupt 1{TH0=0x4c;TL0=0x00;if(++count_t0>=20){count_t0=0;flag=1;}}void dsreset(){uint i;ds=0;i=103;while(i>0)i--;ds=1;i=4;while(i>0)i--;}bit read_bit(){uint i;bit dat;ds=0;i++;ds=1;i++;i++;dat=ds;i=8;while(i>0)i--;return dat;}uchar read_byte(){uchar i,j,dat;dat=0;for(i=1;i<=8;i++){j=read_bit();dat=(j<<7)|(dat>>1);}return dat;}void write_byte(uchar dat){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++;}}}void begin_change(){dsreset();delay(1);write_byte(0xcc);write_byte(0x44);}float get_temp(){uchar a,b;uint temp;float f_temp;dsreset();delay(1);write_byte(0xcc);write_byte(0xbe);a=read_byte();b=read_byte();temp=b;temp<<=8;temp=temp|a;f_temp=temp*;temp=f_temp*10+;f_temp=f_temp+;return f_temp;}void main(){init();while(1){if(flag==1){flag=0;begin_change();TI=1;printf("The tempeature is %f\n",get_temp());while(!TI);TI=0;}}}