forked from tygermeow/hoomaluo_PM001-ADE9000
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Hoomaluo_ADE9000.cpp
557 lines (473 loc) · 17.6 KB
/
Hoomaluo_ADE9000.cpp
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
#include <Arduino.h>
#include <SPI.h>
#include "Hoomaluo_ADE9000.h"
#include <Wire.h>
/*EEPROM data structure*/
uint32_t ADE9000_CalibrationRegAddress[CALIBRATION_CONSTANTS_ARRAY_SIZE]={ADDR_AIGAIN, ADDR_BIGAIN, ADDR_CIGAIN, ADDR_NIGAIN,ADDR_AVGAIN, ADDR_BVGAIN, ADDR_CVGAIN,ADDR_APHCAL0, ADDR_BPHCAL0, ADDR_CPHCAL0, ADDR_APGAIN, ADDR_BPGAIN, ADDR_CPGAIN};
uint32_t ADE9000_Eeprom_CalibrationRegAddress[CALIBRATION_CONSTANTS_ARRAY_SIZE]={ADDR_AIGAIN_EEPROM, ADDR_BIGAIN_EEPROM, ADDR_CIGAIN_EEPROM, ADDR_NIGAIN_EEPROM,ADDR_AVGAIN_EEPROM, ADDR_BVGAIN_EEPROM, ADDR_CVGAIN_EEPROM,ADDR_APHCAL0_EEPROM, ADDR_BPHCAL0_EEPROM, ADDR_CPHCAL0_EEPROM, ADDR_APGAIN_EEPROM, ADDR_BPGAIN_EEPROM, ADDR_CPGAIN_EEPROM};
ADE9000Class::ADE9000Class()
{
}
/*
Description: Initializes the ADE9000. The initial settings for registers are defined in ADE9000API.h header file
Input: Register settings in header files
Output:
*/
void ADE9000Class::SetupADE9000(void)
{
SPI_Write_16(ADDR_PGA_GAIN,ADE9000_PGA_GAIN);
SPI_Write_32(ADDR_CONFIG0,ADE9000_CONFIG0);
SPI_Write_16(ADDR_CONFIG1,ADE9000_CONFIG1);
SPI_Write_16(ADDR_CONFIG2,ADE9000_CONFIG2);
SPI_Write_16(ADDR_CONFIG3,ADE9000_CONFIG3);
SPI_Write_16(ADDR_ACCMODE,ADE9000_ACCMODE);
SPI_Write_16(ADDR_TEMP_CFG,ADE9000_TEMP_CFG);
SPI_Write_16(ADDR_ZX_LP_SEL,ADE9000_ZX_LP_SEL);
SPI_Write_32(ADDR_MASK0,ADE9000_MASK0);
SPI_Write_32(ADDR_MASK1,ADE9000_MASK1);
SPI_Write_32(ADDR_EVENT_MASK,ADE9000_EVENT_MASK);
SPI_Write_16(ADDR_WFB_CFG,ADE9000_WFB_CFG);
SPI_Write_32(ADDR_VLEVEL,ADE9000_VLEVEL);
SPI_Write_32(ADDR_DICOEFF,ADE9000_DICOEFF);
SPI_Write_16(ADDR_RUN,ADE9000_RUN_ON); //DSP ON
SPI_Write_16(ADDR_EGY_TIME,ADE9000_EGY_TIME);
SPI_Write_16(ADDR_EP_CFG,ADE9000_EP_CFG); //Energy accumulation ON
}
/*
Description: Initializes the arduino SPI port using SPI.h library
Input: SPI speed, chip select pin
Output:-
*/
void ADE9000Class::SPI_Init(uint32_t SPI_speed , uint8_t chipSelect_Pin)
{
SPI.begin(); //Initiate SPI port
SPI.beginTransaction(SPISettings(SPI_speed,MSBFIRST,SPI_MODE0)); //Setup SPI parameters
pinMode(chipSelect_Pin, OUTPUT); //Set Chip select pin as output
digitalWrite(chipSelect_Pin, HIGH); //Set Chip select pin high
_chipSelect_Pin = chipSelect_Pin;
}
/*
Description: Writes 16bit data to a 16 bit register.
Input: Register address, data
Output:-
*/
void ADE9000Class:: SPI_Write_16(uint16_t Address , uint16_t Data )
{
uint16_t temp_address;
digitalWrite(_chipSelect_Pin, LOW);
temp_address = ((Address << 4) & 0xFFF0); //shift address to align with cmd packet
SPI.transfer16(temp_address);
SPI.transfer16(Data);
digitalWrite(_chipSelect_Pin, HIGH);
}
/*
Description: Writes 32bit data to a 32 bit register.
Input: Register address, data
Output:-
*/
void ADE9000Class:: SPI_Write_32(uint16_t Address , uint32_t Data )
{
uint16_t temp_address;
uint16_t temp_highpacket;
uint16_t temp_lowpacket;
temp_highpacket= (Data & 0xFFFF0000)>>16;
temp_lowpacket= (Data & 0x0000FFFF);
digitalWrite(_chipSelect_Pin, LOW);
temp_address = ((Address << 4) & 0xFFF0); //shift address to align with cmd packet
SPI.transfer16(temp_address);
SPI.transfer16(temp_highpacket);
SPI.transfer16(temp_lowpacket);
digitalWrite(_chipSelect_Pin, HIGH);
}
/*
Description: Reads 16bit data from register.
Input: Register address
Output: 16 bit data
*/
uint16_t ADE9000Class:: SPI_Read_16(uint16_t Address)
{
uint16_t temp_address;
uint16_t returnData;
digitalWrite(_chipSelect_Pin, LOW);
temp_address = (((Address << 4) & 0xFFF0)+8);
SPI.transfer16(temp_address);
returnData = SPI.transfer16(0);
digitalWrite(_chipSelect_Pin, HIGH);
return returnData;
}
/*
Description: Reads 32bit data from register.
Input: Register address
Output: 32 bit data
*/
uint32_t ADE9000Class:: SPI_Read_32(uint16_t Address)
{
uint16_t temp_address;
uint16_t temp_highpacket;
uint16_t temp_lowpacket;
uint32_t returnData;
digitalWrite(_chipSelect_Pin, LOW);
temp_address = (((Address << 4) & 0xFFF0)+8);
SPI.transfer16(temp_address);
temp_highpacket = SPI.transfer16(0);
temp_lowpacket = SPI.transfer16(0);
digitalWrite(_chipSelect_Pin, HIGH);
returnData = temp_highpacket << 16;
returnData = returnData + temp_lowpacket;
return returnData;
}
/*
Description: Burst reads the content of waveform buffer. This function only works with resampled data. Configure waveform buffer to have Resampled data, and burst enabled (BURST_CHAN=0000 in WFB_CFG Register).
Input: The starting address. Use the starting address of a data set. e.g 0x800, 0x804 etc to avoid data going into incorrect arrays.
Read_Element_Length is the number of data sets to read. If the starting address is 0x800, the maximum sets to read are 512.
Output: Resampled data returned in structure
*/
void ADE9000Class:: SPI_Burst_Read_Resampled_Wfb(uint16_t Address, uint16_t Read_Element_Length, ResampledWfbData *ResampledData)
{
uint16_t temp;
uint16_t i;
digitalWrite(_chipSelect_Pin, LOW);
SPI.transfer16(((Address << 4) & 0xFFF0)+8); //Send the starting address
//burst read the data upto Read_Length
for(i=0;i<Read_Element_Length;i++)
{
ResampledData->IA_Resampled[i] = SPI.transfer16(0);
ResampledData->VA_Resampled[i] = SPI.transfer16(0);
ResampledData->IB_Resampled[i] = SPI.transfer16(0);
ResampledData->VB_Resampled[i] = SPI.transfer16(0);
ResampledData->IC_Resampled[i] = SPI.transfer16(0);
ResampledData->VC_Resampled[i] = SPI.transfer16(0);
ResampledData->IN_Resampled[i] = SPI.transfer16(0);
}
digitalWrite(_chipSelect_Pin, HIGH);
}
/*
Description: Reads the Active power registers AWATT,BWATT and CWATT
Input: Structure name
Output: Active power codes stored in respective structure
*/
void ADE9000Class:: ReadActivePowerRegs(ActivePowerRegs *Data)
{
Data->ActivePowerReg_A = int32_t (SPI_Read_32(ADDR_AWATT));
Data->ActivePowerReg_B = int32_t (SPI_Read_32(ADDR_BWATT));
Data->ActivePowerReg_C = int32_t (SPI_Read_32(ADDR_CWATT));
}
void ADE9000Class:: ReadReactivePowerRegs(ReactivePowerRegs *Data)
{
Data->ReactivePowerReg_A = int32_t (SPI_Read_32(ADDR_AVAR));
Data->ReactivePowerReg_B = int32_t (SPI_Read_32(ADDR_BVAR));
Data->ReactivePowerReg_C = int32_t (SPI_Read_32(ADDR_CVAR));
}
void ADE9000Class:: ReadApparentPowerRegs(ApparentPowerRegs *Data)
{
Data->ApparentPowerReg_A = int32_t (SPI_Read_32(ADDR_AVA));
Data->ApparentPowerReg_B = int32_t (SPI_Read_32(ADDR_BVA));
Data->ApparentPowerReg_C = int32_t (SPI_Read_32(ADDR_CVA));
}
void ADE9000Class:: ReadVoltageRMSRegs(VoltageRMSRegs *Data)
{
Data->VoltageRMSReg_A = int32_t (SPI_Read_32(ADDR_AVRMS));
Data->VoltageRMSReg_B = int32_t (SPI_Read_32(ADDR_BVRMS));
Data->VoltageRMSReg_C = int32_t (SPI_Read_32(ADDR_CVRMS));
}
void ADE9000Class:: ReadCurrentRMSRegs(CurrentRMSRegs *Data)
{
Data->CurrentRMSReg_A = int32_t (SPI_Read_32(ADDR_AIRMS));
Data->CurrentRMSReg_B = int32_t (SPI_Read_32(ADDR_BIRMS));
Data->CurrentRMSReg_C = int32_t (SPI_Read_32(ADDR_CIRMS));
Data->CurrentRMSReg_N = int32_t (SPI_Read_32(ADDR_NIRMS));
}
void ADE9000Class:: ReadFundActivePowerRegs(FundActivePowerRegs *Data)
{
Data->FundActivePowerReg_A = int32_t (SPI_Read_32(ADDR_AFWATT));
Data->FundActivePowerReg_B = int32_t (SPI_Read_32(ADDR_BFWATT));
Data->FundActivePowerReg_C = int32_t (SPI_Read_32(ADDR_CFWATT));
}
void ADE9000Class:: ReadFundReactivePowerRegs(FundReactivePowerRegs *Data)
{
Data->FundReactivePowerReg_A = int32_t (SPI_Read_32(ADDR_AFVAR));
Data->FundReactivePowerReg_B = int32_t (SPI_Read_32(ADDR_BFVAR));
Data->FundReactivePowerReg_C = int32_t (SPI_Read_32(ADDR_CFVAR));
}
void ADE9000Class:: ReadFundApparentPowerRegs(FundApparentPowerRegs *Data)
{
Data->FundApparentPowerReg_A = int32_t (SPI_Read_32(ADDR_AFVA));
Data->FundApparentPowerReg_B = int32_t (SPI_Read_32(ADDR_BFVA));
Data->FundApparentPowerReg_C = int32_t (SPI_Read_32(ADDR_CFVA));
}
void ADE9000Class:: ReadFundVoltageRMSRegs(FundVoltageRMSRegs *Data)
{
Data->FundVoltageRMSReg_A = int32_t (SPI_Read_32(ADDR_AVFRMS));
Data->FundVoltageRMSReg_B = int32_t (SPI_Read_32(ADDR_BVFRMS));
Data->FundVoltageRMSReg_C = int32_t (SPI_Read_32(ADDR_CVFRMS));
}
void ADE9000Class:: ReadFundCurrentRMSRegs(FundCurrentRMSRegs *Data)
{
Data->FundCurrentRMSReg_A = int32_t (SPI_Read_32(ADDR_AIFRMS));
Data->FundCurrentRMSReg_B = int32_t (SPI_Read_32(ADDR_BIFRMS));
Data->FundCurrentRMSReg_C = int32_t (SPI_Read_32(ADDR_CIFRMS));
}
void ADE9000Class:: ReadHalfVoltageRMSRegs(HalfVoltageRMSRegs *Data)
{
Data->HalfVoltageRMSReg_A = int32_t (SPI_Read_32(ADDR_AVRMSONE));
Data->HalfVoltageRMSReg_B = int32_t (SPI_Read_32(ADDR_BVRMSONE));
Data->HalfVoltageRMSReg_C = int32_t (SPI_Read_32(ADDR_CVRMSONE));
}
void ADE9000Class:: ReadHalfCurrentRMSRegs(HalfCurrentRMSRegs *Data)
{
Data->HalfCurrentRMSReg_A = int32_t (SPI_Read_32(ADDR_AIRMSONE));
Data->HalfCurrentRMSReg_B = int32_t (SPI_Read_32(ADDR_BIRMSONE));
Data->HalfCurrentRMSReg_C = int32_t (SPI_Read_32(ADDR_CIRMSONE));
Data->HalfCurrentRMSReg_N = int32_t (SPI_Read_32(ADDR_NIRMSONE));
}
void ADE9000Class:: ReadTen12VoltageRMSRegs(Ten12VoltageRMSRegs *Data)
{
Data->Ten12VoltageRMSReg_A = int32_t (SPI_Read_32(ADDR_AVRMS1012));
Data->Ten12VoltageRMSReg_B = int32_t (SPI_Read_32(ADDR_BVRMS1012));
Data->Ten12VoltageRMSReg_C = int32_t (SPI_Read_32(ADDR_CVRMS1012));
}
void ADE9000Class:: ReadTen12CurrentRMSRegs(Ten12CurrentRMSRegs *Data)
{
Data->Ten12CurrentRMSReg_A = int32_t (SPI_Read_32(ADDR_AIRMS1012));
Data->Ten12CurrentRMSReg_B = int32_t (SPI_Read_32(ADDR_BIRMS1012));
Data->Ten12CurrentRMSReg_C = int32_t (SPI_Read_32(ADDR_CIRMS1012));
Data->Ten12CurrentRMSReg_N = int32_t (SPI_Read_32(ADDR_NIRMS1012));
}
void ADE9000Class:: ReadVoltageTHDRegsnValues(VoltageTHDRegs *Data)
{
uint32_t tempReg;
float tempValue;
tempReg=int32_t (SPI_Read_32(ADDR_AVTHD)); //Read THD register
Data->VoltageTHDReg_A = tempReg;
tempValue=(float)tempReg*100/(float)134217728; //Calculate THD in %
Data->VoltageTHDValue_A=tempValue;
tempReg=int32_t (SPI_Read_32(ADDR_BVTHD)); //Read THD register
Data->VoltageTHDReg_B = tempReg;
tempValue=(float)tempReg*100/(float)134217728; //Calculate THD in %
Data->VoltageTHDValue_B=tempValue;
tempReg=int32_t (SPI_Read_32(ADDR_CVTHD)); //Read THD register
Data->VoltageTHDReg_C = tempReg;
tempValue=(float)tempReg*100/(float)134217728; //Calculate THD in %
Data->VoltageTHDValue_C=tempValue;
}
void ADE9000Class:: ReadCurrentTHDRegsnValues(CurrentTHDRegs *Data)
{
uint32_t tempReg;
float tempValue;
tempReg=int32_t (SPI_Read_32(ADDR_AITHD)); //Read THD register
Data->CurrentTHDReg_A = tempReg;
tempValue=(float)tempReg*100/(float)134217728; //Calculate THD in %
Data->CurrentTHDValue_A=tempValue;
tempReg=int32_t (SPI_Read_32(ADDR_BITHD)); //Read THD register
Data->CurrentTHDReg_B = tempReg;
tempValue=(float)tempReg*100/(float)134217728; //Calculate THD in %
Data->CurrentTHDValue_B=tempValue;
tempReg=int32_t (SPI_Read_32(ADDR_CITHD)); //Read THD register
Data->CurrentTHDReg_C = tempReg;
tempValue=(float)tempReg*100/(float)134217728; //Calculate THD in %
Data->CurrentTHDValue_C=tempValue;
}
void ADE9000Class:: ReadPowerFactorRegsnValues(PowerFactorRegs *Data)
{
uint32_t tempReg;
float tempValue;
tempReg=int32_t (SPI_Read_32(ADDR_APF)); //Read PF register
Data->PowerFactorReg_A = tempReg;
tempValue=(float)tempReg/(float)134217728; //Calculate PF
Data->PowerFactorValue_A=tempValue;
tempReg=int32_t (SPI_Read_32(ADDR_BPF)); //Read PF register
Data->PowerFactorReg_B = tempReg;
tempValue=(float)tempReg/(float)134217728; //Calculate PF
Data->PowerFactorValue_B=tempValue;
tempReg=int32_t (SPI_Read_32(ADDR_CPF)); //Read PF register
Data->PowerFactorReg_C = tempReg;
tempValue=(float)tempReg/(float)134217728; //Calculate PF
Data->PowerFactorValue_C=tempValue;
}
void ADE9000Class:: ReadPeriodRegsnValues(PeriodRegs *Data)
{
uint32_t tempReg;
float tempValue;
tempReg=int32_t (SPI_Read_32(ADDR_APERIOD)); //Read PERIOD register
Data->PeriodReg_A = tempReg;
tempValue=(float)(8000*65536)/(float)(tempReg+1); //Calculate Frequency
Data->FrequencyValue_A = tempValue;
tempReg=int32_t (SPI_Read_32(ADDR_BPERIOD)); //Read PERIOD register
Data->PeriodReg_B = tempReg;
tempValue=(float)(8000*65536)/(float)(tempReg+1); //Calculate Frequency
Data->FrequencyValue_B = tempValue;
tempReg=int32_t (SPI_Read_32(ADDR_CPERIOD)); //Read PERIOD register
Data->PeriodReg_C = tempReg;
tempValue=(float)(8000*65536)/(float)(tempReg+1); //Calculate Frequency
Data->FrequencyValue_C = tempValue;
}
void ADE9000Class:: ReadAngleRegsnValues(AngleRegs *Data)
{
uint32_t tempReg;
uint16_t temp;
float mulConstant;
float tempValue;
temp=SPI_Read_16(ADDR_ACCMODE); //Read frequency setting register
if((temp&0x0100)>=0)
{
mulConstant=0.02109375; //multiplier constant for 60Hz system
}
else
{
mulConstant=0.017578125; //multiplier constant for 50Hz system
}
tempReg=int16_t (SPI_Read_32(ADDR_ANGL_VA_VB)); //Read ANGLE register
Data->AngleReg_VA_VB=tempReg;
tempValue=tempReg*mulConstant; //Calculate Angle in degrees
Data->AngleValue_VA_VB=tempValue;
tempReg=int16_t (SPI_Read_32(ADDR_ANGL_VB_VC));
Data->AngleReg_VB_VC=tempReg;
tempValue=tempReg*mulConstant;
Data->AngleValue_VB_VC=tempValue;
tempReg=int16_t (SPI_Read_32(ADDR_ANGL_VA_VC));
Data->AngleReg_VA_VC=tempReg;
tempValue=tempReg*mulConstant;
Data->AngleValue_VA_VC=tempValue;
tempReg=int16_t (SPI_Read_32(ADDR_ANGL_VA_IA));
Data->AngleReg_VA_IA=tempReg;
tempValue=tempReg*mulConstant;
Data->AngleValue_VA_IA=tempValue;
tempReg=int16_t (SPI_Read_32(ADDR_ANGL_VB_IB));
Data->AngleReg_VB_IB=tempReg;
tempValue=tempReg*mulConstant;
Data->AngleValue_VB_IB=tempValue;
tempReg=int16_t (SPI_Read_32(ADDR_ANGL_VC_IC));
Data->AngleReg_VC_IC=tempReg;
tempValue=tempReg*mulConstant;
Data->AngleValue_VC_IC=tempValue;
tempReg=int16_t (SPI_Read_32(ADDR_ANGL_IA_IB));
Data->AngleReg_IA_IB=tempReg;
tempValue=tempReg*mulConstant;
Data->AngleValue_IA_IB=tempValue;
tempReg=int16_t (SPI_Read_32(ADDR_ANGL_IB_IC));
Data->AngleReg_IB_IC=tempReg;
tempValue=tempReg*mulConstant;
Data->AngleValue_IB_IC=tempValue;
tempReg=int16_t (SPI_Read_32(ADDR_ANGL_IA_IC));
Data->AngleReg_IA_IC=tempReg;
tempValue=tempReg*mulConstant;
Data->AngleValue_IA_IC=tempValue;
}
/*
Description: Starts a new acquisition cycle. Waits for constant time and returns register value and temperature in Degree Celsius
Input: Structure name
Output: Register reading and temperature value in Degree Celsius
*/
void ADE9000Class:: ReadTempRegnValue(TemperatureRegnValue *Data)
{
uint32_t trim;
uint16_t gain;
uint16_t offset;
uint16_t tempReg;
float tempValue;
SPI_Write_16(ADDR_TEMP_CFG,ADE9000_TEMP_CFG);//Start temperature acquisition cycle with settings in defined in ADE9000_TEMP_CFG
delay(2); //delay of 2ms. Increase delay if TEMP_TIME is changed
trim = SPI_Read_32(ADDR_TEMP_TRIM);
gain= (trim & 0xFFFF); //Extract 16 LSB
offset= ((trim>>16)&0xFFFF); //Extract 16 MSB
tempReg= SPI_Read_16(ADDR_TEMP_RSLT); //Read Temperature result register
tempValue= (float)(offset>>5)-((float)tempReg*(float)gain/(float)65536);
Data->Temperature_Reg=tempReg;
Data->Temperature=tempValue;
}
/*
Description: Writes one byte of data to EEPROM
Input: Data and EEPROM address
Output:-
*/
void ADE9000Class:: writeByteToEeprom(uint16_t dataAddress, uint8_t data)
{
uint8_t temp;
temp= (dataAddress>>8);
Wire.beginTransmission(ADE9000_EEPROM_ADDRESS); // device address is specified in datasheet
Wire.write(byte(temp)); // MSB Address
temp= (dataAddress & (0xFF));
Wire.write(byte(temp)); //LSB Address
Wire.write(byte(data)); //
Wire.endTransmission(); // stop transmitting
}
/*
Description: REads one byte of data from EEPROM
Input: EEPROM address
Output:- 8 bit data
*/
uint8_t ADE9000Class:: ReadByteFromEeprom(uint16_t dataAddress)
{
uint8_t returndata;
uint8_t temp;
temp= (dataAddress>>8);
Wire.beginTransmission(byte(ADE9000_EEPROM_ADDRESS)); // device address is specified in datasheet
Wire.write(byte(temp)); // MSB
temp= (dataAddress & (0xFF));
Wire.write(byte(temp)); // LSB
Wire.endTransmission();
Wire.requestFrom(byte(ADE9000_EEPROM_ADDRESS),1);
if (Wire.available())
{
returndata = Wire.read();
}
return returndata;
}
/*
Description: Writes 4 bytes into EEPROM in continuous locations
Input: Data and EEPROM address
Output:-
*/
void ADE9000Class:: writeWordToEeprom(uint16_t address, uint32_t data)
{
uint8_t temp;
uint32_t returnedValue;
temp = (data &(0xFF)); //LSB
writeByteToEeprom(address,temp);
delay(10);
temp = ((data>>8) &(0xFF));
writeByteToEeprom(address+1,temp);
temp = ((data>>16) &(0xFF));
delay(10);
writeByteToEeprom(address+2,temp);
temp = ((data>>24) &(0xFF)); //MSB
delay(10);
writeByteToEeprom(address+3,temp);
delay(10);
returnedValue = readWordFromEeprom(address);
if(returnedValue!=data)
{
Serial.println("Write Not Successful"); //Check if data write is successful
Serial.println("Address: ");
Serial.print(address);
Serial.println("Data: ");
Serial.print(data,HEX);
}
}
/*
Description: Reads 4 bytes stored in EEPROM
Input: Starting EEPROM address
Output:- 4 byte wide data
*/
uint32_t ADE9000Class:: readWordFromEeprom(uint16_t address)
{
uint32_t returndata;
returndata=0;
uint8_t temp;
temp = ReadByteFromEeprom(address);
returndata = temp; //LSB
delay(10);
temp = ReadByteFromEeprom(address+1);
returndata |= (temp<<8);
delay(10);
temp = ReadByteFromEeprom(address+2);
returndata |= (temp<<16);
delay(10);
temp = ReadByteFromEeprom(address+3);
returndata |= (temp<<24); //MSB
return returndata;
}
long ADE9000Class:: twos_compliment(long bitreg32) {
if(bitreg32 > 2,147,483,647) return (~bitreg32) + 1;
else return bitreg32;
}