From 3dc3917a0608a9fd3de2e9952b15fcc55bf42f44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mehmet=20Gu=CC=88nce=20Akkoyun?= Date: Wed, 3 Apr 2024 15:45:10 +0300 Subject: [PATCH] function update --- library.json | 2 +- library.properties | 2 +- src/Definitions.h | 194 +++--- src/MAX78630.h | 1439 +++++++++++++++++++++++--------------------- 4 files changed, 863 insertions(+), 774 deletions(-) diff --git a/library.json b/library.json index f7b3ae9..5f3e75a 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "MAX78630", - "version": "4.11.1", + "version": "4.12.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 23e96c1..18368c0 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=MAX78630 -version=4.11.1 +version=4.12.0 author=Gunce Akkoyun maintainer=Gunce Akkoyun sentence=Enables reading polyphase energy-monitor parameters of LVX EnergyBoard. diff --git a/src/Definitions.h b/src/Definitions.h index f30afaf..07f16a5 100644 --- a/src/Definitions.h +++ b/src/Definitions.h @@ -1,91 +1,137 @@ +// Command Codes +#define __MAX78630_HEADER__ (uint8_t)0xAA +#define __MAX78630_CLEAR_ADRS__ (uint8_t)0xA0 +#define __MAX78630_RW_ADRS_LO_BYTE__ (uint8_t)0xA1 +#define __MAX78630_RW_ADRS_HI_BYTE__ (uint8_t)0xA2 +#define __MAX78630_RW_ADRS__ (uint8_t)0xA3 +#define __MAX78630_DE_SELECT_DEVICE__ (uint8_t)0xC0 +#define __MAX78630_SELECT_DEVICE__ (uint8_t)0xCF +#define __MAX78630_WRITE_BYTES__ (uint8_t)0xD0 +#define __MAX78630_WRITE_3_BYTES__ (uint8_t)0xD3 +#define __MAX78630_READ_BYTES__ (uint8_t)0xE0 +#define __MAX78630_READ_3_BYTES__ (uint8_t)0xE3 + +// Response Codes +#define __MAX78630_ACK_DATA__ (uint8_t)0xAA +#define __MAX78630_AUTO_REPORT__ (uint8_t)0xAE +#define __MAX78630_ACK_NO_DATA__ (uint8_t)0xAD +#define __MAX78630_NACK__ (uint8_t)0xB0 +#define __MAX78630_BAD_CMD__ (uint8_t)0xBC +#define __MAX78630_CHK_SUM_BAD__ (uint8_t)0xBD +#define __MAX78630_BUFF_OVRFLW__ (uint8_t)0xBF + +// Payload Size Codes +#define __MAX78630_1_BYTE__ (uint8_t)0x01 +#define __MAX78630_2_BYTES__ (uint8_t)0x02 +#define __MAX78630_3_BYTES__ (uint8_t)0x03 +#define __MAX78630_4_BYTES__ (uint8_t)0x04 +#define __MAX78630_5_BYTES__ (uint8_t)0x05 +#define __MAX78630_6_BYTES__ (uint8_t)0x06 +#define __MAX78630_7_BYTES__ (uint8_t)0x07 +#define __MAX78630_8_BYTES__ (uint8_t)0x08 +#define __MAX78630_9_BYTES__ (uint8_t)0x09 +#define __MAX78630_10_BYTES__ (uint8_t)0x0A + // Function Definitions -#define __MAX78630_CLEAR__ (uint8_t)0 -#define __MAX78630_GET__ (uint8_t)1 -#define __MAX78630_SET__ (uint8_t)2 +#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 -#define __Phase_S__ (uint8_t)2 -#define __Phase_T__ (uint8_t)3 -#define __Phase_Avg__ (uint8_t)4 -#define __Phase_Any__ (uint8_t)5 +#define __Phase_R__ (uint8_t)1 +#define __Phase_S__ (uint8_t)2 +#define __Phase_T__ (uint8_t)3 +#define __Phase_Avg__ (uint8_t)4 +#define __Phase_Any__ (uint8_t)5 // Measurement Type -#define __RMS__ (uint8_t)1 -#define __Instant__ (uint8_t)2 -#define __Fundamental__ (uint8_t)3 -#define __Harmonic__ (uint8_t)4 -#define __Peak__ (uint8_t)5 -#define __Active__ (uint8_t)6 -#define __ReActive__ (uint8_t)7 -#define __Apparent__ (uint8_t)8 -#define __Fund_ReActive__ (uint8_t)9 -#define __Harm_ReActive__ (uint8_t)10 -#define __Fund_VA__ (uint8_t)11 -#define __Active_Received__ (uint8_t)12 -#define __Active_Delivered__ (uint8_t)13 -#define __ReActive_Received__ (uint8_t)14 -#define __ReActive_Delivered__ (uint8_t)15 -#define __Energy_Reset__ (uint8_t)16 -#define __Phase_Compensation__ (uint8_t)17 +#define __RMS__ (uint8_t)1 +#define __Instant__ (uint8_t)2 +#define __Fundamental__ (uint8_t)3 +#define __Harmonic__ (uint8_t)4 +#define __Peak__ (uint8_t)5 +#define __Active__ (uint8_t)6 +#define __ReActive__ (uint8_t)7 +#define __Apparent__ (uint8_t)8 +#define __Fund_ReActive__ (uint8_t)9 +#define __Harm_ReActive__ (uint8_t)10 +#define __Fund_VA__ (uint8_t)11 +#define __Active_Received__ (uint8_t)12 +#define __Active_Delivered__ (uint8_t)13 +#define __ReActive_Received__ (uint8_t)14 +#define __ReActive_Delivered__ (uint8_t)15 +#define __Energy_Reset__ (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 +#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 -#define __VRMS_MAX__ (uint8_t)2 -#define __T_MIN__ (uint8_t)3 -#define __T_MAX__ (uint8_t)4 -#define __F_MIN__ (uint8_t)5 -#define __F_MAX__ (uint8_t)6 -#define __IRMS_MAX__ (uint8_t)7 -#define __PF_MIN__ (uint8_t)8 -#define __VIMB_MAX__ (uint8_t)9 -#define __IIMB_MAX__ (uint8_t)10 -#define __VSAG_LIM__ (uint8_t)11 +#define __VRMS_MIN__ (uint8_t)1 +#define __VRMS_MAX__ (uint8_t)2 +#define __T_MIN__ (uint8_t)3 +#define __T_MAX__ (uint8_t)4 +#define __F_MIN__ (uint8_t)5 +#define __F_MAX__ (uint8_t)6 +#define __IRMS_MAX__ (uint8_t)7 +#define __PF_MIN__ (uint8_t)8 +#define __VIMB_MAX__ (uint8_t)9 +#define __IIMB_MAX__ (uint8_t)10 +#define __VSAG_LIM__ (uint8_t)11 // Mask Definitions -#define __MASK_AL1__ (uint8_t)0x01 -#define __MASK_AL2__ (uint8_t)0x02 +#define __MASK_AL1__ (uint8_t)0x01 +#define __MASK_AL2__ (uint8_t)0x02 + +// Alarm Type Definitions +#define __ALARM_LOW_VOLTAGE__ (uint16_t)1 +#define __ALARM_HIGH_VOLTAGE__ (uint16_t)2 +#define __ALARM_HIGH_CURRENT__ (uint16_t)3 +#define __ALARM_LOW_FREQUENCY__ (uint16_t)4 +#define __ALARM_HIGH_FREQUENCY__ (uint16_t)5 +#define __ALARM_VOLTAGE_IMBAL__ (uint16_t)6 +#define __ALARM_CURRENT_IMBAL__ (uint16_t)7 +#define __ALARM_LOW_TEMPERATURE__ (uint16_t)8 +#define __ALARM_HIGH_TEMPERATURE__ (uint16_t)9 +#define __ALARM_LOW_POWERFACTOR__ (uint16_t)10 // Defect Bit Definitions -#define __BIT_OV_FREQ__ (uint8_t)22 -#define __BIT_UN_FREQ__ (uint8_t)21 -#define __BIT_OV_TEMP__ (uint8_t)20 -#define __BIT_UN_TEMP__ (uint8_t)19 -#define __BIT_OV_VRMSC__ (uint8_t)18 -#define __BIT_UN_VRMSC__ (uint8_t)17 -#define __BIT_OV_VRMSB__ (uint8_t)16 -#define __BIT_UN_VRMSB__ (uint8_t)15 -#define __BIT_OV_VRMSA__ (uint8_t)14 -#define __BIT_UN_VRMSA__ (uint8_t)13 -#define __BIT_UN_PFC__ (uint8_t)12 -#define __BIT_UN_PFB__ (uint8_t)11 -#define __BIT_UN_PFA__ (uint8_t)10 -#define __BIT_OV_IRMSC__ (uint8_t)9 -#define __BIT_OV_IRMSB__ (uint8_t)8 -#define __BIT_OV_IRMSA__ (uint8_t)7 -#define __BIT_VC_SAG__ (uint8_t)6 -#define __BIT_VB_SAG__ (uint8_t)5 -#define __BIT_VA_SAG__ (uint8_t)4 -#define __BIT_V_IMBAL__ (uint8_t)3 -#define __BIT_I_IMBAL__ (uint8_t)2 +#define __BIT_OV_FREQ__ (uint8_t)22 +#define __BIT_UN_FREQ__ (uint8_t)21 +#define __BIT_OV_TEMP__ (uint8_t)20 +#define __BIT_UN_TEMP__ (uint8_t)19 +#define __BIT_OV_VRMSC__ (uint8_t)18 +#define __BIT_UN_VRMSC__ (uint8_t)17 +#define __BIT_OV_VRMSB__ (uint8_t)16 +#define __BIT_UN_VRMSB__ (uint8_t)15 +#define __BIT_OV_VRMSA__ (uint8_t)14 +#define __BIT_UN_VRMSA__ (uint8_t)13 +#define __BIT_UN_PFC__ (uint8_t)12 +#define __BIT_UN_PFB__ (uint8_t)11 +#define __BIT_UN_PFA__ (uint8_t)10 +#define __BIT_OV_IRMSC__ (uint8_t)9 +#define __BIT_OV_IRMSB__ (uint8_t)8 +#define __BIT_OV_IRMSA__ (uint8_t)7 +#define __BIT_VC_SAG__ (uint8_t)6 +#define __BIT_VB_SAG__ (uint8_t)5 +#define __BIT_VA_SAG__ (uint8_t)4 +#define __BIT_V_IMBAL__ (uint8_t)3 +#define __BIT_I_IMBAL__ (uint8_t)2 // Monitor Address Definitions -#define __MAX78630_MONITOR_1__ (uint8_t)1 -#define __MAX78630_MONITOR_2__ (uint8_t)2 -#define __MAX78630_MONITOR_3__ (uint8_t)3 -#define __MAX78630_MONITOR_4__ (uint8_t)4 -#define __MAX78630_MONITOR_5__ (uint8_t)5 -#define __MAX78630_MONITOR_6__ (uint8_t)6 -#define __MAX78630_MONITOR_7__ (uint8_t)7 -#define __MAX78630_MONITOR_8__ (uint8_t)8 +#define __MAX78630_MONITOR_1__ (uint8_t)1 +#define __MAX78630_MONITOR_2__ (uint8_t)2 +#define __MAX78630_MONITOR_3__ (uint8_t)3 +#define __MAX78630_MONITOR_4__ (uint8_t)4 +#define __MAX78630_MONITOR_5__ (uint8_t)5 +#define __MAX78630_MONITOR_6__ (uint8_t)6 +#define __MAX78630_MONITOR_7__ (uint8_t)7 +#define __MAX78630_MONITOR_8__ (uint8_t)8 // Alarm Mask Definitions -#define __MAX78630_ALARM_1__ (uint8_t)1 -#define __MAX78630_ALARM_2__ (uint8_t)2 \ No newline at end of file +#define __MAX78630_ALARM_1__ (uint8_t)1 +#define __MAX78630_ALARM_2__ (uint8_t)2 \ No newline at end of file diff --git a/src/MAX78630.h b/src/MAX78630.h index 19f6548..1b2bd19 100644 --- a/src/MAX78630.h +++ b/src/MAX78630.h @@ -45,6 +45,32 @@ const uint8_t Data_Type; }; + // Calculate CheckSum Function + uint8_t CheckSum(const uint8_t _Low_Address, const uint8_t _High_Address) { + + // Calculate CheckSum + return(0x100 - ((__MAX78630_HEADER__ + __MAX78630_7_BYTES__ + __MAX78630_RW_ADRS__ + _Low_Address + _High_Address + __MAX78630_READ_3_BYTES__) % 256)); + + } + uint8_t CheckSum(const uint8_t _Low_Address, const uint8_t _High_Address, const uint8_t _Data1, const uint8_t _Data2, const uint8_t _Data3) { + + // Calculate CheckSum + return(0x100 - ((__MAX78630_HEADER__ + __MAX78630_10_BYTES__ + __MAX78630_RW_ADRS__ + _Low_Address + _High_Address + __MAX78630_WRITE_3_BYTES__ + _Data1 + _Data2 + _Data3) % 256)); + + } + uint8_t CheckSum(const uint8_t _Response[6]) { + + // Calculate CheckSum + return(0x100 - ((_Response[0] + _Response[1] + _Response[2] + _Response[3] + _Response[4]) % 256)); + + } + uint8_t CheckSum(const uint8_t _Address) { + + // Calculate CheckSum + return(0x100 - ((__MAX78630_HEADER__ + __MAX78630_4_BYTES__ + _Address) % 256)); + + } + // Clear Serial Buffer Function void Clear_Buffer(void) { @@ -58,7 +84,8 @@ Serial_Energy->read(); // Stream Delay - delay(5); + delayMicroseconds(20); + //delay(5); } @@ -81,26 +108,17 @@ 7. byte is the CRC correction byte (CHK) */ - // Clear Serial Buffer - this->Clear_Buffer(); - - // Command Send Delay - delay(5); - - // Calculate CheckSum - uint8_t _Request_CheckSum = 0x100 - ((0xAA + 0x07 + 0xA3 + _Command.Low_Address + _Command.High_Address + 0xE3) % 256); - // Send Command - Serial_Energy->write(0xAA); - Serial_Energy->write(0x07); - Serial_Energy->write(0xA3); + Serial_Energy->write(__MAX78630_HEADER__); + Serial_Energy->write(__MAX78630_7_BYTES__); + Serial_Energy->write(__MAX78630_RW_ADRS__); Serial_Energy->write(_Command.Low_Address); Serial_Energy->write(_Command.High_Address); - Serial_Energy->write(0xE3); - Serial_Energy->write(_Request_CheckSum); + Serial_Energy->write(__MAX78630_READ_3_BYTES__); + Serial_Energy->write(this->CheckSum(_Command.Low_Address, _Command.High_Address)); // Command Send Delay - delay(20); + delay(3); // Declare Variable uint8_t _Response[6]; @@ -117,18 +135,16 @@ _Response_Order++; // Stream Delay - delay(5); + delayMicroseconds(200); + //delay(1); } - // Calculate Response CheckSum - uint8_t _Response_CheckSum = 0x100 - ((_Response[0] + _Response[1] + _Response[2] + _Response[3] + _Response[4]) % 256); - // Declare Raw Data Variable double _Data_SUM = 0; // Control Received Data - if (_Response[0] == 0xAA and _Response[1] == 0x06 and _Response[5] == _Response_CheckSum) { + if (_Response[0] == __MAX78630_HEADER__ and _Response[1] == __MAX78630_6_BYTES__ and _Response[5] == this->CheckSum(_Response)) { // Combine Read Bytes uint32_t _Data_RAW = ((uint32_t)(_Response[4]) << 16 | ((uint32_t)_Response[3]) << 8 | ((uint32_t)_Response[2])); @@ -166,22 +182,22 @@ uint8_t _Parameter3 = _Data >> 16; // Send Command - Serial_Energy->write(0xAA); - Serial_Energy->write(0x0A); - Serial_Energy->write(0xA3); + Serial_Energy->write(__MAX78630_HEADER__); + Serial_Energy->write(__MAX78630_10_BYTES__); + Serial_Energy->write(__MAX78630_RW_ADRS__); Serial_Energy->write(_Command.Low_Address); Serial_Energy->write(_Command.High_Address); - Serial_Energy->write(0xD3); + Serial_Energy->write(__MAX78630_WRITE_3_BYTES__); Serial_Energy->write(_Parameter1); Serial_Energy->write(_Parameter2); Serial_Energy->write(_Parameter3); - Serial_Energy->write(0x100 - ((0xAA + 0x0A + 0xA3 + _Command.Low_Address + _Command.High_Address + 0xD3 + _Parameter1 + _Parameter2 + _Parameter3) % 256)); + Serial_Energy->write(this->CheckSum(_Command.Low_Address, _Command.High_Address, _Parameter1, _Parameter2, _Parameter3)); // Command Delay - delay(20); + delay(5); // End Function - if (Serial_Energy->read() == 0xAD) return(true); + if (Serial_Energy->read() == __MAX78630_ACK_NO_DATA__) return(true); // End Function return(false); @@ -189,7 +205,7 @@ } // Data Converter Function - uint32_t FtoS(double _Variable, uint8_t _Data_Type) { + int32_t FtoS(double _Variable, uint8_t _Data_Type) { // Declare Data Variable uint32_t _Data = 0x00; @@ -360,10 +376,6 @@ // Set Register bool _Set = this->Register_Pointer_Set(MMADDR0, __MAX78630_Monitor_1_Type__); - // Clear Register - 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(_Set); @@ -472,10 +484,6 @@ // Set Register bool _Set = this->Register_Pointer_Set(MMADDR7, __MAX78630_Monitor_8_Type__); - // Clear Register - 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(_Set); @@ -611,7 +619,6 @@ } - // Protected Context protected: @@ -644,20 +651,17 @@ // Select IC Function bool Select_IC(uint8_t _Address) { - // Command Send Delay - delay(5); - // 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__) + Serial_Energy->write(__MAX78630_HEADER__); // Header (0xAA) + Serial_Energy->write(__MAX78630_4_BYTES__); // Total Sended Byte (0x04) + Serial_Energy->write(_Address); // IC Address (0xC1, 0xC2, 0xC3, 0xC4) + Serial_Energy->write(this->CheckSum(_Address)); // CheckSum (0x__) // Command Send Delay - delay(20); + delay(2); // End Function - if (Serial_Energy->read() == 0xAD) return(true); + if (Serial_Energy->read() == __MAX78630_ACK_NO_DATA__) return(true); // End Function return(false); @@ -743,477 +747,195 @@ } - /* Min Max Functions */ + /* Calibration Functions */ - // Min Record Value Function - float Min_Value(const uint8_t _Function, const uint8_t _MM_ADDR, const uint16_t _Scale = 1) { + // Calibration Function + bool Calibration(const uint8_t _Type, const uint8_t _Phase, const float _Gain, const float _Offset) { - // Control for Function - if (_Function == __MAX78630_SET__) return(0); + // Control Input Parameters + if (_Phase > 3) return(false); - // Control for Mask - if (_MM_ADDR > 8) return(0); + // Control for Calibration Type + switch (_Type) { - // Decide Address - switch (_MM_ADDR) { + // Voltage Calibration + case __Voltage__: { - case __MAX78630_MONITOR_1__: { + // Control for Phase + switch (_Phase) { + + // Phase R + case __Phase_R__: { - // Define Register - Register MIN0 {0x01, 0x89, 23}; // Minimum Recorded Value 1 + // Define Register + Register V1_GAIN {0x00, 0x5D, 21}; + Register V1_OFFS {0x00, 0x6F, 23}; - // Return Register - float _Min_Value = this->Register_Pointer_Read(MIN0) * _Scale; + 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 - // Clear Register - if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MIN0, 0x00); + // End Function + return(_Result_Gain and _Result_Offset); - // End Function - return(_Min_Value); + } - } - case __MAX78630_MONITOR_2__: { + // Phase S + case __Phase_S__: { - // Define Register - Register MIN1 {0x01, 0x8C, 23}; // Minimum Recorded Value 2 + // Define Register + Register V2_GAIN {0x00, 0x60, 21}; + Register V2_OFFS {0x00, 0x72, 23}; - // Return Register - float _Min_Value = this->Register_Pointer_Read(MIN1) * _Scale; + 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 - // Clear Register - if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MIN1, 0x00); + // End Function + return(_Result_Gain and _Result_Offset); - // End Function - return(_Min_Value); + } - } - case __MAX78630_MONITOR_3__: { + // Phase T + case __Phase_T__: { - // Define Register - Register MIN2 {0x01, 0x8F, 23}; // Minimum Recorded Value 3 + // Define Register + Register V3_GAIN {0x00, 0x63, 21}; + Register V3_OFFS {0x00, 0x75, 23}; - // Return Register - float _Min_Value = this->Register_Pointer_Read(MIN2) * _Scale; + 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 - // Clear Register - if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MIN2, 0x00); + // End Function + return(_Result_Gain and _Result_Offset); - // End Function - return(_Min_Value); + } + + // No Phase + default: { + + // End Function + return(false); + + } + + } } - case __MAX78630_MONITOR_4__: { - // Define Register - Register MIN3 {0x01, 0x92, 23}; // Minimum Recorded Value 4 + // Current Calibration + case __Current__: { - // Return Register - float _Min_Value = this->Register_Pointer_Read(MIN3) * _Scale; + // Control for Phase + switch (_Phase) { + + // Phase R + case __Phase_R__: { - // Clear Register - if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MIN3, 0x00); + // Define Register + Register I1_GAIN {0x00, 0x54, 21}; + Register I1_OFFS {0x00, 0x66, 23}; - // End Function - return(_Min_Value); + 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 - } - case __MAX78630_MONITOR_5__: { + // End Function + return(_Result_Gain and _Result_Offset); - // Define Register - Register MIN4 {0x01, 0x95, 23}; // Minimum Recorded Value 5 + } - // Return Register - float _Min_Value = this->Register_Pointer_Read(MIN4) * _Scale; + // Phase S + case __Phase_S__: { - // Clear Register - if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MIN4, 0x00); + // Define Register + Register I2_GAIN {0x00, 0x57, 21}; + Register I2_OFFS {0x00, 0x69, 23}; - // End Function - return(_Min_Value); + 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 - } - case __MAX78630_MONITOR_6__: { + // End Function + return(_Result_Gain and _Result_Offset); - // Define Register - Register MIN5 {0x01, 0x98, 23}; // Minimum Recorded Value 6 + } - // Return Register - float _Min_Value = this->Register_Pointer_Read(MIN5) * _Scale; + // Phase T + case __Phase_T__: { - // Clear Register - if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MIN5, 0x00); + // Define Register + Register I3_GAIN {0x00, 0x5A, 21}; + Register I3_OFFS {0x00, 0x6C, 23}; - // End Function - return(_Min_Value); + 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 - } - case __MAX78630_MONITOR_7__: { + // End Function + return(_Result_Gain and _Result_Offset); - // Define Register - Register MIN6 {0x01, 0x9B, 0}; // Minimum Recorded Value 7 + } - // Return Register - float _Min_Value = this->Register_Pointer_Read(MIN6) * _Scale; + // No Phase + default: { - // Clear Register - if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MIN6, 0x00); + // End Function + return(false); - // End Function - return(_Min_Value); + } + + } } - case __MAX78630_MONITOR_8__: { + + // Temperature Calibration + case __Temperature__: { // Define Register - Register MIN7 {0x01, 0x9E, 0}; // Minimum Recorded Value 8 + Register T_GAIN {0x00, 0x78, 0}; + Register T_OFFS {0x00, 0x7B, 0}; - // Return Register - float _Min_Value = this->Register_Pointer_Read(MIN7) * _Scale; + 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 - // Clear Register - if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MIN7, 0x00); + // End Function + return(_Result_Gain and _Result_Offset); + + } + + // No Calibration + default: { // End Function - return(_Min_Value); + return(false); } } - // End Function - return(0); - } - // Max Record Value Function - float Max_Value(const uint8_t _Function, const uint8_t _MM_ADDR, const uint16_t _Scale = 1) { + // Coefficients Function + float HPF_COEF(const uint8_t _Function, const uint8_t _Type, float _COEF) { - // Control for Function - if (_Function == __MAX78630_SET__) return(0); + // Control for Type + if (_Type == __Voltage__) { - // Control for Mask - if (_MM_ADDR > 8) return(0); + // Define Register + Register HPF_COEF_V {0x00, 0x3F, 23}; // Voltage input HPF coefficient. Positive values only - // Decide Address - switch (_MM_ADDR) { + // Read Register + float _Result = this->Register_Pointer_Read(HPF_COEF_V); - case __MAX78630_MONITOR_1__: { + // Set Register + if (_Function == __MAX78630_SET__) this->Register_Pointer_Set(HPF_COEF_V, this->FtoS(_COEF, 23)); - // Define Register - Register MAX0 {0x01, 0xA1, 23}; // Maximum Recorded Value 1 + // End Function + return(_Result); - // Return Register - float _Max_Value = this->Register_Pointer_Read(MAX0) * _Scale; + } else if (_Type == __Current__) { - // Clear Register - if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MAX0, 0x00); + // Define Register + Register HPF_COEF_I {0x00, 0x3C, 23}; // Current input HPF coefficient. Positive values only - // End Function - return(_Max_Value); - - } - case __MAX78630_MONITOR_2__: { - - // Define Register - Register MAX1 {0x01, 0xA4, 23}; // Maximum Recorded Value 2 - - // Return Register - float _Max_Value = this->Register_Pointer_Read(MAX1) * _Scale; - - // Clear Register - if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MAX1, 0x00); - - // End Function - return(_Max_Value); - - } - case __MAX78630_MONITOR_3__: { - - // Define Register - Register MAX2 {0x01, 0xA7, 23}; // Maximum Recorded Value 3 - - // Return Register - float _Max_Value = this->Register_Pointer_Read(MAX2) * _Scale; - - // Clear Register - if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MAX2, 0x00); - - // End Function - return(_Max_Value); - - } - case __MAX78630_MONITOR_4__: { - - // Define Register - Register MAX3 {0x01, 0xAA, 23}; // Maximum Recorded Value 4 - - // Return Register - float _Max_Value = this->Register_Pointer_Read(MAX3) * _Scale; - - // Clear Register - if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MAX3, 0x00); - - // End Function - return(_Max_Value); - - } - case __MAX78630_MONITOR_5__: { - - // Define Register - Register MAX4 {0x01, 0xAD, 23}; // Maximum Recorded Value 5 - - // Return Register - float _Max_Value = this->Register_Pointer_Read(MAX4) * _Scale; - - // Clear Register - if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MAX4, 0x00); - - // End Function - return(_Max_Value); - - } - case __MAX78630_MONITOR_6__: { - - // Define Register - Register MAX5 {0x01, 0xB0, 23}; // Maximum Recorded Value 6 - - // Return Register - float _Max_Value = this->Register_Pointer_Read(MAX5) * _Scale; - - // Clear Register - if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MAX5, 0x00); - - // End Function - return(_Max_Value); - - } - case __MAX78630_MONITOR_7__: { - - // Define Register - Register MAX6 {0x01, 0xB3, 0}; // Maximum Recorded Value 7 - - // Return Register - float _Max_Value = this->Register_Pointer_Read(MAX6) * _Scale; - - // Clear Register - if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MAX6, 0x00); - - // End Function - return(_Max_Value); - - } - case __MAX78630_MONITOR_8__: { - - // Define Register - Register MAX7 {0x01, 0xB6, 0}; // Maximum Recorded Value 8 - - // Return Register - float _Max_Value = this->Register_Pointer_Read(MAX7) * _Scale; - - // Clear Register - if (_Function == __MAX78630_CLEAR__) Register_Pointer_Set(MAX7, 0x00); - - // End Function - return(_Max_Value); - - } - - } - - // End Function - return(0); - - } - - /* Calibration Functions */ - - // Calibration Function - bool Calibration(const uint8_t _Type, const uint8_t _Phase, const float _Gain, const float _Offset) { - - // Control Input Parameters - if (_Phase > 3) return(false); - - // Control for Calibration Type - switch (_Type) { - - // Voltage Calibration - case __Voltage__: { - - // Control for Phase - switch (_Phase) { - - // Phase R - case __Phase_R__: { - - // Define Register - Register V1_GAIN {0x00, 0x5D, 21}; - Register V1_OFFS {0x00, 0x6F, 23}; - - 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); - - } - - // Phase S - case __Phase_S__: { - - // 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 - - // End Function - return(_Result_Gain and _Result_Offset); - - } - - // Phase T - case __Phase_T__: { - - // Define Register - Register V3_GAIN {0x00, 0x63, 21}; - Register V3_OFFS {0x00, 0x75, 23}; - - 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 - - // End Function - return(_Result_Gain and _Result_Offset); - - } - - // No Phase - default: { - - // End Function - return(false); - - } - - } - - } - - // Current Calibration - case __Current__: { - - // Control for Phase - switch (_Phase) { - - // Phase R - case __Phase_R__: { - - // Define Register - Register I1_GAIN {0x00, 0x54, 21}; - Register I1_OFFS {0x00, 0x66, 23}; - - 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); - - } - - // Phase S - case __Phase_S__: { - - // 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 - - // End Function - return(_Result_Gain and _Result_Offset); - - } - - // Phase T - case __Phase_T__: { - - // 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 - - // End Function - return(_Result_Gain and _Result_Offset); - - } - - // No Phase - default: { - - // End Function - return(false); - - } - - } - - } - - // Temperature Calibration - case __Temperature__: { - - // Define Register - Register T_GAIN {0x00, 0x78, 0}; - Register T_OFFS {0x00, 0x7B, 0}; - - 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 - - // End Function - return(_Result_Gain and _Result_Offset); - - } - - // No Calibration - default: { - - // End Function - return(false); - - } - - } - - } - - // Coefficients Function - float HPF_COEF(const uint8_t _Function, const uint8_t _Type, float _COEF) { - - // Control for Type - if (_Type == __Voltage__) { - - // Define Register - Register HPF_COEF_V {0x00, 0x3F, 23}; // Voltage input HPF coefficient. Positive values only - - // Read Register - float _Result = this->Register_Pointer_Read(HPF_COEF_V); - - // Set Register - if (_Function == __MAX78630_SET__) this->Register_Pointer_Set(HPF_COEF_V, this->FtoS(_COEF, 23)); - - // End Function - return(_Result); - - } else if (_Type == __Current__) { - - // Define Register - Register HPF_COEF_I {0x00, 0x3C, 23}; // Current input HPF coefficient. Positive values only - - // Read Register - float _Result = this->Register_Pointer_Read(HPF_COEF_I); + // Read Register + float _Result = this->Register_Pointer_Read(HPF_COEF_I); // Set Register if (_Function == __MAX78630_SET__) this->Register_Pointer_Set(HPF_COEF_I, this->FtoS(_COEF, 23)); @@ -1592,7 +1314,6 @@ } - // Public Context public: @@ -1769,9 +1490,6 @@ // Set Low Temperature Alarm --> AL2 this->Alarm_Mask(__MAX78630_SET__, __MAX78630_ALARM_2__, __BIT_UN_TEMP__, true); - // Clear Status - this->Status(__MAX78630_GET__); - } } @@ -2755,37 +2473,213 @@ // Decide Type switch (_Type) { - case __Active__: { + case __Active__: { + + // Define Register + Register WATT_T {0x01, 0x38, 23}; // Active Power average (Total / 3) + + // Get Register + return(this->Register_Pointer_Read(WATT_T) * __MAX78630_Config_VScale__ * __MAX78630_Config_IScale__); + + // End Case + break; + + } + case __ReActive__: { + + // Define Register + Register VAR_T {0x01, 0x3B, 23}; // Reactive Power average (Total / 3) + + // Get Register + return(this->Register_Pointer_Read(VAR_T) * __MAX78630_Config_VScale__ * __MAX78630_Config_IScale__); + + // End Case + break; + + } + case __Apparent__: { + + // Define Register + Register VA_T {0x01, 0x3E, 23}; // Apparent Power average (Total / 3) + + // Get Register + return(this->Register_Pointer_Read(VA_T) * __MAX78630_Config_VScale__ * __MAX78630_Config_IScale__); + + // End Case + break; + + } + + } + + // End Case + break; + + } + + } + + // End Function + return(0); + + } + + // Energy Measurement Function. + float Energy(const uint8_t _Phase, const uint8_t _Type) { + + // Decide Phase + switch (_Phase) { + + case __Phase_R__: { + + // Decide Type + switch (_Type) { + + case __Active_Received__: { + + // Define Register + Register WHA_POS {0x01, 0xDD, 0}; // Received Active Energy Counter + + // Get Register + return(this->Register_Pointer_Read(WHA_POS)); + + // End Case + break; + + } + case __Active_Delivered__: { + + // Define Register + Register WHA_NEG {0x01, 0xE6, 0}; // Delivered Active Energy Counter + + // Get Register + return(this->Register_Pointer_Read(WHA_NEG)); + + // End Case + break; + + } + case __ReActive_Received__: { + + // Define Register + Register VARHA_POS {0x02, 0x13, 0}; // Reactive Energy Leading Counter + + // Get Register + return(this->Register_Pointer_Read(VARHA_POS)); + + // End Case + break; + + } + case __ReActive_Delivered__: { + + // Define Register + Register VARHA_NEG {0x02, 0x1C, 0}; // Reactive Energy Lagging Counter + + // Get Register + return(this->Register_Pointer_Read(VARHA_NEG)); + + // End Case + break; + + } + case __Energy_Reset__: { + + // Define Register + Register WHA_POS {0x01, 0xDD, 0}; // Received Active Energy Counter + Register WHA_NEG {0x01, 0xE6, 0}; // Delivered Active Energy Counter + Register VARHA_POS {0x02, 0x13, 0}; // Reactive Energy Leading Counter + Register VARHA_NEG {0x02, 0x1C, 0}; // Reactive Energy Lagging Counter + + // Clear Register + this->Register_Pointer_Set(WHA_POS, 0x00); + this->Register_Pointer_Set(VARHA_POS, 0x00); + this->Register_Pointer_Set(WHA_NEG, 0x00); + this->Register_Pointer_Set(VARHA_NEG, 0x00); + + // End Function + return(1); + + // End Case + break; + + } + + } + + // End Case + break; + + } + case __Phase_S__: { + + // Decide Type + switch (_Type) { + + case __Active_Received__: { + + // Define Register + Register WHB_POS {0x01, 0xEF, 0}; // Received Active Energy Counter + + // Get Register + return(this->Register_Pointer_Read(WHB_POS)); + + // End Case + break; + + } + case __Active_Delivered__: { + + // Define Register + Register WHB_NEG {0x01, 0xF8, 0}; // Delivered Active Energy Counter + + // Get Register + return(this->Register_Pointer_Read(WHB_NEG)); + + // End Case + break; + + } + case __ReActive_Received__: { // Define Register - Register WATT_T {0x01, 0x38, 23}; // Active Power average (Total / 3) + Register VARHB_POS {0x02, 0x25, 0}; // Reactive Energy Leading Counter // Get Register - return(this->Register_Pointer_Read(WATT_T) * __MAX78630_Config_VScale__ * __MAX78630_Config_IScale__); + return(this->Register_Pointer_Read(VARHB_POS)); // End Case break; } - case __ReActive__: { + case __ReActive_Delivered__: { // Define Register - Register VAR_T {0x01, 0x3B, 23}; // Reactive Power average (Total / 3) + Register VARHB_NEG {0x02, 0x2E, 0}; // Reactive Energy Lagging Counter // Get Register - return(this->Register_Pointer_Read(VAR_T) * __MAX78630_Config_VScale__ * __MAX78630_Config_IScale__); + return(this->Register_Pointer_Read(VARHB_NEG)); // End Case break; } - case __Apparent__: { + case __Energy_Reset__: { // Define Register - Register VA_T {0x01, 0x3E, 23}; // Apparent Power average (Total / 3) + Register WHB_POS {0x01, 0xEF, 0}; // Received Active Energy Counter + Register WHB_NEG {0x01, 0xF8, 0}; // Delivered Active Energy Counter + Register VARHB_POS {0x02, 0x25, 0}; // Reactive Energy Leading Counter + Register VARHB_NEG {0x02, 0x2E, 0}; // Reactive Energy Lagging Counter - // Get Register - return(this->Register_Pointer_Read(VA_T) * __MAX78630_Config_VScale__ * __MAX78630_Config_IScale__); + // Clear Register + this->Register_Pointer_Set(WHB_POS, 0x00); + this->Register_Pointer_Set(VARHB_POS, 0x00); + this->Register_Pointer_Set(WHB_NEG, 0x00); + this->Register_Pointer_Set(VARHB_NEG, 0x00); + + // End Function + return(1); // End Case break; @@ -2798,21 +2692,7 @@ break; } - - } - - // End Function - return(0); - - } - - // Energy Measurement Function. - float Energy(const uint8_t _Phase, const uint8_t _Type) { - - // Decide Phase - switch (_Phase) { - - case __Phase_R__: { + case __Phase_T__: { // Decide Type switch (_Type) { @@ -2820,10 +2700,10 @@ case __Active_Received__: { // Define Register - Register WHA_POS {0x01, 0xDD, 0}; // Received Active Energy Counter + Register WHC_POS {0x02, 0x01, 0}; // Received Active Energy Counter // Get Register - return(this->Register_Pointer_Read(WHA_POS)); + return(this->Register_Pointer_Read(WHC_POS)); // End Case break; @@ -2832,10 +2712,10 @@ case __Active_Delivered__: { // Define Register - Register WHA_NEG {0x01, 0xE6, 0}; // Delivered Active Energy Counter + Register WHC_NEG {0x02, 0x0A, 0}; // Delivered Active Energy Counter // Get Register - return(this->Register_Pointer_Read(WHA_NEG)); + return(this->Register_Pointer_Read(WHC_NEG)); // End Case break; @@ -2844,10 +2724,10 @@ case __ReActive_Received__: { // Define Register - Register VARHA_POS {0x02, 0x13, 0}; // Reactive Energy Leading Counter + Register VARHC_POS {0x02, 0x37, 0}; // Reactive Energy Leading Counter // Get Register - return(this->Register_Pointer_Read(VARHA_POS)); + return(this->Register_Pointer_Read(VARHC_POS)); // End Case break; @@ -2856,10 +2736,10 @@ case __ReActive_Delivered__: { // Define Register - Register VARHA_NEG {0x02, 0x1C, 0}; // Reactive Energy Lagging Counter + Register VARHC_NEG {0x02, 0x40, 0}; // Reactive Energy Lagging Counter // Get Register - return(this->Register_Pointer_Read(VARHA_NEG)); + return(this->Register_Pointer_Read(VARHC_NEG)); // End Case break; @@ -2868,16 +2748,16 @@ case __Energy_Reset__: { // Define Register - Register WHA_POS {0x01, 0xDD, 0}; // Received Active Energy Counter - Register WHA_NEG {0x01, 0xE6, 0}; // Delivered Active Energy Counter - Register VARHA_POS {0x02, 0x13, 0}; // Reactive Energy Leading Counter - Register VARHA_NEG {0x02, 0x1C, 0}; // Reactive Energy Lagging Counter + 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(WHA_POS, 0x00); - this->Register_Pointer_Set(VARHA_POS, 0x00); - this->Register_Pointer_Set(WHA_NEG, 0x00); - this->Register_Pointer_Set(VARHA_NEG, 0x00); + 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); @@ -2893,211 +2773,331 @@ break; } - case __Phase_S__: { - // Decide Type - switch (_Type) { + } - case __Active_Received__: { + // End Function + return(0); - // Define Register - Register WHB_POS {0x01, 0xEF, 0}; // Received Active Energy Counter + } - // Get Register - return(this->Register_Pointer_Read(WHB_POS)); + // IC Temperature Measurement Function. + float Temperature(void) { - // End Case - break; + // Define Register + Register TEMPC {0x01, 0x74, 10}; // Chip Temperature (CelsiusĀ°) - } - case __Active_Delivered__: { + // Return Register + return(this->Register_Pointer_Read(TEMPC)); - // Define Register - Register WHB_NEG {0x01, 0xF8, 0}; // Delivered Active Energy Counter + } - // Get Register - return(this->Register_Pointer_Read(WHB_NEG)); + // Set Harmonic Channel Function. TODO: Not Tested. + uint8_t Harmonic(const uint8_t _Function, uint32_t _Harmonic) { - // End Case - break; + // Define Register + Register HARM {0x00, 0x4B, 0}; // Harmonic Selector, default: 1 (fundamental) - } - case __ReActive_Received__: { + // Control for Function + if (_Function == __MAX78630_GET__) { - // Define Register - Register VARHB_POS {0x02, 0x25, 0}; // Reactive Energy Leading Counter + // Read Register + return(this->Register_Pointer_Read(HARM)); - // Get Register - return(this->Register_Pointer_Read(VARHB_POS)); + } else if (_Function == __MAX78630_SET__) { - // End Case - break; + // Set Register + return(this->Register_Pointer_Set(HARM, _Harmonic)); - } - case __ReActive_Delivered__: { + } else { - // Define Register - Register VARHB_NEG {0x02, 0x2E, 0}; // Reactive Energy Lagging Counter + // End Function + return(0); + + } + + } + + /* Min Max Functions */ + + // Min Record Value Function + float Min_Value(const uint8_t _Function, const uint8_t _MM_ADDR, const uint16_t _Scale = 1) { + + // Control for Function + if (_Function == __MAX78630_SET__) return(0); + + // Control for Mask + if (_MM_ADDR > 8) return(0); + + // Decide Address + switch (_MM_ADDR) { + + case __MAX78630_MONITOR_1__: { + + // Define Register + Register MIN0 {0x01, 0x89, 23}; // Minimum Recorded Value 1 + + // Return Register + float _Min_Value = this->Register_Pointer_Read(MIN0) * _Scale; + + // Clear Register + Register_Pointer_Set(MIN0, 0x00); + + // End Function + return(_Min_Value); + + } + case __MAX78630_MONITOR_2__: { + + // Define Register + Register MIN1 {0x01, 0x8C, 23}; // Minimum Recorded Value 2 + + // Return Register + float _Min_Value = this->Register_Pointer_Read(MIN1) * _Scale; + + // Clear Register + Register_Pointer_Set(MIN1, 0x00); + + // End Function + return(_Min_Value); + + } + case __MAX78630_MONITOR_3__: { + + // Define Register + Register MIN2 {0x01, 0x8F, 23}; // Minimum Recorded Value 3 + + // Return Register + float _Min_Value = this->Register_Pointer_Read(MIN2) * _Scale; + + // Clear Register + Register_Pointer_Set(MIN2, 0x00); + + // End Function + return(_Min_Value); + + } + case __MAX78630_MONITOR_4__: { + + // Define Register + Register MIN3 {0x01, 0x92, 23}; // Minimum Recorded Value 4 + + // Return Register + float _Min_Value = this->Register_Pointer_Read(MIN3) * _Scale; + + // Clear Register + Register_Pointer_Set(MIN3, 0x00); + + // End Function + return(_Min_Value); + + } + case __MAX78630_MONITOR_5__: { + + // Define Register + Register MIN4 {0x01, 0x95, 23}; // Minimum Recorded Value 5 + + // Return Register + float _Min_Value = this->Register_Pointer_Read(MIN4) * _Scale; + + // Clear Register + Register_Pointer_Set(MIN4, 0x00); + + // End Function + return(_Min_Value); + + } + case __MAX78630_MONITOR_6__: { + + // Define Register + Register MIN5 {0x01, 0x98, 23}; // Minimum Recorded Value 6 + + // Return Register + float _Min_Value = this->Register_Pointer_Read(MIN5) * _Scale; + + // Clear Register + Register_Pointer_Set(MIN5, 0x00); + + // End Function + return(_Min_Value); + + } + case __MAX78630_MONITOR_7__: { + + // Define Register + Register MIN6 {0x01, 0x9B, 0}; // Minimum Recorded Value 7 + + // Return Register + float _Min_Value = this->Register_Pointer_Read(MIN6) * _Scale; + + // Clear Register + Register_Pointer_Set(MIN6, 0x00); + + // End Function + return(_Min_Value); + + } + case __MAX78630_MONITOR_8__: { + + // Define Register + Register MIN7 {0x01, 0x9E, 16}; // Minimum Recorded Value 8 - // Get Register - return(this->Register_Pointer_Read(VARHB_NEG)); + // Return Register + float _Min_Value = this->Register_Pointer_Read(MIN7) * _Scale; - // End Case - break; + // Clear Register + Register_Pointer_Set(MIN7, 0x00); - } - case __Energy_Reset__: { + // End Function + return(_Min_Value); - // Define Register - Register WHB_POS {0x01, 0xEF, 0}; // Received Active Energy Counter - Register WHB_NEG {0x01, 0xF8, 0}; // Delivered Active Energy Counter - Register VARHB_POS {0x02, 0x25, 0}; // Reactive Energy Leading Counter - Register VARHB_NEG {0x02, 0x2E, 0}; // Reactive Energy Lagging Counter + } - // Clear Register - this->Register_Pointer_Set(WHB_POS, 0x00); - this->Register_Pointer_Set(VARHB_POS, 0x00); - this->Register_Pointer_Set(WHB_NEG, 0x00); - this->Register_Pointer_Set(VARHB_NEG, 0x00); + } - // End Function - return(1); + // End Function + return(0); - // End Case - break; + } - } + // Max Record Value Function + float Max_Value(const uint8_t _Function, const uint8_t _MM_ADDR, const uint16_t _Scale = 1) { - } + // Control for Function + if (_Function == __MAX78630_SET__) return(0); - // End Case - break; + // Control for Mask + if (_MM_ADDR > 8) return(0); - } - case __Phase_T__: { + // Decide Address + switch (_MM_ADDR) { - // Decide Type - switch (_Type) { + case __MAX78630_MONITOR_1__: { - case __Active_Received__: { + // Define Register + Register MAX0 {0x01, 0xA1, 23}; // Maximum Recorded Value 1 - // Define Register - Register WHC_POS {0x02, 0x01, 0}; // Received Active Energy Counter + // Return Register + float _Max_Value = this->Register_Pointer_Read(MAX0) * _Scale; - // Get Register - return(this->Register_Pointer_Read(WHC_POS)); + // Clear Register + Register_Pointer_Set(MAX0, 0x00); - // End Case - break; + // End Function + return(_Max_Value); - } - case __Active_Delivered__: { + } + case __MAX78630_MONITOR_2__: { - // Define Register - Register WHC_NEG {0x02, 0x0A, 0}; // Delivered Active Energy Counter + // Define Register + Register MAX1 {0x01, 0xA4, 23}; // Maximum Recorded Value 2 - // Get Register - return(this->Register_Pointer_Read(WHC_NEG)); + // Return Register + float _Max_Value = this->Register_Pointer_Read(MAX1) * _Scale; - // End Case - break; + // Clear Register + Register_Pointer_Set(MAX1, 0x00); - } - case __ReActive_Received__: { + // End Function + return(_Max_Value); - // Define Register - Register VARHC_POS {0x02, 0x37, 0}; // Reactive Energy Leading Counter + } + case __MAX78630_MONITOR_3__: { - // Get Register - return(this->Register_Pointer_Read(VARHC_POS)); + // Define Register + Register MAX2 {0x01, 0xA7, 23}; // Maximum Recorded Value 3 - // End Case - break; + // Return Register + float _Max_Value = this->Register_Pointer_Read(MAX2) * _Scale; - } - case __ReActive_Delivered__: { + // Clear Register + Register_Pointer_Set(MAX2, 0x00); - // Define Register - Register VARHC_NEG {0x02, 0x40, 0}; // Reactive Energy Lagging Counter + // End Function + return(_Max_Value); - // Get Register - return(this->Register_Pointer_Read(VARHC_NEG)); + } + case __MAX78630_MONITOR_4__: { - // End Case - break; + // Define Register + Register MAX3 {0x01, 0xAA, 23}; // Maximum Recorded Value 4 - } - case __Energy_Reset__: { + // Return Register + float _Max_Value = this->Register_Pointer_Read(MAX3) * _Scale; - // 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 + Register_Pointer_Set(MAX3, 0x00); - // 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(_Max_Value); - // End Function - return(1); + } + case __MAX78630_MONITOR_5__: { - // End Case - break; + // Define Register + Register MAX4 {0x01, 0xAD, 23}; // Maximum Recorded Value 5 - } + // Return Register + float _Max_Value = this->Register_Pointer_Read(MAX4) * _Scale; - } + // Clear Register + Register_Pointer_Set(MAX4, 0x00); - // End Case - break; + // End Function + return(_Max_Value); } + case __MAX78630_MONITOR_6__: { - } + // Define Register + Register MAX5 {0x01, 0xB0, 23}; // Maximum Recorded Value 6 - // End Function - return(0); + // Return Register + float _Max_Value = this->Register_Pointer_Read(MAX5) * _Scale; - } + // Clear Register + Register_Pointer_Set(MAX5, 0x00); - // IC Temperature Measurement Function. - float Temperature(void) { + // End Function + return(_Max_Value); - // Define Register - Register TEMPC {0x01, 0x74, 10}; // Chip Temperature (CelsiusĀ°) + } + case __MAX78630_MONITOR_7__: { - // Return Register - return(this->Register_Pointer_Read(TEMPC)); + // Define Register + Register MAX6 {0x01, 0xB3, 0}; // Maximum Recorded Value 7 - } + // Return Register + float _Max_Value = this->Register_Pointer_Read(MAX6) * _Scale; - // Set Harmonic Channel Function. TODO: Not Tested. - uint8_t Harmonic(const uint8_t _Function, uint32_t _Harmonic) { + // Clear Register + Register_Pointer_Set(MAX6, 0x00); - // Define Register - Register HARM {0x00, 0x4B, 0}; // Harmonic Selector, default: 1 (fundamental) + // End Function + return(_Max_Value); - // Control for Function - if (_Function == __MAX78630_GET__) { + } + case __MAX78630_MONITOR_8__: { - // Read Register - return(this->Register_Pointer_Read(HARM)); + // Define Register + Register MAX7 {0x01, 0xB6, 16}; // Maximum Recorded Value 8 - } else if (_Function == __MAX78630_SET__) { + // Return Register + float _Max_Value = this->Register_Pointer_Read(MAX7) * _Scale; - // Set Register - return(this->Register_Pointer_Set(HARM, _Harmonic)); + // Clear Register + Register_Pointer_Set(MAX7, 0x00); - } else { + // End Function + return(_Max_Value); - // End Function - return(0); + } } + // End Function + return(0); + } /* Limit Functions */ @@ -3105,11 +3105,8 @@ // Adjust Limit Parameters Function. void Adjust_Limit(void) { - // Update Status - this->Status(__MAX78630_GET__); - - // Control for OV Temp - if (bitRead(this->Device.Status, __BIT_OV_TEMP__)) { + // Control for High Temperature + if (this->Alarm(__ALARM_HIGH_TEMPERATURE__)) { // Set Limit this->Limit(__MAX78630_SET__, __T_MAX__, (this->Limit_Variables.T_Max - __MAX78630_Limit_Temperature_Max_Diff__)); @@ -3121,8 +3118,8 @@ } - // Control for OV Temp - if (bitRead(this->Device.Status, __BIT_UN_TEMP__)) { + // Control for Low Temperature + if (this->Alarm(__ALARM_LOW_TEMPERATURE__)) { // Set Limit this->Limit(__MAX78630_SET__, __T_MIN__, (this->Limit_Variables.T_Min + __MAX78630_Limit_Temperature_Min_Diff__)); @@ -3134,8 +3131,8 @@ } - // Control for OV Voltage - if (bitRead(this->Device.Status, __BIT_OV_VRMSA__) or bitRead(this->Device.Status, __BIT_OV_VRMSB__) or bitRead(this->Device.Status, __BIT_OV_VRMSC__)) { + // Control for High Voltage + if (this->Alarm(__ALARM_HIGH_VOLTAGE__)) { // Set Limit this->Limit(__MAX78630_SET__, __VRMS_MAX__, (this->Limit_Variables.V_Max - __MAX78630_Limit_Voltage_Max_Diff__)); @@ -3147,8 +3144,8 @@ } - // Control for UV Voltage - if (bitRead(this->Device.Status, __BIT_UN_VRMSA__) or bitRead(this->Device.Status, __BIT_UN_VRMSB__) or bitRead(this->Device.Status, __BIT_UN_VRMSC__)) { + // Control for Low Voltage + if (this->Alarm(__ALARM_LOW_VOLTAGE__)) { // Set Limit this->Limit(__MAX78630_SET__, __VRMS_MIN__, (this->Limit_Variables.V_Min + __MAX78630_Limit_Voltage_Min_Diff__)); @@ -3161,7 +3158,7 @@ } // Control for High Frequency - if (bitRead(this->Device.Status, __BIT_OV_FREQ__)) { + if (this->Alarm(__ALARM_HIGH_FREQUENCY__)) { // Set Limit this->Limit(__MAX78630_SET__, __F_MAX__, (this->Limit_Variables.FQ_Max - __MAX78630_Limit_Frequency_Max_Diff__)); @@ -3172,9 +3169,9 @@ this->Limit(__MAX78630_SET__, __F_MAX__, this->Limit_Variables.FQ_Max); } - + // Control for Low Frequency - if (bitRead(this->Device.Status, __BIT_UN_FREQ__)) { + if (this->Alarm(__ALARM_LOW_FREQUENCY__)) { // Set Limit this->Limit(__MAX78630_SET__, __F_MIN__, (this->Limit_Variables.FQ_Min + __MAX78630_Limit_Frequency_Min_Diff__)); @@ -3187,7 +3184,7 @@ } // Control for Voltage Imbalance - if (bitRead(this->Device.Status, __BIT_V_IMBAL__)) { + if (this->Alarm(__ALARM_VOLTAGE_IMBAL__)) { // Set Limit this->Limit(__MAX78630_SET__, __VIMB_MAX__, (this->Limit_Variables.VImb_Max + __MAX78630_Limit_VImb_Max_Diff__)); @@ -3200,7 +3197,7 @@ } // Control for Current Imbalance - if (bitRead(this->Device.Status, __BIT_I_IMBAL__)) { + if (this->Alarm(__ALARM_CURRENT_IMBAL__)) { // Set Limit this->Limit(__MAX78630_SET__, __IIMB_MAX__, (this->Limit_Variables.IImb_Max + __MAX78630_Limit_IImb_Max_Diff__)); @@ -3216,83 +3213,129 @@ /* Alarm Functions */ - // Over Voltage Alarm - inline bool Alarm_OV(void) { + // Get Alarm Status Function. + bool Alarm(const uint16_t _Alarm_Type) { - // 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__)); + // Get Device Status + this->Status(__MAX78630_GET__); - } + // Control for Alarm Type + switch (_Alarm_Type) { - // Under Voltage Alarm - inline bool Alarm_UV(void) { + // High Voltage Alarm + case __ALARM_HIGH_VOLTAGE__ : { - // 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__)); + // Control for Over Voltage + if (bitRead(this->Device.Status, __BIT_OV_VRMSA__) || bitRead(this->Device.Status, __BIT_OV_VRMSB__) || bitRead(this->Device.Status, __BIT_OV_VRMSC__)) return(true); - } + // End Switch + break; - // 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__)); + // Low Voltage Alarm + case __ALARM_LOW_VOLTAGE__ : { - } + // Control for Under Voltage + if (bitRead(this->Device.Status, __BIT_UN_VRMSA__) || bitRead(this->Device.Status, __BIT_UN_VRMSB__) || bitRead(this->Device.Status, __BIT_UN_VRMSC__)) return(true); - // Over Frequency Alarm - inline bool Alarm_OFQ(void) { + // End Switch + break; - // End Function - return(bitRead(this->Device.Status, __BIT_OV_FREQ__)); + } - } + // High Current Alarm + case __ALARM_HIGH_CURRENT__ : { - // Under Frequency Alarm - inline bool Alarm_UFQ(void) { + // Control for Over Current + if (bitRead(this->Device.Status, __BIT_OV_IRMSA__) || bitRead(this->Device.Status, __BIT_OV_IRMSB__) || bitRead(this->Device.Status, __BIT_OV_IRMSC__)) return(true); - // End Function - return(bitRead(this->Device.Status, __BIT_UN_FREQ__)); + // End Switch + break; - } + } - // Voltage Imbalance Alarm - inline bool Alarm_VImb(void) { + // High Frequency Alarm + case __ALARM_HIGH_FREQUENCY__ : { - // End Function - return(bitRead(this->Device.Status, __BIT_V_IMBAL__)); + // Control for Over Frequency + if (bitRead(this->Device.Status, __BIT_OV_FREQ__)) return(true); - } + // End Switch + break; - // Current Imbalance Alarm - inline bool Alarm_IImb(void) { + } - // End Function - return(bitRead(this->Device.Status, __BIT_I_IMBAL__)); + // Low Frequency Alarm + case __ALARM_LOW_FREQUENCY__ : { - } + // Control for Under Frequency + if (bitRead(this->Device.Status, __BIT_UN_FREQ__)) return(true); - // Under Power Factor Alarm - inline bool Alarm_UPF(void) { + // End Switch + break; - // 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__)); + } - } + // Voltage Imbalance Alarm + case __ALARM_VOLTAGE_IMBAL__ : { - // Over Temperature Alarm - inline bool Alarm_OT(void) { + // Control for Voltage Imbalance + if (bitRead(this->Device.Status, __BIT_V_IMBAL__)) return(true); - // End Function - return(bitRead(this->Device.Status, __BIT_OV_TEMP__)); + // End Switch + break; - } + } + + // Current Imbalance Alarm + case __ALARM_CURRENT_IMBAL__ : { + + // Control for Current Imbalance + if (bitRead(this->Device.Status, __BIT_I_IMBAL__)) return(true); + + // End Switch + break; + + } + + // Low Power Factor Alarm + case __ALARM_LOW_POWERFACTOR__ : { + + // Control for Low Power Factor + if (bitRead(this->Device.Status, __BIT_UN_PFA__) || bitRead(this->Device.Status, __BIT_UN_PFB__) || bitRead(this->Device.Status, __BIT_UN_PFC__)) return(true); + + // End Switch + break; - // Under Temperature Alarm - inline bool Alarm_UT(void) { + } + + // High Temperature Alarm + case __ALARM_HIGH_TEMPERATURE__ : { + + // Control for Over Temperature + if (bitRead(this->Device.Status, __BIT_OV_TEMP__)) return(true); + + // End Switch + break; + + } + + // Low Temperature Alarm + case __ALARM_LOW_TEMPERATURE__ : { + + // Control for Under Temperature + if (bitRead(this->Device.Status, __BIT_UN_TEMP__)) return(true); + + // End Switch + break; + + } + + } // End Function - return(bitRead(this->Device.Status, __BIT_UN_TEMP__)); + return(false); }