蓝桥杯单片机DS18B20温度读取

流程:①:DS18B20复位。

②:写入字节0xcc,跳过ROM指令。

③:写入字节0x44,开始温度转换。

④:延时700~900ms

⑤:DS18B20复位。

⑥:写入字节0xcc,跳过ROM指令。

⑦:写入字节0xbe,读取高速暂存器。

⑧:读取暂存器的第0字节,即温度数据的LSB。

⑨:读取暂存器的第1字节,即温度数据的MSB。

⑩:DS18B20复位,表示读取数据结束。

11:将LSB和MSB整合成为一个16位数据。

12:判断读取结果的符号,进行正负温度的数据处理。

void Read_DS18B20_temp()

{

unsigned char LSB,MSB;

init_ds18b20();

Write_DS18B20(0xcc);

Write_DS18B20(0x44);

Delay(1000);

init_ds18b20();

Write_DS18B20(0xcc);

Write_DS18B20(0xbe);

LSB=Read_DS18B20();

MSB=Read_DS18B20();

temp=MSB;

temp<<8;

temp=temp|LSB;

if((temp&0xf800)==0x0000)

{

temp>>=4;

temp=temp*10;

temp=temp+(LSB &0x0f)*0.0625;

}

}

如果最高5位都为0,说明这个温度是个整数。如果都是1,说明温度是个负数。

DS18B20的分辨率为0.0625.读出的数据是正数时,将LSB和MSB整合的16位整数,直接乘以0.0625即可。读出的数据为负数时,则需要将LSB和MSB整合的16位整数取反加1后,再乘以0.0625。

下面是完整代码:

DS18B20.c:

#include "reg52.h"
#include "absacc.h"
#include "onewire.h"
unsigned char SMGNoDot_CA[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char SMGDot_CA[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
unsigned int temp=0;
void DisPlay_All(unsigned char dat)
{
	P0=0xff;
	P2=P2&0x1f|0xc0;
	P2=P2&0x1f;
	P0=dat;
	P2=P2&0x1f|0xe0;
	P2=P2&0x1f;
}
void DelaySMG(unsigned int t)
{
	while(t--);
}
void DisPlaySMG_Bit(unsigned char pos,unsigned char dat)
{
	P0=0xff;
	P2=P2&0x1f|0xe0;
	P2=P2&0x1f;
	P0=0x01<<pos;
	P2=P2&0x1f|0xc0;
	P2=P2&0x1f;
	P0=dat;
	P2=P2&0x1f|0xe0;
	P2=P2&0x1f;
}
void DisPlaySMG_Temp()
{
	DisPlaySMG_Bit(7,SMGNoDot_CA[temp%10]);
	DelaySMG(100);
	DisPlaySMG_Bit(6,SMGDot_CA[(temp%100)/10]);
	DelaySMG(100);
	DisPlaySMG_Bit(5,SMGNoDot_CA[temp/100]);
	DelaySMG(100);
	DisPlaySMG_Bit(4,0xff);
	DelaySMG(100);
	DisPlaySMG_Bit(3,0xff);
	DelaySMG(100);
	DisPlaySMG_Bit(2,0xff);
	DelaySMG(100);
	DisPlaySMG_Bit(1,0xff);
	DelaySMG(100);
	DisPlaySMG_Bit(0,0xff);
	DelaySMG(100);
	DisPlay_All(0xff);
}
void Delay(unsigned int t)
{
	while(t--);
	{
		DisPlaySMG_Temp();
	}
}
void Read_DS18B20_temp()
{
	unsigned char LSB,MSB;
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	Delay(1000);
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);
	LSB=Read_DS18B20();
	MSB=Read_DS18B20();
	temp=MSB;
	temp<<8;
	temp=temp|LSB;
	if((temp&0xf800)==0x0000)
	{
		temp>>=4;
		temp=temp*10;
		temp=temp+(LSB &0x0f)*0.0625;
	}
	
	
}
void main()
{
	P0=0;
	P2=P2&0x1f|0xa0;
	P2=P2&0x1f;
	P0=0xff;
	P2=P2&0x1f|0x80;
	P2=P2&0x1f;
	while(1)
	{
		Read_DS18B20_temp();
		DisPlaySMG_Temp();
	}
}

 onewire.h:

#include "reg52.h"

sbit DQ = P1^4;

//µ¥×ÜÏßÑÓʱº¯Êý
#ifndef STC12  
void Delay_OneWire(unsigned int t)  //STC89C52RC
{
	while(t--);
}
#else
void Delay_OneWire(unsigned int t)  //STC12C5260S2
{
	unsigned char i;
	while(t--){
		for(i=0;i<12;i++);
	}
}
#endif

//ͨ¹ýµ¥×ÜÏßÏòDS18B20дһ¸ö×Ö½Ú
void Write_DS18B20(unsigned char dat)
{
	unsigned char i;
	for(i=0;i<8;i++)
	{
		DQ = 0;
		DQ = dat&0x01;
		Delay_OneWire(50);
		DQ = 1;
		dat >>= 1;
	}
	Delay_OneWire(50);
}

//´Ó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(50);
	}
	return dat;
}

//DS18B20³õʼ»¯
bit init_ds18b20(void)
{
  	bit initflag = 0;
  	
  	DQ = 1;
  	Delay_OneWire(120);
  	DQ = 0;
  	Delay_OneWire(800); // ÑÓʱ´óÓÚ480us
  	DQ = 1;
  	Delay_OneWire(100);  // 14
  	initflag = DQ;     // initflagµÈÓÚ1³õʼ»¯Ê§°Ü
  	Delay_OneWire(50);
  
  	return initflag;
}

//DS18B20ζȲɼ¯³ÌÐò£ºÕûÊý
// unsigned char rd_temperature(void)
// {
//     unsigned char low,high;
//   	char temp;
//   
//   	init_ds18b20();
//   	Write_DS18B20(0xCC);
//   	Write_DS18B20(0x44); //Æô¶¯Î¶Èת»»
//   	Delay_OneWire(200);

//   	init_ds18b20();
//   	Write_DS18B20(0xCC);
//   	Write_DS18B20(0xBE); //¶ÁÈ¡¼Ä´æÆ÷

//   	low = Read_DS18B20(); //µÍ×Ö½Ú
//   	high = Read_DS18B20(); //¸ß×Ö½Ú
// /** ¾«¶ÈΪ1ÉãÊÏ¶È */  
//   	temp = high<<4;
//   	temp |= (low>>4);
//   
//   	return temp;
// }

//DS18B20ζȲɼ¯³ÌÐò£º¸¡µãÊý
float rd_temperature_f(void)
{
    unsigned int temp;
	float temperature;
    unsigned char low,high;
  
  	init_ds18b20();
  	Write_DS18B20(0xCC);
  	Write_DS18B20(0x44); //Æô¶¯Î¶Èת»»
  	Delay_OneWire(200);

  	init_ds18b20();
  	Write_DS18B20(0xCC);
  	Write_DS18B20(0xBE); //¶ÁÈ¡¼Ä´æÆ÷

  	low = Read_DS18B20(); //µÍ×Ö½Ú
  	high = Read_DS18B20(); //¸ß×Ö½Ú
/** ¾«¶ÈΪ0.0625ÉãÊÏ¶È */  
	temp = (high&0x0f);
	temp <<= 8;
	temp |= low;
	temperature = temp*0.0625;
  
  	return temperature;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值