我用51单片机读18B20的温度,读不出来,程序是这样的
#include #include #define unchar unsigned char #define unint unsigned int ***it tda=P1^4; unchar i,t; unint tt; void delay(unchar z) { uncharx,y; for(x=0;x for(y=0;y<110;y++); } void reset() { tda=1; i++; tda=0; i=103; while(i>0) i--; tda=1; i=4; while(i>0) i--; } void write(unchar date) { unchartemp; for(t=0;t<8;t++) { temp=date&0x01; date=date>>1; if(temp==1) { tda=0; i++; i++; tda=1; i=8; while(i>0) i--; } else { tda=0; i=8; while(i>0) i--; tda=1; i++; i++; } tda=1; } } bit read_bit() { bitdat; tda=0; i++; tda=1; i++; i++; dat=tda; i=8; while(i>0) i--; return(dat); } unchar read() { unchardat,j,z; dat=0; for(z=1;z<=8;z++) { j=read_bit(); dat=(j<<7)|(dat>>1); } return(dat); } void start() { reset(); delay(1); write(0xcc); write(0x44); } unint get() { unchara,b; uninttemp; temp=0x00; reset(); delay(1); write(0xcc); write(0xbe); a=read(); b=read(); temp=b; temp=temp<<8; temp=temp|a; temp=temp*0.0625+0.5; return(temp); } main() { while(1) { start(); delay(5); tt=get(); if(tt>0&&tt<=25) P2=0x01; if(tt>25&&tt<=30) P2=0x02; if(tt>30&&tt<=35) P2=0x04; if(tt>35) P2=0x0f; if(tt==0) P2=0x00; } }
|