Skip to content

Commit 073df67

Browse files
committed
Fix MMode0 register binary work fixed
1 parent 0ab6d1f commit 073df67

File tree

2 files changed

+85
-79
lines changed

2 files changed

+85
-79
lines changed

MCM_ATM90E32.cpp

Lines changed: 83 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ double ATM90E32::CalculateVIOffset(unsigned short regh_addr, unsigned short regl
176176

177177
offset = val; // keep lower 16 bits
178178

179-
//CommEnergyIC(WRITE, CfgRegAccEn, 0x55AA); // 7F enable register config access
180-
//CommEnergyIC(WRITE, offset_reg, offset);
181-
//CommEnergyIC(WRITE, CfgRegAccEn, 0x0000); // 7F end configuration
179+
// CommEnergyIC(WRITE, CfgRegAccEn, 0x55AA); // 7F enable register config access
180+
// CommEnergyIC(WRITE, offset_reg, offset);
181+
// CommEnergyIC(WRITE, CfgRegAccEn, 0x0000); // 7F end configuration
182182

183183
Serial.print(", Offset: ");
184184
Serial.println(offset);
@@ -217,9 +217,9 @@ double ATM90E32::CalculatePowerOffset(unsigned short regh_addr, unsigned short r
217217

218218
offset = val; // keep lower 16 bits
219219

220-
//CommEnergyIC(WRITE, CfgRegAccEn, 0x55AA); // 7F enable register config access
221-
//CommEnergyIC(WRITE, offset_reg, offset);
222-
//CommEnergyIC(WRITE, CfgRegAccEn, 0x0000); // 7F end configuration
220+
// CommEnergyIC(WRITE, CfgRegAccEn, 0x55AA); // 7F enable register config access
221+
// CommEnergyIC(WRITE, offset_reg, offset);
222+
// CommEnergyIC(WRITE, CfgRegAccEn, 0x0000); // 7F end configuration
223223

224224
Serial.print(", Offset configured:");
225225
Serial.println(offset);
@@ -610,15 +610,16 @@ bool GainIsValid(int value)
610610
- Set serialFlag to true for serial debugging
611611
- Use SPI MODE 0 for the ATM90E32
612612
*/
613-
void ATM90E32::begin(int pin, unsigned short lineFreq, unsigned short sumMode, unsigned short iagain, unsigned short ibgain, unsigned short icgain, unsigned short ucal, unsigned short icalA, unsigned short icalB, unsigned short icalC)
613+
void ATM90E32::begin(int pin, unsigned short lineFreq, unsigned short phases, unsigned short sumMode, unsigned short iagain, unsigned short ibgain, unsigned short icgain, unsigned short ucal, unsigned short icalA, unsigned short icalB, unsigned short icalC)
614614
{
615615
_cs = pin; // SS PIN
616616
_lineFreq = lineFreq; // frequency of grid
617+
_phases = phases; // Split-phase or 3-Phase system
617618
_sumMode = sumMode; // addition mode of energy
618619
_iagain = iagain; // PGA Gain for current channel A
619620
_ibgain = ibgain; // PGA Gain for current channel B
620621
_icgain = icgain; // PGA Gain for current channel C
621-
_ucal = ucal; // voltage rms gain
622+
_ucal = ucal; // voltage RMS gain
622623
_icalA = icalA; // CT1 for single split phase meter
623624
_icalB = icalB; // CT2, not used for single split phase meter
624625
_icalC = icalC; // CT3, used as CT2 for single split phase meter
@@ -642,100 +643,104 @@ void ATM90E32::begin(int pin, unsigned short lineFreq, unsigned short sumMode, u
642643
unsigned short FreqHiThresh;
643644
unsigned short FreqLoThresh;
644645

645-
uint16_t config = 0b0000000110011101; // 16-bit unsigned variable
646+
// --- config layout (bits) ---
647+
// [12] : line freq (0=50 Hz, 1=60 Hz)
648+
// [8] : phase flag (1=split-phase, 0=3-phase)
649+
// [4:3] : sum mode (00=arithmetic, 11=absolute) // per your code
650+
// [2:0] : phase mode (101=split-phase, 111=3-phase)
646651

647-
if (_lineFreq != 60 && _lineFreq != 50)
648-
{
649-
Serial.println("Not supported frequency selected");
650-
Serial.println("Using default of 50 Hz");
652+
// --- gain packing in current_gain ---
653+
// [1:0] IA, [3:2] IB, [5:4] IC (00=1x, 01=2x, 10=4x)
651654

652-
sagV = 190;
653-
FreqHiThresh = 51 * 100;
654-
FreqLoThresh = 49 * 100;
655-
656-
config |= (0 << 12);
657-
}
658-
else if (_lineFreq == 50)
655+
auto gainCode = [](unsigned short g) -> uint8_t
659656
{
660-
sagV = 190;
661-
FreqHiThresh = 51 * 100;
662-
FreqLoThresh = 49 * 100;
663-
664-
config |= (0 << 12);
665-
}
666-
else if (_lineFreq == 60)
657+
// map 1->00, 2->01, 4->10 ; default to 00 if invalid
658+
if (g == 1)
659+
return 0b00;
660+
if (g == 2)
661+
return 0b01;
662+
if (g == 4)
663+
return 0b10;
664+
return 0b00;
665+
};
666+
667+
uint16_t config = 0b0000000110011101; // 0x019D
668+
669+
// ---- LINE FREQ bit12
670+
config &= ~(1u << 12);
671+
if (_lineFreq == 60)
667672
{
668673
sagV = 90;
669674
FreqHiThresh = 61 * 100;
670675
FreqLoThresh = 59 * 100;
671-
672-
config |= (1 << 12);
673-
}
674-
675-
if (_sumMode)
676-
{
677-
config |= (0b11 << 3);
676+
config |= (1u << 12);
678677
}
679678
else
680-
{
681-
config |= (0b00 << 3);
679+
{ // default 50 Hz for invalid or 50
680+
if (_lineFreq != 50)
681+
{
682+
Serial.println("Not supported frequency selected");
683+
Serial.println("Using default of 50 Hz");
684+
}
685+
sagV = 190;
686+
FreqHiThresh = 51 * 100;
687+
FreqLoThresh = 49 * 100;
682688
}
683689

690+
// ---- SUM MODE bits [4:3]
691+
config &= ~(0b11u << 3);
692+
config |= ((_sumMode ? 0b11u : 0b00u) << 3);
693+
694+
// Compute sag threshold (unchanged math)
684695
vSagTh = (sagV * 100 * sqrt(2)) / (2 * _ucal / 32768);
685696

686-
uint8_t current_gain = 0b000000; // 16-bit unsigned variable
697+
// ---- CURRENT GAINS packed into current_gain
698+
uint8_t current_gain = 0;
699+
uint8_t ia = gainCode(_iagain);
700+
uint8_t ib = gainCode(_ibgain);
701+
uint8_t ic = gainCode(_icgain);
687702

688-
if (GainIsValid(_iagain))
689-
{
690-
if (_iagain == 4)
691-
{
692-
current_gain |= (_iagain - 2 << 0);
693-
}
694-
else
695-
{
696-
current_gain |= (_iagain - 1 << 0);
697-
}
698-
}
699-
else
700-
{
703+
if (ia == 0b00 && _iagain != 1)
701704
Serial.println("IA gain is not valid, using default 1x");
702-
current_gain |= (0 << 0);
703-
}
705+
if (ib == 0b00 && _ibgain != 1)
706+
Serial.println("IB gain is not valid, using default 1x");
707+
if (ic == 0b00 && _icgain != 1)
708+
Serial.println("IC gain is not valid, using default 1x");
704709

705-
if (GainIsValid(_ibgain))
706-
{
707-
if (_ibgain == 4)
708-
{
709-
current_gain |= (_ibgain - 2 << 2);
710-
}
711-
else
712-
{
713-
current_gain |= (_ibgain - 1 << 2);
714-
}
715-
}
716-
else
710+
// place IA at [1:0], IB at [3:2], IC at [5:4]
711+
current_gain |= (ia << 0);
712+
current_gain |= (ib << 2);
713+
current_gain |= (ic << 4);
714+
715+
// ---- PHASE MODE: bits [2:0] and bit8 flag
716+
config &= ~0b111u; // clear bits 2..0
717+
config &= ~(1u << 8); // clear bit 8 first
718+
719+
if (_phases == 2)
717720
{
718-
Serial.println("IB gain is not valid, using default 1x");
719-
current_gain |= (0 << 2);
721+
Serial.println("Split-Phase Mode");
722+
config |= 0b101u; // bits 2..0
723+
config |= (1u << 8); // bit8 = 1 for split-phase
720724
}
721-
722-
if (GainIsValid(_icgain))
725+
else if (_phases == 3)
723726
{
724-
if (_icgain == 4)
725-
{
726-
current_gain |= (_icgain - 2 << 4);
727-
}
728-
else
729-
{
730-
current_gain |= (_icgain - 1 << 4);
731-
}
727+
Serial.println("3-Phase Mode");
728+
config |= 0b111u; // bits 2..0
729+
// bit8 remains 0 for 3-phase
732730
}
733731
else
734732
{
735-
Serial.println("IC gain is not valid, using default 1x");
736-
current_gain |= (0 << 4);
733+
Serial.println("IC phase mode out of range, using default Split-phase");
734+
config |= 0b101u;
735+
config |= (1u << 8);
737736
}
738737

738+
// --- print as 16 bits with leading zeros
739+
Serial.print("MMODE: ");
740+
for (int i = 15; i >= 0; --i)
741+
Serial.print((config >> i) & 1);
742+
Serial.println();
743+
739744
// Initialize registers
740745
CommEnergyIC(WRITE, SoftReset, 0x789A); // 70 Perform soft reset
741746
CommEnergyIC(WRITE, CfgRegAccEn, 0x55AA); // 7F enable register config access

MCM_ATM90E32.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ class ATM90E32
246246
unsigned short CommEnergyIC(unsigned char RW, unsigned short address, unsigned short val);
247247
int _cs;
248248
unsigned short _lineFreq;
249+
unsigned short _phases;
249250
unsigned short _sumMode;
250251
unsigned short _iagain;
251252
unsigned short _ibgain;
@@ -264,7 +265,7 @@ class ATM90E32
264265
~ATM90E32(void);
265266

266267
/* Initialization Functions */
267-
void begin(int pin, unsigned short lineFreq, unsigned short sumMode, unsigned short iagain, unsigned short ibgain, unsigned short icgain, unsigned short ucal, unsigned short icalA, unsigned short icalB, unsigned short icalC);
268+
void begin(int pin, unsigned short lineFreq, unsigned short phases, unsigned short sumMode, unsigned short iagain, unsigned short ibgain, unsigned short icgain, unsigned short ucal, unsigned short icalA, unsigned short icalB, unsigned short icalC);
268269

269270
double CalculateVIOffset(unsigned short regh_addr, unsigned short regl_addr, unsigned short offset_reg);
270271
double CalculatePowerOffset(unsigned short regh_addr, unsigned short regl_addr, unsigned short offset_reg);

0 commit comments

Comments
 (0)