蓝桥杯之DS18B20

1、显示整数 

#include <STC15F2K60S2.h>
#include <intrins.h>
//***************定义DS18B24引脚*************
sbit DQ=P1^4;
//****************段码***********************
unsigned char code duanma[18]=
{0xc0,//0
0xcf,//1
0xa4,//2
0xb0,//3
0x99,//4
0x92,//5
0x82,//6
0xf8,//7
0x80,//8
0x90,//9
0x88,//A
0x80,//B
0xc6,//C
0xc0,//D
0x86,//E
0x8e,//F
0xbf,//-
0x7f//.
};

//***************系统初始化****************
void init_system()
{
	P2=0XA0;
	P0=0X00;
	P2=0X80;
	P0=0XFF;
	P2=0X00;
}
//*****************500us延时**********************
void Delay500us()		//@11.0592MHz
{
	unsigned char i, j;

	_nop_();
	_nop_();
	i = 6;
	j = 93;
	do
	{
		while (--j);
	} while (--i);
}
//***********************60us延时************************
void Delay60us()		//@11.0592MHz
{
	unsigned char i;

	_nop_();
	_nop_();
	i = 163;
	while (--i);
}
//**********************1us****************
void Delay1us()		//@11.0592MHz
{
	_nop_();
	_nop_();
	_nop_();
}

//**********************DS18B20初始化*************************
void init_ds()
{
	unsigned char i;
	DQ=1;
	DQ=0;
	Delay500us();
	DQ=1;
	while(DQ)
	{
		i++;
		if(i==3)
		{
			break;
		}
		Delay500us();
	}
	Delay500us();
}
//****************写入****************
void write_ds(unsigned char dat)
{
	unsigned char i;
	for(i=0;i<8;i++)
	{
		DQ=0;
		Delay1us();
		DQ=dat&0x01;
		dat>>=1;
		Delay60us();
		DQ=1;
	}
}//*******************读**********************
unsigned char read_ds()
{
	unsigned char i,temp;
	for(i=0;i<8;i++)
	{
		DQ=0;
		temp>>=1;
		DQ=1;
		Delay1us();
		if(DQ)
		{
			temp|=0x80;
		}
		Delay60us();
	}
	return temp;
}
//****************读取温度********************
unsigned char read_t()
{
	unsigned char low,high,temper;
	init_ds();
	write_ds(0xcc);
	write_ds(0x44);

	init_ds();
	write_ds(0xcc);
	write_ds(0xbe);

	low=read_ds();
	high=read_ds();

	temper=low>>4;
	temper|=high<<4;

	return temper;
}
//*********************数码管延时1毫米*****************
void Delay1ms()		//@11.0592MHz
{
	unsigned char i, j;

	_nop_();
	_nop_();
	_nop_();
	i = 11;
	j = 190;
	do
	{
		while (--j);
	} while (--i);
}
//***********************显示函数*********************
void ds(unsigned char yi,unsigned char er)
{
	P2=0XC0;
	P0=0X40;
	P2=0XE0;
	P0=duanma[yi];
	Delay1ms();

	P2=0XC0;
	P0=0X80;
	P2=0XE0;
	P0=duanma[er];
	Delay1ms();

	P2=0X00;
}
void main()
{
	unsigned char i,j,temper;
	init_system();
	while(1)
	{
		temper=read_t();
		i=temper/10;//十位
		j=temper%10;//个位
		ds(i,j);
	}
}

2、更改后,显示带小数部分

