⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ds18b20_driver.bak

📁 该程序是基于c8051f020的平台
💻 BAK
字号:
#define  DS18B20_Driver
#include <DS18B20_Driver.h>

void DS18B20_Init(void)
{
	unsigned char Init_Data[3]={20,10,0x3f};	//分辨率为10位
	unsigned char i=0;
	Reset();						//复位
	Write_Byte(SKIP_ROM);			//跳过ROM命令
	Write_Byte(WRITE_SCRATCHPAD);	//写存储器命令
	for(i=0;i<3;i++)
	{
		Write_Byte(Init_Data[i]);
	}
	Reset();						//复位
	Write_Byte(SKIP_ROM);			//跳过ROM命令
	Write_Byte(COPY_SCRATCHPAD);	//拷贝存储器命令
}
//************************************************************
//************************************************************

uint Get_Temp(void)
{
	unsigned char i=0;
	unsigned char TempH=0,TempL=0;
      unsigned int Temp = 0;
   //	float temprature=0;

	Reset();						//复位
	Write_Byte(SKIP_ROM);			//跳过ROM命令
	Write_Byte(CONVERT_T);			//启动转换命令
//	Wire_Delay(50000);
	Reset();						//复位
	Write_Byte(SKIP_ROM);			//跳过ROM命令
	Write_Byte(READ_SCRATCHPAD);	//读存储器命令
	TempL=Read_Byte();				//读取温度值低8位
	TempH=Read_Byte();				//读取温度值高8位

      //TempH<<=4;
      //temprature=TempH+TempL*0.0625;
      //return(temprature);
      Temp = TempH ;
      Temp<<=8;
      Temp = Temp|TempL;
      return(Temp);
}

void Wire_Delay(unsigned int num)
{
	unsigned int i=0;
	i=num;
	while(--i);
}

unsigned char Reset(void)
{
	unsigned char Presence_Flag=0;	//主机接收到1,说明从机不在线,0为在线
	DQ=0;				//拉低DQ线
	Wire_Delay(480);	//延时至少480微秒
	DQ=1;				//释放DQ线
	Wire_Delay(70);		//等待18B20响应
	Wire_Delay(120);	//在采样周期的中间采样(采样周期为60~240微秒)
	Presence_Flag=DQ;	//采样
	Wire_Delay(120);	//等待剩余的采样周期
	Wire_Delay(240);	//等待时序结束
	return(Presence_Flag);
}

unsigned char Read_Bit(void)
{
	unsigned char readbit=0;
	DQ=0;			//拉低DQ
	Wire_Delay(1);	//延时至少1微秒
	P1MDOUT &=0xef;
	DQ=1;			//释放DQ
	Wire_Delay(10);	//最多延时14微秒采样,此处为10微秒
	readbit=DQ;
	Wire_Delay(110);	//读取周期为(60~120)微秒
	P1MDOUT|=0x10;
	return(readbit);
}


void Write_Bit(unsigned char num)
{
	DQ=0;
	Wire_Delay(10);
	if(num & 0x01)
	{DQ=1;}
	else
	{DQ=0;}
	Wire_Delay(50);
	DQ=1;
	Wire_Delay(60);
}
void Write_Byte(unsigned char val)
{
	unsigned char i=0;
	for(i=0;i<8;i++)
	{
		Write_Bit(val);
		val>>=1;
	}
}

unsigned char Read_Byte(void)
{
	unsigned char i=0,receive_data=0;
	for(i=0;i<8;i++)
	{
		receive_data>>=1;
		if(Read_Bit())
		{
			receive_data |=0x80;
		}
		else
		{
			receive_data &=0x7f;
		}
	}
	return(receive_data);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -