Notes on the BQ2013H Gas Gauge

(Back to the bike lamp project)

The BQ2013H gas gauge was used to provide battery capacity feedback to the user. This IC measures the charge/discharge current and calculates the battery pack's capacity by accumulation. This IC was chosen for it's wide chemistry operation and high battery capacity support. As a lot of the Texas Instruments' gas gauges, the BQ2013H uses the signal wire HDQ communication protocol. A custom C++ HDQ library had to be written to support this protocol.

  1. Library Usage
    1. HDQ Library
    2. BQ2013H Library
  2. The HDQ Communication Protocol
  3. Datasheet & Application Notes

Library Usage

HDQ Library

The microcontroller's UART being already used, HDQ was timing implemented on a discreet digital IO port.
Here is the usage documentation for the HDQ class:

/**
 * Constructor
 * @param pinArg: pin number to attach to
**/
HDQ(uint8_t pinArg = HDQ_DEFAULT_PIN);

/**
 * sendBreak: writes a break to the HDQ line
**/
void doBreak(void);

/**
 * write: send a payload to the device
 * @param reg: the address of the register to write to
 * @param payload: data to be sent
 * @return: false, unless if verif is set, then 
 *          it will read back the register and 
 *          return true if it matches the payload
**/
boolean write(uint8_t reg, uint8_t payload, boolean verif);
boolean write(uint8_t reg, uint8_t payload);

/**
 * read: read from the device
 * @param register: address of the register to read
 * @return a uint8_t integer
**/
uint8_t read(uint8_t reg);

BQ2013H Library

All of the IC's registers are implemented through the BQ2013H class.
The following shows a few of them through the usage example (using the Arduino API for serial UART output)

GASGAUGE.writeBatteryID(B11001001);

Serial.print("readBatteryID: ");
Serial.println(GASGAUGE.readBatteryID(), BIN);

Serial.print("Battery voltage: ");
float voltage   = GASGAUGE.readBatteryVoltage();
Serial.println(voltage, DEC);
uint8_t units   = (uint8_t) voltage/1;
uint8_t decimals= (uint8_t) (voltage - units)<<8;

Serial.print("Battery voltage: ");
Serial.print(units, DEC);
Serial.print(".");
Serial.println(decimals, DEC);

BQ2013H Function Prototypes

(Not fully documented yet)

boolean readChargeStatus(void);
boolean readBatteryReplaced(void);
boolean readValidDischarge(void);
// Returns VDQ & EDV1 with a mask of 0x03 where EDV1 is MSB and VDQ is LSB
uint8_t readEndOfDischarge(void);
uint8_t readTemperature(void);
uint8_t readAvailableCharge(void);
uint8_t readNominalCharge(void);
void writeNominalCharge(uint16_t nominalCharge);
uint8_t readBatteryID(void);
void writeBatteryID(uint8_t battID);
uint8_t readLastDischarge(void);
void writeLastDischarge(uint8_t lastDischarge);
boolean readChargeRate(void);
boolean readOverload(void);
/**
 * readProgramBit: read a program pin values (High, Low, Float);
 * @param pinNumber: the number of program pin (1 to 6);
 * @return: 0xFF on errors or depending of the state of the pin:
 *              - floating: 0x00
 *              - low:      0x01
 *              - high:     0x02
**/
uint8_t readProgramBit(uint8_t pinNumber);
uint8_t readOutputControl(void);
void writeOutputControl(uint8_t octlRegister);
uint16_t readOffsetAdjusment(void);
void writeOffsetAdjusment(uint16_t offsetMicroVolts);
float readSelfDischargeRate(void);
void writeSelfDischargeRate(float selfDischarge);
/**
 * readDigitalMagnitudeFilter: reads the current DMF
 * @return uint8_t: Vsrd & Vsrq threshold in µVolt
**/
uint16_t readDigitalMagnitudeFilter(void);
/**
 * writeDigitalMagnitudeFilter: writes a new DMF
 * @param srdqValue: DMF value in µVolt
**/
void writeDigitalMagnitudeFilter(uint16_t srdqValue);
uint8_t readLoadCompensation(void);
void writeLoadCompensation(uint8_t loadComp);
uint8_t readChargeCompensation(void);
void writeChargeCompensation(uint8_t chargeComp);
float readBatteryVoltage(void);
// Uses PPFC
void doReset(void);

The HDQ Communication Protocol

The following is taken from Texas Instruments' HDQ Communication Basics Application Note.

Bit timing schematic
HDQ Bit Timing Schematic
Bit timing table
HDQ Bit Timing Table

Datasheet & Application Notes

(Back to top)