#include <STC15F2K60S2.h>
#include <intrins.h>
sbit DQ = P1^4;  //?????
unsigned short temp;
//****************??***********************
unsigned char code duanma[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char code duanma_dot[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
//***************?????****************
void init_system()
{
	P2=0XA0;
	P0=0X00;
	P2=0X80;
	P0=0XFF;
	P2=0X00;
}

//???????
void Delay_OneWire(unsigned int t)  //STC89C52RC
{
	unsigned char i=0;
	while(t--)
	{
		for(i=0;i<12;i++);
	}
		
}

//??????DS18B20?????
void Write_DS18B20(unsigned char dat)
{
	unsigned char i;
	for(i=0;i<8;i++)
	{
		DQ = 0;
		DQ = dat&0x01;
		Delay_OneWire(5);
		DQ = 1;
		dat >>= 1;
	}
	Delay_OneWire(5);
}

//?DS18B20??????
unsigned char Read_DS18B20(void)
{
	unsigned char i;
	unsigned char dat;
  
	for(i=0;i<8;i++)
	{
		DQ = 0;
		dat >>= 1;
		DQ = 1;
		if(DQ)
		{
			dat |= 0x80;
		}	    
		Delay_OneWire(5);
	}
	return dat;
}

//DS18B20?????
bit init_ds18b20(void)
{
  	bit initflag = 0;
  	
  	DQ = 1;
  	Delay_OneWire(12);
  	DQ = 0;
  	Delay_OneWire(80);
  	DQ = 1;
  	Delay_OneWire(10); 
    initflag = DQ;     
  	Delay_OneWire(5);
  
  	return initflag;
}

//void Delay750ms()		//@11.0592MHz
//{
//	unsigned char i, j, k;

//	_nop_();
//	_nop_();
//	i = 32;
//	j = 133;
//	k = 87;
//	do
//	{
//		do
//		{
//			while (--k);
//		} while (--j);
//	} while (--i);
//}

float rd_temperature(void)
{
	unsigned char low,high;
	init_ds18b20();
	Delay_OneWire(1); 
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
  
	//Delay750ms();
	
	init_ds18b20();
	Delay_OneWire(1); 
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);

  low=  Read_DS18B20();
	high= Read_DS18B20();
	//temp=high*256+low;
	//return temp*0.0625;
	temp=high;
	temp=temp<<8;
	temp=temp|low;
	
	temp>>=4;
	temp=temp*10;
	temp=temp+(low&0x0f)*0.625;
	return temp;
}

//*********************?????1??*****************
void Delay1ms()		//@11.0592MHz
{
	unsigned char i, j;

	_nop_();
	_nop_();
	_nop_();
	i = 11;
	j = 190;
	do
	{
		while (--j);
	} while (--i);
}

//***********************????*********************
void ds()
{

	
	P2=0XC0;//com
	P0=0X80;//7
	P2=0XE0;
	P0=duanma[temp%10];
	P2=0X00;
	Delay1ms();

	P2=0XC0;
	P0=0X40;//6
	P2=0XE0;
	P0=duanma_dot[(temp%100)/10];
	P2=0X00;
	Delay1ms();

	P2=0XC0;//com
	P0=0X20;//5
	P2=0XE0;
	P0=duanma[temp/100];
	P2=0X00;
	Delay1ms();

	P2=0XC0;//com
	P0=0X10;//4
	P2=0XE0;
	P0=0xff;
	P2=0X00;
	Delay1ms();

	P2=0XC0;//com
	P0=0X08;//3
	P2=0XE0;
	P0=0xff;
	P2=0X00;
	Delay1ms();

	P2=0XC0;//com
	P0=0X04;//2
	P2=0XE0;
	P0=0xff;
	P2=0X00;
	Delay1ms();

	P2=0XC0;//com
	P0=0X02;//1
	P2=0XE0;
	P0=0xff;
	P2=0X00;
	Delay1ms();

	P2=0XC0;//com
	P0=0X01;//0
	P2=0XE0;
	P0=0xff;
	P2=0X00;
	Delay1ms();

}

void main()
{
	//float temp;
	init_system();
	while(1)
	{
		//temp=rd_temperature();
		rd_temperature();
		//temp=temp*10;
		ds();
		
	}
}

或者

#include <STC15F2K60S2.h>
#include <intrins.h>
sbit DQ = P1^4;  //?????
unsigned short low,high,temp;
//****************??***********************
unsigned char code duanma[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char code duanma_dot[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
//***************?????****************
void init_system()
{
	P2=0XA0;
	P0=0X00;
	P2=0X80;
	P0=0XFF;
	P2=0X00;
}

//???????
void Delay_OneWire(unsigned int t)  //STC89C52RC
{
	unsigned char i=0;
	while(t--)
	{
		for(i=0;i<12;i++);
	}
		
}

//??????DS18B20?????
void Write_DS18B20(unsigned char dat)
{
	unsigned char i;
	for(i=0;i<8;i++)
	{
		DQ = 0;
		DQ = dat&0x01;
		Delay_OneWire(5);
		DQ = 1;
		dat >>= 1;
	}
	Delay_OneWire(5);
}

//?DS18B20??????
unsigned char Read_DS18B20(void)
{
	unsigned char i;
	unsigned char dat;
  
	for(i=0;i<8;i++)
	{
		DQ = 0;
		dat >>= 1;
		DQ = 1;
		if(DQ)
		{
			dat |= 0x80;
		}	    
		Delay_OneWire(5);
	}
	return dat;
}

//DS18B20?????
bit init_ds18b20(void)
{
  	bit initflag = 0;
  	
  	DQ = 1;
  	Delay_OneWire(12);
  	DQ = 0;
  	Delay_OneWire(80);
  	DQ = 1;
  	Delay_OneWire(10); 
    initflag = DQ;     
  	Delay_OneWire(5);
  
  	return initflag;
}

//void Delay750ms()		//@11.0592MHz
//{
//	unsigned char i, j, k;

//	_nop_();
//	_nop_();
//	i = 32;
//	j = 133;
//	k = 87;
//	do
//	{
//		do
//		{
//			while (--k);
//		} while (--j);
//	} while (--i);
//}

float rd_temperature(void)
{
	//unsigned char low,high;
	init_ds18b20();
	Delay_OneWire(1); 
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
  
	//Delay750ms();
	
	init_ds18b20();
	Delay_OneWire(1); 
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);

  low=  Read_DS18B20();
	high= Read_DS18B20();
	temp=high*256+low;
	//return temp*0.0625;
	
	//temp=high*256+low*0.0625;
	
//	temp=high;
//	temp=temp<<8;
//	temp=temp|low;
//	
	temp>>=4;
	temp=temp*10;
	temp=temp+(low&0x0f)*0.625;
	return temp;
}

//*********************?????1??*****************
void Delay1ms()		//@11.0592MHz
{
	unsigned char i, j;

	_nop_();
	_nop_();
	_nop_();
	i = 11;
	j = 190;
	do
	{
		while (--j);
	} while (--i);
}

//***********************????*********************
void ds()
{

	
	P2=0XC0;//com
	P0=0X80;//7
	P2=0XE0;
	P0=duanma[temp%10];
	P2=0X00;
	Delay1ms();

	P2=0XC0;
	P0=0X40;//6
	P2=0XE0;
	P0=duanma_dot[(temp%100)/10];
	P2=0X00;
	Delay1ms();

	P2=0XC0;//com
	P0=0X20;//5
	P2=0XE0;
	P0=duanma[temp/100];
	P2=0X00;
	Delay1ms();

	P2=0XC0;//com
	P0=0X10;//4
	P2=0XE0;
	P0=0xff;
	P2=0X00;
	Delay1ms();

	P2=0XC0;//com
	P0=0X08;//3
	P2=0XE0;
	P0=0xff;
	P2=0X00;
	Delay1ms();

	P2=0XC0;//com
	P0=0X04;//2
	P2=0XE0;
	P0=0xff;
	P2=0X00;
	Delay1ms();

	P2=0XC0;//com
	P0=0X02;//1
	P2=0XE0;
	P0=0xff;
	P2=0X00;
	Delay1ms();

	P2=0XC0;//com
	P0=0X01;//0
	P2=0XE0;
	P0=0xff;
	P2=0X00;
	Delay1ms();

}

void main()
{
	//float temp;
	init_system();
	while(1)
	{
		//temp=rd_temperature();
		rd_temperature();
		//temp=temp*10;
		ds();
		
	}
}

### 蓝桥杯单片机DS18B20常见问题及解决方案 #### DS18B20初始化失败 当尝试初始化DS18B20时可能出现失败的情况。这通常是因为总线上的通信未成功建立,可能是由于硬件连接不稳定或是程序中的延时设置不当所致。 对于这个问题的一个有效处理方式是在初始化之后加入适当的等待时间来确保设备能够响应命令[^1]: ```c bit init_ds18b20(void) { // 初始化代码... _delay_ms(50); // 添加适当延迟以稳定初始化过程 } ``` #### 温度读取异常值(如85°C) 某些情况下,在初次启动或特定环境下可能会得到不合理的温度数值比如85摄氏度。这种现象通常是由于传感器内部状态尚未完全准备好返回实际测量数据所引起的。 针对此情况可以采取循环重试机制直到获取到合理范围内的温度值为止[^3]: ```c void init_rd_temperature(void){ unsigned char low=0,high=0; do{ init_ds18b20(); Write_DS18B20(0xcc); Write_DS18B20(0x44); init_ds18b20(); Write_DS18B20(0xcc); Write_DS18B20(0xbe); low = Read_DS18B20(); high = Read_DS18B20(); high = (high << 4) | (low >> 4); }while(high == 25 || high == 85); // 过滤掉错误的初始读数 } ``` #### 多个DS18B20共存于同一总线上引发冲突 在同一根I/O线上挂接多个相同类型的器件可能导致地址识别混乱从而影响正常工作。为了避免这种情况发生,建议每次只操作一个指定ROM编号的目标节点;如果确实需要支持多点测温,则应考虑使用具备更复杂寻址功能的产品替代标准版DS18B20. 另外值得注意的是,虽然上述措施可以在一定程度上缓解由物理层面上的原因造成的冲突,但在设计电路布局阶段就应当充分考虑到信号完整性等因素的影响,尽可能减少潜在干扰源的存在。 #### 底层驱动函数理解不足 参赛者可能对官方提供的`OneWire`协议实现细节不够熟悉,特别是如何通过调用这些接口完成具体的交互流程。以下是几个常用的API说明及其应用场景[^2]: - `init_ds18b20()`:用于向DS18B20发送复位脉冲并确认其存在与否; - `Write_DS18B20(dat)`:负责将一字节的数据写入目标设备; - `Read_DS18B20()`:从选定单元处提取当前寄存器里的内容。 掌握好这几个基本构件有助于构建更加健壮的应用逻辑框架。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值