16 bit Data Register available??

I am using the following code [snipit] to read a temp sensor:

  i2c.beginTransmission(addr);
  i2c.write(0x0E);
  byte[] in1 = i2c.read(2);
  byte lowerLevel1 = in1[0];
  byte upperLevel1 = in1[1];
  int temperature1 = ((upperLevel1<<8) | lowerLevel1);
  if (temperature1 > 2047) {
    temperature1 = temperature1 - 4096;
  }

So, am having an issue with the result (random negative values)!

I ported (if this is the correct terminology) the code below:

// convert the upper byte of the 12bit signed value into proper 16bit signed
// format, if value is negative
 if (tmp & 0b00001000) tmp |= 0b11111000;

 // construct the 16bit signed value, shifting the upper byte 8 bits up and adding
// the lower byte
raw = (tmp << 8) | rxBuffer[0];

I believe the problem may be the use of "int" a 32 bit register . . . whereas this shifting is 16 bit?

Can someone confirm this or spot an issue in my conversion to Processing.

Thanks in advance . . .

Tagged:

Answers

Sign In or Register to comment.