//从ds18b20得到温度值 //精度:0.1C //返回值:温度值 (-550~1250) short DS18B20_Get_Temp(void) { u8 temp; u8 TL,TH; short tem; DS18B20_Start (); // ds1820 start convert DS18B20_Rst(); DS18B20_Check(); DS18B20_Write_Byte(0xcc);// skip rom DS18B20_Write_Byte(0xbe);// convert TL=DS18B20_Read_Byte(); // LSB TH=DS18B20_Read_Byte(); // MSB if(TH>7) { TH=~TH; TL=~TL; temp=0;//温度为负 }else temp=1;//温度为正 tem=TH; //获得高八位 tem<<=8; tem+=TL;//获得底八位 tem=(float)tem*0.625;//转换 if(temp)return tem; //返回温度值 else return -tem; }
//UPDATE: //修正输出值分辨率,修改返回属性为double //从ds18b20得到温度值 //精度:0.0625C //返回值:温度值 (-55~125) double DS18B20_Get_Temp(void) { u8 zhengfu; u8 TL,TH; short temRaw; double finalTemp; DS18B20_Start (); // ds1820 start convert DS18B20_Rst(); DS18B20_Check(); DS18B20_Write_Byte(0xcc);// skip rom DS18B20_Write_Byte(0xbe);// convert TL=DS18B20_Read_Byte(); // LSB TH=DS18B20_Read_Byte(); // MSB if(TH>7) { TH=~TH; TL=~TL; zhengfu=0;//温度为负 }else zhengfu=1;//温度为正 temRaw=TH; //获得高八位 temRaw<<=8; temRaw+=TL;//获得底八位 finalTemp=(double)temRaw*0.0625;//转换 return zhengfu?finalTemp:-finalTemp; }
short DS18B20_Get_Temp(u8 ch) { u8 temp; u8 TL,TH; short tem; u32 ads[8]={0x186e0d3b,0x182d843b,0x186e743b,0x186b0c3b,0x1873ba3b,0x1835163b,0x1834fa3b,0x1873983b}; u32 crc[8]={0x7f000000,0xa4000000,0x71000000,0x9a000000,0x6d000000,0x39000000,0xa0000000,0xb5000000}; DS18B20_Rst(); DS18B20_Check(); delay_us(50); DS18B20_Write_Byte(0x55); delay_us(10); DS18B20_Write_Addr(ads[ch],crc[ch]); delay_us(10); DS18B20_Write_Byte(0xbe);// convert TL=DS18B20_Read_Byte(); // LSB TH=DS18B20_Read_Byte(); // MSB if(TH>7) { TH=~TH; TL=~TL; temp=0;//negative }else temp=1;//positive tem=TH; //Hreg tem<<=8; tem+=TL;//Lreg tem=(float)tem*0.625;//convert if(temp)return tem; //return else return -tem; }
//从ds18b20得到温度值 //精度:0.1C //返回值:温度值 (-550~1250) short DS18B20_Get_Temp(void) { u8 flag; u8 TL,TH; short Temperature; float Temperature1; DS18B20_Rst(); DS18B20_Answer_Check(); DS18B20_Write_Byte(0xcc);// skip rom DS18B20_Write_Byte(0x44);// convert // ds1820 start convert DS18B20_Rst(); DS18B20_Answer_Check(); DS18B20_Write_Byte(0xcc);// skip rom DS18B20_Write_Byte(0xbe);// convert TL=DS18B20_Read_Byte(); // LSB TH=DS18B20_Read_Byte(); // MSB if( TH&0xfc) { flag=1; Temperature=(TH<<8)|TL; Temperature1=(~ Temperature)+1; Temperature1*=0.0625; } else { flag=0; Temperature1=((TH<<8)|TL)*0.0625; } Temperature = Temperature1*100; return Temperature; }