ZHCADH7 December 2023 BQ76905 , BQ76907
以下示例代码是用 Python 编写的,旨在通过 EV2400 模块或通过 BQ76905 或 BQ76907 评估模块上的 USB 连接器从 PC 与 BQ7690x 器件进行通信。该代码显示了简单 的I2C 读取和写入函数的创建、 DataRAM_Read 函数(也可用于执行子命令,因其遵循相同的格式)的创建,以及显示校验和及长度计算的 DataRAM_Write 函数的创建。代码的主要部分介绍了本文档前面三个章节中涉及的所有示例。
这个简单的代码示例旨在说明 I2C 命令的基本命令结构。微控制器代码示例也适用于 I2C。
'''
/* BQ7690x example Program demonstrates examples for direct commands, subcommands, and writing / reading from device RAM.
'''
I2C_ADDR = 0x10 # BQ7690x default responder address
numCells = 5 # Set to 7 for BQ76907
def DataRAM_Read(addr, length):
'''
Write address location to 0x3E and read back from 0x40
Used to read dataflssh and for subcommands
'''
addressBlock = [(addr%256), (addr/256)]
I2C_Write(I2C_ADDR, 0x3E, addressBlock)
value = I2C_Read(I2C_ADDR, 0x40,length)
return value
def DataRAM_Write(addr, block):
'''
Write address location to 0x3E and Checksum,length to 0x60
Used to write dataflssh
Add 2 to length for Rev A0 of Octane
'''
addressBlock = [(addr%256), (addr/256)]
wholeBlock = addressBlock + block
I2C_Write(I2C_ADDR, 0x3E, wholeBlock) # Write Data Block
# Write Data Checksum and length to 0x60, required for RAM writes
I2C_Write(I2C_ADDR, 0x60, [~sum(wholeBlock) & 0xff, len(wholeBlock)+2])
return
def ReadCellVoltage(cell):
'''
Reads a specific cell voltage
'''
cmd_addr = 0x14 + (cell * 2)
result = I2C_Read(I2C_ADDR, cmd_addr, 2)
print ("Cell", cell, " = ", (result[1]*256 + result[0]), " mV")
return
def ReadAllCellVoltages():
'''
Reads all cell voltages, Stack voltage, PACK voltage, and LD voltage
'''
cmd_addr = 0x12
for i in range(0,numCells):
cmd_addr += 2
result = I2C_Read(I2C_ADDR, cmd_addr,2)
print ("Cell", (i+1), " = ", (result[1]*256 + result[0]), " mV")
result = I2C_Read(I2C_ADDR, 0x26,2)
print ("Stack Voltage = ", (result[1]*256 + result[0]), " mV")
result = I2C_Read(I2C_ADDR, 0x22,2)
print "REG18 Voltage = ", (result[1]*256 + result[0]), " ADC Counts"
result = I2C_Read(I2C_ADDR, 0x24,2)
print ("VSS Voltage = ", (result[1]*256 + result[0]), " ADC Counts")
return
def crc8(b,key):
crc = 0
ptr = 0
for j in range(len(b),0,-1):
for k in range(8):
i = 128 / (2**k)
if ((crc & 0x80) != 0):
crc = crc * 2
crc = crc ^ key
else:
crc = crc * 2
if ((b[ptr] & i) != 0):
crc = crc ^ key
ptr = ptr + 1
return crc
##########################################
# Start of Main Script
##########################################
################ Direct Command Examples ################
#Write Alarm Enable to 0x0060 - FULLSCAN, ADSCAN
I2C_Write(I2C_ADDR, 0x66, [0x60, 0x00])
#Read Voltage on Cell #1
result = I2C_Read(I2C_ADDR, 0x14, 2)
print ("Cell 1 = ", (result[1]*256 + result[0]), " mV")
#Read Internal Temperature
result = I2C_Read(I2C_ADDR, 0x28, 2)
print ("Internal Temp = ", ((result[1]*256 + result[0])), "degrees C")
#Read CC2 Current Measurement
result = I2C_Read(I2C_ADDR, 0x3A, 2)
print ("CC2", (result[1]*256 + result[0]), " mA")
################ Subcommand Examples ################
## Command-only Subcomands ##
#Read Device Number
b = DataRAM_Read(0x0001,6)
print ("Device_Number = " '{0:04X}'.format(b[1]*256+b[0]))
#FET_ENABLE
I2C_Write(I2C_ADDR, 0x3E, [0x22, 0x00])
#Cell Balance Write Command - starts balancing on specified cells when written
DataRAM_Write(0x0083, [0x02])
#Cell Balance Read Command - read which cells are being balanced
b = DataRAM_Read(0x0083,1)
print ("Cell Balancing = 0x" '{0:02X}'.format(b[0]))
#RESET - returns device to default settings
I2C_Write(I2C_ADDR, 0x3E, [0x12, 0x00])
sleep(2)
# Read 'Enabled Protections A' RAM register 0x9024
b = DataRAM_Read(0x9024,1)
print ("Enabled Protections A = 0x" '{0:02X}'.format(b[0]))
#Set CONFIG_UPDATE Mode (RAM registers can be written while in
#CONFIG_UPDATE mode and will take effect after exiting CONFIG_UPDATE mode
I2C_Write(I2C_ADDR, 0x3E, [0x90, 0x00])
#Write to 'Enabled Protections A' RAM register to enable CUV protection
DataRAM_Write(0x9024, [0xE1])
#Write to 'VCell Mode' RAM register to configure for a 9-cell battery
DataRAM_Write(0x901B, [0x04])
#Exit CONFIG_UPDATE Mode
I2C_Write(I2C_ADDR, 0x3E, [0x92, 0x00])
# CRC8 Example Calculation
TestValue = [0x10, 0x14, 0x11, 0x68]
crcKey = 0x107
check = 0xff & crc8(TestValue,crcKey)
print "crc8 check = 0x" '{0:02X}'.format(check)
ReadAllCellVoltages()
在 BQ76905 评估模块中运行示例 Python 脚本的输出如下。
('Cell 1 = ', 3089, ' mV')
('Internal Temp = ', 29, 'degrees C')
('CC2', 45, ' mA')
Device_Number = 7605
Cell Balancing = 0x02
Enabled Protections A = 0xA1
crc8 check = 0x33
('Cell', 1, ' = ', 3089, ' mV')
('Cell', 2, ' = ', 3082, ' mV')
('Cell', 3, ' = ', 3093, ' mV')
('Cell', 4, ' = ', 3089, ' mV')
('Cell', 5, ' = ', 3089, ' mV')
('Stack Voltage = ', 15471, ' mV')
REG18 Voltage = 19153 ADC Counts
('VSS Voltage = ', 3, ' ADC Counts')