forked from tygermeow/hoomaluo_PM001-ADE9000
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hoomaluo_ADE9000.h
319 lines (273 loc) · 10.2 KB
/
Hoomaluo_ADE9000.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#ifndef ADE9000API_h
#define ADE9000API_h
/****************************************************************************************************************
Includes
***************************************************************************************************************/
#include "Arduino.h"
#include "Hoomaluo_ADE9000RegMap.h"
/****************************************************************************************************************
Definitions
****************************************************************************************************************/
// Configuration registers
#define ADE9000_PGA_GAIN 0x0000 // PGA@0x0000. Gain of all channels=1
#define ADE9000_CONFIG0 0x00000000 // Integrator disabled
#define ADE9000_CONFIG1 0x0002 // CF3/ZX pin outputs Zero crossing
#define ADE9000_CONFIG2 0x0C00 // Default High pass corner frequency of 1.25Hz
#define ADE9000_CONFIG3 0x0000 // Peak and overcurrent detection disabled
#define ADE9000_ACCMODE 0x0100 // 60Hz operation, 3P4W Wye configuration, signed accumulation
// Clear bit 8 i.e. ACCMODE=0x00xx for 50Hz operation
// ACCMODE=0x0x9x for 3Wire delta when phase B is used as reference
#define ADE9000_TEMP_CFG 0x000C // Temperature sensor enabled
#define ADE9000_ZX_LP_SEL 0x001E // Line period and zero crossing obtained from combined signals VA,VB and VC
#define ADE9000_MASK0 0x00000001 // Enable EGYRDY interrupt
#define ADE9000_MASK1 0x00000000 // MASK1 interrupts disabled
#define ADE9000_EVENT_MASK 0x00000000 // Events disabled
#define ADE9000_VLEVEL 0x0022EA28 // Assuming Vnom=1/2 of full scale.
// Refer Technical reference manual for detailed calculations.
#define ADE9000_DICOEFF 0x00000000 // Set DICOEFF= 0xFFFFE000 when integrator is enabled
// Constant Definitions
#define ADE90xx_FDSP 8000 // ADE9000 FDSP: 8000sps, ADE9078 FDSP: 4000sps
#define ADE9000_RUN_ON 0x0001 // DSP ON
// Energy Accumulation Settings
#define ADE9000_EP_CFG 0x0011 // Enable energy accumulation, accumulate samples at 8ksps
// latch energy accumulation after EGYRDY
// If accumulation is changed to half line cycle mode, change EGY_TIME
#define ADE9000_EGY_TIME 0x1F3F // Accumulate 8000 samples
// Waveform buffer Settings
#define ADE9000_WFB_CFG 0x1000 // Neutral current samples enabled, Resampled data enabled
// Burst all channels
#define WFB_ELEMENT_ARRAY_SIZE 512 // size of buffer to read. 512 Max.Each element IA,VA...IN has max 512 points
// [Size of waveform buffer/number of sample sets = 2048/4 = 512]
// (Refer ADE9000 technical reference manual for more details)
// Full scale Codes referred from Datasheet.Respective digital codes are produced when ADC inputs are at full scale. Donot Change.
#define ADE9000_RMS_FULL_SCALE_CODES 52702092
#define ADE9000_WATT_FULL_SCALE_CODES 20694066
#define ADE9000_RESAMPLED_FULL_SCALE_CODES 18196
#define ADE9000_PCF_FULL_SCALE_CODES 74532013
// Size of array reading calibration constants from EEPROM
#define CALIBRATION_CONSTANTS_ARRAY_SIZE 13
#define ADE9000_EEPROM_ADDRESS 0x54 //1010xxxy xxx---> 100(A2,A1,A0 defined by hardware). y (1: Read, 0:Write)
#define EEPROM_WRITTEN 0x01
// Address of registers stored in EEPROM.Calibration data is 4 bytes
#define ADDR_CHECKSUM_EEPROM 0x0800 // Simple checksum to verify data transmission errors. Add all the registers upto CPGAIN. The lower 32 bits should match data starting @ADDR_CHECKSUM_EEPROM
#define ADDR_EEPROM_WRITTEN_BYTE 0x0804 //1--> EEPROM Written, 0--> Not written. One byte only
#define ADDR_AIGAIN_EEPROM 0x0004
#define ADDR_BIGAIN_EEPROM 0x000C
#define ADDR_CIGAIN_EEPROM 0x0010
#define ADDR_NIGAIN_EEPROM 0x0014
#define ADDR_AVGAIN_EEPROM 0x0018
#define ADDR_BVGAIN_EEPROM 0x001C
#define ADDR_CVGAIN_EEPROM 0x0020
#define ADDR_APHCAL0_EEPROM 0x0024
#define ADDR_BPHCAL0_EEPROM 0x0028
#define ADDR_CPHCAL0_EEPROM 0x002C
#define ADDR_APGAIN_EEPROM 0x0030
#define ADDR_BPGAIN_EEPROM 0x0034
#define ADDR_CPGAIN_EEPROM 0x0038
/****************************************************************************************************************
EEPROM Global Variables
****************************************************************************************************************/
extern uint32_t calibrationDatafromEEPROM[CALIBRATION_CONSTANTS_ARRAY_SIZE];
extern uint32_t ADE9000_CalibrationRegAddress[CALIBRATION_CONSTANTS_ARRAY_SIZE];
extern uint32_t ADE9000_Eeprom_CalibrationRegAddress[CALIBRATION_CONSTANTS_ARRAY_SIZE];
/****************************************************************************************************************
Structures and Global Variables
****************************************************************************************************************/
typedef struct
{
int16_t VA_Resampled[WFB_ELEMENT_ARRAY_SIZE];
int16_t IA_Resampled[WFB_ELEMENT_ARRAY_SIZE];
int16_t VB_Resampled[WFB_ELEMENT_ARRAY_SIZE];
int16_t IB_Resampled[WFB_ELEMENT_ARRAY_SIZE];
int16_t VC_Resampled[WFB_ELEMENT_ARRAY_SIZE];
int16_t IC_Resampled[WFB_ELEMENT_ARRAY_SIZE];
int16_t IN_Resampled[WFB_ELEMENT_ARRAY_SIZE];
} ResampledWfbData;
typedef struct
{
int32_t ActivePowerReg_A;
int32_t ActivePowerReg_B;
int32_t ActivePowerReg_C;
} ActivePowerRegs;
typedef struct
{
int32_t ReactivePowerReg_A;
int32_t ReactivePowerReg_B;
int32_t ReactivePowerReg_C;
} ReactivePowerRegs;
typedef struct
{
int32_t ApparentPowerReg_A;
int32_t ApparentPowerReg_B;
int32_t ApparentPowerReg_C;
} ApparentPowerRegs;
typedef struct
{
int32_t VoltageRMSReg_A;
int32_t VoltageRMSReg_B;
int32_t VoltageRMSReg_C;
} VoltageRMSRegs;
typedef struct
{
int32_t CurrentRMSReg_A;
int32_t CurrentRMSReg_B;
int32_t CurrentRMSReg_C;
int32_t CurrentRMSReg_N;
} CurrentRMSRegs;
typedef struct
{
int32_t FundActivePowerReg_A;
int32_t FundActivePowerReg_B;
int32_t FundActivePowerReg_C;
} FundActivePowerRegs;
typedef struct
{
int32_t FundReactivePowerReg_A;
int32_t FundReactivePowerReg_B;
int32_t FundReactivePowerReg_C;
} FundReactivePowerRegs;
typedef struct
{
int32_t FundApparentPowerReg_A;
int32_t FundApparentPowerReg_B;
int32_t FundApparentPowerReg_C;
} FundApparentPowerRegs;
typedef struct
{
int32_t FundVoltageRMSReg_A;
int32_t FundVoltageRMSReg_B;
int32_t FundVoltageRMSReg_C;
} FundVoltageRMSRegs;
typedef struct
{
int32_t FundCurrentRMSReg_A;
int32_t FundCurrentRMSReg_B;
int32_t FundCurrentRMSReg_C;
//Fundamental neutral RMS is not calculated
} FundCurrentRMSRegs;
typedef struct
{
int32_t HalfVoltageRMSReg_A;
int32_t HalfVoltageRMSReg_B;
int32_t HalfVoltageRMSReg_C;
} HalfVoltageRMSRegs;
typedef struct
{
int32_t HalfCurrentRMSReg_A;
int32_t HalfCurrentRMSReg_B;
int32_t HalfCurrentRMSReg_C;
int32_t HalfCurrentRMSReg_N;
} HalfCurrentRMSRegs;
typedef struct
{
int32_t Ten12VoltageRMSReg_A;
int32_t Ten12VoltageRMSReg_B;
int32_t Ten12VoltageRMSReg_C;
} Ten12VoltageRMSRegs;
typedef struct
{
int32_t Ten12CurrentRMSReg_A;
int32_t Ten12CurrentRMSReg_B;
int32_t Ten12CurrentRMSReg_C;
int32_t Ten12CurrentRMSReg_N;
} Ten12CurrentRMSRegs;
typedef struct
{
int32_t VoltageTHDReg_A;
int32_t VoltageTHDReg_B;
int32_t VoltageTHDReg_C;
float VoltageTHDValue_A;
float VoltageTHDValue_B;
float VoltageTHDValue_C;
} VoltageTHDRegs;
typedef struct
{
int32_t CurrentTHDReg_A;
int32_t CurrentTHDReg_B;
int32_t CurrentTHDReg_C;
float CurrentTHDValue_A;
float CurrentTHDValue_B;
float CurrentTHDValue_C;
} CurrentTHDRegs;
typedef struct
{
int32_t PowerFactorReg_A;
int32_t PowerFactorReg_B;
int32_t PowerFactorReg_C;
float PowerFactorValue_A;
float PowerFactorValue_B;
float PowerFactorValue_C;
} PowerFactorRegs;
typedef struct
{
int32_t PeriodReg_A;
int32_t PeriodReg_B;
int32_t PeriodReg_C;
float FrequencyValue_A;
float FrequencyValue_B;
float FrequencyValue_C;
} PeriodRegs;
typedef struct
{
int16_t AngleReg_VA_VB;
int16_t AngleReg_VB_VC;
int16_t AngleReg_VA_VC;
int16_t AngleReg_VA_IA;
int16_t AngleReg_VB_IB;
int16_t AngleReg_VC_IC;
int16_t AngleReg_IA_IB;
int16_t AngleReg_IB_IC;
int16_t AngleReg_IA_IC;
float AngleValue_VA_VB;
float AngleValue_VB_VC;
float AngleValue_VA_VC;
float AngleValue_VA_IA;
float AngleValue_VB_IB;
float AngleValue_VC_IC;
float AngleValue_IA_IB;
float AngleValue_IB_IC;
float AngleValue_IA_IC;
} AngleRegs;
typedef struct
{
int16_t Temperature_Reg;
float Temperature;
} TemperatureRegnValue;
class ADE9000Class
{
public:
ADE9000Class();
void SetupADE9000(void);
// SPI Functions
void SPI_Init(uint32_t SPI_speed , uint8_t chipSelect_Pin);
void SPI_Write_16(uint16_t Address , uint16_t Data );
void SPI_Write_32(uint16_t Address , uint32_t Data );
uint16_t SPI_Read_16(uint16_t Address);
uint32_t SPI_Read_32(uint16_t Address);
void SPI_Burst_Read_Resampled_Wfb(uint16_t Address, uint16_t Read_Element_Length, ResampledWfbData *ResampledData);
// ADE9000 Calculated Parameter Read Functions
void ReadActivePowerRegs(ActivePowerRegs *Data);
void ReadReactivePowerRegs(ReactivePowerRegs *Data);
void ReadApparentPowerRegs(ApparentPowerRegs *Data);
void ReadVoltageRMSRegs(VoltageRMSRegs *Data);
void ReadCurrentRMSRegs(CurrentRMSRegs *Data);
void ReadFundActivePowerRegs(FundActivePowerRegs *Data);
void ReadFundReactivePowerRegs(FundReactivePowerRegs *Data);
void ReadFundApparentPowerRegs(FundApparentPowerRegs *Data);
void ReadFundVoltageRMSRegs(FundVoltageRMSRegs *Data);
void ReadFundCurrentRMSRegs(FundCurrentRMSRegs *Data);
void ReadHalfVoltageRMSRegs(HalfVoltageRMSRegs *Data);
void ReadHalfCurrentRMSRegs(HalfCurrentRMSRegs *Data);
void ReadTen12VoltageRMSRegs(Ten12VoltageRMSRegs *Data);
void ReadTen12CurrentRMSRegs(Ten12CurrentRMSRegs *Data);
void ReadVoltageTHDRegsnValues(VoltageTHDRegs *Data);
void ReadCurrentTHDRegsnValues(CurrentTHDRegs *Data);
void ReadPowerFactorRegsnValues(PowerFactorRegs *Data);
void ReadPeriodRegsnValues(PeriodRegs *Data);
void ReadAngleRegsnValues(AngleRegs *Data);
void ReadTempRegnValue(TemperatureRegnValue *Data);
long twos_compliment(long bitreg32); //RETURNS THE APPROPRIATE INTEGER FOR TWOS COMPLIMENT CONVERSION
private:
uint8_t _chipSelect_Pin;
};
#endif