I am trying to write to a charging IC (BQ24193) and I am trying to figure out how to use I2C to write to a specific register so I can set up the charging IC properly. I have watched a few videos on the communication protocol, but I don't understand how to write to a register directly. Your help would be appreciated. Thanks.
Datasheet: http://www.ti.com/lit/ds/symlink/bq24193.pdf
-
1use Wire libraryJuraj– Juraj ♦2019年09月06日 05:18:41 +00:00Commented Sep 6, 2019 at 5:18
-
figure 18 in the datasheetjsotola– jsotola2019年09月06日 05:31:58 +00:00Commented Sep 6, 2019 at 5:31
1 Answer 1
The Arduino uses the Wire.h
library to communicate on an I2C bus.
Writing to a register in I2C is usually done by sending two bytes to the correct I2C address. The first byte is usually the register address, and the second is the value to place into the register:
#define CHIP_ADDRESS 0x6B
#define REGISTER_ADDRESS 0x12
Wire.beginTransmission(CHIP_ADDRESS);
Wire.write(REGISTER_ADDRESS);
Wire.write(0x18);
Wire.endTransmission();
Reading from a register is slightly more tricky, in that first you need to write to the chip to set the register address, then as a separate operation you need to read from that address:
Wire.beginTransmission(CHIP_ADDRESS);
Wire.write(REGISTER_ADDRESS);
Wire.endTransmission();
Wire.requestFrom(CHIP_ADDRESS, 1); // We want one byte
uint8_t val = Wire.read();
-
So with my IC, how do I find the address for each register? I checked the datasheet, and it only included the slave address.Jay– Jay2019年09月06日 11:12:34 +00:00Commented Sep 6, 2019 at 11:12
-
@Jay page 26: "8.5 Register Map"Sim Son– Sim Son2019年09月06日 17:50:30 +00:00Commented Sep 6, 2019 at 17:50