mirror of https://github.com/arendst/Tasmota.git
Fix CSE7761 default calibration
This commit is contained in:
parent
b101657fba
commit
86e2bc53be
|
@ -31,9 +31,9 @@
|
||||||
|
|
||||||
//#define CSE7761_SIMULATE
|
//#define CSE7761_SIMULATE
|
||||||
|
|
||||||
#define CSE7761_UREF 10000 // Gain 1 * 10000 in V
|
#define CSE7761_UREF 42563 // RmsUc
|
||||||
#define CSE7761_IREF 160000 // Gain 16 * 10000 in A
|
#define CSE7761_IREF 52241 // RmsIAC
|
||||||
#define CSE7761_PREF 50000 // in W
|
#define CSE7761_PREF 44513 // PowerPAC
|
||||||
|
|
||||||
#define CSE7761_REG_SYSCON 0x00 // System Control Register
|
#define CSE7761_REG_SYSCON 0x00 // System Control Register
|
||||||
#define CSE7761_REG_EMUCON 0x01 // Metering control register
|
#define CSE7761_REG_EMUCON 0x01 // Metering control register
|
||||||
|
@ -80,6 +80,7 @@ struct {
|
||||||
uint32_t current_rms[2] = { 0 };
|
uint32_t current_rms[2] = { 0 };
|
||||||
uint32_t energy[2] = { 0 };
|
uint32_t energy[2] = { 0 };
|
||||||
uint32_t active_power[2] = { 0 };
|
uint32_t active_power[2] = { 0 };
|
||||||
|
uint16_t coefficient[8] = { 0 };
|
||||||
uint8_t energy_update = 0;
|
uint8_t energy_update = 0;
|
||||||
uint8_t init = 4;
|
uint8_t init = 4;
|
||||||
uint8_t ready = 0;
|
uint8_t ready = 0;
|
||||||
|
@ -162,16 +163,36 @@ uint32_t Cse7761ReadFallback(uint32_t reg, uint32_t prev) {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint32_t Cse7761Ref(uint32_t unit) {
|
||||||
|
switch (unit) {
|
||||||
|
case 1: return 0x400000 * 100 / CSE7761Data.coefficient[RmsUC];
|
||||||
|
case 2: return (0x800000 * 100 / CSE7761Data.coefficient[RmsIAC]) * 10; // Stay within 32 bits
|
||||||
|
case 3: return 0x80000000 / CSE7761Data.coefficient[PowerPAC];
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
bool Cse7761ChipInit(void) {
|
bool Cse7761ChipInit(void) {
|
||||||
uint16_t calc_chksum = 0xFFFF;
|
uint16_t calc_chksum = 0xFFFF;
|
||||||
for (uint32_t i = 0; i < 8; i++) {
|
for (uint32_t i = 0; i < 8; i++) {
|
||||||
calc_chksum = Cse7761Read(CSE7761_REG_RMSIAC + i);
|
CSE7761Data.coefficient[i] = Cse7761Read(CSE7761_REG_RMSIAC + i);
|
||||||
|
calc_chksum += CSE7761Data.coefficient[i];
|
||||||
}
|
}
|
||||||
calc_chksum = ~calc_chksum;
|
calc_chksum = ~calc_chksum;
|
||||||
// uint16_t dummy = Cse7761Read(CSE7761_REG_COEFFOFFSET);
|
// uint16_t dummy = Cse7761Read(CSE7761_REG_COEFFOFFSET);
|
||||||
uint16_t coeff_chksum = Cse7761Read(CSE7761_REG_COEFFCHKSUM);
|
uint16_t coeff_chksum = Cse7761Read(CSE7761_REG_COEFFCHKSUM);
|
||||||
if (calc_chksum != coeff_chksum) {
|
if ((calc_chksum != coeff_chksum) || (!calc_chksum)) {
|
||||||
AddLog(LOG_LEVEL_DEBUG, PSTR("C61: Not calibrated"));
|
AddLog(LOG_LEVEL_DEBUG, PSTR("C61: Default calibration"));
|
||||||
|
CSE7761Data.coefficient[RmsIAC] = CSE7761_IREF;
|
||||||
|
// CSE7761Data.coefficient[RmsIBC] = 0xCC05;
|
||||||
|
CSE7761Data.coefficient[RmsUC] = CSE7761_UREF;
|
||||||
|
CSE7761Data.coefficient[PowerPAC] = CSE7761_PREF;
|
||||||
|
// CSE7761Data.coefficient[PowerPBC] = 0xADD7;
|
||||||
|
}
|
||||||
|
if (HLW_PREF_PULSE == Settings.energy_power_calibration) {
|
||||||
|
Settings.energy_voltage_calibration = Cse7761Ref(1);
|
||||||
|
Settings.energy_current_calibration = Cse7761Ref(2);
|
||||||
|
Settings.energy_power_calibration = Cse7761Ref(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
Cse7761Write(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_ENABLE_WRITE);
|
Cse7761Write(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_ENABLE_WRITE);
|
||||||
|
@ -317,8 +338,7 @@ void Cse7761GetData(void) {
|
||||||
// The active power parameter PowerA/B is in two’s complement format, 32-bit data, the highest bit is Sign bit.
|
// The active power parameter PowerA/B is in two’s complement format, 32-bit data, the highest bit is Sign bit.
|
||||||
uint32_t value = Cse7761ReadFallback(CSE7761_REG_RMSU, CSE7761Data.voltage_rms);
|
uint32_t value = Cse7761ReadFallback(CSE7761_REG_RMSU, CSE7761Data.voltage_rms);
|
||||||
#ifdef CSE7761_SIMULATE
|
#ifdef CSE7761_SIMULATE
|
||||||
// value = 2342160; // 234.2V
|
value = 2342160; // 237.7V
|
||||||
value = 2000000; // 200V
|
|
||||||
#endif
|
#endif
|
||||||
CSE7761Data.voltage_rms = (value >= 0x800000) ? 0 : value;
|
CSE7761Data.voltage_rms = (value >= 0x800000) ? 0 : value;
|
||||||
|
|
||||||
|
@ -335,14 +355,12 @@ void Cse7761GetData(void) {
|
||||||
|
|
||||||
value = Cse7761ReadFallback(CSE7761_REG_RMSIB, CSE7761Data.current_rms[1]);
|
value = Cse7761ReadFallback(CSE7761_REG_RMSIB, CSE7761Data.current_rms[1]);
|
||||||
#ifdef CSE7761_SIMULATE
|
#ifdef CSE7761_SIMULATE
|
||||||
// value = 29760; // 0.186A
|
value = 29760; // 0.185A
|
||||||
value = 800000; // 5A
|
|
||||||
#endif
|
#endif
|
||||||
CSE7761Data.current_rms[1] = ((value >= 0x800000) || (value < 1600)) ? 0 : value; // No load threshold of 10mA
|
CSE7761Data.current_rms[1] = ((value >= 0x800000) || (value < 1600)) ? 0 : value; // No load threshold of 10mA
|
||||||
value = Cse7761ReadFallback(CSE7761_REG_POWERPB, CSE7761Data.active_power[1]);
|
value = Cse7761ReadFallback(CSE7761_REG_POWERPB, CSE7761Data.active_power[1]);
|
||||||
#ifdef CSE7761_SIMULATE
|
#ifdef CSE7761_SIMULATE
|
||||||
// value = 2126641; // 42.5W
|
value = 2126641; // 44.05W
|
||||||
value = 50000000; // 1000W
|
|
||||||
#endif
|
#endif
|
||||||
CSE7761Data.active_power[1] = (0 == CSE7761Data.current_rms[1]) ? 0 : (value & 0x80000000) ? (~value) + 1 : value;
|
CSE7761Data.active_power[1] = (0 == CSE7761Data.current_rms[1]) ? 0 : (value & 0x80000000) ? (~value) + 1 : value;
|
||||||
|
|
||||||
|
@ -352,24 +370,25 @@ void Cse7761GetData(void) {
|
||||||
CSE7761Data.active_power[0], CSE7761Data.active_power[1]);
|
CSE7761Data.active_power[0], CSE7761Data.active_power[1]);
|
||||||
|
|
||||||
if (Energy.power_on) { // Powered on
|
if (Energy.power_on) { // Powered on
|
||||||
|
// Voltage = RmsU * RmsUC * 10 / 0x400000
|
||||||
|
// Energy.voltage[0] = (float)(((uint64_t)CSE7761Data.voltage_rms * CSE7761Data.coefficient[RmsUC] * 10) >> 22) / 1000; // V
|
||||||
Energy.voltage[0] = ((float)CSE7761Data.voltage_rms / Settings.energy_voltage_calibration); // V
|
Energy.voltage[0] = ((float)CSE7761Data.voltage_rms / Settings.energy_voltage_calibration); // V
|
||||||
|
|
||||||
for (uint32_t channel = 0; channel < 2; channel++) {
|
for (uint32_t channel = 0; channel < 2; channel++) {
|
||||||
Energy.data_valid[channel] = 0;
|
Energy.data_valid[channel] = 0;
|
||||||
|
// Active power = PowerPA * PowerPAC * 1000 / 0x80000000
|
||||||
|
// Energy.active_power[channel] = (float)(((uint64_t)CSE7761Data.active_power[channel] * CSE7761Data.coefficient[PowerPAC + channel] * 1000) >> 31) / 1000; // W
|
||||||
Energy.active_power[channel] = (float)CSE7761Data.active_power[channel] / Settings.energy_power_calibration; // W
|
Energy.active_power[channel] = (float)CSE7761Data.active_power[channel] / Settings.energy_power_calibration; // W
|
||||||
if (0 == Energy.active_power[channel]) {
|
if (0 == Energy.active_power[channel]) {
|
||||||
Energy.current[channel] = 0;
|
Energy.current[channel] = 0;
|
||||||
} else {
|
} else {
|
||||||
|
// Current = RmsIA * RmsIAC / 0x800000
|
||||||
|
// Energy.current[channel] = (float)(((uint64_t)CSE7761Data.current_rms[channel] * CSE7761Data.coefficient[RmsIAC + channel]) >> 23) / 1000; // A
|
||||||
Energy.current[channel] = (float)CSE7761Data.current_rms[channel] / Settings.energy_current_calibration; // A
|
Energy.current[channel] = (float)CSE7761Data.current_rms[channel] / Settings.energy_current_calibration; // A
|
||||||
CSE7761Data.energy[channel] += Energy.active_power[channel];
|
CSE7761Data.energy[channel] += Energy.active_power[channel];
|
||||||
CSE7761Data.energy_update++;
|
CSE7761Data.energy_update++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
} else { // Powered off
|
|
||||||
Energy.data_valid[0] = ENERGY_WATCHDOG;
|
|
||||||
Energy.data_valid[1] = ENERGY_WATCHDOG;
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -428,11 +447,6 @@ void Cse7761SnsInit(void) {
|
||||||
SetSerial(38400, TS_SERIAL_8E1);
|
SetSerial(38400, TS_SERIAL_8E1);
|
||||||
ClaimSerial();
|
ClaimSerial();
|
||||||
}
|
}
|
||||||
if (HLW_PREF_PULSE == Settings.energy_power_calibration) {
|
|
||||||
Settings.energy_voltage_calibration = CSE7761_UREF;
|
|
||||||
Settings.energy_current_calibration = CSE7761_IREF;
|
|
||||||
Settings.energy_power_calibration = CSE7761_PREF;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
TasmotaGlobal.energy_driver = ENERGY_NONE;
|
TasmotaGlobal.energy_driver = ENERGY_NONE;
|
||||||
}
|
}
|
||||||
|
@ -455,15 +469,15 @@ bool Cse7761Command(void) {
|
||||||
uint32_t value = (uint32_t)(CharToFloat(XdrvMailbox.data) * 100); // 1.23 = 123
|
uint32_t value = (uint32_t)(CharToFloat(XdrvMailbox.data) * 100); // 1.23 = 123
|
||||||
|
|
||||||
if (CMND_POWERCAL == Energy.command_code) {
|
if (CMND_POWERCAL == Energy.command_code) {
|
||||||
if (1 == XdrvMailbox.payload) { XdrvMailbox.payload = CSE7761_PREF; }
|
if (1 == XdrvMailbox.payload) { XdrvMailbox.payload = Cse7761Ref(3); }
|
||||||
// Service in xdrv_03_energy.ino
|
// Service in xdrv_03_energy.ino
|
||||||
}
|
}
|
||||||
else if (CMND_VOLTAGECAL == Energy.command_code) {
|
else if (CMND_VOLTAGECAL == Energy.command_code) {
|
||||||
if (1 == XdrvMailbox.payload) { XdrvMailbox.payload = CSE7761_UREF; }
|
if (1 == XdrvMailbox.payload) { XdrvMailbox.payload = Cse7761Ref(1); }
|
||||||
// Service in xdrv_03_energy.ino
|
// Service in xdrv_03_energy.ino
|
||||||
}
|
}
|
||||||
else if (CMND_CURRENTCAL == Energy.command_code) {
|
else if (CMND_CURRENTCAL == Energy.command_code) {
|
||||||
if (1 == XdrvMailbox.payload) { XdrvMailbox.payload = CSE7761_IREF; }
|
if (1 == XdrvMailbox.payload) { XdrvMailbox.payload = Cse7761Ref(2); }
|
||||||
// Service in xdrv_03_energy.ino
|
// Service in xdrv_03_energy.ino
|
||||||
}
|
}
|
||||||
else if (CMND_POWERSET == Energy.command_code) {
|
else if (CMND_POWERSET == Energy.command_code) {
|
||||||
|
|
Loading…
Reference in New Issue