基于51单片机的DS18B20温度计

今天做的这个例程,特写此文

注意事项:
1.符号要注意,别打成中文的了,错图如下
此处就是符号打成中文错误提示
2.宏定义不能定义两次哦
在本次实验我采用了两种定义方式typedef和definee将unsigned int 定义成uint,不能同时存在哈,同时存在报错如下
错误提示图
3.这两种写法都不报错
在这里插入图片描述
附代码

#include "ds.h"

void delay1ms(uint y)
{
	uint x;
	for( ;y>0;y--)
	{
		for(x=110;x>0;x--);
	}
}									//y为1时,延时1毫秒

uchar Ds18b20Init()		//对DS18B20初始化
{
	uchar i;
	DSPORT=0;
	i=70;
	while(i--);
	DSPORT=1;
	i=0;
	while(DSPORT)
	{
		Delay1ms(1);
		i++;
		if(i>5)		//等待大于5ms
		{
			return 0;		//初始化失败
		}
	}
	return 1;		//初始化成功
}

void Ds18b20WriteByte(uchar dat)		//向18B20写入一个字节
{
	uint 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;
	}
}

uchar Ds18b20ReadByte()		//从18B20读取一个字节
{
	uchar byte,bi;
	uint i,j;
	for(j=8;j>0;j--)
	{
		DSPORT=0;
		i++;
		DSPORT=1;
		i++;
		bi=DSPORT;
		byte=(byte>>1)|(bi<<7);
		i=4;
		while(i--);
	}
	return byte;
}

void Ds18b20ChangeTemp()		//让18b20开始转换温度
{
	Ds18b20Init();
	Delay1ms(1);
	Ds18b20WriteByte(0xcc);		//跳过ROM操作命令
	Ds18b20WriteByte(0x44);		//温度转换命令
}

void Ds18b20ReadTempCom()		//发送读取温度命令
{
	Ds18b20Init();
	Delay1ms(1);
	Ds18b20WriteByte(0xcc);		//跳过ROM操作命令
	Ds18b20WriteByte(0xbe);		//发送读取温度命令
}

int Ds18b20ReadTemp()		//读取温度
{
	int temp=0;
	uchar tmh,tml;
	Ds18b20ChangeTemp();
	Ds18b20ReadTempCom();
	tml=Ds18b20ReadByte();		//先读低字节
	tmh=Ds18b20ReadByte();		//再读高字节
	temp=tmh;
	temp<<=8;
	temp|=tml;
	return temp;
}
/********************************************
*         DS18B20温度传感器实验        *
实验现象:下载程序后,在温度传感器接口处,按照丝印方向插好
温度传感器,数码管就会显示检测的温度;
注意事项:暂无
*******************************************/

#include "reg52.h"
#include "ds.h"

//typedef unsigned char uchar;
//typedef unsigned int uint;

sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;

char num=0;
uchar DisplayData[8];
uchar code smgduan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

void delay(uint i)
{
	while(i--);
}										//延时函数,i=1时大约延时10us

void datapros(int temp) 	 
{
   	float tp;  
	if(temp< 0)				//当温度值为负数
  	{
		DisplayData[0] = 0x40; 	  //   ’-‘号
		//因为读取的温度是实际温度的补码,所以减1,再取反求出原码
		temp=temp-1;
		temp=~temp;
		tp=temp;
		temp=tp*0.0625*100+0.5;	
		//留两个小数点就*100,+0.5是四舍五入,因为C语言浮点数转换为整型的时候把小数点
		//后面的数自动去掉,不管是否大于0.5,而+0.5之后大于0.5的就是进1了,小于0.5的就
		//算加上0.5,还是在小数点后面。
 
  	}
 	else
  	{			
		DisplayData[0] = 0x00;
		tp=temp;//因为数据处理有小数点所以将温度赋给一个浮点型变量
		//如果温度是正的那么,那么正数的原码就是补码它本身
		temp=tp*0.0625*100+0.5;	
		//留两个小数点就*100,+0.5是四舍五入,因为C语言浮点数转换为整型的时候把小数点
		//后面的数自动去掉,不管是否大于0.5,而+0.5之后大于0.5的就是进1了,小于0.5的就
		//算加上0.5,还是在小数点后面。
	}
	DisplayData[1] = smgduan[temp / 10000];
	DisplayData[2] = smgduan[temp % 10000 / 1000];
	DisplayData[3] = smgduan[temp % 1000 / 100] | 0x80;
	DisplayData[4] = smgduan[temp % 100 / 10];
	DisplayData[5] = smgduan[temp % 10];
}


/*******************************************************************************
* 函数名         :DigDisplay()
* 函数功能		 :数码管显示函数
* 输入           : 无
* 输出         	 : 无
*******************************************************************************/
void DigDisplay()
{
	uchar i;
	for(i=0;i<6;i++)
	{
		switch(i)	 //位选,选择点亮的数码管,
		{
			case(0):
				LSA=0;LSB=0;LSC=0; break;//显示第0位
			case(1):
				LSA=1;LSB=0;LSC=0; break;//显示第1位
			case(2):
				LSA=0;LSB=1;LSC=0; break;//显示第2位
			case(3):
				LSA=1;LSB=1;LSC=0; break;//显示第3位
			case(4):
				LSA=0;LSB=0;LSC=1; break;//显示第4位
			case(5):
				LSA=1;LSB=0;LSC=1; break;//显示第5位	
		}
		P0=DisplayData[5-i];//发送数据
		delay(100); //间隔一段时间扫描	
		P0=0x00;//消隐
	}		
}

/*******************************************************************************
* 函 数 名       : main
* 函数功能		 : 主函数
* 输    入       : 无
* 输    出    	 : 无
*******************************************************************************/
void main()
{	
	while(1)
	{
		datapros(Ds18b20ReadTemp());	 //数据处理函数
		DigDisplay();//数码管显示函数		
	}		
}

在这里插入代码片
  • 2
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值