From 77ae2b6d576847c39f29a758a757c9353d96d6d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mehmet=20Gu=CC=88nce=20Akkoyun?= Date: Mon, 18 Mar 2024 16:00:43 +0300 Subject: [PATCH] Function update --- library.json | 2 +- library.properties | 2 +- src/Config.h | 24 + src/Definitions.h | 14 +- src/MAX78630.h | 2502 ++++++++++++++++++++------------------------ 5 files changed, 1161 insertions(+), 1383 deletions(-) diff --git a/library.json b/library.json index 45739eb..d38c9cd 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "MAX78630", - "version": "4.10.0", + "version": "4.11.0", "keywords": "MAX78630, Maxim, Silergy, Energy, Electric, Poly, Phase, Metering, Measure, Active, Reactive, Apparent, Voltage, Current, Power Factor, Cos Fi, Temperature, Fundamental, Harmonic, Defect, Alarm, Min, Max", "description": "The EnergyBoard is an energy measurement module for poly phase power-monitoring systems. It is designed for real-time monitoring for a variety of typical three-phase configurations in industrial applications. The EnergyBoard provides up to six analog inputs for interfacing to voltage and current sensors. Scaled voltages from the sensors are fed to the single converter front-end using a high-resolution delta-sigma converter. Supported current sensors include current transformers (CTs), Rogowski coils, and resistive shunts.", "authors": diff --git a/library.properties b/library.properties index a0fa68b..5c775c3 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=MAX78630 -version=4.10.0 +version=4.11.0 author=Gunce Akkoyun maintainer=Gunce Akkoyun sentence=Enables reading polyphase energy-monitor parameters of LVX EnergyBoard. diff --git a/src/Config.h b/src/Config.h index a34efed..bc1a1a8 100644 --- a/src/Config.h +++ b/src/Config.h @@ -108,38 +108,62 @@ #ifndef __MAX78630_Monitor_1_Type__ #define __MAX78630_Monitor_1_Type__ (uint8_t)0x30 // 0x30 = VRMS_R #endif +#ifndef __MAX78630_Monitor_1_Scale__ + #define __MAX78630_Monitor_1_Scale__ __MAX78630_Config_VScale__ +#endif // Monitor 2 #ifndef __MAX78630_Monitor_2_Type__ #define __MAX78630_Monitor_2_Type__ (uint8_t)0x31 // 0x31 = VRMS_S #endif +#ifndef __MAX78630_Monitor_2_Scale__ + #define __MAX78630_Monitor_2_Scale__ __MAX78630_Config_VScale__ +#endif // Monitor 3 #ifndef __MAX78630_Monitor_3_Type__ #define __MAX78630_Monitor_3_Type__ (uint8_t)0x32 // 0x32 = VRMS_T #endif +#ifndef __MAX78630_Monitor_3_Scale__ + #define __MAX78630_Monitor_3_Scale__ __MAX78630_Config_VScale__ +#endif // Monitor 4 #ifndef __MAX78630_Monitor_4_Type__ #define __MAX78630_Monitor_4_Type__ (uint8_t)0x47 // 0x47 = IRMS_R #endif +#ifndef __MAX78630_Monitor_4_Scale__ + #define __MAX78630_Monitor_4_Scale__ __MAX78630_Config_IScale__ +#endif // Monitor 5 #ifndef __MAX78630_Monitor_5_Type__ #define __MAX78630_Monitor_5_Type__ (uint8_t)0x48 // 0x48 = IRMS_S #endif +#ifndef __MAX78630_Monitor_5_Scale__ + #define __MAX78630_Monitor_5_Scale__ __MAX78630_Config_IScale__ +#endif // Monitor 6 #ifndef __MAX78630_Monitor_6_Type__ #define __MAX78630_Monitor_6_Type__ (uint8_t)0x49 // 0x49 = IRMS_T #endif +#ifndef __MAX78630_Monitor_6_Scale__ + #define __MAX78630_Monitor_6_Scale__ __MAX78630_Config_IScale__ +#endif // Monitor 7 #ifndef __MAX78630_Monitor_7_Type__ #define __MAX78630_Monitor_7_Type__ (uint8_t)0x7A // 0x7A = PF_T #endif +#ifndef __MAX78630_Monitor_7_Scale__ + #define __MAX78630_Monitor_7_Scale__ (uint16_t)1 +#endif // Monitor 8 #ifndef __MAX78630_Monitor_8_Type__ #define __MAX78630_Monitor_8_Type__ (uint8_t)0x80 // 0x80 = FREQ +#endif +#ifndef __MAX78630_Monitor_8_Scale__ + #define __MAX78630_Monitor_8_Scale__ (uint16_t)1 #endif \ No newline at end of file diff --git a/src/Definitions.h b/src/Definitions.h index 9ac654b..f30afaf 100644 --- a/src/Definitions.h +++ b/src/Definitions.h @@ -1,6 +1,7 @@ // Function Definitions -#define __MAX78630_GET__ (bool)true -#define __MAX78630_SET__ (bool)false +#define __MAX78630_CLEAR__ (uint8_t)0 +#define __MAX78630_GET__ (uint8_t)1 +#define __MAX78630_SET__ (uint8_t)2 // Phase Type #define __Phase_R__ (uint8_t)1 @@ -26,7 +27,14 @@ #define __ReActive_Received__ (uint8_t)14 #define __ReActive_Delivered__ (uint8_t)15 #define __Energy_Reset__ (uint8_t)16 -#define __Phase_Compensation__ (uint8_t)16 +#define __Phase_Compensation__ (uint8_t)17 + +// Calibration Type +#define __Voltage__ (uint8_t)1 +#define __Current__ (uint8_t)2 +#define __Temperature__ (uint8_t)3 +#define __Active_Power__ (uint8_t)4 +#define __ReActive_Power__ (uint8_t)5 // Limit Type #define __VRMS_MIN__ (uint8_t)1 diff --git a/src/MAX78630.h b/src/MAX78630.h index 4c1313a..9e11429 100644 --- a/src/MAX78630.h +++ b/src/MAX78630.h @@ -25,6 +25,19 @@ uint32_t Status = 0; } Device; + // Limit Variables + struct Limit_Struct { + float V_Min = __MAX78630_Limit_Voltage_Min__; + float V_Max = __MAX78630_Limit_Voltage_Max__; + float I_Max = __MAX78630_Limit_Current_Max__; + float FQ_Min = __MAX78630_Limit_Frequency_Min__; + float FQ_Max = __MAX78630_Limit_Frequency_Max__; + float VImb_Max = __MAX78630_Limit_VImb_Max__; + float IImb_Max = __MAX78630_Limit_IImb_Max__; + float T_Min = __MAX78630_Limit_Temperature_Min__; + float T_Max = __MAX78630_Limit_Temperature_Max__; + } Limit_Variables; + // IC Register Address Structure struct Register { const uint8_t High_Address; @@ -175,165 +188,151 @@ } - // Select IC Function - bool Select_IC(uint8_t _Address) { + // Data Converter Function + uint32_t FtoS(double _Variable, uint8_t _Data_Type) { - // Command Send Delay - delay(5); + // Declare Data Variable + uint32_t _Data = 0x00; - // Send Command - Serial_Energy->write(0xAA); // Header (0xAA) - Serial_Energy->write(0x04); // Total Sended Byte (0x04) - Serial_Energy->write(_Address); // IC Address (0xC1, 0xC2, 0xC3, 0xC4) - Serial_Energy->write(0x100 - ((0xAA + 0x04 + _Address) % 256)); // CheckSum (0x__) + // Convert Loop + for (uint8_t i = 0; i < 24; i++) { + + // Calculate Bit Multiplier + double _Bit_Multiplier = pow(2, (-_Data_Type - (i - 23))); - // Command Send Delay - delay(20); + // Control for Bit + if (_Variable >= _Bit_Multiplier) { + + // Set Bit Control + bitSet(_Data, 23 - i); - // End Function - if (Serial_Energy->read() == 0xAD) return(true); + // Handle Data + _Variable -= _Bit_Multiplier; - // End Function - return(false); + } else { - } + // Set Bit Control + bitClear(_Data, 23 - i); - // Set Voltage Scale Function - bool Set_VScale(const uint32_t _Voltage_Scale) { + } - // Define Register - Register VSCALE {0x01, 0x44, 0}; // Scratch register (see Scaling Registers section) + } - // Set Register - return(this->Register_Pointer_Set(VSCALE, _Voltage_Scale)); + // End Function + return(_Data); } - // Set Current Scale Function - bool Set_IScale(const uint32_t _Current_Scale) { - - // Define Register - Register IFSCALE {0x01, 0x41, 0}; // Scratch register (see Scaling Registers section) + /* Scale Config Functions */ - // Set Register - return(this->Register_Pointer_Set(IFSCALE, _Current_Scale)); + // VScale Function + uint32_t VScale(const uint8_t _Function = __MAX78630_SET__) { - } + // Define Register + Register VSCALE {0x01, 0x44, 0}; // Scratch register (see Scaling Registers section) - // Bucket Size Set Function. - bool Set_Bucket(const uint32_t _Bucket_H, const uint32_t _Bucket_L) { + // Control for Function + if (_Function == __MAX78630_SET__) { - // Define Register - Register BUCKET_HIGH {0x01, 0xD4, 0}; // Energy Bucket Size – High word - Register BUCKET_LOW {0x01, 0xD1, 0}; // Energy Bucket Size – Low word + // Set Register + return(this->Register_Pointer_Set(VSCALE, __MAX78630_Config_VScale__)); - // End Function - return(this->Register_Pointer_Set(BUCKET_LOW, _Bucket_L) && this->Register_Pointer_Set(BUCKET_HIGH, _Bucket_H)); + } else if (_Function == __MAX78630_GET__) { - } + // Read Register + return(this->Register_Pointer_Read(VSCALE)); - // Set Min-Max Control Address - bool Set_Min_Max_Address(const uint8_t _MM_ADDR, const uint32_t _Mask) { + } else { - // Decide Address - switch (_MM_ADDR) { + // End Function + return(false); - case __MAX78630_MONITOR_1__: { + } - // Define Register - Register MMADDR0 {0x01, 0xB9, 0}; // Min/Max Monitor address 1 + // End Function + return(false); - // Get Register - return(this->Register_Pointer_Set(MMADDR0, _Mask)); + } - // End Case - break; + // IScale Function + uint32_t IScale(const uint8_t _Function = __MAX78630_SET__) { - } - case __MAX78630_MONITOR_2__: { + // Define Register + Register ISCALE {0x01, 0x41, 0}; // Scratch register (see Scaling Registers section) - // Define Register - Register MMADDR1 {0x01, 0xBC, 0}; // Min/Max Monitor address 2 + // Control for Function + if (_Function == __MAX78630_SET__) { - // Get Register - return(this->Register_Pointer_Set(MMADDR1, _Mask)); + // Set Register + return(this->Register_Pointer_Set(ISCALE, __MAX78630_Config_IScale__)); - // End Case - break; + } else if (_Function == __MAX78630_GET__) { - } - case __MAX78630_MONITOR_3__: { + // Read Register + return(this->Register_Pointer_Read(ISCALE)); - // Define Register - Register MMADDR2 {0x01, 0xBF, 0}; // Min/Max Monitor address 3 + } else { - // Get Register - return(this->Register_Pointer_Set(MMADDR2, _Mask)); + // End Function + return(false); - // End Case - break; + } - } - case __MAX78630_MONITOR_4__: { + // End Function + return(false); - // Define Register - Register MMADDR3 {0x01, 0xC2, 0}; // Min/Max Monitor address 4 + } - // Get Register - return(this->Register_Pointer_Set(MMADDR3, _Mask)); + // Bucket High Function + uint32_t Bucket_H(const uint8_t _Function = __MAX78630_SET__) { - // End Case - break; + // Define Register + Register BUCKET_HIGH {0x01, 0xD4, 0}; // Energy Bucket Size – High word - } - case __MAX78630_MONITOR_5__: { + // Control for Function + if (_Function == __MAX78630_SET__) { - // Define Register - Register MMADDR4 {0x01, 0xC5, 0}; // Min/Max Monitor address 5 + // Set Register + return(this->Register_Pointer_Set(BUCKET_HIGH, __MAX78630_Config_BucketH__)); - // Get Register - return(this->Register_Pointer_Set(MMADDR4, _Mask)); + } else if (_Function == __MAX78630_GET__) { - // End Case - break; + // Read Register + return(this->Register_Pointer_Read(BUCKET_HIGH)); - } - case __MAX78630_MONITOR_6__: { + } else { - // Define Register - Register MMADDR5 {0x01, 0xC8, 0}; // Min/Max Monitor address 6 + // End Function + return(false); - // Get Register - return(this->Register_Pointer_Set(MMADDR5, _Mask)); + } - // End Case - break; + // End Function + return(false); - } - case __MAX78630_MONITOR_7__: { + } - // Define Register - Register MMADDR6 {0x01, 0xCB, 0}; // Min/Max Monitor address 7 + // Bucket Low Function + uint32_t Bucket_L(const uint8_t _Function = __MAX78630_SET__) { - // Get Register - return(this->Register_Pointer_Set(MMADDR6, _Mask)); + // Define Register + Register BUCKET_LOW {0x01, 0xD1, 0}; // Energy Bucket Size – Low word - // End Case - break; + // Control for Function + if (_Function == __MAX78630_SET__) { - } - case __MAX78630_MONITOR_8__: { + // Set Register + return(this->Register_Pointer_Set(BUCKET_LOW, __MAX78630_Config_BucketL__)); - // Define Register - Register MMADDR7 {0x01, 0xCE, 0}; // Min/Max Monitor address 8 + } else if (_Function == __MAX78630_GET__) { - // Get Register - return(this->Register_Pointer_Set(MMADDR7, _Mask)); + // Read Register + return(this->Register_Pointer_Read(BUCKET_LOW)); - // End Case - break; + } else { - } + // End Function + return(false); } @@ -342,130 +341,143 @@ } - // Clear Min Value From Address - bool Clear_Min_Value(const uint8_t _MM_ADDR) { + /* Min Max Functions */ + + // Set Min-Max Control Address + bool Set_Min_Max_Mask(const uint8_t _MM_ADDR) { + + // Control for Mask + if (_MM_ADDR > 8) return(false); // Decide Address switch (_MM_ADDR) { - case 1: { + case __MAX78630_MONITOR_1__: { // Define Register - Register MIN0 {0x01, 0x89, 23}; // Minimum Recorded Value 1 + Register MMADDR0 {0x01, 0xB9, 0}; // Min/Max Monitor address 1 + + // Set Register + bool _Set = this->Register_Pointer_Set(MMADDR0, __MAX78630_Monitor_1_Type__); // Clear Register - Register_Pointer_Set(MIN0, 0x00); + this->Min_Value(__MAX78630_CLEAR__, __MAX78630_MONITOR_1__, __MAX78630_Monitor_1_Scale__); + this->Max_Value(__MAX78630_CLEAR__, __MAX78630_MONITOR_1__, __MAX78630_Monitor_1_Scale__); // End Function - return(true); - - // End Case - break; + return(_Set); } - case 2: { + case __MAX78630_MONITOR_2__: { // Define Register - Register MIN1 {0x01, 0x8C, 23}; // Minimum Recorded Value 2 + Register MMADDR1 {0x01, 0xBC, 0}; // Min/Max Monitor address 2 + + // Set Register + bool _Set = this->Register_Pointer_Set(MMADDR1, __MAX78630_Monitor_2_Type__); // Clear Register - Register_Pointer_Set(MIN1, 0x00); + this->Min_Value(__MAX78630_CLEAR__, __MAX78630_MONITOR_2__, __MAX78630_Monitor_2_Scale__); + this->Max_Value(__MAX78630_CLEAR__, __MAX78630_MONITOR_2__, __MAX78630_Monitor_2_Scale__); // End Function - return(true); - - // End Case - break; + return(_Set); } - case 3: { + case __MAX78630_MONITOR_3__: { // Define Register - Register MIN2 {0x01, 0x8F, 23}; // Minimum Recorded Value 3 + Register MMADDR2 {0x01, 0xBF, 0}; // Min/Max Monitor address 3 + + // Set Register + bool _Set = this->Register_Pointer_Set(MMADDR2, __MAX78630_Monitor_3_Type__); // Clear Register - Register_Pointer_Set(MIN2, 0x00); + this->Min_Value(__MAX78630_CLEAR__, __MAX78630_MONITOR_3__, __MAX78630_Monitor_3_Scale__); + this->Max_Value(__MAX78630_CLEAR__, __MAX78630_MONITOR_3__, __MAX78630_Monitor_3_Scale__); // End Function - return(true); - - // End Case - break; + return(_Set); } - case 4: { + case __MAX78630_MONITOR_4__: { // Define Register - Register MIN3 {0x01, 0x92, 23}; // Minimum Recorded Value 4 + Register MMADDR3 {0x01, 0xC2, 0}; // Min/Max Monitor address 4 + + // Set Register + bool _Set = this->Register_Pointer_Set(MMADDR3, __MAX78630_Monitor_4_Type__); // Clear Register - Register_Pointer_Set(MIN3, 0x00); + this->Min_Value(__MAX78630_CLEAR__, __MAX78630_MONITOR_4__, __MAX78630_Monitor_4_Scale__); + this->Max_Value(__MAX78630_CLEAR__, __MAX78630_MONITOR_4__, __MAX78630_Monitor_4_Scale__); // End Function - return(true); - - // End Case - break; + return(_Set); } - case 5: { + case __MAX78630_MONITOR_5__: { // Define Register - Register MIN4 {0x01, 0x95, 23}; // Minimum Recorded Value 5 + Register MMADDR4 {0x01, 0xC5, 0}; // Min/Max Monitor address 5 + + // Set Register + bool _Set = this->Register_Pointer_Set(MMADDR4, __MAX78630_Monitor_5_Type__); // Clear Register - Register_Pointer_Set(MIN4, 0x00); + this->Min_Value(__MAX78630_CLEAR__, __MAX78630_MONITOR_5__, __MAX78630_Monitor_5_Scale__); + this->Max_Value(__MAX78630_CLEAR__, __MAX78630_MONITOR_5__, __MAX78630_Monitor_5_Scale__); // End Function - return(true); - - // End Case - break; + return(_Set); } - case 6: { + case __MAX78630_MONITOR_6__: { // Define Register - Register MIN5 {0x01, 0x98, 23}; // Minimum Recorded Value 6 + Register MMADDR5 {0x01, 0xC8, 0}; // Min/Max Monitor address 6 + + // Set Register + bool _Set = this->Register_Pointer_Set(MMADDR5, __MAX78630_Monitor_6_Type__); // Clear Register - Register_Pointer_Set(MIN5, 0x00); + this->Min_Value(__MAX78630_CLEAR__, __MAX78630_MONITOR_6__, __MAX78630_Monitor_6_Scale__); + this->Max_Value(__MAX78630_CLEAR__, __MAX78630_MONITOR_6__, __MAX78630_Monitor_6_Scale__); // End Function - return(true); - - // End Case - break; + return(_Set); } - case 7: { + case __MAX78630_MONITOR_7__: { // Define Register - Register MIN6 {0x01, 0x9B, 0}; // Minimum Recorded Value 7 + Register MMADDR6 {0x01, 0xCB, 0}; // Min/Max Monitor address 7 + + // Set Register + bool _Set = this->Register_Pointer_Set(MMADDR6, __MAX78630_Monitor_7_Type__); // Clear Register - Register_Pointer_Set(MIN6, 0x00); + this->Min_Value(__MAX78630_CLEAR__, __MAX78630_MONITOR_7__, __MAX78630_Monitor_7_Scale__); + this->Max_Value(__MAX78630_CLEAR__, __MAX78630_MONITOR_7__, __MAX78630_Monitor_7_Scale__); // End Function - return(true); - - // End Case - break; + return(_Set); } - case 8: { + case __MAX78630_MONITOR_8__: { // Define Register - Register MIN7 {0x01, 0x9E, 0}; // Minimum Recorded Value 8 + Register MMADDR7 {0x01, 0xCE, 0}; // Min/Max Monitor address 8 + + // Set Register + bool _Set = this->Register_Pointer_Set(MMADDR7, __MAX78630_Monitor_8_Type__); // Clear Register - Register_Pointer_Set(MIN7, 0x00); + this->Min_Value(__MAX78630_CLEAR__, __MAX78630_MONITOR_8__, __MAX78630_Monitor_8_Scale__); + this->Max_Value(__MAX78630_CLEAR__, __MAX78630_MONITOR_8__, __MAX78630_Monitor_8_Scale__); // End Function - return(true); - - // End Case - break; + return(_Set); } @@ -476,1381 +488,1320 @@ } - // Clear Max Value From Address - bool Clear_Max_Value(const uint8_t _MM_ADDR) { + /* Alarm Functions */ - // Decide Address - switch (_MM_ADDR) { + // Alarm Sticky Interrupt Function. + bool Sticky(const uint8_t _Function = __MAX78630_GET__, const bool _Sticky = false) { - case 1: { + // Define Register + Register STICKY {0x00, 0x2D, 0}; - // Define Register - Register MAX0 {0x01, 0xA1, 23}; // Maximum Recorded Value 1 + // Get Current Register + uint32_t _Current_Sticky = this->Register_Pointer_Read(STICKY); - // Clear Register - Register_Pointer_Set(MAX0, 0x00); + // Control for Function + if (_Function == __MAX78630_SET__) { - // End Function - return(true); + // Set Sticky Bit + if (_Sticky) { - // End Case - break; + // Set Register + _Current_Sticky |= 0x7FFFFC; + + } else { + + // Set Register + _Current_Sticky &= 0xFF800003; } - case 2: { - // Define Register - Register MAX1 {0x01, 0xA4, 23}; // Maximum Recorded Value 2 + // End Function + this->Register_Pointer_Set(STICKY, _Current_Sticky); - // Clear Register - Register_Pointer_Set(MAX1, 0x00); + } - // End Function - return(true); + // End Function + return(bitRead(_Current_Sticky, 0) && bitRead(_Current_Sticky, 23)); - // End Case - break; - - } - case 3: { - - // Define Register - Register MAX2 {0x01, 0xA7, 23}; // Maximum Recorded Value 3 + } - // Clear Register - Register_Pointer_Set(MAX2, 0x00); + // Alarm Mask Function. + uint32_t Alarm_Mask(const uint8_t _Function = __MAX78630_GET__, const uint8_t _Mask, const uint8_t _Bit, const bool _Status) { - // End Function - return(true); + // Control for Mask + if (_Mask == __MASK_AL1__) { - // End Case - break; + // Define Register + Register MASK1 {0x00, 0x1E, 0}; // Alarm Mask address 1 - } - case 4: { + // Read Register + uint32_t _MASK = this->Register_Pointer_Read(MASK1); - // Define Register - Register MAX3 {0x01, 0xAA, 23}; // Maximum Recorded Value 4 + // Set Mask bit + if (_Function == __MAX78630_SET__) { - // Clear Register - Register_Pointer_Set(MAX3, 0x00); + // Set Mask bit + if (_Status) { - // End Function - return(true); + // Set Bit + bitSet(_MASK, _Bit); - // End Case - break; + } else { - } - case 5: { + // Clear Bit + bitClear(_MASK, _Bit); - // Define Register - Register MAX4 {0x01, 0xAD, 23}; // Maximum Recorded Value 5 + } - // Clear Register - Register_Pointer_Set(MAX4, 0x00); + // Write MASK1 Register + this->Register_Pointer_Set(MASK1, _MASK); // End Function - return(true); - - // End Case - break; + return(_MASK); } - case 6: { - - // Define Register - Register MAX5 {0x01, 0xB0, 23}; // Maximum Recorded Value 6 - // Clear Register - Register_Pointer_Set(MAX5, 0x00); + // End Function + return(_MASK); - // End Function - return(true); + } else if (_Mask == __MASK_AL2__) { - // End Case - break; + // Define Register + Register MASK2 {0x00, 0x21, 0}; // Alarm Mask address 2 - } - case 7: { + // Read Register + uint32_t _MASK = this->Register_Pointer_Read(MASK2); - // Define Register - Register MAX6 {0x01, 0xB3, 0}; // Maximum Recorded Value 7 + // Set Mask bit + if (_Function == __MAX78630_SET__) { - // Clear Register - Register_Pointer_Set(MAX6, 0x00); + // Set Mask bit + if (_Status) { - // End Function - return(true); + // Set Bit + bitSet(_MASK, _Bit); - // End Case - break; + } else { - } - case 8: { + // Clear Bit + bitClear(_MASK, _Bit); - // Define Register - Register MAX7 {0x01, 0xB6, 0}; // Maximum Recorded Value 8 + } - // Clear Register - Register_Pointer_Set(MAX7, 0x00); + // Write MASK2 Register + this->Register_Pointer_Set(MASK2, _MASK); // End Function - return(true); - - // End Case - break; + return(_MASK); } - } - - // End Function - return(false); - - } - - // Set Sticky Function - bool Set_Sticky(const bool _Sticky = false) { - - // Define Register - Register STICKY {0x00, 0x2D, 0}; - - // Get Current Register - uint32_t _Current_Sticky = this->Register_Pointer_Read(STICKY); - - // Set Sticky Bit - if (_Sticky) { - - // Set Register - _Current_Sticky |= 0x7FFFFC; + // End Function + return(_MASK); } else { - // Set Register - _Current_Sticky &= 0xFF800003; + // End Function + return(0); } // End Function - return(this->Register_Pointer_Set(STICKY, _Current_Sticky)); + return(0); } - // Set Alarm Mask - bool Set_Alarm_Mask(const uint8_t _Mask_Addres, const uint8_t _Mask, const bool _Status) { - - // Decide Address - switch (_Mask_Addres) { - - case __MAX78630_ALARM_1__: { - - // Define Register - Register MASK1 {0x00, 0x1E, 0}; // Alarm Mask address 1 - // Read MASK1 Register - uint32_t _MASK1_Current = this->Register_Pointer_Read(MASK1); + // Protected Context + protected: - // Set Mask bit - if (_Status) { - - // Set Bit - bitSet(_MASK1_Current, _Mask); - - } else { - - // Clear Bit - bitClear(_MASK1_Current, _Mask); - - } + /* IC Utility Functions */ - // Write MASK1 Register - return(this->Register_Pointer_Set(MASK1, _MASK1_Current)); + // Get Device Firmware Function + uint32_t Firmware(void) { - // End Case - break; + // Define Register + Register FW_VERSION {0x00, 0x03, 0}; // Hardware and firmware version - } - case __MAX78630_ALARM_2__: { + // Return Register + return(this->Register_Pointer_Read(FW_VERSION)); - // Define Register - Register MASK2 {0x00, 0x21, 0}; // Alarm Mask address 2 + // Default Firmware: 0x0004D912 - // Read MASK2 Register - uint32_t _MASK2_Current = this->Register_Pointer_Read(MASK2); + } - // Set Mask bit - if (_Status) { + // Get Device Address Function. + uint32_t Address(void) { - // Set Bit - bitSet(_MASK2_Current, _Mask); + // Define Register + Register DEVADDR {0x00, 0x4E, 0}; // High order address bits for I2C and UART interfaces - } else { + // Return Register + return(this->Register_Pointer_Read(DEVADDR)); - // Clear Bit - bitClear(_MASK2_Current, _Mask); + } - } + // Select IC Function + bool Select_IC(uint8_t _Address) { - // Get Register - return(this->Register_Pointer_Set(MASK2, _MASK2_Current)); + // Command Send Delay + delay(5); - // End Case - break; + // Send Command + Serial_Energy->write(0xAA); // Header (0xAA) + Serial_Energy->write(0x04); // Total Sended Byte (0x04) + Serial_Energy->write(_Address); // IC Address (0xC1, 0xC2, 0xC3, 0xC4) + Serial_Energy->write(0x100 - ((0xAA + 0x04 + _Address) % 256)); // CheckSum (0x__) - } + // Command Send Delay + delay(20); - } + // End Function + if (Serial_Energy->read() == 0xAD) return(true); // End Function return(false); } - // Data Converter Function - uint32_t FtoS(double _Variable, uint8_t _Data_Type) { - - // Declare Data Variable - uint32_t _Data = 0x00; - - // Convert Loop - for (uint8_t i = 0; i < 24; i++) { - - // Calculate Bit Multiplier - double _Bit_Multiplier = pow(2, (-_Data_Type - (i - 23))); + // Baud Rate Function + uint32_t Baud(const uint8_t _Function = __MAX78630_GET__, const uint32_t _Baud) { - // Control for Bit - if (_Variable >= _Bit_Multiplier) { - - // Set Bit Control - bitSet(_Data, 23 - i); + // Define Register + Register BAUD {0x00, 0x51, 0}; // Baud rate for UART interface - // Handle Data - _Variable -= _Bit_Multiplier; + // Control for Function + if (_Function == __MAX78630_SET__) { - } else { + // Set Register + return(this->Register_Pointer_Set(BAUD, _Baud)); - // Set Bit Control - bitClear(_Data, 23 - i); + } else if (_Function == __MAX78630_GET__) { - } + // Read Register + return(this->Register_Pointer_Read(BAUD)); } // End Function - return(_Data); + return(false); } - // Public Context - public: + // Get Device Frame Rate Function. + uint32_t Frame(void) { - // Limit Variables - struct Limit_Struct { - float V_Min = __MAX78630_Limit_Voltage_Min__; - float V_Max = __MAX78630_Limit_Voltage_Max__; - float I_Max = __MAX78630_Limit_Current_Max__; - float FQ_Min = __MAX78630_Limit_Frequency_Min__; - float FQ_Max = __MAX78630_Limit_Frequency_Max__; - float VImb_Max = __MAX78630_Limit_VImb_Max__; - float IImb_Max = __MAX78630_Limit_IImb_Max__; - float T_Min = __MAX78630_Limit_Temperature_Min__; - float T_Max = __MAX78630_Limit_Temperature_Max__; - } Limit; + // Define Register + Register FRAME {0x00, 0x12, 0}; // Low-rate sample counter - // Constructor - explicit MAX78630(Stream &_Serial) { - - // Set Serial Port - Serial_Energy = & _Serial; + // Return Register + return(this->Register_Pointer_Read(FRAME)); } - // Begin and Set Start Parameters. - bool Begin(const uint8_t _Address = 4) { - - // Clear Serial Buffer - this->Clear_Buffer(); - - // Declare Device Address - uint8_t _Device_Address = 0xC0; - - // Set Device Address - if (_Address == 0) _Device_Address = 0xC0; - if (_Address == 1) _Device_Address = 0xC1; - if (_Address == 2) _Device_Address = 0xC2; - if (_Address == 3) _Device_Address = 0xC3; - if (_Address == 4) _Device_Address = 0xC4; - - // Control Connection - if (this->Select_IC(_Device_Address)) { - - // Control Firmware - if (this->Get_Firmware() == __MAX78630_Firmware__) { - - // Get Status - this->Get_Status(); - - // Set Device Found - this->Device.Found = true; - - } else { + // Get Sample Counter Function + uint32_t Cycle(void) { - // Set Device Found - this->Device.Found = false; + // Define Register + Register CYCLE {0x00, 0x0F, 0}; // High-rate sample counter - } + // Return Register + return(this->Register_Pointer_Read(CYCLE)); - } else { + } - // Set Device Found - this->Device.Found = false; + // Get Divisor Value Function + uint32_t Divisor(void) { - } + // Define Register + Register DIVISOR {0x00, 0x0C, 0}; // Actual samples in previous accumulation interval - // End Function - return(this->Device.Found); + // Return Register + return(this->Register_Pointer_Read(DIVISOR)); } - // Get Device Firmware Function - uint32_t Get_Firmware(void) { + // Get Samples per Accumulation Interval Function. + uint32_t Samples(void) { // Define Register - Register FW_VERSION {0x00, 0x03, 0}; // Hardware and firmware version + Register SAMPLES {0x00, 0x09, 0}; // Minimum high-rate samples per accumulation interval // Return Register - return(this->Register_Pointer_Read(FW_VERSION)); - - // Default Firmware: 0x0004D912 + return(this->Register_Pointer_Read(SAMPLES)); } - // Setup Device Function - bool Setup(const bool _Scale = true, const bool _Bucket = true, const bool _Limit = true, const bool _MinMax = true, const bool _Alarm = true) { + // Get System Status Function. + uint32_t System_Status(void) { - // Control for Device - if (this->Device.Found) { + // Define Register + Register SYSSTAT {0x02, 0x43, 0}; // Bit 23 is a sticky register with status of any SPI Errors - // Set Scale - if (_Scale) { + // Return Register + return(this->Register_Pointer_Read(SYSSTAT)); - // Set Voltage Scale - this->Set_VScale(__MAX78630_Config_VScale__); + } - // Set Current Scale - this->Set_IScale(__MAX78630_Config_IScale__); + /* Min Max Functions */ - } + // Min Record Value Function + float Min_Value(const bool _Function = __MAX78630_GET__, const uint8_t _MM_ADDR, const uint16_t _Scale = 1) { - // Set Bucket - if (_Bucket) { + // Control for Function + if (_Function == __MAX78630_SET__) return(0); - // Set Bucket Size - this->Set_Bucket(__MAX78630_Config_BucketH__, __MAX78630_Config_BucketL__); + // Control for Mask + if (_MM_ADDR > 8) return(0); - } + // Decide Address + switch (_MM_ADDR) { - // Set Limit - if (_Limit) { + case __MAX78630_MONITOR_1__: { - // Set Voltage Min Limit - this->Set_Limit(__VRMS_MIN__, this->Limit.V_Min); + // Define Register + Register MIN0 {0x01, 0x89, 23}; // Minimum Recorded Value 1 - // Set Voltage Max Limit - this->Set_Limit(__VRMS_MAX__, this->Limit.V_Max); + // Return Register + float _Min_Value = this->Register_Pointer_Read(MIN0) * _Scale; - // Set Current Max Limit - this->Set_Limit(__IRMS_MAX__, this->Limit.I_Max); + // Clear Register + if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MIN0, 0x00); - // Set Frequency Min Limit - this->Set_Limit(__F_MIN__, this->Limit.FQ_Min); + // End Function + return(_Min_Value); - // Set Frequency Max Limit - this->Set_Limit(__F_MAX__, this->Limit.FQ_Max); + } + case __MAX78630_MONITOR_2__: { - // Set Voltage Imbalance Max Limit - this->Set_Limit(__VIMB_MAX__, this->Limit.VImb_Max); + // Define Register + Register MIN1 {0x01, 0x8C, 23}; // Minimum Recorded Value 2 - // Set Current Imbalance Max Limit - this->Set_Limit(__IIMB_MAX__, this->Limit.IImb_Max); + // Return Register + float _Min_Value = this->Register_Pointer_Read(MIN1) * _Scale; - // Set Temperature Min Limit - this->Set_Limit(__T_MIN__, this->Limit.T_Min); + // Clear Register + if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MIN1, 0x00); - // Set Temperature Max Limit - this->Set_Limit(__T_MAX__, this->Limit.T_Max); + // End Function + return(_Min_Value); } + case __MAX78630_MONITOR_3__: { - // Set Min Max - if (_MinMax) { + // Define Register + Register MIN2 {0x01, 0x8F, 23}; // Minimum Recorded Value 3 - // Set Min/Max Monitor Address 1 - this->Set_Min_Max_Address(__MAX78630_MONITOR_1__, __MAX78630_Monitor_1_Type__); - this->Clear_Min_Value(__MAX78630_MONITOR_1__); - this->Clear_Max_Value(__MAX78630_MONITOR_1__); + // Return Register + float _Min_Value = this->Register_Pointer_Read(MIN2) * _Scale; - // Set Min/Max Monitor Address 2 - this->Set_Min_Max_Address(__MAX78630_MONITOR_2__, __MAX78630_Monitor_2_Type__); - this->Clear_Min_Value(__MAX78630_MONITOR_2__); - this->Clear_Max_Value(__MAX78630_MONITOR_2__); + // Clear Register + if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MIN2, 0x00); - // Set Min/Max Monitor Address 3 - this->Set_Min_Max_Address(__MAX78630_MONITOR_3__, __MAX78630_Monitor_3_Type__); - this->Clear_Min_Value(__MAX78630_MONITOR_3__); - this->Clear_Max_Value(__MAX78630_MONITOR_3__); + // End Function + return(_Min_Value); - // Set Min/Max Monitor Address 4 - this->Set_Min_Max_Address(__MAX78630_MONITOR_4__, __MAX78630_Monitor_4_Type__); - this->Clear_Min_Value(__MAX78630_MONITOR_4__); - this->Clear_Max_Value(__MAX78630_MONITOR_4__); + } + case __MAX78630_MONITOR_4__: { - // Set Min/Max Monitor Address 5 - this->Set_Min_Max_Address(__MAX78630_MONITOR_5__, __MAX78630_Monitor_5_Type__); - this->Clear_Min_Value(__MAX78630_MONITOR_5__); - this->Clear_Max_Value(__MAX78630_MONITOR_5__); + // Define Register + Register MIN3 {0x01, 0x92, 23}; // Minimum Recorded Value 4 - // Set Min/Max Monitor Address 6 - this->Set_Min_Max_Address(__MAX78630_MONITOR_6__, __MAX78630_Monitor_6_Type__); - this->Clear_Min_Value(__MAX78630_MONITOR_6__); - this->Clear_Max_Value(__MAX78630_MONITOR_6__); + // Return Register + float _Min_Value = this->Register_Pointer_Read(MIN3) * _Scale; - // Set Min/Max Monitor Address 7 - this->Set_Min_Max_Address(__MAX78630_MONITOR_7__, __MAX78630_Monitor_7_Type__); - this->Clear_Min_Value(__MAX78630_MONITOR_7__); - this->Clear_Max_Value(__MAX78630_MONITOR_7__); + // Clear Register + if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MIN3, 0x00); - // Set Min/Max Monitor Address 8 - this->Set_Min_Max_Address(__MAX78630_MONITOR_8__, __MAX78630_Monitor_8_Type__); - this->Clear_Min_Value(__MAX78630_MONITOR_8__); - this->Clear_Max_Value(__MAX78630_MONITOR_8__); + // End Function + return(_Min_Value); } + case __MAX78630_MONITOR_5__: { - // Set Alarm - if (_Alarm) { - - // Set Sticky - this->Set_Sticky(__MAX78630_Config_Status_Sticky__); - - // Set High Voltage Alarm --> AL1 - this->Set_Alarm_Mask(__MAX78630_ALARM_1__, __BIT_OV_VRMSA__, true); - this->Set_Alarm_Mask(__MAX78630_ALARM_1__, __BIT_OV_VRMSB__, true); - this->Set_Alarm_Mask(__MAX78630_ALARM_1__, __BIT_OV_VRMSC__, true); - - // Set Low Voltage Alarm --> AL1 - this->Set_Alarm_Mask(__MAX78630_ALARM_1__, __BIT_UN_VRMSA__, true); - this->Set_Alarm_Mask(__MAX78630_ALARM_1__, __BIT_UN_VRMSB__, true); - this->Set_Alarm_Mask(__MAX78630_ALARM_1__, __BIT_UN_VRMSC__, true); + // Define Register + Register MIN4 {0x01, 0x95, 23}; // Minimum Recorded Value 5 - // Set High Frequency Alarm --> AL1 - this->Set_Alarm_Mask(__MAX78630_ALARM_1__, __BIT_OV_FREQ__, true); + // Return Register + float _Min_Value = this->Register_Pointer_Read(MIN4) * _Scale; - // Set Low Frequency Alarm --> AL1 - this->Set_Alarm_Mask(__MAX78630_ALARM_1__, __BIT_UN_FREQ__, true); + // Clear Register + if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MIN4, 0x00); - // Set Voltage Imbalance Alarm --> AL2 - this->Set_Alarm_Mask(__MAX78630_ALARM_2__, __BIT_V_IMBAL__, true); + // End Function + return(_Min_Value); - // Set Current Imbalance Alarm --> AL2 - this->Set_Alarm_Mask(__MAX78630_ALARM_2__, __BIT_I_IMBAL__, true); + } + case __MAX78630_MONITOR_6__: { - // Set Low Power Factor Alarm --> AL2 - this->Set_Alarm_Mask(__MAX78630_ALARM_2__, __BIT_UN_PFA__, true); - this->Set_Alarm_Mask(__MAX78630_ALARM_2__, __BIT_UN_PFB__, true); - this->Set_Alarm_Mask(__MAX78630_ALARM_2__, __BIT_UN_PFC__, true); + // Define Register + Register MIN5 {0x01, 0x98, 23}; // Minimum Recorded Value 6 - // Set High Temperature Alarm --> AL2 - this->Set_Alarm_Mask(__MAX78630_ALARM_2__, __BIT_OV_TEMP__, true); + // Return Register + float _Min_Value = this->Register_Pointer_Read(MIN5) * _Scale; - // Set Low Temperature Alarm --> AL2 - this->Set_Alarm_Mask(__MAX78630_ALARM_2__, __BIT_UN_TEMP__, true); + // Clear Register + if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MIN5, 0x00); - // Clear Status - this->Clear_Status(); + // End Function + return(_Min_Value); } + case __MAX78630_MONITOR_7__: { - // End Function - return(true); - - } else { - - // End Function - return(false); - - } + // Define Register + Register MIN6 {0x01, 0x9B, 0}; // Minimum Recorded Value 7 - } + // Return Register + float _Min_Value = this->Register_Pointer_Read(MIN6) * _Scale; - // Limit Set Function. - bool Set_Limit(const uint8_t _Limit_Type, const float _Limit_Value) { + // Clear Register + if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MIN6, 0x00); - // Decide Type - switch (_Limit_Type) { + // End Function + return(_Min_Value); - case __VRMS_MIN__: { + } + case __MAX78630_MONITOR_8__: { // Define Register - Register VRMS_MIN {0x00, 0xB1, 23}; // Voltage lower alarm limit. Positive values only + Register MIN7 {0x01, 0x9E, 0}; // Minimum Recorded Value 8 - // Get Register - return(this->Register_Pointer_Set(VRMS_MIN, this->FtoS(_Limit_Value / __MAX78630_Config_VScale__, VRMS_MIN.Data_Type))); + // Return Register + float _Min_Value = this->Register_Pointer_Read(MIN7) * _Scale; + + // Clear Register + if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MIN7, 0x00); - // End Case - break; + // End Function + return(_Min_Value); } - case __VRMS_MAX__: { - // Define Register - Register VRMS_MAX {0x00, 0xB4, 23}; // Voltage upper alarm limit. Positive values only + } - // Get Register - return(this->Register_Pointer_Set(VRMS_MAX, this->FtoS(_Limit_Value / __MAX78630_Config_VScale__, VRMS_MAX.Data_Type))); + // End Function + return(0); - // End Case - break; + } - } - case __IRMS_MAX__: { + // Max Record Value Function + float Max_Value(const bool _Function = __MAX78630_GET__, const uint8_t _MM_ADDR, const uint16_t _Scale = 1) { - // Define Register - Register IRMS_MAX {0x00, 0xF3, 23}; // Current upper alarm limit. Positive values only + // Control for Function + if (_Function == __MAX78630_SET__) return(0); - // Get Register - return(this->Register_Pointer_Set(IRMS_MAX, this->FtoS(_Limit_Value / __MAX78630_Config_IScale__, IRMS_MAX.Data_Type))); + // Control for Mask + if (_MM_ADDR > 8) return(0); - // End Case - break; + // Decide Address + switch (_MM_ADDR) { - } - case __F_MIN__: { + case __MAX78630_MONITOR_1__: { // Define Register - Register F_MIN {0x01, 0x83, 16}; // Frequency Alarm Lower Limit + Register MAX0 {0x01, 0xA1, 23}; // Maximum Recorded Value 1 - // Get Register - return(this->Register_Pointer_Set(F_MIN, this->FtoS(_Limit_Value, F_MIN.Data_Type))); + // Return Register + float _Max_Value = this->Register_Pointer_Read(MAX0) * _Scale; - // End Case - break; + // Clear Register + if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MAX0, 0x00); + + // End Function + return(_Max_Value); } - case __F_MAX__: { + case __MAX78630_MONITOR_2__: { // Define Register - Register F_MAX {0x01, 0x86, 16}; // Frequency Alarm Upper Limit + Register MAX1 {0x01, 0xA4, 23}; // Maximum Recorded Value 2 - // Get Register - return(this->Register_Pointer_Set(F_MAX, this->FtoS(_Limit_Value, F_MAX.Data_Type))); + // Return Register + float _Max_Value = this->Register_Pointer_Read(MAX1) * _Scale; - // End Case - break; + // Clear Register + if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MAX1, 0x00); + + // End Function + return(_Max_Value); } - case __T_MIN__: { + case __MAX78630_MONITOR_3__: { // Define Register - Register T_MIN {0x01, 0x7A, 10}; // Temperature Alarm Lower Limit + Register MAX2 {0x01, 0xA7, 23}; // Maximum Recorded Value 3 - // Get Register - return(this->Register_Pointer_Set(T_MIN, this->FtoS(_Limit_Value, T_MIN.Data_Type))); + // Return Register + float _Max_Value = this->Register_Pointer_Read(MAX2) * _Scale; - // End Case - break; + // Clear Register + if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MAX2, 0x00); + + // End Function + return(_Max_Value); } - case __T_MAX__: { + case __MAX78630_MONITOR_4__: { // Define Register - Register T_MAX {0x01, 0x7D, 10}; // Temperature Alarm Upper Limit + Register MAX3 {0x01, 0xAA, 23}; // Maximum Recorded Value 4 - // Get Register - return(this->Register_Pointer_Set(T_MAX, this->FtoS(_Limit_Value, T_MAX.Data_Type))); + // Return Register + float _Max_Value = this->Register_Pointer_Read(MAX3) * _Scale; - // End Case - break; + // Clear Register + if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MAX3, 0x00); + + // End Function + return(_Max_Value); } - case __PF_MIN__: { + case __MAX78630_MONITOR_5__: { // Define Register - Register PF_MIN {0x01, 0x71, 22}; // Power Factor lower alarm limit + Register MAX4 {0x01, 0xAD, 23}; // Maximum Recorded Value 5 - // Get Register - return(this->Register_Pointer_Set(PF_MIN, this->FtoS(_Limit_Value, PF_MIN.Data_Type))); + // Return Register + float _Max_Value = this->Register_Pointer_Read(MAX4) * _Scale; - // End Case - break; + // Clear Register + if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MAX4, 0x00); + + // End Function + return(_Max_Value); } - case __VSAG_LIM__: { + case __MAX78630_MONITOR_6__: { // Define Register - Register VSAG_LIM {0x00, 0xB7, 23}; // RMS Voltage Sag threshold. Positive values only + Register MAX5 {0x01, 0xB0, 23}; // Maximum Recorded Value 6 - // Get Register - return(this->Register_Pointer_Set(VSAG_LIM, this->FtoS(_Limit_Value / __MAX78630_Config_VScale__, VSAG_LIM.Data_Type))); + // Return Register + float _Max_Value = this->Register_Pointer_Read(MAX5) * _Scale; - // End Case - break; + // Clear Register + if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MAX5, 0x00); + + // End Function + return(_Max_Value); } - case __VIMB_MAX__: { + case __MAX78630_MONITOR_7__: { // Define Register - Register V_IMB_MAX {0x00, 0x81, 23}; // Voltage imbalance alarm limit. Positive values only + Register MAX6 {0x01, 0xB3, 0}; // Maximum Recorded Value 7 - // Get Register - return(this->Register_Pointer_Set(V_IMB_MAX, this->FtoS(_Limit_Value, V_IMB_MAX.Data_Type))); + // Return Register + float _Max_Value = this->Register_Pointer_Read(MAX6) * _Scale; - // End Case - break; + // Clear Register + if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MAX6, 0x00); + + // End Function + return(_Max_Value); } - case __IIMB_MAX__: { + case __MAX78630_MONITOR_8__: { // Define Register - Register I_IMB_MAX {0x00, 0x84, 23}; // Current imbalance alarm limit. Positive values only - - // Get Register - return(this->Register_Pointer_Set(I_IMB_MAX, this->FtoS(_Limit_Value, I_IMB_MAX.Data_Type))); + Register MAX7 {0x01, 0xB6, 0}; // Maximum Recorded Value 8 - // End Case - break; + // Return Register + float _Max_Value = this->Register_Pointer_Read(MAX7) * _Scale; - } - default: { + // Clear Register + if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MAX7, 0x00); // End Function - return(false); + return(_Max_Value); } } // End Function - return(false); + return(0); } - /* IC Utility Functions */ + /* Calibration Functions */ - // Get Device Address Function. - uint32_t Get_Device_Address(void) { + // Calibration Function + bool Calibration(const uint8_t _Type, const uint8_t _Phase, const float _Gain, const float _Offset) { - // Define Register - Register DEVADDR {0x00, 0x4E, 0}; // High order address bits for I2C and UART interfaces + // Control Input Parameters + if (_Phase > 3) return(false); - // Return Register - return(this->Register_Pointer_Read(DEVADDR)); + // Control for Calibration Type + switch (_Type) { - } + // Voltage Calibration + case __Voltage__: { - // Get Communication Baud Function. - uint32_t Get_Baud(void) { + // Control for Phase + switch (_Phase) { + + // Phase R + case __Phase_R__: { - // Define Register - Register BAUD {0x00, 0x51, 0}; // Baud rate for UART interface + // Define Register + Register V1_GAIN {0x00, 0x5D, 21}; + Register V1_OFFS {0x00, 0x6F, 23}; - // Return Register - return(this->Register_Pointer_Read(BAUD)); + bool _Result_Gain = this->Register_Pointer_Set(V1_GAIN, this->FtoS(_Gain, V1_GAIN.Data_Type)); // Write Gain + bool _Result_Offset = this->Register_Pointer_Set(V1_OFFS, this->FtoS(_Offset, V1_GAIN.Data_Type)); // Write Offset - } + // End Function + return(_Result_Gain and _Result_Offset); - // Set Baud Function - bool Set_Baud(const uint32_t _Baud) { + } - // Define Register - Register BAUD {0x00, 0x51, 0}; // Baud rate for UART interface + // Phase S + case __Phase_S__: { - // Return Result - return(this->Register_Pointer_Set(BAUD, _Baud)); + // Define Register + Register V2_GAIN {0x00, 0x60, 21}; + Register V2_OFFS {0x00, 0x72, 23}; - } + bool _Result_Gain = this->Register_Pointer_Set(V2_GAIN, this->FtoS(_Gain, V2_GAIN.Data_Type)); // Write Gain + bool _Result_Offset = this->Register_Pointer_Set(V2_OFFS, this->FtoS(_Offset, V2_GAIN.Data_Type)); // Write Offset - // Get System Status Function. - uint32_t Get_System_Stat(void) { + // End Function + return(_Result_Gain and _Result_Offset); - // Define Register - Register SYSSTAT {0x02, 0x43, 0}; // Bit 23 is a sticky register with status of any SPI Errors + } - // Return Register - return(this->Register_Pointer_Read(SYSSTAT)); + // Phase T + case __Phase_T__: { - } + // Define Register + Register V3_GAIN {0x00, 0x63, 21}; + Register V3_OFFS {0x00, 0x75, 23}; - // Get Device Frame Rate Function. - uint32_t Get_Frame(void) { + bool _Result_Gain = this->Register_Pointer_Set(V3_GAIN, this->FtoS(_Gain, V3_GAIN.Data_Type)); // Write Gain + bool _Result_Offset = this->Register_Pointer_Set(V3_OFFS, this->FtoS(_Offset, V3_GAIN.Data_Type)); // Write Offset - // Define Register - Register FRAME {0x00, 0x12, 0}; // Low-rate sample counter + // End Function + return(_Result_Gain and _Result_Offset); - // Return Register - return(this->Register_Pointer_Read(FRAME)); + } - } + // No Phase + default: { - // Get Sample Counter Function - uint32_t Get_Cycle(void) { + // End Function + return(false); - // Define Register - Register CYCLE {0x00, 0x0F, 0}; // High-rate sample counter + } + + } - // Return Register - return(this->Register_Pointer_Read(CYCLE)); + } - } + // Current Calibration + case __Current__: { - // Get Divisor Value Function - uint32_t Get_Divisor(void) { + // Control for Phase + switch (_Phase) { + + // Phase R + case __Phase_R__: { - // Define Register - Register DIVISOR {0x00, 0x0C, 0}; // Actual samples in previous accumulation interval + // Define Register + Register I1_GAIN {0x00, 0x54, 21}; + Register I1_OFFS {0x00, 0x66, 23}; - // Return Register - return(this->Register_Pointer_Read(DIVISOR)); + bool _Result_Gain = this->Register_Pointer_Set(I1_GAIN, this->FtoS(_Gain, I1_GAIN.Data_Type)); // Write Gain + bool _Result_Offset = this->Register_Pointer_Set(I1_OFFS, this->FtoS(_Offset, I1_GAIN.Data_Type)); // Write Offset - } + // End Function + return(_Result_Gain and _Result_Offset); - // Get Samples per Accumulation Interval Function. - uint32_t Get_Samples(void) { + } - // Define Register - Register SAMPLES {0x00, 0x09, 0}; // Minimum high-rate samples per accumulation interval + // Phase S + case __Phase_S__: { - // Return Register - return(this->Register_Pointer_Read(SAMPLES)); + // Define Register + Register I2_GAIN {0x00, 0x57, 21}; + Register I2_OFFS {0x00, 0x69, 23}; - } + bool _Result_Gain = this->Register_Pointer_Set(I2_GAIN, this->FtoS(_Gain, I2_GAIN.Data_Type)); // Write Gain + bool _Result_Offset = this->Register_Pointer_Set(I2_OFFS, this->FtoS(_Offset, I2_GAIN.Data_Type)); // Write Offset - /* Scale Config Functions */ + // End Function + return(_Result_Gain and _Result_Offset); - // Get VSCALE Function. - uint32_t Get_VSCale(void) { + } - // Define Register - Register VSCALE {0x01, 0x44, 0}; // Scratch register (see Scaling Registers section) + // Phase T + case __Phase_T__: { - // Return Register - return(this->Register_Pointer_Read(VSCALE)); + // Define Register + Register I3_GAIN {0x00, 0x5A, 21}; + Register I3_OFFS {0x00, 0x6C, 23}; - } + bool _Result_Gain = this->Register_Pointer_Set(I3_GAIN, this->FtoS(_Gain, I3_GAIN.Data_Type)); // Write Gain + bool _Result_Offset = this->Register_Pointer_Set(I3_OFFS, this->FtoS(_Offset, I3_GAIN.Data_Type)); // Write Offset - // Get ISCALE Function. - uint32_t Get_ISCale(void) { + // End Function + return(_Result_Gain and _Result_Offset); - // Define Register - Register IFSCALE {0x01, 0x41, 0}; // Scratch register (see Scaling Registers section) + } - // Return Register - return(this->Register_Pointer_Read(IFSCALE)); + // No Phase + default: { - } + // End Function + return(false); - // Bucket Size Get Function. - uint32_t Get_Bucket_H(void) { + } + + } - // Define Register - Register BUCKET_HIGH {0x01, 0xD4, 0}; // Energy Bucket Size – High word + } - // Set Command - return(this->Register_Pointer_Read(BUCKET_HIGH)); + // Temperature Calibration + case __Temperature__: { - } + // Define Register + Register T_GAIN {0x00, 0x78, 0}; + Register T_OFFS {0x00, 0x7B, 0}; - // Bucket Size Get Function. - uint32_t Get_Bucket_L(void) { + bool _Result_Gain = this->Register_Pointer_Set(T_GAIN, this->FtoS(_Gain, T_GAIN.Data_Type)); // Write Gain + bool _Result_Offset = this->Register_Pointer_Set(T_OFFS, this->FtoS(_Offset, T_GAIN.Data_Type)); // Write Offset - // Define Register - Register BUCKET_LOW {0x01, 0xD1, 0}; // Energy Bucket Size – Low word + // End Function + return(_Result_Gain and _Result_Offset); + + } - // Set Command - return(this->Register_Pointer_Read(BUCKET_LOW)); + // No Calibration + default: { + + // End Function + return(false); + + } + + } } - /* Min Max Functions */ + // Coefficients Function + float HPF_COEF(const bool _Function = __MAX78630_GET__, const uint8_t _Type, float _COEF) { - // Get Min Value From Address - float Get_Min_Value(const uint8_t _MM_ADDR) { + // Control for Type + if (_Type == __Voltage__) { - // Decide Address - switch (_MM_ADDR) { + // Define Register + Register HPF_COEF_V {0x00, 0x3F, 23}; // Voltage input HPF coefficient. Positive values only - case __MAX78630_MONITOR_1__: { + // Read Register + float _Result = this->Register_Pointer_Read(HPF_COEF_V); - // Define Register - Register MIN0 {0x01, 0x89, 23}; // Minimum Recorded Value 1 + // Set Register + if (_Function == __MAX78630_SET__) this->Register_Pointer_Set(HPF_COEF_V, this->FtoS(_COEF, 23)); - // Get Register - return(this->Register_Pointer_Read(MIN0) * __MAX78630_Config_VScale__); + // End Function + return(_Result); - // End Case - break; + } else if (_Type == __Current__) { - } - case __MAX78630_MONITOR_2__: { + // Define Register + Register HPF_COEF_I {0x00, 0x3C, 23}; // Current input HPF coefficient. Positive values only - // Define Register - Register MIN1 {0x01, 0x8C, 23}; // Minimum Recorded Value 2 + // Read Register + float _Result = this->Register_Pointer_Read(HPF_COEF_I); - // Get Register - return(this->Register_Pointer_Read(MIN1) * __MAX78630_Config_VScale__); + // Set Register + if (_Function == __MAX78630_SET__) this->Register_Pointer_Set(HPF_COEF_I, this->FtoS(_COEF, 23)); - // End Case - break; + // End Function + return(_Result); - } - case __MAX78630_MONITOR_3__: { + } else { + + // End Function + return(0); + + } + + // End Function + return(0); + + } + + // Offset Function + float Offset(const bool _Function = __MAX78630_GET__, const uint8_t _Type, const uint8_t _Phase, float _Offset) { + + // Control for Type + if (_Type == __Current__) { + + // Control for _Phase + if (_Phase == __Phase_R__) { // Define Register - Register MIN2 {0x01, 0x8F, 23}; // Minimum Recorded Value 3 + Register IARMS_OFF {0x00, 0xC3, 23}; - // Get Register - return(this->Register_Pointer_Read(MIN2) * __MAX78630_Config_VScale__); + // Read Register + float _Result = this->Register_Pointer_Read(IARMS_OFF); - // End Case - break; + // Control for Function + if (_Function == __MAX78630_SET__) this->Register_Pointer_Set(IARMS_OFF, this->FtoS(_Offset, 23)); - } - case __MAX78630_MONITOR_4__: { + // End Function + return(_Result); + + } else if (_Phase == __Phase_S__) { // Define Register - Register MIN3 {0x01, 0x92, 23}; // Minimum Recorded Value 4 + Register IBRMS_OFF {0x00, 0xC6, 23}; - // Get Register - return(this->Register_Pointer_Read(MIN3) * __MAX78630_Config_IScale__); + // Read Register + float _Result = this->Register_Pointer_Read(IBRMS_OFF); - // End Case - break; + // Control for Function + if (_Function == __MAX78630_SET__) this->Register_Pointer_Set(IBRMS_OFF, this->FtoS(_Offset, 23)); - } - case __MAX78630_MONITOR_5__: { + // End Function + return(_Result); + + } else if (_Phase == __Phase_T__) { // Define Register - Register MIN4 {0x01, 0x95, 23}; // Minimum Recorded Value 5 + Register ICRMS_OFF {0x00, 0xC9, 23}; - // Get Register - return(this->Register_Pointer_Read(MIN4) * __MAX78630_Config_IScale__); + // Read Register + float _Result = this->Register_Pointer_Read(ICRMS_OFF); - // End Case - break; + // Control for Function + if (_Function == __MAX78630_SET__) this->Register_Pointer_Set(ICRMS_OFF, this->FtoS(_Offset, 23)); + + // End Function + return(_Result); + + } else { + + // End Function + return(0); } - case __MAX78630_MONITOR_6__: { + + } else if (_Type == __Active_Power__) { + + // Control for _Phase + if (_Phase == __Phase_R__) { // Define Register - Register MIN5 {0x01, 0x98, 23}; // Minimum Recorded Value 6 + Register P_ACTIVE_OFF {0x01, 0x14, 23}; - // Get Register - return(this->Register_Pointer_Read(MIN5) * __MAX78630_Config_IScale__); + // Read Register + float _Result = this->Register_Pointer_Read(P_ACTIVE_OFF); - // End Case - break; + // Control for Function + if (_Function == __MAX78630_SET__) this->Register_Pointer_Set(P_ACTIVE_OFF, this->FtoS(_Offset, 23)); - } - case __MAX78630_MONITOR_7__: { + // End Function + return(_Result); + + } else if (_Phase == __Phase_S__) { // Define Register - Register MIN6 {0x01, 0x9B, 0}; // Minimum Recorded Value 7 + Register Q_ACTIVE_OFF {0x01, 0x17, 23}; - // Get Register - return(this->Register_Pointer_Read(MIN6)); + // Read Register + float _Result = this->Register_Pointer_Read(Q_ACTIVE_OFF); - // End Case - break; + // Control for Function + if (_Function == __MAX78630_SET__) this->Register_Pointer_Set(Q_ACTIVE_OFF, this->FtoS(_Offset, 23)); - } - case __MAX78630_MONITOR_8__: { + // End Function + return(_Result); + + } else if (_Phase == __Phase_T__) { // Define Register - Register MIN7 {0x01, 0x9E, 0}; // Minimum Recorded Value 8 + Register S_ACTIVE_OFF {0x01, 0x1A, 23}; - // Get Register - return(this->Register_Pointer_Read(MIN7)); + // Read Register + float _Result = this->Register_Pointer_Read(S_ACTIVE_OFF); - // End Case - break; + // Control for Function + if (_Function == __MAX78630_SET__) this->Register_Pointer_Set(S_ACTIVE_OFF, this->FtoS(_Offset, 23)); + + // End Function + return(_Result); + + } else { + + // End Function + return(0); } - } + } else if (_Type == __ReActive_Power__) { - // End Function - return(-101); + // Control for _Phase + if (_Phase == __Phase_R__) { - } + // Define Register + Register QA_OFFS {0x01, 0x0B, 23}; - // Get Max Value From Address - float Get_Max_Value(const uint8_t _MM_ADDR) { + // Read Register + float _Result = this->Register_Pointer_Read(QA_OFFS); - // Decide Address - switch (_MM_ADDR) { + // Control for Function + if (_Function == __MAX78630_SET__) this->Register_Pointer_Set(QA_OFFS, this->FtoS(_Offset, 23)); - case __MAX78630_MONITOR_1__: { + // End Function + return(_Result); + + } else if (_Phase == __Phase_S__) { // Define Register - Register MAX0 {0x01, 0xA1, 23}; // Maximum Recorded Value 1 + Register QB_OFFS {0x01, 0x0E, 23}; - // Get Register - return(this->Register_Pointer_Read(MAX0) * __MAX78630_Config_VScale__); + // Read Register + float _Result = this->Register_Pointer_Read(QB_OFFS); - // End Case - break; + // Control for Function + if (_Function == __MAX78630_SET__) this->Register_Pointer_Set(QB_OFFS, this->FtoS(_Offset, 23)); - } - case __MAX78630_MONITOR_2__: { + // End Function + return(_Result); + + } else if (_Phase == __Phase_T__) { // Define Register - Register MAX1 {0x01, 0xA4, 23}; // Maximum Recorded Value 2 + Register QC_OFFS {0x01, 0x11, 23}; - // Get Register - return(this->Register_Pointer_Read(MAX1) * __MAX78630_Config_VScale__); + // Read Register + float _Result = this->Register_Pointer_Read(QC_OFFS); - // End Case - break; + // Control for Function + if (_Function == __MAX78630_SET__) this->Register_Pointer_Set(QC_OFFS, this->FtoS(_Offset, 23)); + + // End Function + return(_Result); + + } else { + + // End Function + return(0); } - case __MAX78630_MONITOR_3__: { - // Define Register - Register MAX2 {0x01, 0xA7, 23}; // Maximum Recorded Value 3 + } else { - // Get Register - return(this->Register_Pointer_Read(MAX2) * __MAX78630_Config_VScale__); + // End Function + return(0); - // End Case - break; + } - } - case __MAX78630_MONITOR_4__: { + // End Function + return(0); - // Define Register - Register MAX3 {0x01, 0xAA, 23}; // Maximum Recorded Value 4 + } - // Get Register - return(this->Register_Pointer_Read(MAX3) * __MAX78630_Config_IScale__); + /* Limit Functions */ - // End Case - break; + // Limit Function + float Limit(const uint8_t _Function = __MAX78630_GET__, const uint8_t _Limit_Type, float _Limit_Value) { - } - case __MAX78630_MONITOR_5__: { + // Decide Type + switch (_Limit_Type) { + + case __VRMS_MIN__: { // Define Register - Register MAX4 {0x01, 0xAD, 23}; // Maximum Recorded Value 5 + Register VRMS_MIN {0x00, 0xB1, 23}; // Voltage lower alarm limit. Positive values only - // Get Register - return(this->Register_Pointer_Read(MAX4) * __MAX78630_Config_IScale__); + // Read Register + float _VRMS_MIN = this->Register_Pointer_Read(VRMS_MIN) * __MAX78630_Config_VScale__; - // End Case - break; + // Control Function + if (_Function == __MAX78630_SET__) this->Register_Pointer_Set(VRMS_MIN, this->FtoS(_Limit_Value / __MAX78630_Config_VScale__, VRMS_MIN.Data_Type)); + + // End Function + return(_VRMS_MIN); } - case __MAX78630_MONITOR_6__: { + case __VRMS_MAX__: { // Define Register - Register MAX5 {0x01, 0xB0, 23}; // Maximum Recorded Value 6 + Register VRMS_MAX {0x00, 0xB4, 23}; // Voltage upper alarm limit. Positive values only - // Get Register - return(this->Register_Pointer_Read(MAX5) * __MAX78630_Config_IScale__); + // Read Register + float _VRMS_MAX = this->Register_Pointer_Read(VRMS_MAX) * __MAX78630_Config_VScale__; - // End Case - break; + // Control Function + if (_Function == __MAX78630_SET__) this->Register_Pointer_Set(VRMS_MAX, this->FtoS(_Limit_Value / __MAX78630_Config_VScale__, VRMS_MAX.Data_Type)); + + // End Function + return(_VRMS_MAX); } - case __MAX78630_MONITOR_7__: { + case __IRMS_MAX__: { // Define Register - Register MAX6 {0x01, 0xB3, 0}; // Maximum Recorded Value 7 + Register IRMS_MAX {0x00, 0xF3, 23}; // Current upper alarm limit. Positive values only - // Get Register - return(this->Register_Pointer_Read(MAX6)); + // Read Register + float _IRMS_MAX = this->Register_Pointer_Read(IRMS_MAX) * __MAX78630_Config_IScale__; - // End Case - break; + // Control Function + if (_Function == __MAX78630_SET__) this->Register_Pointer_Set(IRMS_MAX, this->FtoS(_Limit_Value / __MAX78630_Config_IScale__, IRMS_MAX.Data_Type)); + + // End Function + return(_IRMS_MAX); } - case __MAX78630_MONITOR_8__: { + case __F_MIN__: { // Define Register - Register MAX7 {0x01, 0xB6, 0}; // Maximum Recorded Value 8 + Register F_MIN {0x01, 0x83, 16}; // Frequency Alarm Lower Limit - // Get Register - return(this->Register_Pointer_Read(MAX7)); + // Read Register + float _F_MIN = this->Register_Pointer_Read(F_MIN); - // End Case - break; + // Control Function + if (_Function == __MAX78630_SET__) this->Register_Pointer_Set(F_MIN, this->FtoS(_Limit_Value, F_MIN.Data_Type)); + + // End Function + return(_F_MIN); } + case __F_MAX__: { - } + // Define Register + Register F_MAX {0x01, 0x86, 16}; // Frequency Alarm Upper Limit - // End Function - return(-101); + // Read Register + float _F_MAX = this->Register_Pointer_Read(F_MAX); - } + // Control Function + if (_Function == __MAX78630_SET__) this->Register_Pointer_Set(F_MAX, this->FtoS(_Limit_Value, F_MAX.Data_Type)); - /* Calibration Functions */ + // End Function + return(_F_MAX); + + } + case __T_MIN__: { - // Voltage Calibration Function - bool Voltage_Calibration(const uint8_t _Phase, const float _Gain, const float _Offset) { + // Define Register + Register T_MIN {0x01, 0x7A, 10}; // Temperature Alarm Lower Limit - Register V1_GAIN {0x00, 0x5D, 21}; // Voltage Gain Calibration. Positive values only - Register V2_GAIN {0x00, 0x60, 21}; // Voltage Gain Calibration. Positive values only - Register V3_GAIN {0x00, 0x63, 21}; // Voltage Gain Calibration. Positive values only - Register V1_OFFS {0x00, 0x6F, 23}; // Voltage Offset Calibration - Register V2_OFFS {0x00, 0x72, 23}; // Voltage Offset Calibration - Register V3_OFFS {0x00, 0x75, 23}; // Voltage Offset Calibration + // Read Register + float _T_MIN = this->Register_Pointer_Read(T_MIN); - // Control Input Parameters - if (_Phase > 3) return(false); + // Control Function + if (_Function == __MAX78630_SET__) this->Register_Pointer_Set(T_MIN, this->FtoS(_Limit_Value, T_MIN.Data_Type)); - // Phase R Calibration - if (_Phase == __Phase_R__) { + // End Function + return(_T_MIN); - bool _Result_Gain = this->Register_Pointer_Set(V1_GAIN, this->FtoS(_Gain, V1_GAIN.Data_Type)); // Write Gain - bool _Result_Offset = this->Register_Pointer_Set(V1_OFFS, this->FtoS(_Offset, V1_GAIN.Data_Type)); // Write Offset + } + case __T_MAX__: { - // End Function - return(_Result_Gain and _Result_Offset); + // Define Register + Register T_MAX {0x01, 0x7D, 10}; // Temperature Alarm Upper Limit - } + // Read Register + float _T_MAX = this->Register_Pointer_Read(T_MAX); - // Phase S Calibration - if (_Phase == __Phase_S__) { + // Control Function + if (_Function == __MAX78630_SET__) this->Register_Pointer_Set(T_MAX, this->FtoS(_Limit_Value, T_MAX.Data_Type)); - bool _Result_Gain = this->Register_Pointer_Set(V2_GAIN, this->FtoS(_Gain, V2_GAIN.Data_Type)); // Write Gain - bool _Result_Offset = this->Register_Pointer_Set(V2_OFFS, this->FtoS(_Offset, V2_GAIN.Data_Type)); // Write Offset + // End Function + return(_T_MAX); - // End Function - return(_Result_Gain and _Result_Offset); + } + case __PF_MIN__: { - } + // Define Register + Register PF_MIN {0x01, 0x71, 22}; // Power Factor lower alarm limit - // Phase T Calibration - if (_Phase == __Phase_T__) { + // Read Register + float _PF_MIN = this->Register_Pointer_Read(PF_MIN); - bool _Result_Gain = this->Register_Pointer_Set(V3_GAIN, this->FtoS(_Gain, V3_GAIN.Data_Type)); // Write Gain - bool _Result_Offset = this->Register_Pointer_Set(V3_OFFS, this->FtoS(_Offset, V3_GAIN.Data_Type)); // Write Offset + // Control Function + if (_Function == __MAX78630_SET__) this->Register_Pointer_Set(PF_MIN, this->FtoS(_Limit_Value, PF_MIN.Data_Type)); - // End Function - return(_Result_Gain and _Result_Offset); + // End Function + return(_PF_MIN); - } + } + case __VSAG_LIM__: { - // End Function - return(false); + // Define Register + Register VSAG_LIM {0x00, 0xB7, 23}; // RMS Voltage Sag threshold. Positive values only - } + // Read Register + float _VSAG_LIM = this->Register_Pointer_Read(VSAG_LIM) * __MAX78630_Config_VScale__; - // Current Calibration Function - bool Current_Calibration(const uint8_t _Phase, const float _Gain, const float _Offset) { + // Control Function + if (_Function == __MAX78630_SET__) this->Register_Pointer_Set(VSAG_LIM, this->FtoS(_Limit_Value / __MAX78630_Config_VScale__, VSAG_LIM.Data_Type)); - Register I1_GAIN {0x00, 0x54, 21}; // Current Gain Calibration.Positive values only - Register I2_GAIN {0x00, 0x57, 21}; // Current Gain Calibration.Positive values only - Register I3_GAIN {0x00, 0x5A, 21}; // Current Gain Calibration.Positive values only - Register I1_OFFS {0x00, 0x66, 23}; // Current Offset Calibration - Register I2_OFFS {0x00, 0x69, 23}; // Current Offset Calibration - Register I3_OFFS {0x00, 0x6C, 23}; // Current Offset Calibration + // End Function + return(_VSAG_LIM); - // Control Input Parameters - if (_Phase > 3) return(false); + } + case __VIMB_MAX__: { + + // Define Register + Register V_IMB_MAX {0x00, 0x81, 23}; // Voltage imbalance alarm limit. Positive values only - // Phase R Calibration - if (_Phase == __Phase_R__) { + // Read Register + float _V_IMB_MAX = this->Register_Pointer_Read(V_IMB_MAX); - bool _Result_Gain = this->Register_Pointer_Set(I1_GAIN, this->FtoS(_Gain, I1_GAIN.Data_Type)); // Write Gain - bool _Result_Offset = this->Register_Pointer_Set(I1_OFFS, this->FtoS(_Offset, I1_GAIN.Data_Type)); // Write Offset + // Control Function + if (_Function == __MAX78630_SET__) this->Register_Pointer_Set(V_IMB_MAX, this->FtoS(_Limit_Value, V_IMB_MAX.Data_Type)); - // End Function - return(_Result_Gain and _Result_Offset); + // End Function + return(_V_IMB_MAX); - } + } + case __IIMB_MAX__: { - // Phase S Calibration - if (_Phase == __Phase_S__) { + // Define Register + Register I_IMB_MAX {0x00, 0x84, 23}; // Current imbalance alarm limit. Positive values only - bool _Result_Gain = this->Register_Pointer_Set(I2_GAIN, this->FtoS(_Gain, I2_GAIN.Data_Type)); // Write Gain - bool _Result_Offset = this->Register_Pointer_Set(I2_OFFS, this->FtoS(_Offset, I2_GAIN.Data_Type)); // Write Offset + // Read Register + float _I_IMB_MAX = this->Register_Pointer_Read(I_IMB_MAX); - // End Function - return(_Result_Gain and _Result_Offset); + // Control Function + if (_Function == __MAX78630_SET__) this->Register_Pointer_Set(I_IMB_MAX, this->FtoS(_Limit_Value, I_IMB_MAX.Data_Type)); - } + // End Function + return(_I_IMB_MAX); - // Phase T Calibration - if (_Phase == __Phase_T__) { + } + default: { - bool _Result_Gain = this->Register_Pointer_Set(I3_GAIN, this->FtoS(_Gain, I3_GAIN.Data_Type)); // Write Gain - bool _Result_Offset = this->Register_Pointer_Set(I3_OFFS, this->FtoS(_Offset, I3_GAIN.Data_Type)); // Write Offset + // End Function + return(0); - // End Function - return(_Result_Gain and _Result_Offset); + } } // End Function - return(false); + return(0); } - // Temperature Calibration Function - bool Temperature_Calibration(const float _Gain, const float _Offset) { - Register T_GAIN {0x00, 0x78, 0}; // Temperature Slope Calibration - Register T_OFFS {0x00, 0x7B, 0}; // Temperature Offset Calibration - - bool _Result_Gain = this->Register_Pointer_Set(T_GAIN, this->FtoS(_Gain, T_GAIN.Data_Type)); // Write Gain - bool _Result_Offset = this->Register_Pointer_Set(T_OFFS, this->FtoS(_Offset, T_GAIN.Data_Type)); // Write Offset + // Public Context + public: - // End Function - return(_Result_Gain and _Result_Offset); + // Constructor + explicit MAX78630(Stream &_Serial) { + + // Set Serial Port + Serial_Energy = & _Serial; } - // Voltage High Pass Filter Coefficient Function. - float Voltage_HPF_COEF(float _COEF) { - - Register HPF_COEF_V {0x00, 0x3F, 23}; // Voltage input HPF coefficient. Positive values only - - // Declare Variable - bool _Result = 0; + // Begin and Set Start Parameters. + bool Begin(const uint8_t _Address = 4, const bool _Scale = true, const bool _Bucket = true, const bool _Limit = true, const bool _MinMax = true, const bool _Alarm = true) { - // Decide Workflow - if (_COEF == -999) { + // Clear Serial Buffer + this->Clear_Buffer(); - // Read Register - _Result = this->Register_Pointer_Read(HPF_COEF_V); // Measure Phase R + // Declare Device Address + uint8_t _Device_Address = 0xC0; - } else { + // Set Device Address + if (_Address == 0) _Device_Address = 0xC0; + if (_Address == 1) _Device_Address = 0xC1; + if (_Address == 2) _Device_Address = 0xC2; + if (_Address == 3) _Device_Address = 0xC3; + if (_Address == 4) _Device_Address = 0xC4; - // Set Register - _Result = this->Register_Pointer_Set(HPF_COEF_V, this->FtoS(_COEF, 23)); // Measure Phase R + // Control Connection + if (this->Select_IC(_Device_Address)) { - } - - // End Function - return(_Result); + // Control Firmware + if (this->Firmware() == __MAX78630_Firmware__) { - } + // Get Status + this->Status(__MAX78630_GET__); - // Current High Pass Filter Coefficient Function. - float Current_HPF_COEF(const bool _Function = __MAX78630_GET__, float _COEF = 0) { + // Set Device Found + this->Device.Found = true; - // Define Register - Register HPF_COEF_I {0x00, 0x3C, 23}; + } else { - // Control for Function - if (_Function == __MAX78630_GET__) { + // Set Device Found + this->Device.Found = false; - // Read Register - return(this->Register_Pointer_Read(HPF_COEF_I)); + } } else { - // Set Register - bool _Result = this->Register_Pointer_Set(HPF_COEF_I, this->FtoS(_COEF, 23)); - - // End Function - if (_Result) return(1); - else return(0); + // Set Device Found + this->Device.Found = false; } - - // End Function - return(0); - - } - - // Current RMS Offset Set Function. - float Current_RMS_Offset(const bool _Function = __MAX78630_GET__, uint8_t _Phase = __Phase_R__, const float _Offset = 0) { - // Define Register - Register IARMS_OFF {0x00, 0xC3, 23}; - Register IBRMS_OFF {0x00, 0xC6, 23}; - Register ICRMS_OFF {0x00, 0xC9, 23}; - - // Control for Function - if (_Function == __MAX78630_GET__) { + // Control for Device + if (this->Device.Found) { - // Read Register - if (_Phase == __Phase_R__) return(this->Register_Pointer_Read(IARMS_OFF)); // Measure Phase R - if (_Phase == __Phase_S__) return(this->Register_Pointer_Read(IBRMS_OFF)); // Measure Phase S - if (_Phase == __Phase_T__) return(this->Register_Pointer_Read(ICRMS_OFF)); // Measure Phase T + // Set Scale + if (_Scale) { - } else { + // Set Voltage Scale + this->VScale(__MAX78630_SET__); - // Set Register - if (_Phase == __Phase_R__) { + // Set Current Scale + this->IScale(__MAX78630_SET__); - // Set Register - if (this->Register_Pointer_Set(IARMS_OFF, this->FtoS(_Offset, 23))) return(1); - else return(0); + } - } else if (_Phase == __Phase_S__) { - - // Set Register - if (this->Register_Pointer_Set(IBRMS_OFF, this->FtoS(_Offset, 23))) return(1); - else return(0); + // Set Bucket + if (_Bucket) { - } else if (_Phase == __Phase_T__) { - - // Set Register - if (this->Register_Pointer_Set(ICRMS_OFF, this->FtoS(_Offset, 23))) return(1); - else return(0); + // Set Bucket Size + this->Bucket_H(__MAX78630_SET__); + this->Bucket_L(__MAX78630_SET__); } - // End Function - return(0); + // Set Limit + if (_Limit) { - } + // Set Voltage Min Limit + this->Limit(__MAX78630_SET__, __VRMS_MIN__, this->Limit_Variables.V_Min); - // End Function - return(0); + // Set Voltage Max Limit + this->Limit(__MAX78630_SET__, __VRMS_MAX__, this->Limit_Variables.V_Max); - } + // Set Current Max Limit + this->Limit(__MAX78630_SET__, __IRMS_MAX__, this->Limit_Variables.I_Max); - // Active Power Offset Set Function. - float Active_Power_Offset(const bool _Function = __MAX78630_GET__, uint8_t _Phase = __Phase_R__, const float _Offset = 0) { + // Set Frequency Min Limit + this->Limit(__MAX78630_SET__, __F_MIN__, this->Limit_Variables.FQ_Min); - // Define Register - Register PA_OFFS {0x01, 0x14, 23}; - Register PB_OFFS {0x01, 0x17, 23}; - Register PC_OFFS {0x01, 0x1A, 23}; + // Set Frequency Max Limit + this->Limit(__MAX78630_SET__, __F_MAX__, this->Limit_Variables.FQ_Max); - // Control for Function - if (_Function == __MAX78630_GET__) { + // Set Voltage Imbalance Max Limit + this->Limit(__MAX78630_SET__, __VIMB_MAX__, this->Limit_Variables.VImb_Max); - // Read Register - if (_Phase == __Phase_R__) return(this->Register_Pointer_Read(PA_OFFS)); // Measure Phase R - if (_Phase == __Phase_S__) return(this->Register_Pointer_Read(PB_OFFS)); // Measure Phase S - if (_Phase == __Phase_T__) return(this->Register_Pointer_Read(PC_OFFS)); // Measure Phase T + // Set Current Imbalance Max Limit + this->Limit(__MAX78630_SET__, __IIMB_MAX__, this->Limit_Variables.IImb_Max); - } else { + // Set Temperature Min Limit + this->Limit(__MAX78630_SET__, __T_MIN__, this->Limit_Variables.T_Min); - // Set Register - if (_Phase == __Phase_R__) { + // Set Temperature Max Limit + this->Limit(__MAX78630_SET__, __T_MAX__, this->Limit_Variables.T_Max); - // Set Register - if (this->Register_Pointer_Set(PA_OFFS, this->FtoS(_Offset, PA_OFFS.Data_Type))) return(1); - else return(0); + } - } else if (_Phase == __Phase_S__) { - - // Set Register - if (this->Register_Pointer_Set(PB_OFFS, this->FtoS(_Offset, PB_OFFS.Data_Type))) return(1); - else return(0); + // Set Min Max + if (_MinMax) { - } else if (_Phase == __Phase_T__) { - - // Set Register - if (this->Register_Pointer_Set(PC_OFFS, this->FtoS(_Offset, PC_OFFS.Data_Type))) return(1); - else return(0); + // Set Min/Max Monitor Address 1 + this->Set_Min_Max_Mask(__MAX78630_MONITOR_1__); - } + // Set Min/Max Monitor Address 2 + this->Set_Min_Max_Mask(__MAX78630_MONITOR_2__); - // End Function - return(0); + // Set Min/Max Monitor Address 3 + this->Set_Min_Max_Mask(__MAX78630_MONITOR_3__); - } + // Set Min/Max Monitor Address 4 + this->Set_Min_Max_Mask(__MAX78630_MONITOR_4__); - // End Function - return(0); + // Set Min/Max Monitor Address 5 + this->Set_Min_Max_Mask(__MAX78630_MONITOR_5__); - } + // Set Min/Max Monitor Address 6 + this->Set_Min_Max_Mask(__MAX78630_MONITOR_6__); - // ReActive Power Offset Set Function. - float ReActive_Power_Offset(const bool _Function = __MAX78630_GET__, uint8_t _Phase = __Phase_R__, const float _Offset = 0) { + // Set Min/Max Monitor Address 7 + this->Set_Min_Max_Mask(__MAX78630_MONITOR_7__); - // Define Register - Register QA_OFFS {0x01, 0x0B, 23}; - Register QB_OFFS {0x01, 0x0E, 23}; - Register QC_OFFS {0x01, 0x11, 23}; + // Set Min/Max Monitor Address 8 + this->Set_Min_Max_Mask(__MAX78630_MONITOR_8__); - // Control for Function - if (_Function == __MAX78630_GET__) { + } - // Read Register - if (_Phase == __Phase_R__) return(this->Register_Pointer_Read(QA_OFFS)); // Measure Phase R - if (_Phase == __Phase_S__) return(this->Register_Pointer_Read(QB_OFFS)); // Measure Phase S - if (_Phase == __Phase_T__) return(this->Register_Pointer_Read(QC_OFFS)); // Measure Phase T + // Set Alarm + if (_Alarm) { - } else { + // Set Sticky + this->Sticky(__MAX78630_SET__, __MAX78630_Config_Status_Sticky__); - // Set Register - if (_Phase == __Phase_R__) { + // Set High Voltage Alarm --> AL1 + this->Alarm_Mask(__MAX78630_SET__, __MAX78630_ALARM_1__, __BIT_OV_VRMSA__, true); + this->Alarm_Mask(__MAX78630_SET__, __MAX78630_ALARM_1__, __BIT_OV_VRMSB__, true); + this->Alarm_Mask(__MAX78630_SET__, __MAX78630_ALARM_1__, __BIT_OV_VRMSC__, true); - // Set Register - if (this->Register_Pointer_Set(QA_OFFS, this->FtoS(_Offset, QA_OFFS.Data_Type))) return(1); - else return(0); + // Set Low Voltage Alarm --> AL1 + this->Alarm_Mask(__MAX78630_SET__, __MAX78630_ALARM_1__, __BIT_UN_VRMSA__, true); + this->Alarm_Mask(__MAX78630_SET__, __MAX78630_ALARM_1__, __BIT_UN_VRMSB__, true); + this->Alarm_Mask(__MAX78630_SET__, __MAX78630_ALARM_1__, __BIT_UN_VRMSC__, true); - } else if (_Phase == __Phase_S__) { - - // Set Register - if (this->Register_Pointer_Set(QB_OFFS, this->FtoS(_Offset, QB_OFFS.Data_Type))) return(1); - else return(0); + // Set High Frequency Alarm --> AL1 + this->Alarm_Mask(__MAX78630_SET__, __MAX78630_ALARM_1__, __BIT_OV_FREQ__, true); - } else if (_Phase == __Phase_T__) { - - // Set Register - if (this->Register_Pointer_Set(QC_OFFS, this->FtoS(_Offset, QC_OFFS.Data_Type))) return(1); - else return(0); + // Set Low Frequency Alarm --> AL1 + this->Alarm_Mask(__MAX78630_SET__, __MAX78630_ALARM_1__, __BIT_UN_FREQ__, true); - } + // Set Voltage Imbalance Alarm --> AL2 + this->Alarm_Mask(__MAX78630_SET__, __MAX78630_ALARM_2__, __BIT_V_IMBAL__, true); - // End Function - return(0); + // Set Current Imbalance Alarm --> AL2 + this->Alarm_Mask(__MAX78630_SET__, __MAX78630_ALARM_2__, __BIT_I_IMBAL__, true); - } + // Set Low Power Factor Alarm --> AL2 + this->Alarm_Mask(__MAX78630_SET__, __MAX78630_ALARM_2__, __BIT_UN_PFA__, true); + this->Alarm_Mask(__MAX78630_SET__, __MAX78630_ALARM_2__, __BIT_UN_PFB__, true); + this->Alarm_Mask(__MAX78630_SET__, __MAX78630_ALARM_2__, __BIT_UN_PFC__, true); - // End Function - return(0); + // Set High Temperature Alarm --> AL2 + this->Alarm_Mask(__MAX78630_SET__, __MAX78630_ALARM_2__, __BIT_OV_TEMP__, true); - } + // Set Low Temperature Alarm --> AL2 + this->Alarm_Mask(__MAX78630_SET__, __MAX78630_ALARM_2__, __BIT_UN_TEMP__, true); - /* Measurement Functions */ + // Clear Status + this->Status(__MAX78630_GET__); - // Set Harmonic Channel Function. TODO: Not Tested. - uint8_t Set_Harmonic(uint32_t _Harmonic) { + } - Register HARM {0x00, 0x4B, 0}; // Harmonic Selector, default: 1 (fundamental) + } - // Declare Variable - uint32_t _Result = 0; + // End Function + return(this->Device.Found); - // Decide Action - if (_Harmonic == 0) { - - // Read Register - _Result = this->Register_Pointer_Read(HARM); + } - } else { + /* IC Utility Functions */ - // Set Register - if (Register_Pointer_Set(HARM, _Harmonic)) { - - // Set Variable - _Result = _Harmonic; - - } else { + // Status Function + uint32_t Status(const uint8_t _Function = __MAX78630_GET__) { - // Set Variable - _Result = 255; + // Define Register + Register STATUS {0x00, 0x15, 0}; // Alarm and device status bits - } + // Return Register + this->Device.Status = this->Register_Pointer_Read(STATUS); + + // Control for Function + if (_Function == __MAX78630_CLEAR__) this->Register_Pointer_Set(STATUS, 0x00800003); - } - // End Function - return(_Result); + return(this->Device.Status); } + /* Measurement Functions */ + // Voltage Measurement Function. float Voltage(const uint8_t _Phase, const uint8_t _Type) { @@ -3060,211 +3011,113 @@ // Get Register return(this->Register_Pointer_Read(VARHC_POS)); - // End Case - break; - - } - case __ReActive_Delivered__: { - - // Define Register - Register VARHC_NEG {0x02, 0x40, 0}; // Reactive Energy Lagging Counter - - // Get Register - return(this->Register_Pointer_Read(VARHC_NEG)); - - // End Case - break; - - } - case __Energy_Reset__: { - - // Define Register - Register WHC_POS {0x02, 0x01, 0}; // Received Active Energy Counter - Register WHC_NEG {0x02, 0x0A, 0}; // Delivered Active Energy Counter - Register VARHC_POS {0x02, 0x37, 0}; // Reactive Energy Leading Counter - Register VARHC_NEG {0x02, 0x40, 0}; // Reactive Energy Lagging Counter - - // Clear Register - this->Register_Pointer_Set(WHC_POS, 0x00); - this->Register_Pointer_Set(VARHC_POS, 0x00); - this->Register_Pointer_Set(WHC_NEG, 0x00); - this->Register_Pointer_Set(VARHC_NEG, 0x00); - - // End Function - return(1); - - // End Case - break; - - } - - } - - // End Case - break; - - } - - } - - // End Function - return(0); - - } - - // IC Temperature Measurement Function. - float Temperature(void) { - - // Define Register - Register TEMPC {0x01, 0x74, 10}; // Chip Temperature (Celsius°) - - // Return Register - return(this->Register_Pointer_Read(TEMPC)); - - } - - /* Limit and Defect Detect Functions */ - - // Read Alarm Status - bool Alarm(const uint8_t _Alarm_Type) { - - // End Function - return(bitRead(this->Device.Status, _Alarm_Type)); - - } - - // Over Voltage Alarm - inline bool Alarm_OV(void) { - - // End Function - return(bitRead(this->Device.Status, __BIT_OV_VRMSA__) or bitRead(this->Device.Status, __BIT_OV_VRMSB__) or bitRead(this->Device.Status, __BIT_OV_VRMSC__)); - - } - - // Under Voltage Alarm - inline bool Alarm_UV(void) { - - // End Function - return(bitRead(this->Device.Status, __BIT_UN_VRMSA__) or bitRead(this->Device.Status, __BIT_UN_VRMSB__) or bitRead(this->Device.Status, __BIT_UN_VRMSC__)); - - } - - // Over Current Alarm - inline bool Alarm_OC(void) { - - // End Function - return(bitRead(this->Device.Status, __BIT_OV_IRMSA__) or bitRead(this->Device.Status, __BIT_OV_IRMSB__) or bitRead(this->Device.Status, __BIT_OV_IRMSC__)); - - } - - // Over Frequency Alarm - inline bool Alarm_OFQ(void) { - - // End Function - return(bitRead(this->Device.Status, __BIT_OV_FREQ__)); - - } - - // Under Frequency Alarm - inline bool Alarm_UFQ(void) { - - // End Function - return(bitRead(this->Device.Status, __BIT_UN_FREQ__)); - - } + // End Case + break; - // Voltage Imbalance Alarm - inline bool Alarm_VImb(void) { + } + case __ReActive_Delivered__: { - // End Function - return(bitRead(this->Device.Status, __BIT_V_IMBAL__)); + // Define Register + Register VARHC_NEG {0x02, 0x40, 0}; // Reactive Energy Lagging Counter - } + // Get Register + return(this->Register_Pointer_Read(VARHC_NEG)); - // Current Imbalance Alarm - inline bool Alarm_IImb(void) { + // End Case + break; - // End Function - return(bitRead(this->Device.Status, __BIT_I_IMBAL__)); + } + case __Energy_Reset__: { - } + // Define Register + Register WHC_POS {0x02, 0x01, 0}; // Received Active Energy Counter + Register WHC_NEG {0x02, 0x0A, 0}; // Delivered Active Energy Counter + Register VARHC_POS {0x02, 0x37, 0}; // Reactive Energy Leading Counter + Register VARHC_NEG {0x02, 0x40, 0}; // Reactive Energy Lagging Counter - // Under Power Factor Alarm - inline bool Alarm_UPF(void) { + // Clear Register + this->Register_Pointer_Set(WHC_POS, 0x00); + this->Register_Pointer_Set(VARHC_POS, 0x00); + this->Register_Pointer_Set(WHC_NEG, 0x00); + this->Register_Pointer_Set(VARHC_NEG, 0x00); - // End Function - return(bitRead(this->Device.Status, __BIT_UN_PFA__) or bitRead(this->Device.Status, __BIT_UN_PFB__) or bitRead(this->Device.Status, __BIT_UN_PFC__)); + // End Function + return(1); - } + // End Case + break; - // Over Temperature Alarm - inline bool Alarm_OT(void) { + } - // End Function - return(bitRead(this->Device.Status, __BIT_OV_TEMP__)); + } - } + // End Case + break; - // Under Temperature Alarm - inline bool Alarm_UT(void) { + } + + } // End Function - return(bitRead(this->Device.Status, __BIT_UN_TEMP__)); + return(0); } - // Get Status Value - uint32_t Status(void) { + // IC Temperature Measurement Function. + float Temperature(void) { - // End Function - return(this->Device.Status); + // Define Register + Register TEMPC {0x01, 0x74, 10}; // Chip Temperature (Celsius°) + + // Return Register + return(this->Register_Pointer_Read(TEMPC)); } - // Limit Parameters Control Function. - void Get_Status(void) { + // Set Harmonic Channel Function. TODO: Not Tested. + uint8_t Harmonic(const bool _Function = __MAX78630_GET__, uint32_t _Harmonic) { // Define Register - Register STATUS {0x00, 0x15, 0}; // Alarm and device status bits + Register HARM {0x00, 0x4B, 0}; // Harmonic Selector, default: 1 (fundamental) - // Read Status Register - this->Device.Status = this->Register_Pointer_Read(STATUS); + // Control for Function + if (_Function == __MAX78630_GET__) { - } + // Read Register + return(this->Register_Pointer_Read(HARM)); - // Clear IC Status Register. - void Clear_Status(void) { + } else if (_Function == __MAX78630_SET__) { - // Define Register - Register STATUS {0x00, 0x15, 0}; // Alarm and device status bits + // Set Register + return(this->Register_Pointer_Set(HARM, _Harmonic)); - // Read STATUS Register - uint32_t _STATUS_Current = this->Register_Pointer_Read(STATUS); + } else { - // Clear Alarm Bits - _STATUS_Current &= 0x00800003; + // End Function + return(0); - // Write STATUS Register - this->Register_Pointer_Set(STATUS, _STATUS_Current); + } } + /* Limit Functions */ + // Adjust Limit Parameters Function. void Adjust_Limit(void) { // Update Status - this->Get_Status(); + this->Status(__MAX78630_GET__); // Control for OV Temp if (bitRead(this->Device.Status, __BIT_OV_TEMP__)) { // Set Limit - this->Set_Limit(__T_MAX__, (this->Limit.T_Max - __MAX78630_Limit_Temperature_Max_Diff__)); + this->Limit(__MAX78630_SET__, __T_MAX__, (this->Limit_Variables.T_Max - __MAX78630_Limit_Temperature_Max_Diff__)); } else { // Set Limit - this->Set_Limit(__T_MAX__, this->Limit.T_Max); + this->Limit(__MAX78630_SET__, __T_MAX__, this->Limit_Variables.T_Max); } @@ -3272,12 +3125,12 @@ if (bitRead(this->Device.Status, __BIT_UN_TEMP__)) { // Set Limit - this->Set_Limit(__T_MIN__, (this->Limit.T_Min + __MAX78630_Limit_Temperature_Min_Diff__)); + this->Limit(__MAX78630_SET__, __T_MIN__, (this->Limit_Variables.T_Min + __MAX78630_Limit_Temperature_Min_Diff__)); } else { // Set Limit - this->Set_Limit(__T_MIN__, this->Limit.T_Min); + this->Limit(__MAX78630_SET__, __T_MIN__, this->Limit_Variables.T_Min); } @@ -3285,12 +3138,12 @@ if (bitRead(this->Device.Status, __BIT_OV_VRMSA__) or bitRead(this->Device.Status, __BIT_OV_VRMSB__) or bitRead(this->Device.Status, __BIT_OV_VRMSC__)) { // Set Limit - this->Set_Limit(__VRMS_MAX__, (this->Limit.V_Max - __MAX78630_Limit_Voltage_Max_Diff__)); + this->Limit(__MAX78630_SET__, __VRMS_MAX__, (this->Limit_Variables.V_Max - __MAX78630_Limit_Voltage_Max_Diff__)); } else { // Set Limit - this->Set_Limit(__VRMS_MAX__, this->Limit.V_Max); + this->Limit(__MAX78630_SET__, __VRMS_MAX__, this->Limit_Variables.V_Max); } @@ -3298,12 +3151,12 @@ if (bitRead(this->Device.Status, __BIT_UN_VRMSA__) or bitRead(this->Device.Status, __BIT_UN_VRMSB__) or bitRead(this->Device.Status, __BIT_UN_VRMSC__)) { // Set Limit - this->Set_Limit(__VRMS_MIN__, (this->Limit.V_Min + __MAX78630_Limit_Voltage_Min_Diff__)); + this->Limit(__MAX78630_SET__, __VRMS_MIN__, (this->Limit_Variables.V_Min + __MAX78630_Limit_Voltage_Min_Diff__)); } else { // Set Limit - this->Set_Limit(__VRMS_MIN__, this->Limit.V_Min); + this->Limit(__MAX78630_SET__, __VRMS_MIN__, this->Limit_Variables.V_Min); } @@ -3311,12 +3164,12 @@ if (bitRead(this->Device.Status, __BIT_OV_FREQ__)) { // Set Limit - this->Set_Limit(__F_MAX__, (this->Limit.FQ_Max - __MAX78630_Limit_Frequency_Max_Diff__)); + this->Limit(__MAX78630_SET__, __F_MAX__, (this->Limit_Variables.FQ_Max - __MAX78630_Limit_Frequency_Max_Diff__)); } else { // Set Limit - this->Set_Limit(__F_MAX__, this->Limit.FQ_Max); + this->Limit(__MAX78630_SET__, __F_MAX__, this->Limit_Variables.FQ_Max); } @@ -3324,12 +3177,12 @@ if (bitRead(this->Device.Status, __BIT_UN_FREQ__)) { // Set Limit - this->Set_Limit(__F_MIN__, (this->Limit.FQ_Min + __MAX78630_Limit_Frequency_Min_Diff__)); + this->Limit(__MAX78630_SET__, __F_MIN__, (this->Limit_Variables.FQ_Min + __MAX78630_Limit_Frequency_Min_Diff__)); } else { // Set Limit - this->Set_Limit(__F_MIN__, this->Limit.FQ_Min); + this->Limit(__MAX78630_SET__, __F_MIN__, this->Limit_Variables.FQ_Min); } @@ -3337,12 +3190,12 @@ if (bitRead(this->Device.Status, __BIT_V_IMBAL__)) { // Set Limit - this->Set_Limit(__VIMB_MAX__, (this->Limit.VImb_Max + __MAX78630_Limit_VImb_Max_Diff__)); + this->Limit(__MAX78630_SET__, __VIMB_MAX__, (this->Limit_Variables.VImb_Max + __MAX78630_Limit_VImb_Max_Diff__)); } else { // Set Limit - this->Set_Limit(__VIMB_MAX__, this->Limit.VImb_Max); + this->Limit(__MAX78630_SET__, __VIMB_MAX__, this->Limit_Variables.VImb_Max); } @@ -3350,203 +3203,96 @@ if (bitRead(this->Device.Status, __BIT_I_IMBAL__)) { // Set Limit - this->Set_Limit(__IIMB_MAX__, (this->Limit.IImb_Max + __MAX78630_Limit_IImb_Max_Diff__)); + this->Limit(__MAX78630_SET__, __IIMB_MAX__, (this->Limit_Variables.IImb_Max + __MAX78630_Limit_IImb_Max_Diff__)); } else { // Set Limit - this->Set_Limit(__IIMB_MAX__, this->Limit.IImb_Max); + this->Limit(__MAX78630_SET__, __IIMB_MAX__, this->Limit_Variables.IImb_Max); } } - //Limit Read Function. - float Get_Limit(const uint8_t _Limit_Type) { - - // Decide Type - switch (_Limit_Type) { - - case __VRMS_MIN__: { - - // Define Register - Register VRMS_MIN {0x00, 0xB1, 23}; // Voltage lower alarm limit. Positive values only - - // Get Register - return(this->Register_Pointer_Read(VRMS_MIN) * __MAX78630_Config_VScale__); - - // End Case - break; - - } - case __VRMS_MAX__: { - - // Define Register - Register VRMS_MAX {0x00, 0xB4, 23}; // Voltage upper alarm limit. Positive values only - - // Get Register - return(this->Register_Pointer_Read(VRMS_MAX) * __MAX78630_Config_VScale__); - - // End Case - break; - - } - case __IRMS_MAX__: { - - // Define Register - Register IRMS_MAX {0x00, 0xF3, 23}; // Current upper alarm limit. Positive values only - - // Get Register - return(this->Register_Pointer_Read(IRMS_MAX) * __MAX78630_Config_IScale__); - - // End Case - break; - - } - case __F_MIN__: { - - // Define Register - Register F_MIN {0x01, 0x83, 16}; // Frequency Alarm Lower Limit - - // Get Register - return(this->Register_Pointer_Read(F_MIN)); - - // End Case - break; - - } - case __F_MAX__: { - - // Define Register - Register F_MAX {0x01, 0x86, 16}; // Frequency Alarm Upper Limit - - // Get Register - return(this->Register_Pointer_Read(F_MAX)); - - // End Case - break; - - } - case __T_MIN__: { - - // Define Register - Register T_MIN {0x01, 0x7A, 10}; // Temperature Alarm Lower Limit - - // Get Register - return(this->Register_Pointer_Read(T_MIN)); - - // End Case - break; - - } - case __T_MAX__: { - - // Define Register - Register T_MAX {0x01, 0x7D, 10}; // Temperature Alarm Upper Limit - - // Get Register - return(this->Register_Pointer_Read(T_MAX)); - - // End Case - break; - - } - case __PF_MIN__: { - - // Define Register - Register PF_MIN {0x01, 0x71, 22}; // Power Factor lower alarm limit + /* Alarm Functions */ - // Get Register - return(this->Register_Pointer_Read(PF_MIN)); + // Over Voltage Alarm + inline bool Alarm_OV(void) { - // End Case - break; + // End Function + return(bitRead(this->Device.Status, __BIT_OV_VRMSA__) or bitRead(this->Device.Status, __BIT_OV_VRMSB__) or bitRead(this->Device.Status, __BIT_OV_VRMSC__)); - } - case __VSAG_LIM__: { + } - // Define Register - Register VSAG_LIM {0x00, 0xB7, 23}; // RMS Voltage Sag threshold. Positive values only + // Under Voltage Alarm + inline bool Alarm_UV(void) { - // Get Register - return(this->Register_Pointer_Read(VSAG_LIM) * __MAX78630_Config_VScale__); + // End Function + return(bitRead(this->Device.Status, __BIT_UN_VRMSA__) or bitRead(this->Device.Status, __BIT_UN_VRMSB__) or bitRead(this->Device.Status, __BIT_UN_VRMSC__)); - // End Case - break; + } - } - case __VIMB_MAX__: { + // Over Current Alarm + inline bool Alarm_OC(void) { - // Define Register - Register V_IMB_MAX {0x00, 0x81, 23}; // Voltage imbalance alarm limit. Positive values only + // End Function + return(bitRead(this->Device.Status, __BIT_OV_IRMSA__) or bitRead(this->Device.Status, __BIT_OV_IRMSB__) or bitRead(this->Device.Status, __BIT_OV_IRMSC__)); - // Get Register - return(this->Register_Pointer_Read(V_IMB_MAX)); + } - // End Case - break; + // Over Frequency Alarm + inline bool Alarm_OFQ(void) { - } - case __IIMB_MAX__: { + // End Function + return(bitRead(this->Device.Status, __BIT_OV_FREQ__)); - // Define Register - Register I_IMB_MAX {0x00, 0x84, 23}; // Current imbalance alarm limit. Positive values only + } - // Get Register - return(this->Register_Pointer_Read(I_IMB_MAX)); + // Under Frequency Alarm + inline bool Alarm_UFQ(void) { - // End Case - break; + // End Function + return(bitRead(this->Device.Status, __BIT_UN_FREQ__)); - } + } - } + // Voltage Imbalance Alarm + inline bool Alarm_VImb(void) { // End Function - return(0); + return(bitRead(this->Device.Status, __BIT_V_IMBAL__)); } - // Set Sticky Function - bool Get_Sticky(void) { - - // Define Register - Register STICKY {0x00, 0x2D, 0}; - - // Return Result - uint32_t _Sticky = this->Register_Pointer_Read(STICKY); + // Current Imbalance Alarm + inline bool Alarm_IImb(void) { // End Function - return(bitRead(_Sticky, 0) && bitRead(_Sticky, 23)); + return(bitRead(this->Device.Status, __BIT_I_IMBAL__)); } - // Get Alarm Mask - uint32_t Get_Alarm_Mask(const uint8_t _Mask) { - - // Control for __MASK_AL1__ - if (_Mask == __MASK_AL1__) { - - // Define Register - Register MASK1 {0x00, 0x1E, 0}; // Alarm Mask address 1 + // Under Power Factor Alarm + inline bool Alarm_UPF(void) { - // Get Register - return(this->Register_Pointer_Read(MASK1)); + // End Function + return(bitRead(this->Device.Status, __BIT_UN_PFA__) or bitRead(this->Device.Status, __BIT_UN_PFB__) or bitRead(this->Device.Status, __BIT_UN_PFC__)); - } else if (_Mask == __MASK_AL2__) { + } - // Define Register - Register MASK2 {0x00, 0x21, 0}; // Alarm Mask address 2 + // Over Temperature Alarm + inline bool Alarm_OT(void) { - // Get Register - return(this->Register_Pointer_Read(MASK2)); + // End Function + return(bitRead(this->Device.Status, __BIT_OV_TEMP__)); - } else { + } - // End Function - return(0); + // Under Temperature Alarm + inline bool Alarm_UT(void) { - } + // End Function + return(bitRead(this->Device.Status, __BIT_UN_TEMP__)); }