I2C Communication with the Sparfun Babysitter

Hello ! :smiley:

I am trying to use the Battery Babysitter:

https://www.sparkfun.com/products/13777

with a PIC 16F18875.

I have the arduino library to help me but I need to adpat it to my PIC.

https://github.com/sparkfun/Battery_Bab … Q27441.cpp

So the arduino using c++ and pic is C so I removed the class part of the code. I could reuse all the functions except the last 2 ones that are being use from all the others :

// Read a specified number of bytes over I2C at a given subAddress
int16_t BQ27441::i2cReadBytes(uint8_t subAddress, uint8_t * dest, uint8_t count)
{
    int16_t timeout = BQ72441_I2C_TIMEOUT;  
    Wire.beginTransmission(_deviceAddress);
    Wire.write(subAddress);
    Wire.endTransmission(true);

    Wire.requestFrom(_deviceAddress, count);
    while ((Wire.available() < count) && timeout--)
        delay(1);
    if (timeout)
    {
        for (int i=0; i<count; i++)
        {
            dest[i] = Wire.read();
        }
    }

    return timeout;
}

// Write a specified number of bytes over I2C to a given subAddress
uint16_t BQ27441::i2cWriteBytes(uint8_t subAddress, uint8_t * src, uint8_t count)
{
    Wire.beginTransmission(_deviceAddress);
    Wire.write(subAddress);
    for (int i=0; i<count; i++)
    {
        Wire.write(src[i]);
    }   
    Wire.endTransmission(true);

    return true;    
}

My problem is with wire library. I have my I2c functions for a PIC Write(data), read() ,stop(), start() ,init(), acknowledge,nacknowledge.

Do you think I can replace the beginTransmission with a init a start and a write(adresss) ?

Same for endtransmission(true) with a stop ?

And for requestFrom and available I have no idea how to do it. Here are the code for them in Arduino

Requestfrom :

Used by the master to request bytes from a slave device. The bytes may then be retrieved with the available() and read() functions.

Available:

Returns the number of bytes available for retrieval with read(). This should be called on a master device after a call to requestFrom() or on a slave inside the onReceive() handler.

My question is how to delete the wire. functions and replace them with my PIC I2C functions

Thank you for the help

uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint32_t iaddress, uint8_t isize, uint8_t sendStop)
{
  if (isize > 0) {
  // send internal address; this mode allows sending a repeated start to access
  // some devices' internal registers. This function is executed by the hardware
  // TWI module on other processors (for example Due's TWI_IADR and TWI_MMR registers)

  beginTransmission(address);

  // the maximum size of internal address is 3 bytes
  if (isize > 3){
    isize = 3;
  }

  // write internal register address - most significant byte first
  while (isize-- > 0)
    write((uint8_t)(iaddress >> (isize*8)));
  endTransmission(false);
  }

  // clamp to buffer length
  if(quantity > BUFFER_LENGTH){
    quantity = BUFFER_LENGTH;
  }
  // perform blocking read into buffer
  uint8_t read = twi_readFrom(address, rxBuffer, quantity, sendStop);
  // set rx buffer iterator vars
  rxBufferIndex = 0;
  rxBufferLength = read;

  return read;
}

int TwoWire::available(void)
{
  return rxBufferLength - rxBufferIndex;
}