基于51单片机的火灾报警【3房间,温度,数码管】(仿真)

 

 

 

#include"Ds18b20.h"
float ds18b20_temp1=0;
float ds18b20_temp2=0;
float ds18b20_temp3=0;
/*******************************************************************************
* 函 数 名         : Delay1ms
* 函数功能		   : 延时函数
* 输    入         : 无
* 输    出         : 无
*******************************************************************************/

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

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

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

void Ds18b20WriteByte1(uchar dat)
{
	uint i, j;

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


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

void  Ds18b20ChangTemp1()
{
	Ds18b20Init1();
	Delay1ms(1);
	Ds18b20WriteByte1(0xcc);		//跳过ROM操作命令		 
	Ds18b20WriteByte1(0x44);	    //温度转换命令
	//Delay1ms(100);	//等待转换成功,而如果你是一直刷着的话,就不用这个延时了
   
}
/*******************************************************************************
* 函 数 名         : Ds18b20ReadTempCom
* 函数功能		   : 发送读取温度命令
* 输    入         : 无
* 输    出         : 无
*******************************************************************************/

void  Ds18b20ReadTempCom1()
{	

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

void Ds18b20ReadTemp1()
{
	uchar temp = 0;
	uchar tmh, tml;
	Ds18b20ChangTemp1();			 	//先写入转换命令
	Ds18b20ReadTempCom1();			//然后等待转换完后发送读取温度命令
	tml = Ds18b20ReadByte1();		//读取温度值共16位,先读低字节
	tmh = Ds18b20ReadByte1();		//再读高字节
	temp = tmh;
	temp <<= 4;
	temp += (tml>>4);
	if(temp>127)
	{	
		temp--;
		temp=~temp;
		temp=temp & 0x7f;
		ds18b20_temp1=temp;
		ds18b20_temp1=-ds18b20_temp1;
	}
	else
		ds18b20_temp1=temp;
	ds18b20_temp1=ds18b20_temp1+(tml & 0x0f)*9/150;
}





/*******************************************************************************
* 函 数 名         : Ds18b20Init
* 函数功能		   : 初始化
* 输    入         : 无
* 输    出         : 初始化成功返回1,失败返回0
*******************************************************************************/

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

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

void Ds18b20WriteByte2(uchar dat)
{
	uint i, j;

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


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

void  Ds18b20ChangTemp2()
{
	Ds18b20Init2();
	Delay1ms(1);
	Ds18b20WriteByte2(0xcc);		//跳过ROM操作命令		 
	Ds18b20WriteByte2(0x44);	    //温度转换命令
	//Delay1ms(100);	//等待转换成功,而如果你是一直刷着的话,就不用这个延时了
   
}
/*******************************************************************************
* 函 数 名         : Ds18b20ReadTempCom
* 函数功能		   : 发送读取温度命令
* 输    入         : 无
* 输    出         : 无
*******************************************************************************/

void  Ds18b20ReadTempCom2()
{	

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

void Ds18b20ReadTemp2()
{
	uchar temp = 0;
	uchar tmh, tml;
	Ds18b20ChangTemp2();			 	//先写入转换命令
	Ds18b20ReadTempCom2();			//然后等待转换完后发送读取温度命令
	tml = Ds18b20ReadByte2();		//读取温度值共16位,先读低字节
	tmh = Ds18b20ReadByte2();		//再读高字节
	temp = tmh;
	temp <<= 4;
	temp += (tml>>4);
	if(temp>127)
	{	
		temp--;
		temp=~temp;
		temp=temp & 0x7f;
		ds18b20_temp2=temp;
		ds18b20_temp2=-ds18b20_temp2;
	}
	else
		ds18b20_temp2=temp;
	ds18b20_temp2=ds18b20_temp2+(tml & 0x0f)*9/150;
}








/*******************************************************************************
* 函 数 名         : Ds18b20Init
* 函数功能		   : 初始化
* 输    入         : 无
* 输    出         : 初始化成功返回1,失败返回0
*******************************************************************************/

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

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

void Ds18b20WriteByte3(uchar dat)
{
	uint i, j;

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


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

void  Ds18b20ChangTemp3()
{
	Ds18b20Init3();
	Delay1ms(1);
	Ds18b20WriteByte3(0xcc);		//跳过ROM操作命令		 
	Ds18b20WriteByte3(0x44);	    //温度转换命令
	//Delay1ms(100);	//等待转换成功,而如果你是一直刷着的话,就不用这个延时了
   
}
/*******************************************************************************
* 函 数 名         : Ds18b20ReadTempCom
* 函数功能		   : 发送读取温度命令
* 输    入         : 无
* 输    出         : 无
*******************************************************************************/

void  Ds18b20ReadTempCom3()
{	

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

void Ds18b20ReadTemp3()
{
	uchar temp = 0;
	uchar tmh, tml;
	Ds18b20ChangTemp3();			 	//先写入转换命令
	Ds18b20ReadTempCom3();			//然后等待转换完后发送读取温度命令
	tml = Ds18b20ReadByte3();		//读取温度值共16位,先读低字节
	tmh = Ds18b20ReadByte3();		//再读高字节
	temp = tmh;
	temp <<= 4;
	temp += (tml>>4);
	if(temp>127)
	{	
		temp--;
		temp=~temp;
		temp=temp & 0x7f;
		ds18b20_temp3=temp;
		ds18b20_temp3=-ds18b20_temp3;
	}
	else
		ds18b20_temp3=temp;
	ds18b20_temp3=ds18b20_temp3+(tml & 0x0f)*9/150;
}

 资料借鉴于此纷传

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

白茶丫

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

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

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

打赏作者

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

抵扣说明:

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

余额充值