基于51单片机的DS18B20测温程序与仿真图

一、基础介绍

时序介绍就暂不介绍,可以网上搜集资料

二、proteus仿真图

仿真图

三、程序代码

#include<reg51.h>
#include"temp.h"

#define duanxuan P1        //段选端口
#define weixuan P2         //位选端口

unsigned char code wxcode[4]={0x01,0x02,0x04,0x08};
unsigned char code dxcode[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
	

sbit DSPORT=P3^7;
int temp;

void DigDisplay(int);


/******************/
void Delay1ms(unsigned int y)
{
	unsigned int x;
	for(y;y>0;y--)
		for(x=110;x>0;x--);
}
/*******************************************************************************
* 函数名         : Ds18b20Init
* 函数功能		   : 初始化
* 输入           : 无
* 输出         	 : 初始化成功返回1,失败返回0
*******************************************************************************/

unsigned char Ds18b20Init()
{
	unsigned int i;
	DSPORT=0;			 //将总线拉低480us~960us
	i=70;	
	while(i--);//延时642us
	DSPORT=1;			//然后拉高总线,如果DS18B20做出反应会将在15us~60us后总线拉低
	i=0;
	while(DSPORT)	//等待DS18B20拉低总线
	{
		i++;
		if(i>5000)//等待>5MS
			return 0;//初始化失败	
	}
	return 1;//初始化成功
}

/*******************************************************************************
* 函数名         : Ds18b20WriteByte
* 函数功能		   : 向18B20写入一个字节
* 输入           : com
* 输出         	 : 无
*******************************************************************************/

void Ds18b20WriteByte(unsigned char dat)
{
	unsigned int i,j;
	for(j=0;j<8;j++)
	{
		DSPORT=0;			//每写入一位数据之前先把总线拉低1us
		i++;
		DSPORT=dat&0x01; //然后写入一个数据,从最低位开始
		i=6;
		while(i--); //延时68us,持续时间最少60us
		DSPORT=1;	//然后释放总线,至少1us给总线恢复时间才能接着写入第二个数值
		dat>>=1;
		DigDisplay(temp);
	}
}
/*******************************************************************************
* 函数名         : Ds18b20ReadByte
* 函数功能		   : 读取一个字节
* 输入           : com
* 输出         	 : 无
*******************************************************************************/


unsigned char Ds18b20ReadByte()
{
	unsigned char byte,bi;
	unsigned int i,j;	
	for(j=8;j>0;j--)
	{
		DSPORT=0;//先将总线拉低1us
		i++;
		DSPORT=1;//然后释放总线
		i++;
		i++;//延时6us等待数据稳定
		bi=DSPORT;	 //读取数据,从最低位开始读取
		/*将byte左移一位,然后与上右移7位后的bi,注意移动之后移掉那位补0。*/
		byte=(byte>>1)|(bi<<7);						  
		i=4;		//读取完之后等待48us再接着读取下一个数
		while(i--);
		DigDisplay(temp);
	}				
	return byte;
}
/*******************************************************************************
* 函数名         : Ds18b20ChangTemp
* 函数功能		   : 让18b20开始转换温度
* 输入           : com
* 输出         	 : 无
*******************************************************************************/

void  Ds18b20ChangTemp()
{
	int i = 50;
	Ds18b20Init();
	Delay1ms(1);
	Ds18b20WriteByte(0xcc);		//跳过ROM操作命令		 
	Ds18b20WriteByte(0x44);	  //温度转换命令
		
	while(i != 0)
	{
		i--;
		DigDisplay(temp);
		
	}
//	Delay1ms(100);
}
/*******************************************************************************
* 函数名         : Ds18b20ReadTempCom
* 函数功能		   : 发送读取温度命令
* 输入           : com
* 输出         	 : 无
*******************************************************************************/

void  Ds18b20ReadTempCom()
{	
	Ds18b20Init();
	Delay1ms(1);
	Ds18b20WriteByte(0xcc);	 //跳过ROM操作命令
	Ds18b20WriteByte(0xbe);	 //发送读取温度命令
}
/*******************************************************************************
* 函数名         : Ds18b20ReadTemp
* 函数功能		   : 读取温度
* 输入           : com
* 输出         	 : 无
*******************************************************************************/

int Ds18b20ReadTemp()
{
	int temp=0;
	unsigned char tmh,tml;
	Ds18b20ChangTemp();			 	//先写入转换命令
	Ds18b20ReadTempCom();			//然后等待转换完后发送读取温度命令
	tml=Ds18b20ReadByte();		//读取温度值共16位,先读低字节
	tmh=Ds18b20ReadByte();		//再读高字节
	temp=tmh;
	temp<<=8;
	temp|=tml;
	return temp;
}
/******************/

void main()
{
	int tp;
	int i= 0;
	while(1)
	{
			temp = Ds18b20ReadTemp();
			if(temp < 0)				
			{
				temp=temp-1;
				temp=~temp;
				tp=temp;
				temp=tp*0.0625*100+0.5;
			}
			else
			{			
				tp=temp;
				temp=tp*0.0625*100+0.5;	
			}
	    DigDisplay(temp);
	}
}

void DigDisplay(int temp) 	 //数码管显示
{
	int bai;
	int shi;
	int ge;
	int yi;
	

	unsigned char i;
	unsigned int j;
	
	bai = temp / 10000;
  shi = temp % 10000 / 1000;
  ge = temp % 1000 / 100;
  yi = temp % 100 / 10;
	for(i=0; i<5; i++)
	{
		 weixuan = wxcode[i];
		 duanxuan = 0xff;
		if(i == 0)
		{
			duanxuan = dxcode[bai];
		}
		if(i == 1)
		{
			duanxuan = dxcode[shi];
		}
		if(i == 2)
		{
			//duanxuan = dxcode[ge]|0x00;
			duanxuan = dxcode[ge]&0x7f;
		}
		if(i == 3)
		{
			duanxuan = dxcode[yi];
		}
		j=10;
		while(j--);
		duanxuan=0xff;
	}
}
  • 38
    点赞
  • 311
    收藏
    觉得还不错? 一键收藏
  • 30
    评论
评论 30
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值