Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 35 additions & 22 deletions energyic_UART.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,48 +137,61 @@ unsigned short ATM90E26_UART::GetSysStatus() {
return CommEnergyIC(1, SysStatus, 0xFFFF);
}

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++) {
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
*/
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
CommEnergyIC(0, SagTh, 0x1F2F); // Voltage sag threshhold

// 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
// This loop iterates though above configurations from 21H-2BH registers.
for (int i = 0; i < CfgRegLen1; i++) {
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(
CommEnergyIC(1, CSOne, 0x0000),
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
// This loop iterates though above configurations from 31H-3AH registers.
for (int i = 0; i < CfgRegLen2; 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:");
Serial.println(
Expand Down
3 changes: 3 additions & 0 deletions energyic_UART.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 // 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
Expand All @@ -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 // Amount of register for Checksum 2
#define APenergy 0x40 // Forward Active Energy
#define ANenergy 0x41 // Reverse Active Energy
#define ATenergy 0x42 // Absolute Active Energy
Expand Down Expand Up @@ -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,
Expand Down