Question using Wire library for SMbus on smart battery (similar to I2C)

Hello, I once asked a question about using the Wire library from Arduino to communicate via SMBus a while back and with the help of fellow members I was able to get it working... to an extent.

Reading from the smart Li-ion battery is a success and now I am trying to write to the smart battery but the register I am trying to write to is confusing me. The datasheet states SMBus protocol: Write Word which if I am correct is 16 bits (2 Bytes), and then the datasheet proceeds to breakdown the 16 bits. This is very helpful except I don't know how to Wire.write to this breakdown of the 16 bits. Currently reading the register after writing to it yields the same bits as before.

Could someone help me? The datasheet is below if you want to skim through it, as well as the code I use to read and write.
sbdat110.pdf (138.8 KB)

#include <Streaming.h>
#include <Wire.h>

#define Addr_BATTERY_ADDRESS             0x0B // Replace with the correct battery address
#define Addr_ManufacturerAccess          0x00
#define Addr_RemainingCapacityAlarm      0x01
#define Addr_RemainingTimeAlarm          0x02
#define Addr_BatteryMode                 0x03
#define Addr_AtRate                      0x04
#define Addr_AtRateTimeToFull            0x05
#define Addr_AtRateTimeToEmpty           0x06
#define Addr_AtRateOK                    0x07
#define Addr_Temperature                 0x08
#define Addr_Voltage                     0x09
#define Addr_Current                     0x0a
#define Addr_AverageCurrent              0x0b
#define Addr_MaxError                    0x0c
#define Addr_RelativeStateOfCharge       0x0d
#define Addr_AbsoluteStateOfCharge       0x0e
#define Addr_RemainingCapacity           0x0f
#define Addr_FullChargeCapacity          0x10
#define Addr_RunTimeToEmpty              0x11
#define Addr_AverageTimeToEmpty          0x12
#define Addr_AverageTimeToFull           0x13
#define Addr_ChargingCurrent             0x14
#define Addr_ChargingVoltage             0x15
#define Addr_BatteryStatus               0x16
#define Addr_CycleCount                  0x17
#define Addr_DesignCapacity              0x18
#define Addr_DesignVoltage               0x19
#define Addr_SpecificationInfo           0x1a
#define Addr_ManufactureDate             0x1b
#define Addr_SerialNumber                0x1c
#define Addr_ManufacturerName            0x20
#define Addr_DeviceName                  0x21
#define Addr_DeviceChemistry             0x22
#define Addr_ManufacturerData            0x23

#define SMBUS_DATA_1                  18
#define SMBUS_CLCK_1                  19

float batt_volt;
float batt_mode;
bool rwBit_0;
bool rwBit_1;
bool rwBit_2;
bool rwBit_3;
bool rwBit_4;
bool rwBit_5;
bool rwBit_6;
bool rwBit_7;
bool rwBit_8;
bool rwBit_9;
bool rwBit_10;
bool rwBit_11;
bool rwBit_12;
bool rwBit_13;
bool rwBit_14;
bool rwBit_15;

void readBatteryVoltage() {
  // Request voltage data from the battery
  Wire.beginTransmission(Addr_BATTERY_ADDRESS);
  Wire.write(Addr_Voltage); // Voltage register address
  Wire.endTransmission(false);
  Wire.requestFrom(Addr_BATTERY_ADDRESS, 2);

  if (Wire.available()) {
    // Read voltage data
    uint16_t voltage = Wire.read();
    voltage |= Wire.read() << 8;

    // Convert the raw data to voltage value
    batt_volt = voltage / 1000.0;
    Serial << "Battery Volt: " << batt_volt << " volts" << endl;
  }
  else{
    batt_volt = batt_volt;
    Serial << "Battery Volt: " << batt_volt << " volts" << endl;
  }
}

void writeBatteryMode() {
  Wire.beginTransmission(Addr_BATTERY_ADDRESS);
  Wire.write(Addr_BatteryMode); // batt Mode register address
  Wire.write(1);
  Wire.endTransmission();

//  if (Wire.available()){
//    Wire.write(1, 2);
//    //Wire.write(0x01);
//  }
}

void readBatteryMode() {
  // Request relative state of charge data from the battery
  int16_t mode;
  Wire.beginTransmission(Addr_BATTERY_ADDRESS);
  Wire.write(Addr_BatteryMode); // batt Mode register address
  Wire.endTransmission(false);
  Wire.requestFrom(Addr_BATTERY_ADDRESS, 2);

  if (Wire.available()>= 2) {
    // Read Battery Mode data
    mode = Wire.read();
    mode |= Wire.read() << 8;

    //rwBit_0 = mode & (0x01);    // bit 0
    rwBit_0 = bitRead(mode, 0);
    rwBit_1 = bitRead(mode, 1);
    rwBit_2 = bitRead(mode, 2);
    rwBit_3 = bitRead(mode, 3);
    rwBit_4 = bitRead(mode, 4);
    rwBit_5 = bitRead(mode, 5);
    rwBit_6 = bitRead(mode, 6);
    rwBit_7 = bitRead(mode, 7);
    rwBit_8 = bitRead(mode, 8);
    rwBit_9 = bitRead(mode, 9);
    rwBit_10 = bitRead(mode, 10);
    rwBit_11 = bitRead(mode, 11);
    rwBit_12 = bitRead(mode, 12);
    rwBit_13 = bitRead(mode, 13);
    rwBit_14 = bitRead(mode, 14);
    rwBit_15 = bitRead(mode, 15);

    Serial << rwBit_15 <<
           rwBit_14 <<
           rwBit_13 <<
           rwBit_12 <<
           rwBit_11 <<
           rwBit_10 <<
           rwBit_9 <<
           rwBit_8 <<
           rwBit_7 <<
           rwBit_6 <<
           rwBit_5 <<
           rwBit_4 <<
           rwBit_3 <<
           rwBit_2 <<
           rwBit_1 <<
           rwBit_0 << endl;

  }
  else {
    Serial << rwBit_15 <<
           rwBit_14 <<
           rwBit_13 <<
           rwBit_12 <<
           rwBit_11 <<
           rwBit_10 <<
           rwBit_9 <<
           rwBit_8 <<
           rwBit_7 <<
           rwBit_6 <<
           rwBit_5 <<
           rwBit_4 <<
           rwBit_3 <<
           rwBit_2 <<
           rwBit_1 <<
           rwBit_0 << endl;
  }
}

void setup() {
  Serial.begin(115200);
  Wire.begin();
  while (!Serial);
  writeBatteryMode();
}

void loop() {
  Serial << "-----------------------------" << endl;
  readBatteryMode();
  readBatteryVoltage();
  Serial << "-----------------------------" << endl;

  delay(500);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.