This commit is contained in:
2025-10-17 00:29:14 +08:00
parent a6ddc01c98
commit e7b0457fb9
9 changed files with 482 additions and 18 deletions

98
code/ina226.c Normal file
View 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);
}

84
code/ina226.h Normal file
View File

@@ -0,0 +1,84 @@
// <20>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>: GB18030
/*
* ina226.h
*
* Created on: 2025<32><35>10<31><30>16<31><36>
* Author: LHYe200
*/
#ifndef CODE_INA226_H_
#define CODE_INA226_H_
#include "zf_common_typedef.h"
#define INA226_NUM 2
#define INA226_ALT_PIN {P15_2, P15_3}
#define INA226_ADDR {0x40, 0x45}
#define INA226_DEF_MODE {0x44, 0x4F}
#define INA226_DEF_CAL {0x0D, 0x1B}
#define INA226_READ_TIME_MS 1
#define INA226_SOFT_WAIT_TIME 0
#define INA226_SDA_PIN P15_1
#define INA226_SCL_PIN P15_0
#define INA226_CONFIG_REG 0x00
#define INA226_SHUNTVOT_REG 0x01
#define INA226_BUSVOT_REG 0x02
#define INA226_POWER_REG 0x03
#define INA226_CURRENT_REG 0x04
#define INA226_CALIBRATION_REG 0x05
typedef struct
{
uint8 addr;
float max_current_A;
float rshunt_mOhm;
float vot_fix_k;
float amp_fix_k;
uint8 mode[2];
} INA226_Config;
typedef struct
{
float c_lsb_mA;
uint8 cal[2];
} INA226_Calibration;
typedef struct
{
float voltage_V;
float current_A;
float power_W;
float shunt_voltage_mV;
float energy_J;
float quantity_C;
uint8 alert_flag;
uint8 org_reg[4][2];
float past_voltage_V[10];
float past_current_A[10];
} INA226_Result;
typedef struct
{
INA226_Config config;
INA226_Calibration calibration;
INA226_Result result;
} INA226_t;
extern INA226_t ina226[INA226_NUM];
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);
void INA226_Read(INA226_t *ina226);
#endif /* CODE_INA226_H_ */

146
code/power_out.c Normal file
View File

