hey
so i'm working on a library / helper class to control / work with a
max17043 data sheet chip this chip is able to measure and calculate a state of your lipo battery
as this is my first time using i2c and in general first time building a library for a bus control device i'm having some problems currently i'm struggling with setting the alert trigger level
work log
QuickStart function ( pretty sure this works no way to confirm )
reset function ( pretty sure this works no way to confirm)
getSoC function (output seems to be right scaled based on vCell's output but not a 100% if the output is right)
getVcell function (seems to be within a responsible margin of error )
ReadConfig function ( output fits what shut be the default setting so function shut be right )
getVersion function ( works gets the right version number)
getAlertThreshold function (works gets the right default value needs to be reconfirmed once the set alert trigger function works)
SleepMode on / off function ( work has not started but help is properly gonna be need )
so my question is can anyone help me write the function that sets the alert trigger level and how can i use the sleep mode part of the chip / access the register which sets sleep mode on and off ?
this is all my code so far
public class Main {
static byte address = 0x36;
static byte VCELL_REGISTER =(byte)0x02;
static byte SOC_REGISTER =(byte)0x04;
static byte MODE_REGISTER =(byte)0x06;
static byte VERSION_REGISTER =(byte)0x08;
static byte CONFIG_REGISTER =(byte)0x0C;
static byte COMMAND_REGISTER= (byte) 0xFE;
public static void main(String[] args) throws IOException, I2CFactory.UnsupportedBusNumberException, InterruptedException {
// write your code here
I2CBus i2c = I2CFactory.getInstance(I2CBus.BUS_1);
I2CDevice device = i2c.getDevice(address);
reset(device);
QuickStart(device);
getSoC(device);
getVCell(device);
ReadConfig(device);
GetVersion(device);
getAlertThreshold(device);
setAlertThreshold(device,20);
getAlertThreshold(device);
}
private static int GetVersion(I2CDevice device) throws IOException {
System.out.println("getting device version // shut be version 3//");
int response = device.read(VERSION_REGISTER);
System.out.println(("repsonse : "+String.format("0x%02x", response) + " raw response : " + response));
// byte r1 = (byte) (response<<8);
//
// System.out.println("modified response : " + r1);
return response;
}
private static void reset(I2CDevice device) throws IOException {
System.out.println("device reset ");
device.write(COMMAND_REGISTER);
device.write((byte)0x00);
device.write((byte)0x54);
}
private static void setAlertThreshold(I2CDevice device,int alertLevel) throws IOException {
System.out.println("Setting the alert threadhold to : " + alertLevel + "% // default level is 4%//");
System.out.println("dos not work atm ");
int response =device.read();
if(alertLevel>32){
alertLevel=32;
}
device.write(CONFIG_REGISTER);
device.write((byte)0x00);
device.write((byte) 0x00);
device.write((byte)0x00);
device.write((byte) 00010);
}
private static void getAlertThreshold(I2CDevice device) throws IOException {
System.out.println("reading the alert Threshold default // default value is 4 % // ");
int response =device.read();
System.out.println(("repsonse : "+String.format("0x%02x", response) + " raw response : " + response));
int r1 = 32 - response & 0x1F;
System.out.println("modified response : " + r1);
}
private static void getSoC(I2CDevice device) throws IOException {
System.out.println("getting soc");
int response = device.read(SOC_REGISTER);
System.out.println("repsonse : "+String.format("0x%02x", response) + " raw response : " + response);
}
private static void getVCell(I2CDevice device) throws IOException {
int response = device.read(VCELL_REGISTER);
byte i = (byte) response;
double i2 = i&0xff;
System.out.println(i);
System.out.println(i2);
double i3 = (5.0/255.0);
System.out.println(i3);
double i4 = i3*i2;
System.out.println(i4);
System.out.println("repsonse : "+String.format("0x%02x", response) + " raw response : " + ((byte) response& 0xff) + " vCell = " + i4 );
}
private static void ReadConfig(I2CDevice device) throws IOException {
int response =0;
System.out.println("reading the config register default value 0x97 ");
// no idear how to change / the gain off chaing this value
response = device.read(CONFIG_REGISTER);
System.out.println(("repsonse : "+String.format("0x%02x", response) + " raw response : " + response));
}
public static void QuickStart(I2CDevice device) throws IOException {
int response = 0;
System.out.println("write the mode Register traying to do a quickStart");
device.write(MODE_REGISTER);
device.write((byte)0x40);
device.write((byte) 0x00);
response = device.read();
System.out.println(("repsonse : "+String.format("0x%02x", response) + " raw response : " + response));
}
}