Go Down

Topic: Converting from Wire.h to I2C.h (Read 53 times) previous topic - next topic

Fulliautomatix

Hi,

I'm having trouble converting a very simple bit of code from using the Wire.h library to using I2C.h instead.

Here's the background, I'm working with the BMP180  pressure sensor https://www.sparkfun.com/products/11824. I've already successfully written a library to work with the module, the library uses I2C.h instead of Wire.h. So far so good.

The I2C address on this device is hardcoded and unchangable, so I've started using an I2C multiplexer to attempt to run three of the modules at the same time. Here's the multiplexer http://www.dsscircuits.com/index.php/i2c-multiplexer.

In the example sketch (provided on the above link) it uses this function to select one of the four channels before accessing the selected pressure module....
Code: [Select]

#define MUX         0x70  //Multiplexer Address

/********************************************************
* When selecting a channel bit2 of the control register
* must be set to a logic 1 to enable channel selection.
* If bit2 is a logic zero then all channels will be disabled.
* If 0xFF is the selected channel it will disable all channels.
********************************************************/
void mux(byte channel)
{
  byte controlRegister = 0x04; 
  controlRegister |= channel;
  Wire.beginTransmission(MUX);
  if (channel == 0xFF){Wire.send(0x00);} //deselect all channels
  else {Wire.send(controlRegister);}     //set to selected channel
  Wire.endTransmission();
}


My version looks (using I2C instead of wire) looks like this....
Code: [Select]
void BarographTrio::mux(byte channel)
{
  byte controlRegister = 0x04; 
  controlRegister |= channel;

    if (channel == 0xFF)
    {
      // disable all channels
      _i2c.write(MUX,(unsigned int)0x00);
    }
    else
    {
      // enable the selected channel
      _i2c.write(MUX,(unsigned int)controlRegister);
    }
}

It compiles ok, executes ok, but doesn't actually seem to select any of the pressure sensors.

help please!

Fulliautomatix

bu99er!

Just released the multiplexer and pressure modules were plugged into a different arduino than was plugged into my laptop. That'll teach me to try to develop stuff on a very small desk with three arduinos, two laptops and a 3 week old kitten on it.

feel free to pile on the normal abuse and sarcasm!

cheers

robtillaart

one remark
Code: [Select]

int mux(byte channel)
{
  byte controlRegister = 0x04; 
  controlRegister |= channel;
  Wire.beginTransmission(MUX);
  if (channel == 0xFF){Wire.send(0x00);} //deselect all channels
  else {Wire.send(controlRegister);}     //set to selected channel
  return Wire.endTransmission();
}


Wire.endTransmission() returns an errorcode that you might want to capture...
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up