找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4830|回复: 5
打印 上一主题 下一主题
收起左侧

protues仿真DS18b20温度显示失败,一直显示0,请各位帮忙看看(程序与仿真见附件)?

[复制链接]
跳转到指定楼层
楼主
//Ds18b20初始化,
bit Ds18b20Init()
{
  bit i;
  DSPORT=1;
  _nop_();  
  DSPORT=0;//拉低总线
  Delaylms(500); //拉低500秒,拉低480秒~960秒即可
  DSPORT=1;//释放总线
  Delaylms(68);//延时68秒。等待DS18b20响应
  i=DSPORT;
  Delaylms(150);
  DSPORT=1;
  _nop_();
  return (i);
}
//写函数
void Ds18b20WriteByte(uchar dat)
{
        uchar i,j;
        for(i=0;j<8;j++)
        {
                DSPORT=0;
                Delaylms(1); //延时1us
                DSPORT=dat&0x01;
                Delaylms(77);//延时77us
                DSPORT=1;
                _nop_();
                dat>>=1;
        }
}
//读函数
uchar Ds18b20ReadByte()
{
        uint i,j;
        uchar bi,byte;
        for(i=8;j>0;j--)
        {
                DSPORT=0;
                i++;
                DSPORT=1;
                i++;
                i++;
                bi=DSPORT;
                Delaylms(77);
                DSPORT=1;
                _nop_();
                byte=(byte>>1)|(bi<<7);       
        }
        return byte;
}

//温度转换指令
void Ds18b20Changetemp()
{
        Ds18b20Init();
        Delaylms(1);
        Ds18b20WriteByte(0xcc);
        Ds18b20WriteByte(0x44);                       
}
//温度读取指令
void Ds18b20ReadTempcom()
{
        Ds18b20Init();
        Delaylms(1);
        Ds18b20WriteByte(0xcc);
        Ds18b20WriteByte(0xbe);//读取RAM
}
//读取温度数据
int Ds18b20ReadTemp()
{
        int temp=0;
        uchar tmh,tml;
        Ds18b20Changetemp();
        Ds18b20ReadTempcom();
        tml= Ds18b20ReadByte();
        tmh= Ds18b20ReadByte();
        temp=tmh;
        temp<<=8;
        temp|=tml;
        return temp;               
}


捕获.PNG (65.14 KB, 下载次数: 61)

捕获.PNG

程序和仿真.zip

143.48 KB, 下载次数: 35

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶2 踩
回复

使用道具 举报

沙发
ID:213173 发表于 2019-12-24 22:58 | 只看该作者

给你改好了
DS18b20+仿真.zip (107.65 KB, 下载次数: 232)

回复

使用道具 举报

板凳
ID:420055 发表于 2019-12-25 09:30 | 只看该作者
你这个晶振多大的
回复

使用道具 举报

地板
ID:319568 发表于 2019-12-26 17:00 | 只看该作者
小黎 发表于 2019-12-25 09:30
你这个晶振多大的

11.0925的,我的延时好像写错了,上次发的程序延时是错的,但我改了之后还是不显示,我去看看一楼给我改好的
回复

使用道具 举报

5#
ID:319568 发表于 2019-12-26 18:39 | 只看该作者

谢谢你,我去看看
回复

使用道具 举报

6#
ID:1075970 发表于 2023-5-27 01:09 | 只看该作者

改时延了?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表