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

📄 ds18b20._c

📁 AVR16源代码 ICC编译器
💻 _C
字号:
/********************************************************************
			ATmega16L开发板18B20测试程序
			晶振频率:	4MHz
			编译:		ICCAVR 6.31
			编写:		QINDAOTANG
********************************************************************/

#include <iom16v.h>
#include <macros.h>
#include "DS18B20.H"
//硬件连接与PD6
#define      DQ       1<<6      
#define      true     0
#define      false     1
float temperature;
/*===================================================================
//	函数功能:	DS18B20数据校验函数
//	形参:		void
//	返回:		unsigned char	校验结果
//	编写:		2004/8/25
//	备注:		CRC公式为:CRC = X^8 + X^5 + X^4 + 1
===================================================================*/
unsigned char crccheck(unsigned char *p,unsigned char len)
{
	unsigned char bit0,cbit,r,temp,i,j,byte;
	temp = 0;
	for(j = 0; j < len; j++)
	{
		byte = p[j];
		for(i = 0; i < 8; i++)
		{
        		cbit = temp & 0x01;
        		bit0 = byte&0x01;
        		temp >>=   1;
        		r = cbit ^ bit0;
        		if(r == 1)
        			temp ^= 0x8c;
        		byte >>= 1;
        	}
    	}
	return	temp;	
}
/*===================================================================
//	函数功能:	us延时函数
//	形参:		void
//	返回:		void
//	编写:		2004/8/25
===================================================================*/
void	delay_us(unsigned int time)
{     
	do
	{
		time--;
	}	
	while (time>1);
}

/*===================================================================
//	函数功能:	判断总线应答
//	形参:		void
//	返回:		unsigned char	true为应答
//	编写:		2004/8/25
===================================================================*/
unsigned char ds1820_ack(void)
{
	unsigned char ack;
	DDRD |= DQ;
	PORTD &= ~DQ;
	delay_us(300);		//480US以上					//	reset
	PORTD |= DQ;
	DDRD &= ~DQ;
	delay_us(25);
	ack = DQ & PIND;
	delay_us(300);							//	host receive
	if(ack)
		return false;
	else
		return true;
}

/*===================================================================
//	函数功能:	从 1-wire 总线上读取一个字节
//	形参:		void
//	返回:		unsigned char	读到的值
//	编写:		2004/8/25
===================================================================*/
unsigned char	read_byte(void)
{
	unsigned char	i;
	unsigned char	value = 0;
	for(i = 8; i > 0; i--)
	{
		value >>= 1;						//	low bit first
		DDRD |= DQ;
		PORTD &= ~DQ;						//	pull DQ low to start timeslot
		delay_us(2);
		PORTD|= DQ;
		DDRD &= ~DQ;						//	release bus
		delay_us(5);
		if(DQ & PIND)
			value|=0x80;
		delay_us(100);
		DDRD |= DQ;
		delay_us(3);						//	time interval
	}
	return(value);
}



/*===================================================================
//	函数功能:	向 1-WIRE 总线上写一个字节
//	形参:		value		写到总线上的值
//	返回:		void
//	编写:		2004/8/25
===================================================================*/
void	write_byte(unsigned char value)
{
	unsigned char	i;
	DDRD|= DQ;
	for(i = 8; i > 0; i--)
	{
		if(value & 0x01)
		{
			PORTD &= ~DQ;					//	pull DQ low to start timeslot
			delay_us(5);
			PORTD |= DQ;
			delay_us(50);
		}
		else
		{
			PORTD&= ~DQ;					//	pull DQ low to start timeslot
			delay_us(50);
			PORTD|= DQ;
			delay_us(5);
		}
		value >>= 1;
	}
}


/*===================================================================
//	函数功能:	读取温度
//	形参:		*temperature	温度存储空间
//	返回:		unsigned char	true为有效
//	编写:		2004/8/25
===================================================================*/
unsigned char	Read_Temperature(void)
{
	unsigned char	i;
	unsigned int t;
	unsigned char	temporary[9]; 
	 
	ds1820_ack();
	write_byte(0xCC);						//	Skip ROM
	write_byte(0x44);						//	Start Conversion
	for(i = 0; i < 15; i++)
		delay_us(50000);
	ds1820_ack();
	write_byte(0xCC);						//	Skip ROM
	write_byte(0xBE);						//	Read Scratch Pad
	for(i = 0; i < 9; i++)
		temporary[i] = read_byte();	
	if(crccheck(temporary,9))
		return	false;
	else
	{	
	    t=temporary[0]+temporary[1]*256;
	    if(temporary[1]>0xf)
	       temperature=(~t+1)*0.0625;
		else
		   {
		   temperature=t*0.0625;
		   }
		
		return	true; 
	}
}

/*===================================================================
//	函数功能:	读取Rom Code
//	形参:		*temp		DS18B20的Rom Code存储空间
//	返回:		unsigned char	true为有效
//	编写:		2004/8/25
===================================================================*/
unsigned char	Read_RomCode(unsigned char *temp)
{
	ds1820_ack();
	write_byte(0x33);
	temp[0] = read_byte();
	temp[1] = read_byte();
	temp[2] = read_byte();
	temp[3] = read_byte();
	temp[4] = read_byte();
	temp[5] = read_byte();
	temp[6] = read_byte();
	temp[7] = read_byte();
	if(crccheck(temp,8))
		return	false;
	else
		return	true;
}

/*===================================================================
//	函数功能:	匹配DS18B20
//	形参:		*p	DS18B20的Rom Code
//	返回:		void
//	编写:		2004/8/25
===================================================================*/
void ds1820_match(unsigned char *p)
{
	unsigned char i;
	ds1820_ack();
	write_byte(0x55);	 
	for(i=0;i<8;i++)
		write_byte(p[i]);
}

⌨️ 快捷键说明

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