18b20測溫c51源程序
for(j=1;j=8;j++)
{
testb=dat 0x01;
dat = dat>>1;
if(testb)
{
DQ1=0;
_nop_();
_nop_();
DQ1=1;
time_delay(60);
}
else
{
DQ1=0;
time_delay(50);
DQ1=1;
_nop_();
_nop_();
}
}
}
int get_temp_1(void)
{
unsigned char idata a=0,b=0;
unsigned char idata i;
EA=0;
ds_reset_1();
check_pre_1();
wr_ds18_1(0xcc);
wr_ds18_1(0x44);
while(BUSY1);
ds_reset_1();
check_pre_1();
wr_ds18_1(0xcc);
wr_ds18_1(0xbe);
a=rd_ds18_1();
b=rd_ds18_1();
i=b; /*若b為1則為負溫 */
i=(i>>4);
if(i==0)
{
f=0;
TMP=((a>>4)|(b4));
a=(a0x0f);
if (a>8)
{
TMP=(TMP+1);
}
}
else
{
f=1;
a=a>>4;
b=b4;
TMP=(a|b);
TMP=~TMP;
TMP=(TMP+1);
}
EA=1;
return(TMP);
}
int get_temp_d(void)
{
unsigned char idata a=0,b=0;
unsigned char idata i,m;
EA=0;
ds_reset_1();//復(fù)位
check_pre_1();
wr_ds18_1(0xcc);
wr_ds18_1(0x44);
while(BUSY1);
ds_reset_1();
check_pre_1();
wr_ds18_1(0xcc);
wr_ds18_1(0xbe);
a=rd_ds18_1();
b=rd_ds18_1();
i=b; /*若b為1則為負溫 */
i=(i>>4);
if(i==0)
{
f=0;
TMP=((a>>4)|(b4));
a=(a0x0f);
TMP_d=a;
}
else
{
f=1;
a=~a;
a=(a+1);
b=~b;
b=(b+1);
m=a;
a=a>>4;
b=b4;
TMP=(a|b);
m=(m0x0f);
TMP_d=m;
}
EA=1;
return(TMP);
}
評論