xiaoyan911 发表于 2011-11-23 19:05:31

温度传感器DS18B20

#include"reg52.h"
#include"intrins.h"
#include"MyFun.h"
#include"DS18B20.h"

uchartemp1=0,temp2=0;   //temp1表示温度整数部分,temp2        表示温度小数部分

void tempdelay(uchar i)          //延时6US
{
        while(i--);
}

void dsreset()                                   //初始化
{
        ds=1;                                                //先将数据线置高电平
        ds=0;                                                //将数据线拉为低电平
        tempdelay(100);                                //延时480~960
        ds=1;                                                //将数据线拉为高电平
        tempdelay(100);                                //延时DS18B20的响应为低电平
        ds=1;                                                //将数据线拉为高电平

}

void tempwritebyte(uchar dat)          //写字节
{
        uchar i;
        for(i=0;i<8;i++)                          
        {
                ds=0;                                          //将数据线先置低电平
                tempdelay(2);                          //延时12US
                ds=dat&0x01;                          // 写入每位
                tempdelay(11);                          // 等待DS18B20采集数据
                ds=1;                                          //将数据线拉为高电平开始下次采集数据
                dat>>=1;                                  // 右移
        }
        ds=1;                                               //将数据线拉为高电平
        delayms(1);
}

uchar tempreadbyte()               //读字节
{
        uchar i,dat=0;
        for(i=0;i<8;i++)
        {
                ds=1;                                  //将数据线拉高到1
                dat>>=1;                          // 右移
                _nop_();_nop_();           //延时2US
                ds=0;                                   // 将数据线拉为低电平
                tempdelay(1);                   //延时6US
                ds=1;                                   // 将数据线拉为高电平
                if(ds==1)                          //        读数据
                dat|=0x80;
                tempdelay(8);
        }
        return(dat);                               //        将字节返回
        ds=1;                                                //读完后将数据线拉为高电平
        delayms(1);
}

void tempdispose()             //温度转化和读数据函数
{
        uchar temph,templ;

       EA=0;                          //关全局中断

        dsreset();
        tempwritebyte(0xcc);                //跳过ROM
        tempwritebyte(0x44);                // 温度转换命令
//        delayms(800);

        dsreset();
        tempwritebyte(0xcc);
        tempwritebyte(0xbe);                //读内部RAM中9字节的温度数据
        templ=tempreadbyte();                   //先读低字节再读高字节
        temph=tempreadbyte();
        temp1=(uchar)(((templ&0xf0)>>4)|((temph&0x0f)<<4)); // 整数部分
        temp2=(uchar)((templ&0x0f)*0.625);

        EA=1;                                       //开全局中断
       
}

dcqq88 发表于 2012-7-23 16:45:08

为什么有一样的???
{:sad:}http://www.amobbs.com/forum.php?mod=viewthread&tid=5223768&highlight=%E6%B8%A9%E5%BA%A6%E4%BC%A0%E6%84%9F%E5%99%A8DS18B20

692446 发表于 2012-12-9 23:22:50

{:huffy:}{:huffy:}{:huffy:}

liudaolunhui 发表于 2012-12-9 23:29:25

{:huffy:}{:huffy:}{:huffy:}{:huffy:}{:huffy:}{:huffy:}{:huffy:}{:huffy:}
{:huffy:}{:huffy:}{:huffy:}{:huffy:}{:huffy:}{:huffy:}{:huffy:}{:huffy:}{:huffy:}
{:huffy:}{:huffy:}{:huffy:}{:huffy:}{:huffy:}{:huffy:}{:huffy:}

lxa0 发表于 2012-12-10 20:10:56

692446 发表于 2012-12-9 23:22 static/image/common/back.gif


发火了??

lxa0 发表于 2012-12-10 20:11:18

liudaolunhui 发表于 2012-12-9 23:29 static/image/common/back.gif
...

发这么大火???

liangjiatian 发表于 2012-12-11 09:21:20

{:lol:}{:lol:}{:lol:}

wantao0916 发表于 2012-12-11 11:47:06

温故而知新。。

孤独stranger 发表于 2013-7-20 11:41:33

这个系统时钟是多大的哦

气球 发表于 2013-7-20 13:23:19

mark!!!!!!!!!!!!!
页: [1]
查看完整版本: 温度传感器DS18B20