系数
This commit is contained in:
98
code/ina226.c
Normal file
98
code/ina226.c
Normal file
@@ -0,0 +1,98 @@
|
||||
// <20>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>: GB18030
|
||||
/*
|
||||
* ina226.c
|
||||
*
|
||||
* Created on: 2025<32><35>10<31><30>16<31><36>
|
||||
* Author: LHYe200
|
||||
*/
|
||||
|
||||
|
||||
#include "ina226.h"
|
||||
#include "zf_driver_soft_iic.h"
|
||||
|
||||
static uint8 is_iic_init = 0;
|
||||
INA226_t ina226[INA226_NUM];
|
||||
|
||||
static soft_iic_info_struct ina226_soft_iic_config;
|
||||
|
||||
|
||||
void INA226_Init()
|
||||
{
|
||||
uint8 addr_list[INA226_NUM] = INA226_ADDR;
|
||||
uint8 mode[2] = INA226_DEF_MODE;
|
||||
float max_current_A = 20.0f;
|
||||
float rshunt_mOhm = 10.0f;
|
||||
float amp_fix_k = 1.0f;
|
||||
float vot_fix_k = 1.0f;
|
||||
uint8 counter;
|
||||
|
||||
for(counter = 0; counter < INA226_NUM; counter++)
|
||||
{
|
||||
INA226_Single_Init(&ina226[counter], addr_list[counter], mode, max_current_A, rshunt_mOhm, amp_fix_k, vot_fix_k);
|
||||
}
|
||||
}
|
||||
|
||||
void INA226_Single_Init(INA226_t *ina226, uint8 addr, uint8 mode[2], float max_current_A, float rshunt_mOhm, float amp_fix_k, float vot_fix_k)
|
||||
{
|
||||
float cal;
|
||||
if(!is_iic_init)
|
||||
{
|
||||
soft_iic_init(&ina226_soft_iic_config, addr, INA226_SOFT_WAIT_TIME, INA226_SCL_PIN, INA226_SDA_PIN);
|
||||
is_iic_init = 1;
|
||||
}
|
||||
ina226->config.addr = addr;
|
||||
ina226->config.mode[0] = mode[0];
|
||||
ina226->config.mode[1] = mode[1];
|
||||
ina226->config.max_current_A = max_current_A;
|
||||
ina226->config.rshunt_mOhm = rshunt_mOhm;
|
||||
ina226->config.vot_fix_k = vot_fix_k;
|
||||
ina226->config.amp_fix_k = amp_fix_k;
|
||||
|
||||
ina226->calibration.c_lsb_mA = max_current_A / 65.535f; // Current LSB
|
||||
cal = (5120.0f) / (ina226->calibration.c_lsb_mA * ina226->config.rshunt_mOhm) * ina226->config.amp_fix_k;
|
||||
ina226->calibration.cal[0] = (uint8)((uint16)cal >> 8);
|
||||
ina226->calibration.cal[1] = (uint8)((uint16)cal & 0xFF);
|
||||
|
||||
memset(&ina226->result, 0, sizeof(ina226->result));
|
||||
|
||||
soft_iic_write_8bit_registers(&ina226_soft_iic_config, INA226_CONFIG_REG, ina226->config.mode, 2);
|
||||
soft_iic_write_8bit_registers(&ina226_soft_iic_config, INA226_CALIBRATION_REG, ina226->calibration.cal, 2);
|
||||
|
||||
}
|
||||
|
||||
void INA226_Read(INA226_t *ina226)
|
||||
{
|
||||
uint8 counter;
|
||||
if(!is_iic_init) return;
|
||||
|
||||
ina226_soft_iic_config.addr = ina226->config.addr;
|
||||
|
||||
// Read voltage, current, power, and shunt voltage registers
|
||||
|
||||
soft_iic_read_8bit_registers(&ina226_soft_iic_config, INA226_BUSVOT_REG, ina226->result.org_reg[1],2);
|
||||
soft_iic_read_8bit_registers(&ina226_soft_iic_config, INA226_CURRENT_REG, ina226->result.org_reg[3],2);
|
||||
soft_iic_read_8bit_registers(&ina226_soft_iic_config, INA226_POWER_REG, ina226->result.org_reg[2],2);
|
||||
soft_iic_read_8bit_registers(&ina226_soft_iic_config, INA226_SHUNTVOT_REG, ina226->result.org_reg[0],2);
|
||||
|
||||
|
||||
ina226->result.voltage_V = (int16)((ina226->result.org_reg[1][0] << 8) | ina226->result.org_reg[1][1]) * 0.00125f;
|
||||
ina226->result.current_A = (int16)((ina226->result.org_reg[3][0] << 8) | ina226->result.org_reg[3][1]) * ina226->calibration.c_lsb_mA * 0.001f;
|
||||
ina226->result.power_W = (int16)((ina226->result.org_reg[2][0] << 8) | ina226->result.org_reg[2][1]) * ina226->calibration.c_lsb_mA * 0.025f;
|
||||
ina226->result.shunt_voltage_mV = (int16)((ina226->result.org_reg[0][0] << 8) | ina226->result.org_reg[0][1]) * 0.0025f;
|
||||
|
||||
ina226->result.voltage_V = ina226->result.voltage_V > 0 ? ina226->result.voltage_V : 0;
|
||||
ina226->result.current_A = ina226->result.current_A > 0 ? ina226->result.current_A : 0;
|
||||
ina226->result.power_W = ina226->result.power_W > 0.016 && ina226->result.current_A > 0 && ina226->result.voltage_V > 0 ? ina226->result.power_W : 0;
|
||||
ina226->result.shunt_voltage_mV = ina226->result.shunt_voltage_mV > 0 ? ina226->result.shunt_voltage_mV : 0;
|
||||
|
||||
for(counter = 0; counter < 9; counter++)
|
||||
{
|
||||
ina226->result.past_voltage_V[counter] = ina226->result.past_voltage_V[counter+1];
|
||||
ina226->result.past_current_A[counter] = ina226->result.past_current_A[counter+1];
|
||||
}
|
||||
ina226->result.past_voltage_V[9] = ina226->result.voltage_V;
|
||||
ina226->result.past_current_A[9] = ina226->result.current_A;
|
||||
|
||||
ina226->result.energy_J += (ina226->result.past_voltage_V[9] * ina226->result.past_current_A[9] + ina226->result.past_voltage_V[8] * ina226->result.past_current_A[8]) * (INA226_READ_TIME_MS / 2000.0f);
|
||||
ina226->result.quantity_C += (ina226->result.past_current_A[9] + ina226->result.past_current_A[8]) * (INA226_READ_TIME_MS / 2000.0f);
|
||||
}
|
||||
Reference in New Issue
Block a user