user3530196
user3530196

Reputation:

How can I convert temperature data from an LM92 in Linux

I am trying to read the temperature data from an LM92. Everything is fine until the temperature goes below 0, then I get bad results. I know the problem is in the conversion of the 12 bit integer with sign bit to a Linux int value, but I have have not been able to find information on doing this. I have some examples that work with Arduino and Microchip, but not with Linux GCC.

Here is an example that works with anything other than Linux:

// LM92 Read
int i2c_lm92Read() {
    unsigned int data;

    data = (unsigned int) i2c_read(0x4B);   
    data <<= 8;
    data |= (unsigned int) i2c_read(0x4B);
    data /= 8;

    return (int) (data * 0.0625);
}

The results I am getting when I hit the LM92 with freeze spray:

LM92: 18C
LM92: 16C
LM92: 16C
LM92: 507C
LM92: 477C
LM92: 475C

Upvotes: 1

Views: 299

Answers (1)

jofel
jofel

Reputation: 3405

Try

data = (unsigned int) i2c_read(0x4B);   
data <<= 8;
data |= (unsigned int) i2c_read(0x4B);
data >>= 3;

if (data >= 4096)
    data -= 8192;

return ( (double) data) * 0.0625;

The constant 8192 is 2^13. In the used 12/13bit representation, -1 is equal to 8191 etc.

See http://www.kerrywong.com/2014/01/19/lm92-library-for-arduino/ for a reference implementation.

Upvotes: 2

Related Questions