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

📄 ds18b20.c

📁 非常经典的基于AVR m16的DS18B20 C 程序 !1
💻 C
字号:
/****************************************
** 文 件 名: DS18B20.c				   **
** 日    期: 2007年04月08日			   **
** 描    述: DS18B20函数			   **
** 作    者:tonghe					   **
** 版	 本: V1.0					   **
** 主控芯片:M16 					   **
** 晶振频率:7.3728MHZ, 波特率9600	   **
****************************************/
#include <iom16v.h>
#include <macros.h>
#include <E:\DS18B20\define.h>
#include <E:\DS18B20\function.h>

/****************************************
* 函数名称: uchar rst_DS18B20(void)		*
* 函数功能:DS18B20初始化				*
* 入    口:无							*
* 返    回:0x00为失败,0xFF为成功		*
****************************************/
uchar rst_DS18B20(void)
   {
	uchar errTime=0;
	RLS_DS18B20;		//释放总线
	_NOP();
	HLD_DS18B20;		//Maga16控制总线
	CLR_DS18B20;		//强制拉低
	delay_us(255);		//209.42us
	delay_us(255);		//209.42us
	delay_us(255);		//83.28us
	//以上的三个延时大于480us
	RLS_DS18B20;		//释放总线,总线自动上拉
	_NOP();			
	while(STU_DS18B20)	
		{
		delay_us(4);	 	//5.15us
		errTime++;
		if(errTime>20)
		return(0x00);		//如果等大于5.15us*20就返回0x00,报告复位失败(实际上只要等待15-60us)
		}
	errTime=0;
	while(!(STU_DS18B20))	
	    {
		delay_us(4);	 	//5.15us
		errTime++;
		if(errTime>50)
		return(0x00);		//如果等大于5.15us*50就返回0x00,报告复位失败(实际上只要等待60-240us)
		}
	return(0xff);
	}

/****************************************
* 函数名称: uchar read_DS18B20(void)	*
* 函数功能:读DS18B20一个字节			*
* 入    口:无							*
* 返    回:读出的数据字节				*
****************************************/
uchar read_DS18B20(void)
    {
	uchar i;
	uchar retVal=0;
	RLS_DS18B20;		//释放总线
	for(i=8;i>0;i--)
	    {
	 	retVal>>=1;
	    HLD_DS18B20;	//Maga16控制总线
		CLR_DS18B20;	//强制拉低
		delay_us(5);		//延时大于1us	
		RLS_DS18B20;		//释放总线,DS18B20会将总线强制拉低
		delay_us(2);	
		if(STU_DS18B20)
		retVal|=0x80;
		delay_us(16);	 	//14.92us
		delay_us(16);	 	//14.92us
		RLS_DS18B20;		//释放总线
		delay_us(35);	 	//30.38us
		}
	delay_us(2);	 		//2.71us(大于1us就行了)
	return(retVal);
	}

/****************************************
* 函数名称: uchar write_DS18B20(void)	*
* 函数功能:写DS18B20一个字节			*
* 入    口:需写入的字节数据wb			*
* 返    回:无							*
****************************************/
void write_DS18B20(uchar wb)
    {
	uchar i;
	uchar temp;
	RLS_DS18B20;		//释放总线
	for(i=0;i<8;i++)
	    {
		HLD_DS18B20;		//Maga16控制总线
		CLR_DS18B20;		//强制拉低
		delay_us(16);	 	//14.92us
		temp=wb>>i;
		temp&=0x01;
		if(temp)
		RLS_DS18B20;		//释放总线
		else
		CLR_DS18B20;		//强制拉低
		delay_us(16);	 	//14.92us
		delay_us(35);	 	//30.38us
		RLS_DS18B20;		//释放总线
		delay_us(2);	 	//2.71us(大于1us就行了)
	    }
    }

/****************************************
* 函数名称: uint read_Temp(void)	    *
* 函数功能:读DS18B20温度				*
* 入    口:无							*
* 返    回:读出的数据字节				*
****************************************/
uint read_Temp(void)
	{
	uchar tempL,tempH;
	uint x;
	rst_DS18B20();
	write_DS18B20(0xcc); 	    //跳过ROM
	write_DS18B20(0x44);		//启动温度转换
	delay_us(1);
	rst_DS18B20();
	write_DS18B20(0xcc);		//跳过ROM
	write_DS18B20(0xbe);		//读数据
	tempL=read_DS18B20();
	tempH=read_DS18B20();
	x=(tempH<<8)|tempL;
	return(x);
	} 


⌨️ 快捷键说明

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