From 2ea54df989d15e209ff9a17b677375323e793053 Mon Sep 17 00:00:00 2001 From: ffonck Date: Thu, 8 Jun 2023 11:00:30 -0400 Subject: [PATCH 1/3] add GetChecksum function --- energyic_UART.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/energyic_UART.cpp b/energyic_UART.cpp index 9dd769c..d9e6a0b 100644 --- a/energyic_UART.cpp +++ b/energyic_UART.cpp @@ -137,6 +137,25 @@ unsigned short ATM90E26_UART::GetSysStatus() { return CommEnergyIC(1, SysStatus, 0xFFFF); } +uint16_t ATM90E26_UART::GetChecksum(const uint16_t *hex_values, int length) +{ + uint16_t value = 0x0000; + uint8_t lsb, msb; + int chk1 = 0, chk2 = 0; + + for (int i = 0; i < length; i++) + { + msb = (hex_values[i] >> 8); + lsb = (hex_values[i] & 0x00FF); + chk1 += msb + lsb; + chk2 ^= msb ^ lsb; + } + value = chk1 % 0x100; + value += (chk2 << 8); + + return value; +} + /* Initialise Energy IC, assume UART has already began in the main code */ From 7bd54317873b8c32c413f708977802adbdd4ed98 Mon Sep 17 00:00:00 2001 From: ffonck Date: Thu, 8 Jun 2023 12:19:32 -0400 Subject: [PATCH 2/3] Now using cpp convention --- energyic_UART.cpp | 44 ++++++++++++++++++++++---------------------- energyic_UART.h | 3 +++ 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/energyic_UART.cpp b/energyic_UART.cpp index d9e6a0b..4da5109 100644 --- a/energyic_UART.cpp +++ b/energyic_UART.cpp @@ -137,7 +137,7 @@ unsigned short ATM90E26_UART::GetSysStatus() { return CommEnergyIC(1, SysStatus, 0xFFFF); } -uint16_t ATM90E26_UART::GetChecksum(const uint16_t *hex_values, int length) +unsigned short ATM90E26_UART::GetChecksum(unsigned short *hex_values, int length) { uint16_t value = 0x0000; uint8_t lsb, msb; @@ -170,17 +170,16 @@ void ATM90E26_UART::InitEnergyIC() { // Set metering calibration values CommEnergyIC(0, CalStart, 0x5678); // Metering calibration startup command. // Register 21 to 2B need to be set - CommEnergyIC(0, PLconstH, 0x00B9); // PL Constant MSB - CommEnergyIC(0, PLconstL, 0xC1F3); // PL Constant LSB - CommEnergyIC(0, Lgain, 0x1D39); // Line calibration gain - CommEnergyIC(0, Lphi, 0x0000); // Line calibration angle - CommEnergyIC(0, PStartTh, 0x08BD); // Active Startup Power Threshold - CommEnergyIC(0, PNolTh, 0x0000); // Active No-Load Power Threshold - CommEnergyIC(0, QStartTh, 0x0AEC); // Reactive Startup Power Threshold - CommEnergyIC(0, QNolTh, 0x0000); // Reactive No-Load Power Threshold - CommEnergyIC(0, MMode, 0x9422); // Metering Mode Configuration. All defaults. - // See pg 31 of datasheet. - CommEnergyIC(0, CSOne, 0x4A34); // Write CSOne, as self calculated + unsigned reg_adr[CfgRegLen1] = {PLconstH,PLconstL,Lgain,Lphi,Ngain,Nphi,PStartTh,PNolTh,QStartTh,QNolTh,MMode}; + unsigned short reg_values[CfgRegLen1] = {0x00B9,0xC1F3,0x1D39,0x0000,0x0000,0x0000,0x08BD,0x0000,0x0AEC,0x0000,0x9422}; + + // This loop iterates though above configurations from 21H-2BH registers. + for (int i = 0; i < CfgRegLen1; i++) { + CommEnergyIC(0, reg_adr[i], reg_values[i]); + } + + // See pg 31 of datasheet. + CommEnergyIC(0, CSOne, GetChecksum(reg_values, CfgRegLen1)); // Write CSOne, as self calculated Serial.print("Checksum 1:"); Serial.println( @@ -188,16 +187,17 @@ void ATM90E26_UART::InitEnergyIC() { HEX); // Checksum 1. Needs to be calculated based off the above values. // Set measurement calibration values - CommEnergyIC( - 0, AdjStart, - 0x5678); // Measurement calibration startup command, registers 31-3A - CommEnergyIC(0, Ugain, 0xD464); // Voltage rms gain - CommEnergyIC(0, IgainL, 0x6E49); // L line current gain - CommEnergyIC(0, Uoffset, 0x0000); // Voltage offset - CommEnergyIC(0, IoffsetL, 0x0000); // L line current offset - CommEnergyIC(0, PoffsetL, 0x0000); // L line active power offset - CommEnergyIC(0, QoffsetL, 0x0000); // L line reactive power offset - CommEnergyIC(0, CSTwo, 0xD294); // Write CSTwo, as self calculated + CommEnergyIC(0, AdjStart, 0x5678); // Measurement calibration startup command, registers 31-3A + + unsigned reg_values2[CfgRegLen2] = {Ugain,IgainL,IgainN,Uoffset,IoffsetL,IoffsetN,PoffsetL,QoffsetL,PoffsetN,QoffsetN}; + unsigned short reg_adr2[CfgRegLen2] = {0xD464,0x6E49,0x7530,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000}; + + // This loop iterates though above configurations from 31H-3AH registers. + for (int i = 0; i < CfgRegLen2; i++) { + CommEnergyIC(0, reg_values2[i], reg_adr2[i]); + } + + CommEnergyIC(0, CSTwo, GetChecksum(reg_values2, CfgRegLen2)); // Write CSTwo, as self calculated Serial.print("Checksum 2:"); Serial.println( diff --git a/energyic_UART.h b/energyic_UART.h index c2a14d4..35aba4e 100644 --- a/energyic_UART.h +++ b/energyic_UART.h @@ -48,6 +48,7 @@ #define QNolTh 0x2A // Reactive No-Load Power Threshold #define MMode 0x2B // Metering Mode Configuration #define CSOne 0x2C // Checksum 1 +#define CfgRegLen1 11 // Length of register for Checksum 1 #define AdjStart 0x30 // Measurement Calibration Start Command #define Ugain 0x31 // Voltage rms Gain #define IgainL 0x32 // L Line Current rms Gain @@ -60,6 +61,7 @@ #define PoffsetN 0x39 // N Line Active Power Offset #define QoffsetN 0x3A // N Line Reactive Power Offset #define CSTwo 0x3B // Checksum 2 +#define CfgRegLen2 10 // Length of register for Checksum 2 #define APenergy 0x40 // Forward Active Energy #define ANenergy 0x41 // Reverse Active Energy #define ATenergy 0x42 // Absolute Active Energy @@ -95,6 +97,7 @@ class ATM90E26_UART { void InitEnergyIC(); unsigned short GetSysStatus(); unsigned short GetMeterStatus(); + unsigned short GetChecksum(unsigned short *hex_values, int length); private: unsigned short CommEnergyIC(unsigned char RW, unsigned char address, From c304c3af0859ea499496fed76fd9a7f7fabca474 Mon Sep 17 00:00:00 2001 From: ffonck Date: Thu, 8 Jun 2023 12:36:59 -0400 Subject: [PATCH 3/3] Now adjusting from C to CPP because inttypes --- energyic_UART.cpp | 36 +++++++++++++++--------------------- energyic_UART.h | 4 ++-- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/energyic_UART.cpp b/energyic_UART.cpp index 4da5109..638dc1b 100644 --- a/energyic_UART.cpp +++ b/energyic_UART.cpp @@ -137,14 +137,12 @@ unsigned short ATM90E26_UART::GetSysStatus() { return CommEnergyIC(1, SysStatus, 0xFFFF); } -unsigned short ATM90E26_UART::GetChecksum(unsigned short *hex_values, int length) -{ - uint16_t value = 0x0000; - uint8_t lsb, msb; +unsigned short ATM90E26_UART::GetChecksum(unsigned short *hex_values, int length) { + unsigned short value = 0x0000; + unsigned char lsb, msb; int chk1 = 0, chk2 = 0; - for (int i = 0; i < length; i++) - { + for (int i = 0; i < length; i++) { msb = (hex_values[i] >> 8); lsb = (hex_values[i] & 0x00FF); chk1 += msb + lsb; @@ -162,6 +160,13 @@ Initialise Energy IC, assume UART has already began in the main code void ATM90E26_UART::InitEnergyIC() { unsigned short systemstatus; + // Base Configuration for 21H-2BH + unsigned char reg_adr1[CfgRegLen1] = {PLconstH,PLconstL,Lgain,Lphi,Ngain,Nphi,PStartTh,PNolTh,QStartTh,QNolTh,MMode}; + unsigned short reg_values1[CfgRegLen1] = {0x00B9,0xC1F3,0x1D39,0x0000,0x0000,0x0000,0x08BD,0x0000,0x0AEC,0x0000,0x9422}; + //Base Configuration for 31H-3AH. + unsigned char reg_adr2[CfgRegLen2] = {Ugain,IgainL,IgainN,Uoffset,IoffsetL,IoffsetN,PoffsetL,QoffsetL,PoffsetN,QoffsetN}; + unsigned short reg_values2[CfgRegLen2] = {0xD464,0x6E49,0x7530,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000}; + CommEnergyIC(0, SoftReset, 0x789A); // Perform soft reset CommEnergyIC(0, FuncEn, 0x0030); // Voltage sag irq=1, report on warnout // pin=1, energy dir change irq=0 @@ -169,17 +174,11 @@ void ATM90E26_UART::InitEnergyIC() { // Set metering calibration values CommEnergyIC(0, CalStart, 0x5678); // Metering calibration startup command. - // Register 21 to 2B need to be set - unsigned reg_adr[CfgRegLen1] = {PLconstH,PLconstL,Lgain,Lphi,Ngain,Nphi,PStartTh,PNolTh,QStartTh,QNolTh,MMode}; - unsigned short reg_values[CfgRegLen1] = {0x00B9,0xC1F3,0x1D39,0x0000,0x0000,0x0000,0x08BD,0x0000,0x0AEC,0x0000,0x9422}; - // This loop iterates though above configurations from 21H-2BH registers. for (int i = 0; i < CfgRegLen1; i++) { - CommEnergyIC(0, reg_adr[i], reg_values[i]); - } - - // See pg 31 of datasheet. - CommEnergyIC(0, CSOne, GetChecksum(reg_values, CfgRegLen1)); // Write CSOne, as self calculated + CommEnergyIC(0, reg_adr1[i], reg_values1[i]); + } // See pg 31 of datasheet. + CommEnergyIC(0, CSOne, GetChecksum(reg_values1, CfgRegLen1)); // Write CSOne, as self calculated Serial.print("Checksum 1:"); Serial.println( @@ -188,15 +187,10 @@ void ATM90E26_UART::InitEnergyIC() { // Set measurement calibration values CommEnergyIC(0, AdjStart, 0x5678); // Measurement calibration startup command, registers 31-3A - - unsigned reg_values2[CfgRegLen2] = {Ugain,IgainL,IgainN,Uoffset,IoffsetL,IoffsetN,PoffsetL,QoffsetL,PoffsetN,QoffsetN}; - unsigned short reg_adr2[CfgRegLen2] = {0xD464,0x6E49,0x7530,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000}; - // This loop iterates though above configurations from 31H-3AH registers. for (int i = 0; i < CfgRegLen2; i++) { - CommEnergyIC(0, reg_values2[i], reg_adr2[i]); + CommEnergyIC(0, reg_adr2[i], reg_values2[i]); } - CommEnergyIC(0, CSTwo, GetChecksum(reg_values2, CfgRegLen2)); // Write CSTwo, as self calculated Serial.print("Checksum 2:"); diff --git a/energyic_UART.h b/energyic_UART.h index 35aba4e..c97ea46 100644 --- a/energyic_UART.h +++ b/energyic_UART.h @@ -48,7 +48,7 @@ #define QNolTh 0x2A // Reactive No-Load Power Threshold #define MMode 0x2B // Metering Mode Configuration #define CSOne 0x2C // Checksum 1 -#define CfgRegLen1 11 // Length of register for Checksum 1 +#define CfgRegLen1 11 // Amount of register for Checksum 1 #define AdjStart 0x30 // Measurement Calibration Start Command #define Ugain 0x31 // Voltage rms Gain #define IgainL 0x32 // L Line Current rms Gain @@ -61,7 +61,7 @@ #define PoffsetN 0x39 // N Line Active Power Offset #define QoffsetN 0x3A // N Line Reactive Power Offset #define CSTwo 0x3B // Checksum 2 -#define CfgRegLen2 10 // Length of register for Checksum 2 +#define CfgRegLen2 10 // Amount of register for Checksum 2 #define APenergy 0x40 // Forward Active Energy #define ANenergy 0x41 // Reverse Active Energy #define ATenergy 0x42 // Absolute Active Energy