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

📄 ds1820.c

📁 DS18B20 for 8051 in C51
💻 C
字号:
//---------------------------------------
// DS1820 driver
// www.GetMicro.com
//---------------------------------------
#include<reg8252.h>
#include<stdio.h>

sbit DQ  =  P2^0;	// connect with DS1820 Data pin

//---------------------------------------
// Delay mS function
//---------------------------------------
void DelayMs(unsigned int count) 
{  // mSec Delay 11.0592 Mhz 
    unsigned int i;		       		// Keil v7.5a 
    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
//----------------------------------------
bit ResetDS1820(void)
{
	bit 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
//-----------------------------------------
bit 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;
}


⌨️ 快捷键说明

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