51单片机之DS18B20温度传感器实验

一、 DS18B20 介绍
在这里插入图片描述
1、温范围-55℃~+125℃,在-10~+85℃时精度为±0.5℃。
2、可编程的分辨率为 9~12 位,对应的可分辨温度分别为 0.5℃、0.25℃、
0.125℃ 和 0.0625℃,可实现高精度测温
在这里插入图片描述
比如我们要计算+85 度,数据输出十六进制是 0X0550,因为高字节的高 5位为 0,表明检测的温度是正温度,0X0550 对应的十进制为 1360,将这个值乘以 12 位精度 0.0625,所以可以得到+85 度。

二、时序
DS18B20 时序包括如下几种:初始化时序、写(0 和 1)时序、读(0 和 1)时序。 DS18B20 发送所有的命令和数据都是字节的低位在前。
DS18B20 的典型温度读取过程为:复位→发 SKIP ROM 命令(0XCC)→发开始转换命令(0X44)→延时→复位→发送 SKIP ROM 命令(0XCC)→发读存储器命令(0XBE)→连续读出两个字节数据(即温度)→结束。

1、编写temp.h文件

#ifndef _temp_H_
#define _temp_H_

#include <reg52.h>

#ifndef uchar
#define uchar unsigned char
#endif

#ifndef uint
#define uint unsigned char
#endif

sbit DSPORT=P3^7;

void Delay1ms(unsigned int );
unsigned char Ds18b20Init();
void Ds18b20WriteByte(unsigned char com);
unsigned char Ds18b20ReadByte();
void  Ds18b20ChangTemp();
void  Ds18b20ReadTempCom();
short Ds18b20ReadTemp();

#endif

2、编写temp.c文件

#include "temp.h"

void Delay1ms(uint y)
{
	uint x;
	for(y;y>0;y--)
		for(x=110;x>0;x--);
}

unsigned char Ds18b20Init()
{
	uint i;
	DSPORT=0;			 
	i=70;	
	while(i--);//??642us
	DSPORT=1;			
	i=0;
	while(DSPORT)
	{
		i++;
		if(i>5000)
			return 0;
	}
	return 1;
}

void Ds18b20WriteByte(uchar dat)
{
	uint i,j;
	for(j=0;j<8;j++)
	{
		DSPORT=0;			
		i++;
		DSPORT=dat&0x01; 
		i=6;
		while(i--);
		DSPORT=1;	
		dat>>=1;
	}
}

unsigned char Ds18b20ReadByte()
{
	uchar byte,bi;
	uint i,j;	
	for(j=8;j>0;j--)
	{
		DSPORT=0;
		i++;
		DSPORT=1;
		i++;
		i++;
		bi=DSPORT;	
		byte=(byte>>1)|(bi<<7);						  
		i=4;
		while(i--);
	}				
	return byte;
}

void  Ds18b20ChangTemp()
{
	Ds18b20Init();
	Delay1ms(1);
	Ds18b20WriteByte(0xcc); 
	Ds18b20WriteByte(0x44);
//	Delay1ms(100);
   
}

void  Ds18b20ReadTempCom()
{	

	Ds18b20Init();
	Delay1ms(1);
	Ds18b20WriteByte(0xcc);	
	Ds18b20WriteByte(0xbe);	 
}

short Ds18b20ReadTemp()
{
	uchar temp=0;
	uchar tmh,tml;
	short tem;

	Ds18b20ChangTemp();			 	
	Ds18b20ReadTempCom();		
	tml=Ds18b20ReadByte();
	tmh=Ds18b20ReadByte();

	if(tmh>7)
    {
        tmh=~tmh;
        tml=~tml; 
        temp=0;
    }
	else
	{
		temp=1;
	} 
		  	  
    tem=tmh; 
    tem<<=8;    
    tem|=tml;
    tem=(double)tem*0.625;
	if(temp)
		return tem; 
	else 
		return -tem; 
}

3、编写main.c文件

#include "reg52.h"
#include "i2c.h"
#include "intrins.h"

typedef unsigned int ui;
typedef unsigned char uc;
//#define led P2  //×¢Ò⣡£¡²»¼Ó";"
//sbit beep=P1^5;
//sbit led=P2^0;
//sbit moto=P1^0;
//sbit k1=P3^1;
//sbit k2=P3^0;
//sbit k3=P3^2;
//sbit k4=P3^3;

sbit LSA=P2^2;//×îµÍλ
sbit LSB=P2^3;//´ÎµÍλ
sbit LSC=P2^4;//¸ßλ
char num=0;
uc disp[8];
uc code smgduan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uc code smgduan1[10]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};
void delay(ui i)
{
	while(i--);
}

void Datapros(int temp)
{
	float tp;
	if(temp<0)
	{
		disp[0] = 0x40;
		temp-=1;
		temp=~temp;
		tp=temp;
		temp=tp*0.0625*100+0.5;
		
	}
	else{
		disp[0] = 0x00;
		tp=temp;
		temp=tp*0.0625*100+0.5;
	}
	disp[1]=smgduan[temp%10000/1000];//ǧλ
	disp[2]=smgduan1[temp%1000/100];//°Ùλ
	disp[3]=smgduan[temp%100/10];//ʮλ
	disp[4]=smgduan[temp%10];//¸öλ
}
void DigDisplay()
{
	ui i;
	for(i=0;i<6;i++)
	{
		switch(i)
		{
			case(0):
				LSA=1;LSB=1;LSC=1; break;//led-8
			case(1):
				LSA=0;LSB=1;LSC=1; break;//led-7
			case(2):
				LSA=1;LSB=0;LSC=1; break;//led-6
			case(3):
				LSA=0;LSB=0;LSC=1; break;//led-5
			case(4):
				LSA=1;LSB=1;LSC=0; break;//led-4
			case(5):
				LSA=0;LSB=1;LSC=0; break;//led-3
		}
		P0=disp[i];
		delay(100);
		P0=0x00;
	}
}
void main()
{
	while(1)
	{
		Datapros(Ds18b20ReadTemp());
		DigDisplay();
	}
}
  • 3
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅破苍穹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值