Hey everybody!
I’m needing some help understanding how to communicate with a battery management system via i2c.
I have a working library that is successfully communicating with the battery management chip over i2c and I’m pulling most of the info I need but there are more registers I need help with accessing.
I’ll try to be as clear as I can, so lets start off with the first task I need help with.
- My current sketch is using the following 3 functions to read registers over the Wire library on a Arduino Micro and its working perfectly.
uint8_t BQ20Z45::read(uint8_t address)
{
uint8_t registerValue;
Wire.beginTransmission(BQ20Z45_Address);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(BQ20Z45_Address,1,true);
registerValue = Wire.read();
Wire.endTransmission();
return registerValue;
}
uint16_t BQ20Z45::read16u(uint8_t address)
{
uint16_t registerValue;
Wire.beginTransmission(BQ20Z45_Address);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(BQ20Z45_Address,2,true);
registerValue = Wire.read();
registerValue |= (Wire.read()<<8);
Wire.endTransmission();
return registerValue;
}
int16_t BQ20Z45::read16(uint8_t address)
{
int16_t registerValue;
Wire.beginTransmission(BQ20Z45_Address);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(BQ20Z45_Address,2,true);
registerValue = Wire.read();
registerValue += (Wire.read()*256);
Wire.endTransmission();
return registerValue;
}
Those work fine for pulling data from registers that only hold and return 1 or 2 Bytes of signed & unsigned int data.
Now I want to read from this register that returns a 2 Byte Hex value held in a unsigned Int data format.
Now I’m thinking that I need to convert that HEX value to string of 16 individual bits. The 0 or 1 status of those bits will tell me whats up based on this graph:
The next register returns the same Hex data type but instead of 2 Bytes it returns 4 + 1 Bytes. See below:
I’m not sure what I should change in my current unsigned int wire request code that is working fine for other registers that are not returning data in HEX format.
uint16_t BQ20Z45::read16u(uint8_t address)
{
uint16_t registerValue;
Wire.beginTransmission(BQ20Z45_Address);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(BQ20Z45_Address,2,true);
registerValue = Wire.read();
registerValue |= (Wire.read()<<8);
Wire.endTransmission();
return registerValue;
}
I’m thinking I need to take the returned “registerValue” and convert the HEX to a string of 1’s and 0’s?
I’m sure its simple but beyond me at the moment. I’ve learned a lot about i2c communication over the last few days and I find it fascinating how it works and works so quickly.
Any help or advice is greatly appreciated