@@ -0,0 +1,146 @@
/*
* power_out.c
*
* Created on: 2025<32><35>10<31><30>16<31><36>
* Author: LHYe200
*/
#include "power_out.h"
#include "zf_driver_adc.h"
#include "zf_driver_pwm.h"
#include "zf_driver_gpio.h"
Power_Out_t power_outs[POWER_OUT_NUM];
void Power_Out_Single_Init(Power_Out_t *power_out, int16 vot_channel, int16 amp_channel, float votK[2], float ampK[2], int16 enable_pin, uint8 can_adjust, int16 vot_pwm_channel)
{
if(enable_pin != POWER_OUT_FUNCTION_DISABLE)
{
gpio_init((gpio_pin_enum)enable_pin, GPO, 0, GPO_PUSH_PULL);
}
power_out->config.voltage_read_channel = vot_channel;
power_out->config.current_read_channel = amp_channel;
Init_lowPass_alpha(&power_out->config.vot_filter, POWER_OUT_READ_TIME_MS / 1000.0f, 10.0f);
Init_lowPass_alpha(&power_out->config.amp_filter, POWER_OUT_READ_TIME_MS / 1000.0f, 10.0f);
power_out->config.votK[0] = votK[0];
power_out->config.votK[1] = votK[1];
power_out->config.ampK[0] = ampK[0];
power_out->config.ampK[1] = ampK[1];
power_out->config.enable_pin = enable_pin;
power_out->config.can_adjust = can_adjust;
power_out->config.vot_pwm_channel = vot_pwm_channel;
power_out->status.voltage_V = 0.0f;
power_out->status.current_A = 0.0f;
power_out->status.power_W = 0.0f;
power_out->status.energy_J = 0.0f;
power_out->status.quantity_C = 0.0f;
memset(power_out->status.past_voltage_V, 0, sizeof(power_out->status.past_voltage_V));
memset(power_out->status.past_current_A, 0, sizeof(power_out->status.past_current_A));
power_out->set_voltage_V = 0.0f;
power_out->set_current_A = 0.0f;
power_out->max_power_W = 300.0f;
power_out->enabled = 0;
if(vot_channel != POWER_OUT_FUNCTION_DISABLE)
{
adc_init((adc_channel_enum)vot_channel, ADC_12BIT);
}
if(amp_channel != POWER_OUT_FUNCTION_DISABLE)
{
adc_init((adc_channel_enum)amp_channel, ADC_12BIT);
}
if(vot_pwm_channel != POWER_OUT_FUNCTION_DISABLE)
{
pwm_init((pwm_channel_enum)vot_pwm_channel, VOT_PWM_FREQUENCY_HZ, 5000);
}
}
void Power_Out_Init()
{
int16 vot_channel_list[POWER_OUT_NUM] = POWER_OUT_VOT_ADC_CHANNEL;
int16 amp_channel_list[POWER_OUT_NUM] = POWER_OUT_AMP_ADC_CHANNEL;
int16 vot_pwm_channel_list[POWER_OUT_NUM] = POWER_OUT_VOT_PWM_CHANNEL;
int16 enable_pin_list[POWER_OUT_NUM] = POWER_OUT_ENABLE_PIN;
float votK[POWER_OUT_NUM][2] = POWER_OUT_VOT_ADC_K;
float ampK[POWER_OUT_NUM][2] = POWER_OUT_AMP_ADC_K;
uint8 counter;
for(counter = 0; counter < POWER_OUT_NUM; counter++)
{
uint8 can_adjust = vot_pwm_channel_list[counter] != -1 ? 1 : 0;
Power_Out_Single_Init(&power_outs[counter], vot_channel_list[counter], amp_channel_list[counter], votK[counter], ampK[counter], enable_pin_list[counter], can_adjust, vot_pwm_channel_list[counter]);
}
}
void Power_Out_Enable(Power_Out_t *power_out, uint8 enable)
{
if(power_out->config.enable_pin != POWER_OUT_FUNCTION_DISABLE)
{
gpio_set_level((gpio_pin_enum)power_out->config.enable_pin, enable);
}
power_out->enabled = enable;
}
void Power_Out_Set_PWM(Power_Out_t *power_out, uint32 duty_cycle)
{
if(power_out->config.can_adjust && power_out->config.vot_pwm_channel != POWER_OUT_FUNCTION_DISABLE)
{
pwm_set_duty((pwm_channel_enum)power_out->config.vot_pwm_channel, duty_cycle);
}
}
void Power_Out_Single_Read(Power_Out_t *power_out)
{
float tmp = 0.0f;
if(power_out->config.voltage_read_channel != POWER_OUT_FUNCTION_DISABLE)
{
// tmp = (float)adc_convert((adc_channel_enum)power_out->config.voltage_read_channel);
tmp = (float)adc_mean_filter_convert((adc_channel_enum)power_out->config.voltage_read_channel,2);
power_out->status.voltage_V = Low_pass_filter(&power_out->config.vot_filter, tmp) * power_out->config.votK[0] + power_out->config.votK[1];
}
else
{
power_out->status.voltage_V = power_out->set_voltage_V;
}
if(power_out->config.current_read_channel != POWER_OUT_FUNCTION_DISABLE)
{
// tmp = (float)adc_convert((adc_channel_enum)power_out->config.current_read_channel);
tmp = (float)adc_mean_filter_convert((adc_channel_enum)power_out->config.current_read_channel,2);
power_out->status.current_A = Low_pass_filter(&power_out->config.amp_filter, tmp) * power_out->config.ampK[0] + power_out->config.ampK[1];
power_out->status.current_A = power_out->status.current_A > 0.0f ? power_out->status.current_A : 0.0f;
}
else
{
power_out->status.current_A = power_out->set_current_A;
}
power_out->status.power_W = power_out->status.voltage_V * power_out->status.current_A;
uint8 counter;
for(counter = 0; counter < 9; counter++)
{
power_out->status.past_voltage_V[counter] = power_out->status.past_voltage_V[counter+1];
power_out->status.past_current_A[counter] = power_out->status.past_current_A[counter+1];
}
power_out->status.past_voltage_V[9] = power_out->status.voltage_V;
power_out->status.past_current_A[9] = power_out->status.current_A;
power_out->status.energy_J += (power_out->status.past_voltage_V[9] * power_out->status.past_current_A[9] + power_out->status.past_voltage_V[8] * power_out->status.past_current_A[8]) * (POWER_OUT_READ_TIME_MS / 2000.0f);
power_out->status.quantity_C += (power_out->status.past_current_A[9] + power_out->status.past_current_A[8]) * (POWER_OUT_READ_TIME_MS / 2000.0f);
}
void Power_Out_Read()
{
uint8 counter;
for(counter = 0; counter < POWER_OUT_NUM; counter++)
{
Power_Out_Single_Read(&power_outs[counter]);
}
}

