|
发表于 2011-12-13 13:05:26
|
显示全部楼层
回复【17楼】ayumi8
能读到负温度么??? 很多驱动都没有负温度处理啊
-----------------------------------------------------------------------
注意一下程序代码就可以了。有个负温符号的,温度一般显示的是绝对值。
/******************* ds 18b20 **********************************/
void One_Wire_Delay(unsigned char delay_time)
{
while(delay_time)delay_time--;//Delay time us :=(8+delay_time*6)us;
}
/*******DS18B20***********************************************************************/
void Initize_One_Wire_Bus(void)
{
One_Wire_Bus=0;
One_Wire_Delay(80);//Bus master pulling low 488us
One_Wire_Bus=1;
One_Wire_Delay(25);//Resister pull up 158us;
}//Intialization the 1-wire devices;
/***********************************************************************/
unsigned char One_Wire_Read_Byte(void)
{
bit temp_bit;
unsigned char i,result=0;
for(i=0;i<8;i++)
{
One_Wire_Bus=0;
One_Wire_Bus=1;
temp_bit=One_Wire_Bus;
One_Wire_Delay(9);//delay 62 us
if(temp_bit)
result|=0x01<<i;
}
return(result);
//return the result of the 1-wire device;
}//Read a byte from the 1-wire bus;
/***********************************************************************/
void One_Wire_Write_Byte(unsigned char oww_dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
One_Wire_Bus=0;
if(oww_dat&0x01)One_Wire_Bus=1;
One_Wire_Delay(20);//delay 128 us
One_Wire_Bus=1;
oww_dat>>=1;
}
One_Wire_Delay(10);
}//Write a byte to the 1-wire bus;
/***********************************************************************/
void Read_18B20(void)
{
unsigned char tempH,tempL;
Initize_One_Wire_Bus();
One_Wire_Write_Byte(SkipRom);
_nop_();
//There is just one DS1820 on the bus;
One_Wire_Write_Byte(ConvertTemperature);
One_Wire_Delay(5);
//Start to convert temperature;
Initize_One_Wire_Bus();
One_Wire_Write_Byte(SkipRom);
_nop_();
One_Wire_Write_Byte(ReadScratchpad);
GetScratchpad[0]=One_Wire_Read_Byte();
//Master samples the LSB temperature from the scratchpad;
GetScratchpad[1]=One_Wire_Read_Byte();
//Master samples the MSB temperature from the scratchpad;
One_Wire_Delay(120);
tempH=(GetScratchpad[1]<<4)|(GetScratchpad[0]>>4);
tempL=(GetScratchpad[0]&0x0f);
Initize_One_Wire_Bus();
//Issue a reset to terminate left parts;
if(tempH&0x80)
{
tempH=~tempH;
tempL=~tempL+1;
ResultSignal=1;
//Negative temperature;
}
ResultTemperatureH=tempH;
ResultTemperatureLL=decimalL[tempL];
ResultTemperatureLH=decimalH[tempL];
//Result of temperature;
}//Read the byte0 and byte1 from scratchpad; |
|