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

📄 ds1820.h

📁 Thermostat Project using DS18B20 and P89V51RD2 Microcontroller. Compiler used is SDCC. Schematic Inc
💻 H
字号:
//Function Library to access DS1820 or DS1821 using 1-wire bus.
//Author: Anurag Chugh (anurag@ucmicrosys.com)
//Original Code was taken from http://www.sixca.com/micro/mcs51/ds1820_51/

//---------------------------------------
// DS1820 driver
// www.GetMicro.com
//---------------------------------------

#ifndef __p89v51rd2_h__
	#include <p89v51rd2.h>
	#define __p89v51rd2_h__
#endif

#include<stdio.h>

#define DQ P3_5	// connect with DS1820 Data pin

//---------------------------------------
// Delay mS function
// 1 milliseconds delay at 11.0592MHz Crystal
//---------------------------------------
void DelayMs(unsigned int count) 
{
    unsigned int i;
    while(count) {
        i = 115; 
		while(i>0) i--;
        count--;
    }
}


//----------------------------------------
// DELAY at 11.0592MHz crystal.
// Calling the routine takes about 22us, and then
// each count takes another 17us.
// test with KEIL C51 V7.5
//----------------------------------------
void DelayUs(int us)
{
	int i;
	for (i=0; i<us; i++);
}

//----------------------------------------
// Reset DS1820
//----------------------------------------
char ResetDS1820(void)
{
	char presence;
	DQ = 0; 		//pull DQ line low
	DelayUs(29);		// leave it low for about 490us
	DQ = 1;			// allow line to return high
	DelayUs(3);		// wait for presence 55 uS
	presence = DQ; 		// get presence signal
	DelayUs(25);		// wait for end of timeslot 316 uS 
	return presence;	// presence signal returned
				// 0=presence, 1 = no part

} 	

//-----------------------------------------
// Read one bit from DS1820
//-----------------------------------------
char ReadBit(void)
{
	unsigned char i=0;
	DQ = 0; 	// pull DQ low to start timeslot
	DQ=1;
	for (i=0; i<3; i++); // delay 17 us from start of timeslot
	return DQ; // return value of DQ line
}

//-----------------------------------------
// Write one bit to DS1820
//-----------------------------------------
void WriteBit(bit Dbit)
{
	unsigned char i=0;	
	DQ=0;	
	DQ = Dbit ? 1:0;
	DelayUs(5); 			// delay about 39 uS
	DQ = 1;
}

//-----------------------------------------
// Read 1 byte from DS1820
//-----------------------------------------
unsigned char ReadByte(void)
{
	unsigned char i;
	unsigned char Din = 0;
	for (i=0;i<8;i++)
	{
		Din|=ReadBit()? 0x01<<i:Din;
		DelayUs(6); 
	}
	return Din;
}

//-----------------------------------------
// Write 1 byte
//-----------------------------------------
void WriteByte(unsigned char Dout)
{
	unsigned char i;
	for (i=0; i<8; i++) // writes byte, one bit at a time
	{	    
		WriteBit((bit)(Dout & 0x1)); 		// write bit in temp into
		Dout = Dout >> 1;
	}
	DelayUs(5);
}

//-----------------------------------------
// Read temperature
//-----------------------------------------
void ReadTemp(unsigned char * buff)
{
	unsigned char n;
	
	EA=0;				// disable all interrupt
	ResetDS1820();
	WriteByte(0xcc);		// skip ROM
	WriteByte(0x44);		// perform temperature conversion
	while (ReadByte()==0xff);	// wait for conversion complete	
	ResetDS1820();
	WriteByte(0xcc);		// skip ROM
	WriteByte(0xbe);		// read the result
    
	for (n=0; n<9; n++)		// read 9 bytes but, use only one byte
	{
		buff[n]=ReadByte();	// read DS1820
	}
	EA=1;
	
	//Excrept from The DS18B20 Datasheet (Page 11 of 22):
	//For example, the master can make all DS18B20s on the bus perform
	//simultaneous temperature conversions by issuing a Skip ROM command followed by a Convert T [44h]
	//command.
	//
	//Note that the Read Scratchpad [BEh] command can follow the Skip ROM command only if there is a
	//single slave device on the bus. In this case, time is saved by allowing the master to read from the slave
	//without sending the device抯 64-bit ROM code. A Skip ROM command followed by a Read Scratchpad
	//command will cause a data collision on the bus if there is more than one slave since multiple devices will
	//attempt to transmit data simultaneously.
}


⌨️ 快捷键说明

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