76
code/power_out.h Normal file
View File

@@ -0,0 +1,76 @@
/*
* power_out.h
*
* Created on: 2025<32><35>10<31><30>16<31><36>
* Author: LHYe200
*/
#ifndef CODE_POWER_OUT_H_
#define CODE_POWER_OUT_H_
#include "zf_common_typedef.h"
#include "filter.h"
#define POWER_OUT_NUM 8
#define POWER_OUT_READ_TIME_MS 1
#define POWER_OUT_FUNCTION_DISABLE -1
#define POWER_OUT_VOT_ADC_CHANNEL {ADC0_CH0_A0, ADC0_CH1_A1, ADC0_CH2_A2, ADC0_CH3_A3, ADC0_CH4_A4, ADC0_CH5_A5, ADC0_CH6_A6, ADC0_CH7_A7}
#define POWER_OUT_AMP_ADC_CHANNEL {ADC2_CH3_A35, ADC2_CH4_A36, ADC2_CH5_A37, ADC2_CH6_A38, ADC2_CH7_A39, ADC2_CH10_A44, ADC2_CH11_A45, ADC2_CH12_A46}
#define POWER_OUT_VOT_PWM_CHANNEL {POWER_OUT_FUNCTION_DISABLE, POWER_OUT_FUNCTION_DISABLE, POWER_OUT_FUNCTION_DISABLE, POWER_OUT_FUNCTION_DISABLE, ATOM1_CH0_P02_0, ATOM1_CH1_P02_1, ATOM1_CH2_P02_2, ATOM1_CH3_P02_3}
#define POWER_OUT_ENABLE_PIN {P33_13, P33_12, P33_11, P33_10, P33_7, P33_6, P33_5, P33_4}
#define POWER_OUT_VOT_ADC_K {{0.0015, -0.0437}, {0.0015, -0.045}, {0.0015, -0.0451}, {0.0015, -0.0451}, {0.0049, 0.0297}, {0.0049, -0.0292}, {0.0049, -0.0179}, {0.0049, 0.0397}}
#define POWER_OUT_AMP_ADC_K {{0.0029, -0.1382}, {0.002868, -0.1325}, {0.0029, -0.1335}, {0.0029, -0.1294}, {0.0029, -0.1083}, {0.0029, -0.1174}, {0.0028, -0.0563}, {0.002831, -0.0786}}
#define VOT_PWM_FREQUENCY_HZ 100000
#define VOT_PROTECT_K {0.9,1.1} // <20><>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5>
#define AMP_PROTECT_K 1.1 // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5>
typedef struct
{
float voltage_V;
float current_A;
float power_W;
float energy_J;
float quantity_C;
float past_voltage_V[10];
float past_current_A[10];
} Power_Out_Status_t;
typedef struct
{
int16 voltage_read_channel;
int16 current_read_channel;
float votK[2]; // <20><>ѹУ׼ϵ<D7BC><CFB5>
float ampK[2]; // <20><><EFBFBD><EFBFBD>У׼ϵ<D7BC><CFB5>
low_pass_filter_t vot_filter;
low_pass_filter_t amp_filter;
int16 enable_pin;
uint8 can_adjust; // <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int16 vot_pwm_channel;
} Power_Out_Config_t;
typedef struct
{
Power_Out_Config_t config;
Power_Out_Status_t status;
float set_voltage_V;
float set_current_A;
float max_power_W;
uint8 enabled;
} Power_Out_t;
extern Power_Out_t power_outs[POWER_OUT_NUM];
void Power_Out_Single_Init(Power_Out_t *power_out, int16 vot_channel, int16 amp_channel, float votK[2], float ampK[2], int16 enable_pin, uint8 can_adjust, int16 vot_pwm_channel);
void Power_Out_Init(void);
void Power_Out_Enable(Power_Out_t *power_out, uint8 enable);
void Power_Out_Set_PWM(Power_Out_t *power_out, uint32 duty_cycle);
void Power_Out_Single_Read(Power_Out_t *power_out);
void Power_Out_Read(void);
#endif /* CODE_POWER_OUT_H_ */

View File

@@ -20,6 +20,11 @@ void Status_LED_Init(void)
gpio_init((gpio_pin_enum)STATUS_LED_3, GPO, GPIO_HIGH, GPO_PUSH_PULL);
gpio_init((gpio_pin_enum)STATUS_LED_4, GPO, GPIO_HIGH, GPO_PUSH_PULL);
gpio_init((gpio_pin_enum)STATUS_LED_5, GPO, GPIO_HIGH, GPO_PUSH_PULL);
gpio_init((gpio_pin_enum)STATUS_LED_6, GPO, GPIO_HIGH, GPO_PUSH_PULL);
gpio_init((gpio_pin_enum)STATUS_LED_7, GPO, GPIO_HIGH, GPO_PUSH_PULL);
gpio_init((gpio_pin_enum)STATUS_LED_8, GPO, GPIO_HIGH, GPO_PUSH_PULL);
gpio_init((gpio_pin_enum)STATUS_LED_9, GPO, GPIO_HIGH, GPO_PUSH_PULL);
gpio_init((gpio_pin_enum)STATUS_LED_10, GPO, GPIO_HIGH, GPO_PUSH_PULL);
}
void Flash_LED(status_led_index_enum led)

View File

@@ -22,7 +22,7 @@ typedef enum // ö
STATUS_LED_7 = P11_6, //WHITE
STATUS_LED_8 = P11_9, //WARMWHITE
STATUS_LED_9 = P10_1, //ICEBLUE
STATUS_LED_20 = P10_2, //PINK
STATUS_LED_10 = P10_2, //PINK
}status_led_index_enum;

View File

@@ -11,7 +11,7 @@
#include "zf_common_headfile.h"
#define VOFA_CLIENT_COM_INTERFACE (1) // 0:<3A><><EFBFBD><EFBFBD> 1:WIFI<46><49><EFBFBD><EFBFBD>SPI 2:<3A>Զ<EFBFBD><D4B6><EFBFBD>
#define VOFA_CLIENT_COM_INTERFACE (0) // 0:<3A><><EFBFBD><EFBFBD> 1:WIFI<46><49><EFBFBD><EFBFBD>SPI 2:<3A>Զ<EFBFBD><D4B6><EFBFBD>
#define VOFA_RECEIVE_CH (64) // <20><><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD>256<35><36>ͨ<EFBFBD><CDA8>
#define VOFA_SEND_CH (32) // <20><><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>