sbit wk=P2^1;//18b20的DQ脚接P2^1
void t(unsigned char x){while(x--);}//延时
void wkcsh(){wk=1;wk=0;t(160);wk=1;t(150)};//初始化
wksj(){unsigned char i;unsigned int x=0,y;for(i=0;i<16;i++){wk=0;wk=1;y=wk;t(20);x>>=1;x=x+(y<<15);wk=1;}}//返回数据
void wkml(unsigned char x){unsigned char i,k;for(i=0;i<8;i++){wk=0;k=x&0x01;if(k==0)wk=0;else wk=1;t(15);x>>=1;wk=1;}}//送命令
cwd(){unsigned int i;wkcsh();wkml(0xcc);wkml(0x44);t(50000);
wkcsh();wkml(0xcc);wkml(0xbe);i=wksj();return x;}//测温度,返回温度值,低四位是小数位,接着七位是整数位
用这个延时void delay(uint i)
{uint j
for(j=0,j}
很多时候读不出是因为延时不对,建议拿示波器看一下波形