文档视界 最新最全的文档下载
当前位置:文档视界 › DS18B20头文件

DS18B20头文件

#ifndef __DS18B20_H__
#define __DS18B20_H__
#include
#include

#define HIGH 1
#define LOW 0
#define ZERO 0
#define MSB 0x80


/*******************************************************************/
sbit DQ=P1^0;
unsigned char dispbuff[8];
unsigned char timenum;
unsigned char disp_bit_count;

/********************************************************************/
unsigned char Get[2];
unsigned char code decimalH[16]={00,06,12,18,25,31,37,43,50,56,62,68,75,81,87,93};
unsigned char ResultH;
unsigned char ResultLH;
bit ResultSignal=0;

/********************************************************************/
void DS18B20_Delay(unsigned char t)
{
while(t)t--;//延时时间:=(8+delay_time*6)us;
}

//*****初始化DS18B20******/
void Initize_DS18B20(void)
{
DQ=0;
DS18B20_Delay(80);//总线拉低 488us
DQ=1;
DS18B20_Delay(25);//总线拉高 158us;
}
/***********************************************************************/

/*******************读一个字节ds18b20 *********************************/
unsigned char DS18B20_Read_Byte(void)
{
bit temp_bit;
unsigned char i,result=0;
for(i=0;i<8;i++)
{
DQ=0;
DQ=1;
temp_bit=DQ;
DS18B20_Delay(9);//延时 62 us
if(temp_bit)
result|=0x01<}
return(result); //返回ds18b20值

}

/*********向ds18b20写一个字节*********************/
void DS18B20_Write_Byte(unsigned char oww_dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ=0;
if(oww_dat&0x01)DQ=1;
DS18B20_Delay(20);// 128 us
DQ=1;
oww_dat>>=1;
}
DS18B20_Delay(10);
}
/******************************************/

int read_temp()
{
unsigned char tempH,tempL;
int Result;
Initize_DS18B20();
DS18B20_Write_Byte(0xcc);
_nop_();
//There is just one DS1820 on the bus;
DS18B20_Write_Byte(0x44);
DS18B20_Delay(20);
//Start to convert temperature;
Initize_DS18B20();
DS18B20_Write_Byte(0xcc);
_nop_();
DS18B20_Write_Byte(0xbe);
Get[0]=DS18B20_Read_Byte();
//Master samples the LSB temperature from the scratchpad;
Get[1]=DS18B20_Read_Byte();
//Master samples the MSB temperature from the scratchpad;
DS18B20_Delay(120);
tempH=(Get[1]<<4)|(Get[0]>>4);
tempL=(Get[0]&0x0f);
Initize_DS18B20();
//Issue a reset to terminate left parts;
if(tempH&0xc0)
{
tempH=~tempH;
tempL=~tempL+1;
ResultSignal=1; //Negative temperature;
}
else ResultSignal=0;

ResultH=tempH;

ResultLH=decimalH[tempL];

Result=ResultH*10+ ResultLH/10 ;
if(ResultSignal)
Result=-Result;
return(Result);
//Result of temperature;
}

//Read the byte0 and byte1 from scratchpad;
/***********************************************************************/


#endif

相关文档