chriscctt 发表于 2012-2-9 15:58:35

各位大侠帮忙看看我的18B20驱动有什么问题,结果相差很大。

/******************************************************************/
/*               DS18B20驱动程序 (12M晶振)                  */
/******************************************************************/
void delay(unsigned int i)
{
   while(i--);
}
//初始化函数
void Init_DS18B20(void)
{
   unsigned char x=0;
   DQ=1;
   delay(8);
   DQ=0;
   delay(80);
   DQ=1;
   delay(14);
   x=DQ;
   delay(20);
}

//读一个字节
int ReadOneByte(void)
{
   unsigned char i=0;
   unsigned char dat=0;
   for (i=8;i>0;i--)
   {
   DQ=0;
   dat>>=1;
   DQ=1;
   if(DQ)
   dat|=0x80;
   delay(4);
   }
   return(dat);
}

//写一个字节
void WriteOneByte(unsigned char dat)
{
   unsigned char i=0;
   for (i=8; i>0; i--)
   {
   DQ=0;
   DQ=dat&0x01;
   delay(5);
   DQ=1;
   dat>>=1;
    }
        delay(4);
}

//DS18B20程序读取温度
int ReadTemperature(void)
{
unsigned char a=0;
unsigned char b=0;
unsigned intt=0;


Init_DS18B20();
WriteOneByte(0xCC); // 跳过读序号列号的操作
WriteOneByte(0x44); // 启动温度转换
delay(125);

Init_DS18B20();
WriteOneByte(0xCC); //跳过读序号列号的操作
WriteOneByte(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度

a=ReadOneByte();
b=ReadOneByte();
t=((b*256)+a)*0.0625;

delay(200);
return(t);
}

raxfeer 发表于 2012-2-9 17:12:16

呵呵,我也用18B20做过项目,结果的确不怎么准的,总是差那么几度,虽然我也没有精确的测试设备提供参考值,但凭感觉觉得它是有误差的,跟程序无关。

chriscctt 发表于 2012-5-7 21:19:56

raxfeer 发表于 2012-2-9 17:12 static/image/common/back.gif
呵呵,我也用18B20做过项目,结果的确不怎么准的,总是差那么几度,虽然我也没有精确的测试设备提供参考值 ...

原来是开中断的原因,不知怎么解决呢

raxfeer 发表于 2012-5-8 12:11:48

chriscctt 发表于 2012-5-7 21:19 static/image/common/back.gif
原来是开中断的原因,不知怎么解决呢

我当初不是用中断做的,照样跟实际温度有偏差。
当然,我也没有一个精确的仪器做参考,仅仅是感觉有偏差。
页: [1]
查看完整版本: 各位大侠帮忙看看我的18B20驱动有什么问题,结果相差很大。