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

📄 ds18b20.c

📁 DS18B20温度传感器读写驱动
💻 C
字号:
#ifndef _DS18B20_H__
#define _DS18B20_H__
#include "c8051F005.h"
#include <intrins.h>

/*
	数字温度传感器DS18b20试验

	试验准备:用连接线将JH端子的DS12B20位与CN7或CN8的P37口相连
	运行此程序,温度值将在数码管上显示.用手触摸T1芯片(DS18B20)
	观察温度变化.

*/
#define uchar unsigned char
#define uint unsigned int
#define TRUE    1
#define FALSE  0
#define DataPortDS1820 P3_3

static uchar  temp[3];
static uint  flag=0;
static void Delay1us(unsigned char us)
{
	while (us)
	{
	  _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); 
	  --us;
	}
}

void Delay15us(void)  
{
	Delay1us(15);
}


void Delay10us(void)
{	
	Delay1us(10);
}

bit RstDS1820(void)  //返回0-有设备连接1-无设备连接
{
unsigned char i;
bit RstFlag;
	RstFlag=1;	
	DataPortDS1820=0;
	for (i=0;i<100/*40*/;i++)	//480us
		Delay15us(); 
	DataPortDS1820=1;
	for (i=0;i<4;i++)	//15us-60us
	{
		Delay15us();
	}
	for (i=0;i<16;i++)	//60us-240us
	{
		Delay15us();
		if (DataPortDS1820==0) 
		RstFlag=0;
	}
	for (i=0;i<16;i++)	//240us
	{
		Delay15us();
	}
	return RstFlag;	
}

void WriteDS1820(unsigned char ch)
{
unsigned char i;
	DataPortDS1820=1;
	Delay1us(1);
	for (i=0;i<8;i++)
	{
		EA=0;
		DataPortDS1820=0;
		Delay15us();
		DataPortDS1820=ch&0x1;
		EA=1;
		Delay15us();Delay15us();Delay15us();
		DataPortDS1820=1;
		ch=ch>>1;
		Delay1us(1);
	}	
}

unsigned char ReadDS1820(void)
{
unsigned char i,ch;
	ch=0;
	DataPortDS1820=1;
	Delay1us(1);
	for (i=0;i<8;i++)
	{
		EA=0;
		DataPortDS1820=0;
		Delay10us();
		DataPortDS1820=1;
		Delay1us(2);
		ch=ch>>1;
		if (DataPortDS1820==1)
		{
			ch=ch+0x80;
		}
		EA=1;
		Delay15us();Delay15us();Delay15us();
	}	
	return ch;
}

void GetROMCode(unsigned char idata *ptr)
{
unsigned char i;
	RstDS1820();
	WriteDS1820(0x33); //28h,1ah,2fh,0bh,00h,00h,00h,89h,
	for (i=0;i<8;i++)
	{
		*(ptr+i)=ReadDS1820();
	}
}	
void MatchROMCode(unsigned char idata *ptr)
{
unsigned char i;
	RstDS1820();
	WriteDS1820(0x55); 
	for (i=0;i<8;i++)
	{
		WriteDS1820(*(ptr+i));
	}
}

bit GetPowerType(void)
{
	WriteDS1820(0xb4); 
	EA=0;
	DataPortDS1820=1;
	Delay10us();
	if (DataPortDS1820)
	{
		EA=1;
		return TRUE;
	}
	else
	{
		EA=1;
		return FALSE;
	}
}
void WriteConfig(unsigned char Config)
{
	WriteDS1820(0x4e); 
	WriteDS1820(0x7f); 
	WriteDS1820(0x0); 
	WriteDS1820(Config); 
}
unsigned char GetConfig(void)
{
	WriteDS1820(0xBE); 
	ReadDS1820();
	ReadDS1820();
	ReadDS1820();
	ReadDS1820();
	return ReadDS1820();
}


void SkipROMCode(void)
{
	RstDS1820();
	WriteDS1820(0xcc); 
}
void StartADC(void)
{

	RstDS1820();		//复位
	WriteDS1820(0xcc);  //广播
	WriteDS1820(0x44); 	//启动AD转换 12bit700ms
}

unsigned int readtemperature(void)
{
unsigned char i,j;
unsigned int T;
	SkipROMCode();
	WriteDS1820(0xBE); 
	i=ReadDS1820();
	j=ReadDS1820();
	StartADC();

	if((j*256+i)>63488)	
	  {
	    T=~(j*256+i)+1;T=T*25;
	    flag=1;  /*//取反加1 */
	   }  
	else
	  { 
	   T = (j*256+i)*25;
	   flag=0;
	  }	
	return(T>>2);

}
 /**温度数值处理**/
 float GetTempValue(void)
 {
    static uint te=0xffff;
   	uint realtemp;
	float Tep;
	realtemp=readtemperature();
	if(te!=realtemp)
	{
	temp[0]=realtemp/1000;
	temp[1]=(realtemp/100)%10;
	temp[2]=(realtemp/10)%10;
	te=realtemp;
	}
	Tep=temp[0]*10+temp[1]+temp[2]*0.1;
	return Tep;        /*(temp[0]<<4|temp[1]);*/
 }

⌨️ 快捷键说明

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