t4k4ll:
What I need write first that I can read the voltage of the battery.writeReg8(0,0); // write 0x00 to register A
Serial.println( readReg8(0), HEX); // read register AThe program will always print only 0x80
Thank you very much for your tips and your time.
I'm using an LTC2943 and couldn't get it to work with the standard Wire library. Tried I2C master library, worked out of the box (i'm guessing because of support for the repeated Start Condition). For the LTC2941 you will probably need to tweak the register adresses. Here's my code printing to an lcd, maybe it will help someone out.
PS. Just looked up the ltc2941 datasheet, it seems it doesn't include an ADC, so no voltage, current, temp readings using that IC, ltc2942 and ltc2943 however do.
#include <LiquidCrystal.h>
#include <I2C.h>
LiquidCrystal lcd(12, 11, 5, 4, 8, 7);
// Define register addresses LTC2493...
#define LTC2943Address 0x64
#define regAddressStatus 0x00
#define regAddressControl 0x01
#define regAddressAccuCharge 0x02
#define regAddressVoltage 0x08
#define regAddressCurrent 0x0E
#define regAddressTemp 0x14
#define senseResistor 0.050
void setup() {
// Start Serial, I2c and LCD
Serial.begin(9600);
I2c.begin();
lcd.begin(20, 4);
// Set control register 01h, ADC to scan Mode (default ADC mode is sleep, no current/voltage/temeperature measurements)
I2c.write(LTC2943Address, regAddressControl, B10111100);
}
void loop() {
// Read single byte from regAddressControl (0x01)
byte LTC2943StatusRaw = readReg8(regAddressControl);
// Read two bytes form regAddressAccuCharge and convert to Accumulated Charge
// When starting up LTC2943 sets the ACR to midscale, will need to calibrate...
int LTC2943AccuChargeRaw = readReg16(regAddressAccuCharge);
float LTC2943AccuCharge = 0.340 * LTC2943AccuChargeRaw;
// Read two bytes from regAddressVoltage and convert to Voltage
int LTC2943VoltageRaw = readReg16(regAddressVoltage);
float LTC2943Voltage = 23.600 * LTC2943VoltageRaw / 65535.000;
// Read two bytes from regAddressTemp and convert to Celsius
unsigned int LTC2943TempRaw = readReg16(regAddressTemp);
float LTC2943Temp = (510.00 * LTC2943TempRaw / 65535.00) - 273.15;
// Read two bytes from regAddressCurrrent and convert to A
word LTC2943CurrentRaw = readReg16(regAddressCurrent);
float LTC2943Current = 0.060 / senseResistor * ( ( LTC2943CurrentRaw - 32767.000 ) / 32767.000 );
// lcd.print received values
lcd.setCursor(0,0);
lcd.print("Qbat: ");
lcd.print(LTC2943AccuCharge, 1);
lcd.print("mAh ");
lcd.setCursor(0,1);
lcd.print("Vbat: ");
lcd.print(LTC2943Voltage, 3);
lcd.print("V ");
lcd.setCursor(0,2);
lcd.print("Temp: ");
lcd.print(LTC2943Temp, 1);
lcd.print("C ");
lcd.setCursor(0,3);
lcd.print("Ibat: ");
lcd.print(LTC2943Current, 3);
lcd.print("A ");
// Wait some...
delay(500);
}
byte readReg8(int regAddress)
{
byte data = 0;
// Point to status register regAddress and receive one byte of data
I2c.read(LTC2943Address,regAddress,1);
data = I2c.receive();
return(data);
}
word readReg16(int regAddress)
{
byte dataH, dataL;
word data = 0;
// Point to status register regAddress and receive two bytes of data
I2c.read(LTC2943Address,regAddress,2);
dataH = I2c.receive();
dataL = I2c.receive();
// Combine MSB and LSB to word
data = word(dataH, dataL);
return(data);
}