Amazon

Thursday 30 April 2015

Interfacing with the BQ24725 battery charger

The BQ24725 is a 1-4 cell battery charge manager with SMBus protocol from Texas Instruments. Here's an example C firmware to interface with the charger using a PIC16 from Microchip. This example is made to charge a 2-cell Lithium-Ion battery 8.4V, 10000 mAh. Note that it is using I2C protocol which is equivalent to SMBUS.


#define CHARGER_OPTIONS_INIT_H 0x09
#define CHARGER_OPTIONS_INIT_L 0x24 //Charger init options
                                                                           //BAT Depletion 62.65%                                                                                                                                                         //(~2.631V/cell),charge current
#define CHARGER_CURRENT_L 0x00
#define CHARGER_CURRENT_H 0x08 //Set Charger current to 2048mA
#define CHARGER_VOLTAGE_L 0xD0
#define CHARGER_VOLTAGE_H 0x20 //Set Charger voltage to 8400mV
#define INPUT_CURRENT_L 0x00 
#define INPUT_CURRENT_H 0x0D //Set Input current to 3328mA
#define Charger_addr 0x12                                 //I2C address of BQ24725

void CHARGER_init()                          //BQ24725 initialization
{
SSP2CON1bits.SSPEN = 1;

    //Set Option to Iout = charge current
I2C_SendCommand_2Bytes(Charger_addr,0x12,CHARGER_OPTIONS_INIT_L,CHARGER_OPTIONS_INIT_H);  

    //Set Charger current                                                                                              
I2C_SendCommand_2Bytes(Charger_addr,0x14,CHARGER_CURRENT_L,CHARGER_CURRENT_H);

   //Set Charger voltage 
I2C_SendCommand_2Bytes(Charger_addr,0x15,CHARGER_VOLTAGE_L,CHARGER_VOLTAGE_H);

    //Set input current
I2C_SendCommand_2Bytes(Charger_addr,0x3F,INPUT_CURRENT_L,INPUT_CURRENT_H);

SSP2CON1bits.SSPEN = 0;
}


void CHARGER_ChargingEnable(char enabled)         //Enable / Disable the charging
{
enabled = 1 - enabled;

SSP2CON1bits.SSPEN = 1;

    //Send the Instruction to the charger
I2C_SendCommand_2Bytes(Charger_addr,0x12,CHARGER_OPTIONS_INIT_L+enabled, CHARGER_OPTIONS_INIT_H);

SSP2CON1bits.SSPEN = 0;
}


If you need a routine for the I2C communication:


void I2C_SendCommand_2Bytes(unsigned char Address, unsigned char Command, unsigned char LowByte, unsigned char HighByte)
{
SSP2CON1bits.SSPEN = 1;
SSP2CON2bits.SEN = 1; //Send start
WaitBufferEmpty();

SSP2BUF = Address;
WaitBufferEmpty();

SSP2BUF = Command; //Send command
WaitBufferEmpty();

SSP2BUF = LowByte; //Send low byte
WaitBufferEmpty();

SSP2BUF = HighByte; //Send high byte
WaitBufferEmpty();

SSP2CON2bits.PEN = 1; //Send stop
WaitBufferEmpty();
return;
}

unsigned char I2C_ReadCommand(unsigned char Address, unsigned char Command)
{
unsigned char Byte_read;

SSP2CON2bits.SEN = 1; //Send start
WaitBufferEmpty();

SSP2BUF = Address;
while (SSP2CON2bits.ACKSTAT); //wait for ACK received
WaitBufferEmpty();

SSP2BUF = Command; //Send command
WaitBufferEmpty();

SSP2CON2bits.RSEN = 1; //Send repeat start
WaitBufferEmpty();

SSP2BUF = Address+1;
while (SSP2CON2bits.ACKSTAT); //wait for ACK received
WaitBufferEmpty();

SSP2CON2bits.RCEN = 1; //Set in receive
WaitBufferEmpty();

Byte_read = SSP2BUF; //Read buffer

SSP2CON2bits.ACKDT = 0;
SSP2CON2bits.ACKEN = 1; //Start Acknowledge sequence
WaitBufferEmpty();

return Byte_read;;
}

unsigned char I2C_ReadByte()
{
char Byte_read;

SSP2CON2bits.RCEN = 1; //Set in receive
WaitBufferEmpty();

Byte_read = SSP2BUF; //Read buffer

SSP2CON2bits.ACKDT = 1;
SSP2CON2bits.ACKEN = 1; //Start Not Acknowledge sequence
WaitBufferEmpty();
SSP2CON2bits.PEN = 1; //Send stop
WaitBufferEmpty();

  return Byte_read;;
}

void WaitBufferEmpty()
{
if (SSP2CON1bits.WCOL)
SSP2CON1bits.WCOL = 0;

SSP2IF = 0;
}





No comments: