huyongji1.1-system/App/energy_manager/energy_manager.c

87 lines
2.9 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// #include "energy_manager.h"
// // 电量初始化在main中上电时调用
// void Energy_Init(void)
// {
// uint8_t energy_bytes[4];
// float saved_energy = 0.0;
// // 读取EEPROM保存的电量值
// printf("Attempting to read energy from EEPROM...\r\n");
// Inf_AT24C02_ReadBytes(ADDR, energy_bytes, 4);
// // 打印原始字节
// printf("EEPROM Raw Data: %02X %02X %02X %02X\r\n",
// energy_bytes[0], energy_bytes[1], energy_bytes[2], energy_bytes[3]);
// // 转换为float并打印实际值
// memcpy(&saved_energy, energy_bytes, sizeof(float));
// printf("Raw float value: %.10f\r\n", saved_energy);
// // 检查数据有效性
// if(saved_energy >= 0.0f) // 只检查非负值
// {
// base_energy = saved_energy;
// analog_status[1] = saved_energy;
// printf("Power On - Load Energy: %.6f KWH\r\n", base_energy);
// }
// else
// {
// printf("Invalid Energy Data (%.6f), Reset to 0\r\n", saved_energy);
// base_energy = 0.0;
// analog_status[1] = 0.0;
// // 立即保存有效值0
// uint8_t zero_bytes[4] = {0};
// printf("Saving zero value to EEPROM...\r\n");
// Inf_AT24C02_WriteBytes(ADDR, zero_bytes, 4);
// }
// }
// // 电量管理在main循环中调用
// void Energy_Management(void)
// {
// static uint32_t last_save_time = 0;
// static uint8_t power_down_flag = 0;
// uint32_t current_time = HAL_GetTick();
// // 打印当前时间
// printf("Current Time: %lu ms\r\n", current_time);
// // 改为1分钟保存一次 (60000ms = 1分钟)
// if(current_time - last_save_time >= 60000) // 原来是420000(7分钟)
// {
// printf("\r\nSaving energy to EEPROM...\r\n"); // 添加调试信息
// uint8_t energy_bytes[4];
// memcpy(energy_bytes, &base_energy, sizeof(float));
// // 打印要保存的电量值
// printf("Energy to save: %.6f KWH\r\n", base_energy);
// Inf_AT24C02_WriteBytes(ADDR, energy_bytes, sizeof(float));
// printf("1min Save: %.6f KWH\r\n", base_energy);
// last_save_time = current_time;
// }
// // 断电保护
// if(V < 100.0 && power_down_flag == 0)
// {
// printf("Voltage is low (V = %.2f), entering power down save mode.\r\n", V);
// uint8_t energy_bytes[4];
// memcpy(energy_bytes, &base_energy, sizeof(float));
// // 打印要保存的电量值
// printf("Energy to save during power down: %.6f KWH\r\n", base_energy);
// Inf_AT24C02_WriteBytes(ADDR, energy_bytes, sizeof(float));
// printf("Power Down Save: %.6f KWH\r\n", base_energy);
// power_down_flag = 1;
// }
// else if(V >= 100.0)
// {
// printf("Voltage is normal (V = %.2f), resetting power down flag.\r\n", V);
// power_down_flag = 0;
// }
// }