Using the new Arduino IDE for ESP8266 and found bugs, report them here

Moderator: igrr

User avatar
By Justblair
#13813
Code: Select allint bmp085ReadInt(unsigned char address)
{
  unsigned char msb, lsb;
 
  Wire.beginTransmission(BMP085_ADDRESS);
  Wire.write(address);
  Wire.endTransmission();
 
  Wire.requestFrom(BMP085_ADDRESS, 2);
  while(Wire.available()<2)
    ;
  msb = Wire.read();
  lsb = Wire.read();
 
  return (int) msb<<8 | lsb;
}


I am trying to read from a BMP085 sensor and something wonky is going on. I think it is in the wire library.

When I call the above function with this command

mc = bmp085ReadInt(0xBC);

The ESP2866 appears to store a value of 54461. The variable mc is used in a compensation (from calibration info) calculation.

When I run the same code on an Uno with the same BMP085, mc stores a value of -11075.

In binary these numbers are only a digit apart:
11010100 10111100
11010100 10111101

I am still trying to work out where it is happening, but it appears that the ESP module is failing to pick up on the fact that this is a signed value being read from the calibration data on the chip...
Last edited by Justblair on Tue Apr 07, 2015 8:04 pm, edited 1 time in total.
User avatar
By picstart
#13817 An I2c interface transfers bytes...Now the bytes can notate numerical values in which case they could be part of a multibyte binary notation representation of a numerical value.
The interface doesn't care the bytes could be representations of ascii characters or the color code for a pixel or anything for that matter.
In you case the BMP085 transfers a two byte representation of a signed 16 bit numerical value, The two bytes need to be placed into a 16 bit signed variable.
User avatar
By Justblair
#13818 AFAIK that is what is happening. the mc variable is an int, the function casts the two bytes to int with the line

return (int) msb<<8 | lsb;

It works fine on an Uno, but as far as I can see not on the ESP8266.

I suppose the next thing for me to check is the values for msb and lsb when reading mc?

Looking at the binary values it looks to me that something has gone wrong at the cast stage. When storing the value the compiler should convert using twos complement when it detects the negative number. That would explain the addition of a 1 on the Uno. (in binary that is).

I checked that the ESP8266 was capable of storing a negative number by doing this
Code: Select allint test = -11075;

setup(){
Serial.begn(9600);
Serial.println(test, DEC);
}


I got the correct answer.
User avatar
By Justblair
#13820 Sorry PicStart, The penny just dropped there (Tired brain!)

I made a schoolboy type error in assuming that the signed int was a 16bit value... Its not. I changed the variable to a short int and cast it as such in the function and yep it reports the correct value now!

Doh!

ps thanks for the help, even if I was a tad slow in understanding you...