# Converting from DS18b20

#### FroceMaster

Joined Jan 28, 2012
683
Hi,
HAve this working code, but not completly, cause when temperature is negative it goes from -1,9 -3,0 - 3,1 ect. never -2,2 -2,3 ect.-

I also add 500 to the result, cause i then dont have to deal with any negative numbers, ect for storing in EEPROM ect.
Code:
 if(reset()==0)
{

write(Skip_ROM);
__delay_us(10);
write(Convert_T);
__delay_ms(DS18B20_CONV_TIME);
reset();
write(Skip_ROM);
__delay_us(10);
//reset();

unsigned int sign = 248 & msb;
if(sign==0)
decimal = lsb & 15;

if(sign!=0)
{
decimal = lsb ^ 15;
decimal = decimal^255;

}

unsigned char places[4]= {0};
if((decimal&8) == 8)
{

places[0] =  places[0] + 5;

}

if((decimal&4)==4)
{
places[0] = places[0] + 2;
places[1] = places[1] + 5;
}

if((decimal&2)==2)
{
places[0] = places[0] + 1;
places[1] = places[1] + 2;
places[2] = places[2] + 5;
}

if((decimal&1)==1)
{
places[1] = places[1] + 6;
if(places[1]>9)
{
places[1]= places[1]-10;
places[0]= places[0]+1;
}

places[2] = places[2] + 2;
places[3] = places[3] + 5;

}

if(sign!=0)
{
integer1 =(places[0]*1000);
integer2 = (places[1]*100);
integer3 = (places[2]*10);
integer4 = (places[3]);
integer = integer1+integer2+integer3+integer4;
if(integer==0)
{
sign_flag=1;
}
integer = 10000-integer;

integer1 = integer/1000;
integer2 = (integer/100)-(10*integer1);
integer3 = (integer/10)-(100*integer1)-(10*integer2);
places[0]= integer1;
places[1]= integer2;
places[2]= integer3;
places[3]= integer4;

}
lsb = lsb >> 3;
msb = msb << 5;
lsb = msb | lsb;
if(sign !=0)
{
lsb =(lsb)^255;
buffer[0]='-';
}
else
buffer[0] = '+';

lsb = lsb >>1;
printf("sign %d\n", sign_flag);

buffer[1] = ((lsb / 100)%10) + 48;
buffer[2] = (((lsb / 10)%10) + 48);
buffer[3] = (((lsb)%10))+ 48 + sign_flag;
buffer[4] = '.';
buffer[5] = ((places[0]%10)+48);
buffer[6] = ((places[1]%10)+48);
buffer[7] = ((places[2]%10)+48);
buffer[8] = ((places[3]%10)+48);
buffer[9] = ' ' ;//0xF8;
buffer[10]= 'C';

total=0;
total=((lsb / 100)%10)* 100;
total=total+(((lsb / 10)%10) * 10);
total=total+(((lsb)% 10))+  sign_flag;
fraction=0;
fraction=((places[0]%10)+48)-48;
if (sign!=0)

{now=500-((total*10)+fraction); // add 50 degree du to no negativ result

}
if (sign==0)
{

}
}
Hope it gives some meaning,
Code is C , for microchip made in MPLAB X.

This graph is done over 1 hour,

#### FroceMaster

Joined Jan 28, 2012
683
Have been testet some code i found online, Works fine, but when temperature reach 0 degrees, code will not work, when temperature is negative.
Code:
 if(reset()==0)
{

write(Skip_ROM);
__delay_us(10);
write(Convert_T);
__delay_ms(DS18B20_CONV_TIME);
reset();
write(Skip_ROM);
__delay_us(10);

TReading = (msb<< 8) + lsb;
SignBit = TReading & 0x8000;  // test most sig bit
if (SignBit) // negative
{
}
Tc_100 = (6 * TReading) + TReading / 4;    // multiply by (100 * 0.0625) or 6.25

Whole = Tc_100 / 100;  // separate off the whole and fractional portions
Fract = Tc_100 % 100;

if (SignBit) // If its negative
{
// do things negativ    //  Use format  ex 5105  = 10,5 degree.
// ex. 6525 = (6525-5000)= 1525 = 15,25 Degree.
//ex. 4750 = (5000-4750) = 250 = -2,50 Degree, with "-" in front,.
now= 5000-((Whole*100)+Fract);  // Sets 5000 as my ZERO. degree, cause i then only need to handle positive numbers.
}
else
{ now=5000+((Whole*100)+Fract);  // if positive, i add 5000

} // end
Any hints, i am not good at using all that bitshifting ect. I just want a Whole Degree and a 1-2 digits DECIMAL.