找回密码
 立即注册
查看: 32|回复: 2

13-1 DS18B20温度读取,江协科技讲普中51单片机开发板STC89C52学习板 配套程序, A2标

[复制链接]
  • 打卡等级:常住居民II
  • 打卡总天数:80
  • 最近打卡:2026-07-23 09:36:34
已绑定手机

231

主题

172

回帖

1081

积分

版主

积分
1081
发表于 2026-7-13 13:24:43 | 显示全部楼层 |阅读模式
13-1 DS18B20温度读取,江协科技讲普中51单片机开发板STC89C52学习板 配套程序, A2标准版
主程序main.C代码如下:

#include <REGX52.H>
#include "LCD1602.h"
#include "DS18B20.h"
#include "Delay.h"

float T;

void main()
{
        DS18B20_ConvertT();                //上电先转换一次温度,防止第一次读数据错误
        Delay(1000);                        //等待转换完成
        LCD_Init();
        LCD_ShowString(1,1,"Temperature:");
        while(1)
        {
                DS18B20_ConvertT();        //转换温度
                T=DS18B20_ReadT();        //读取温度
                if(T<0)                                //如果温度小于0
                {
                        LCD_ShowChar(2,1,'-');        //显示负号
                        T=-T;                        //将温度变为正数
                }
                else                                //如果温度大于等于0
                {
                        LCD_ShowChar(2,1,'+');        //显示正号
                }
                LCD_ShowNum(2,2,T,3);                //显示温度整数部分
                LCD_ShowChar(2,5,'.');                //显示小数点
                LCD_ShowNum(2,6,(unsigned long)(T*10000)%10000,4);//显示温度小数部分
        }
}




13-1 DS18B20温度读取.zip

50.67 KB, 下载次数: 3

回复

使用道具 举报 送花

  • 打卡等级:常住居民II
  • 打卡总天数:80
  • 最近打卡:2026-07-23 09:36:34
已绑定手机

231

主题

172

回帖

1081

积分

版主

积分
1081
发表于 2026-7-13 13:25:08 | 显示全部楼层
13-1 DS18B20温度读取,江协科技讲普中51单片机开发板STC89C52学习板 配套程序, A2标准版
OneWire.C代码如下:

#include <REGX52.H>

//引脚定义
sbit OneWire_DQ=P3^7;

/**
  * @brief  单总线初始化
  * @param  无
  * @retval 从机响应位,0为响应,1为未响应
  */
unsigned char OneWire_Init(void)
{
        unsigned char i;
        unsigned char AckBit;
        OneWire_DQ=1;
        OneWire_DQ=0;
        i = 247;while (--i);                //Delay 500us
        OneWire_DQ=1;
        i = 32;while (--i);                        //Delay 70us
        AckBit=OneWire_DQ;
        i = 247;while (--i);                //Delay 500us
        return AckBit;
}

/**
  * @brief  单总线发送一位
  * @param  Bit 要发送的位
  * @retval 无
  */
void OneWire_SendBit(unsigned char Bit)
{
        unsigned char i;
        OneWire_DQ=0;
        i = 4;while (--i);                        //Delay 10us
        OneWire_DQ=Bit;
        i = 24;while (--i);                        //Delay 50us
        OneWire_DQ=1;
}

/**
  * @brief  单总线接收一位
  * @param  无
  * @retval 读取的位
  */
unsigned char OneWire_ReceiveBit(void)
{
        unsigned char i;
        unsigned char Bit;
        OneWire_DQ=0;
        i = 2;while (--i);                        //Delay 5us
        OneWire_DQ=1;
        i = 2;while (--i);                        //Delay 5us
        Bit=OneWire_DQ;
        i = 24;while (--i);                        //Delay 50us
        return Bit;
}

/**
  * @brief  单总线发送一个字节
  * @param  Byte 要发送的字节
  * @retval 无
  */
void OneWire_SendByte(unsigned char Byte)
{
        unsigned char i;
        for(i=0;i<8;i++)
        {
                OneWire_SendBit(Byte&(0x01<<i));
        }
}

/**
  * @brief  单总线接收一个字节
  * @param  无
  * @retval 接收的一个字节
  */
unsigned char OneWire_ReceiveByte(void)
{
        unsigned char i;
        unsigned char Byte=0x00;
        for(i=0;i<8;i++)
        {
                if(OneWire_ReceiveBit()){Byte|=(0x01<<i);}
        }
        return Byte;
}


回复

使用道具 举报 送花

  • 打卡等级:常住居民II
  • 打卡总天数:80
  • 最近打卡:2026-07-23 09:36:34
已绑定手机

231

主题

172

回帖

1081

积分

版主

积分
1081
发表于 2026-7-13 13:26:15 | 显示全部楼层
13-1 DS18B20温度读取,江协科技讲普中51单片机开发板STC89C52学习板 配套程序, A2标准版
DS18B20.C代码如下:

#include <REGX52.H>
#include "OneWire.h"

//DS18B20指令
#define DS18B20_SKIP_ROM                        0xCC
#define DS18B20_CONVERT_T                        0x44
#define DS18B20_READ_SCRATCHPAD         0xBE

/**
  * @brief  DS18B20开始温度变换
  * @param  无
  * @retval 无
  */
void DS18B20_ConvertT(void)
{
        OneWire_Init();
        OneWire_SendByte(DS18B20_SKIP_ROM);
        OneWire_SendByte(DS18B20_CONVERT_T);
}

/**
  * @brief  DS18B20读取温度
  * @param  无
  * @retval 温度数值
  */
float DS18B20_ReadT(void)
{
        unsigned char TLSB,TMSB;
        int Temp;
        float T;
        OneWire_Init();
        OneWire_SendByte(DS18B20_SKIP_ROM);
        OneWire_SendByte(DS18B20_READ_SCRATCHPAD);
        TLSB=OneWire_ReceiveByte();
        TMSB=OneWire_ReceiveByte();
        Temp=(TMSB<<8)|TLSB;
        T=Temp/16.0;
        return T;
}


回复

使用道具 举报 送花

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

本版积分规则

QQ|手机版|深圳国芯人工智能有限公司 ( 粤ICP备2022108929号-2 )

GMT+8, 2026-7-24 05:55 , Processed in 0.068582 second(s), 50 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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