Using DS1820 data as integers--Solved (3 ways)

I am using the DallasTemperature.h successfully, no complaints. However, there are a number of cases in my control software where I only need to use the integer value of the temperature, which would also allow me to store my 40 variables in a smaller index.

I noticed that if the 12-bit temperature is read as a two-byte integer, it could be shifted left 4 bits and the high byte would contain the integer of the temperature and the low byte would contain the fraction. This would also preserve the sign bit. I am only smart enough to use the function that returns the temperature as a float:

float mytemp1 ;
mytemp1 = sensors1.getTempCByIndex(0) ;

The DallasTemperature.h and DallasTemperature.cpp is a bit difficult to follow. Is there a function that returns the temperature as an integer, or is there a simple way to convert the floating value to a two-byte integer?

It will be automatically translated.
You can use the temperature in 1/100 degrees.

``````// integer 'x' will be 10.
float temperature = 10.95;
int x;
x = (int) temperature;

// y will be 1095;
int y;
y = (int) (temperature * 100.0);
``````

I just use the OneWire library, it only takes a few lines of code to read the sensor. I also like to work in the integer domain to avoid the code size and run time overhead of floating point.

``````#include <OneWire.h>          //http://www.pjrc.com/teensy/td_libs_OneWire.html
#include <Streaming.h>        //http://arduiniana.org/libraries/streaming/

const int DS_PIN = 5;         //DS18B20 temperature sensor
OneWire ds(DS_PIN);

void setup(void)
{
Serial.begin(115200);
}

void loop(void)
{
dsStart();
delay(1000);                //wait for temperature conversion to complete

Serial << (C + 8) / 16 << "C " << (F + 5) / 10 << "F\n";

//    float C = dsReadC16() / 16.0;
//    float F = dsReadF10() / 10.0;
//    Serial << C << "C " << F << "F\n";
}

//start temperature conversion
void dsStart(void)
{
ds.reset();
ds.skip();
ds.write(0x44);
}

//get temperature as °C times 16
{
uint8_t dsData[9];

ds.reset();
ds.skip();

for ( int i=0; i<9; i++) {     //read 9 bytes
}

if (OneWire::crc8(dsData, 8) == dsData[8]) {
return (dsData[1] << 8) + dsData[0];
}
else {
return -9999; //CRC error
}
}

//get temperature as °F * 10