找回密码
 立即注册

QQ登录

只需一步,快速开始

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

能有大佬帮我看看这个单片机DS18B20哪里写错了吗?显示000.0000

[复制链接]
跳转到指定楼层
楼主
ID:1064342 发表于 2023-3-29 22:16 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include <at89c51RC2.h>
#include <intrins.h>


sbit DS18B20_IO = P3^7;

#define DS18B20_SKIP_ROM        0xCC
#define DS18B20_CONVERT_T       0x44
#define DS18B20_READ_SCRATCHPAD 0xBE

unsigned char DS18B20_Start(void) //初始化
{
        unsigned int i;
        unsigned char Ack;
        DS18B20_IO=1;
        DS18B20_IO=0;
        i = 227;while (--i);//延时500us
        DS18B20_IO=1;
        i = 29;while (--i);//延时70us
        Ack=DS18B20_IO;
        i = 227;while (--i);//延时500us
        return Ack;
}

void DS18B20_SendByte(unsigned char Byte)//写入一个字节
{
        unsigned int j,i;
        for(j=0; j<8; j++)
        {        
                DS18B20_IO=0;
                i = 4;while (--i);//延时10us
                DS18B20_IO=Byte&(0x01<<j);
                if(DS18B20_IO==1)
                {               
                        DS18B20_IO=1;               
                }
                i = 23;while (--i);//延时50us        
                DS18B20_IO=1;
                _nop_();_nop_();
        }
}

unsigned char DS18B20_ReceiveByte(void)
{
        unsigned char Byte=0x00;
        unsigned int j,i;
        for(j=0; j<8; j++)
        {
                DS18B20_IO=0;
                i = 2;while (--i);//延时5us
                DS18B20_IO=1;
                i = 2;while (--i);//延时5us
                if(DS18B20_IO==1)
                {
                        Byte|=(0x01<<j);
                }
                i = 23;while (--i);//延时50us
                DS18B20_IO=1;
                _nop_();_nop_();
        }
        return Byte;
}

void ConvertT(void)
{
        DS18B20_Start();
        DS18B20_SendByte(DS18B20_SKIP_ROM);
        DS18B20_SendByte(DS18B20_CONVERT_T);
}

float ReadT(void)
{
        unsigned char LSB,MSB;
        int Tmp;
        float T;
        DS18B20_Start();
        DS18B20_SendByte(DS18B20_SKIP_ROM);
        DS18B20_SendByte(DS18B20_READ_SCRATCHPAD);
        LSB=DS18B20_ReceiveByte();
        MSB=DS18B20_ReceiveByte();
        Tmp=(MSB<<8)|LSB;
        T=Tmp/16.0;
        return T;
}



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

使用道具 举报

沙发
ID:783858 发表于 2023-3-30 08:51 | 只看该作者
温度设计要看你电路那块配合来写程序的。
回复

使用道具 举报

板凳
ID:1064342 发表于 2023-3-30 10:59 | 只看该作者
zhongfofo 发表于 2023-3-30 08:51
温度设计要看你电路那块配合来写程序的。

大佬能帮我看下上面OneWire总线部分吗,我总感觉那里的写入和读出有问题
就是这两个  
void DS18B20_SendByte(unsigned char Byte)
unsigned char DS18B20_ReceiveByte(void)
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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