-
Notifications
You must be signed in to change notification settings - Fork 2
/
ADE7763.ino
648 lines (507 loc) · 16.7 KB
/
ADE7763.ino
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
/**********************************************************************
*
* High level ADE7763 routines for Home energy monitor
*
*
*********************************************************************/
#include "Pins.h"
#include "ADE7763Reg.h"
#include "ADE7763.h"
#include "ADE7763LL.h"
#define SAMPLE_TIME 100 /* ms to wait for sample data */
//#define VRMS_GAIN (1.0/6525.0) // BLUE Clip on
//#define IRMS_GAIN (1.0/185230.0) // BLUE Clip on
//#define VRMS_GAIN (120.0/768000.0)
//#define IRMS_GAIN (350.0/120.0/156400.0)
/* Local variables */
unsigned long sample_; // Generic sample
unsigned long temp_; // Temperature
unsigned long vrms_; // Vrms sampled at ZX
unsigned long irms_; // Irms sampled at ZX
char grabData_ = 0; // Flag used to sample RMS data
char sampleDone_ = 0; // Flag used to sample waveform
/* Gains for each voltage sensor. These are all the same right now */
float vGain[] = {120.0/768000.0,
120.0/768000.0,
120.0/768000.0,
120.0/768000.0,
120.0/768000.0,
120.0/768000.0,
120.0/768000.0,
120.0/768000.0};
/* Gains for each CT. */
float iGain[] = {350.0/120.0/156400.0,
1.0/185230.0,
350.0/120.0/156400.0,
350.0/120.0/156400.0,
350.0/120.0/156400.0,
350.0/120.0/156400.0,
350.0/120.0/156400.0,
350.0/120.0/156400.0};
/**************************************************************************
* ADE_config
* Performs a hardware and software reset of the chip
*
* @return void
*************************************************************************/
void ADE_config(void)
{
unsigned int ui;
unsigned char uc;
/* Reset the ADEs */
ADE_reset();
/* Perform a software reset */
ADE_read (MR_MODE, (unsigned char *)&ui, MR_MODE_CNT);
ui |= MODE_SWRST;
ADE_write (MR_MODE, (unsigned char *)&ui, MR_MODE_CNT);
delayMicroseconds (200); // Wait for reset to take effect
/* Get die version */
ADE_read (MR_DIEREV, (unsigned char *)&uc, MR_DIEREV_CNT);
/* Write to Mode register */
ui = 0x00 |
//MODE_DISHPF | // Disable HPF in Channel 1
//MODE_DISLPF2 | // Disable LPF after the multiplier (LPF2)
MODE_DISCF | // Disable frequency output
MODE_DISSAG | // Disable line voltage sag detection
//MODE_ASUSPEND | // Disable A/D converters
//MODE_TEMPSEL | // Start temperature conversion
//MODE_SWRST | // Software Chip Reset.
//MODE_CYCMODE | // Enable line cycle accumulation mode
//MODE_DISCH1 | // Short out Chan1 (Current)
//MODE_DISCH2 | // Short out Chan2 (Voltage)
//MODE_SWAP | // Swap Chan1 and Chan2
//MODE_DTRT_3K5 | // Waverform data rate to 3.5ksps
MODE_WAV_POWER | // Sample active power
//MODE_POAM | // Accumulated positive power only
0x00;
/* Set the mode */
ADE_write (MR_MODE, (unsigned char *)&ui, MR_MODE_CNT);
/* Write to interrupt enable register */
ui = IRQ_NONE;
ADE_write (MR_IRQEN, (unsigned char *)&ui, MR_IRQEN_CNT);
/* Reset interrupt status (with reset) */
ADE_read (MR_RSTIRQ, (unsigned char *)&ui, MR_RSTIRQ_CNT);
/* Set up the gain register */
uc = 0x00; // ADC1,2 gain = 1 full scale range is +-0.5V
ADE_write (MR_GAIN, (unsigned char *)&uc, MR_GAIN_CNT);
/* Set up the offset correction for ADC1 */
uc = 0x00;
ADE_write (MR_CH1OS, (unsigned char *)&uc, MR_CH1OS_CNT);
/* Set up the offset correction for ADC2 */
uc = 0x00;
ADE_write (MR_CH2OS, (unsigned char *)&uc, MR_CH2OS_CNT);
return;
} /* end ADE_init */
/**************************************************************************
* ADE_reset
*************************************************************************/
void ADE_reset(void) {
/* Strobe the ADE reset line */
digitalWrite(PIN_ADE_RST, 0);
delay(20);
digitalWrite(PIN_ADE_RST, 1);
delay(20);
}
/**************************************************************************
* ADE_setInterrupt
* Performs a hardware and software reset of the chip
*
* @return void
*************************************************************************/
void ADE_setInterrupt(unsigned int interrupt)
{
unsigned int ui;
/* Write to interrupt enable register */
ADE_write (MR_IRQEN, (unsigned char *)&interrupt, MR_IRQEN_CNT);
/* Reset interrupt status (with reset) */
ADE_read (MR_RSTIRQ, (unsigned char *)&ui, MR_RSTIRQ_CNT);
/* At this we will respond to the interrupt requested */
}
/**********************************************************************
*
* Reset peak values
*
* @return void
*
*********************************************************************/
void ADE_resetPeaks(void) {
unsigned char uc;
unsigned int ui;
unsigned long ul;
ul = 0;
ADE_read(MR_RAENERGY, (unsigned char *)&ul, MR_RAENERGY_CNT);
Serial.println("Reset AENERGY");
ul = 0;
ADE_read(MR_RVAENERGY, (unsigned char *)&ul, MR_RVAENERGY_CNT);
Serial.println("Reset VAENERGY");
ui = 0;
ADE_read(MR_RSTIRQ, (unsigned char *)&ui, MR_RSTIRQ_CNT);
Serial.println("Reset IRQ");
ul = 0;
ADE_read(MR_RSTIPEAK, (unsigned char *)&ul, MR_RSTIPEAK_CNT);
Serial.println("Reset IPEAK");
ul = 0;
ADE_read(MR_RSTVPEAK, (unsigned char *)&ul, MR_RSTVPEAK_CNT);
Serial.println("Reset VPEAK");
} /* end ADE_resetPeaks */
/**************************************************************************
* ADE_Sample
*
* This routine will set the proper sample mode in the Mode register, then
* set the flag for the ISR to capture the value at the next zero crossing.
* This routine will block for a period of time should the sample not complete.
*
* @param type The type of sample to collect. Select from MODE_WAV_POWER,
MODE_WAV_RESERVED,
MODE_WAV_ADC1, or
MODE_WAV_ADC2
* @param sample Pointer to a location to place the sample value
* @return 0 for error, 1 for success
*************************************************************************/
char ADE_sample(char type, unsigned long *sample) {
unsigned int ui;
char i;
char retval;
/* Set sample Mode */
ui = 0;
ADE_read (MR_MODE, (unsigned char *)&ui, MR_MODE_CNT);
ui &= ~(MODE_WAVSEL1 | MODE_WAVSEL0);
ui |= type;
ADE_write (MR_MODE, (unsigned char *)&ui, MR_MODE_CNT);
/* Enable sample mode */
sampleDone_ = 0;
ui = 0;
ADE_read (MR_IRQEN, (unsigned char *)&ui, MR_IRQEN_CNT);
ui |= IRQ_WSMP;
ADE_write (MR_IRQEN, (unsigned char *)&ui, MR_IRQEN_CNT);
/* Enable the Sample interrupt */
ADE_setInterrupt(IRQ_WSMP);
/* Wait for sample to come in */
for (i=0; i<SAMPLE_TIME; i++) {
if (sampleDone_ == 0) break;
delay(1);
}
/* If sample did not come in */
if (i == SAMPLE_TIME) {
*sample = 0;
retval = 0;
Serial.println("No data");
} else {
*sample = sample_;
retval = 1;
}
/* Enable the Sample interrupt */
ADE_setInterrupt(IRQ_NONE);
/* Return the error code */
return (retval);
} /* end ADE_sample */
/**************************************************************************
* ADE_sampleRMS
*
* @param vmrs pointer to returned rms voltage
* @param imrs pointer to returned rms current
* @return 0 for fail, 1 for pass
*
*************************************************************************/
char ADE_sampleRMS(unsigned long *vrms, unsigned long *irms) {
unsigned int ui;
int i;
char retval;
/* Enable the ZX interrupt */
ADE_setInterrupt(IRQ_ZX);
delay(18); // Wait for a few zero crossing to go by
/* Set flag used in ISR to capture RMS data at next zero crossings */
grabData_ = 1;
/* Wait for sample to come in */
for (i=0; i<SAMPLE_TIME; i++) {
delay(1);
if (grabData_ == 0) break;
}
/* Turn off interrupts */
ADE_setInterrupt(IRQ_NONE);
if (i == SAMPLE_TIME) {
*irms = 0;
*vrms = 0;
retval = 0;
Serial.println("No data2");
} else {
*irms = irms_;
*vrms = vrms_;
retval = 1;
}
return(retval);
} /* end ADE_sampleRMS */
/**************************************************************************
* ADE_isr
*
* This is the interrupt service routine that gets called each time the
* selected ADE generates an interrupt. Note that no correlation between
* selected node is done here.
*
* It will read the IRQ register and perform the needed operation based
* on what caused the interrupt.
*
* @return void
*************************************************************************/
void ADE_isr(void) {
unsigned int irqstat;
unsigned int irqen;
/* Read IRQ Status and reset */
irqstat = 0;
ADE_read (MR_RSTIRQ, (unsigned char *)&irqstat, MR_RSTIRQ_CNT);
/* Active energy more than 1/2 full */
if (irqstat & IRQ_AEHF) {
}
/* Voltage sag */
if (irqstat & IRQ_SAG) {
}
/* Energy integration over n counts done */
if (irqstat & IRQ_CYCEND) {
}
/* Sample available */
if (irqstat & IRQ_WSMP) {
/* Read waveform register to get sample */
sample_ = 0;
ADE_read (MR_WAVEFORM, (unsigned char *)&sample_, MR_WAVEFORM_CNT);
/* Clear bit in interrupt enable register to stop sampling */
ADE_read (MR_IRQEN, (unsigned char *)&irqen, MR_IRQEN_CNT);
irqen &= ~IRQ_WSMP;
ADE_write (MR_IRQEN, (unsigned char *)&irqen, MR_IRQEN_CNT);
sampleDone_ = 1;
}
/* Zero crossing done */
if (irqstat & IRQ_ZX) {
/* If we are to sample the RMS data */
if (grabData_) {
irms_ = 0;
ADE_read(MR_IRMS, (unsigned char *)&irms_, MR_IRMS_CNT);
vrms_ = 0;
ADE_read(MR_VRMS, (unsigned char *)&vrms_, MR_VRMS_CNT);
grabData_ = 0;
}
}
/* Temperature available */
if (irqstat & IRQ_TEMP) {
temp_ = 0;
ADE_read (MR_WAVEFORM, (unsigned char *)&temp_, MR_WAVEFORM_CNT);
}
/* Reset complete */
if (irqstat & IRQ_RESET) {
}
/* Active energy register overflowed */
if (irqstat & IRQ_AEOF) {
}
/* Channel 2 (V) greater than VPKLVL */
if (irqstat & IRQ_PKV) {
}
/* Channel 1 (I) greater than IPKLVL */
if (irqstat & IRQ_PKI) {
}
/* Apparent energy more than 1/2 full */
if (irqstat & IRQ_VAEHF) {
}
/* Apparent energy register overflowed */
if (irqstat & IRQ_VAEOF) {
}
/* Missing a zero crossing */
if (irqstat & IRQ_ZXTO) {
}
/* Power changed to positive */
if (irqstat & IRQ_PPOS) {
}
/* Power changed to negative */
if (irqstat & IRQ_PNEG) {
}
/* Reserved */
if (irqstat & IRQ_RESERVED) {
}
} /* end ADE_isr */
/**************************************************************************
* ADE_data
*
* Collect and print data
*
* @return void
*************************************************************************/
void ADE_data() {
unsigned long vrms;
unsigned long irms;
float v;
float i;
float p;
/* Get RMS values */
ADE_sampleRMS(&vrms, &irms);
/* Compute calibrate Vrms and Irms */
v = (float)vrms ;//* vGain[0];
i = (float)irms ;// * iGain[0];
/* Computer Power (rms) */
p = v * i;
/* Print data in CSV form for collector */
Serial.print(0);
Serial.print(",");
Serial.print(irms);
Serial.print(",");
Serial.print(i);
Serial.print(",");
Serial.print(vrms);
Serial.print(",");
Serial.print(v);
Serial.print(",");
Serial.print(p);
Serial.println("");
} /* end ADE_data */
/**************************************************************************
* ADE_dump
* Dump many of the ADE registers
*
* @return void
*************************************************************************/
void ADE_dump(void) {
unsigned char uc;
unsigned int ui;
unsigned long ul;
ul = 0;
ADE_read(MR_WAVEFORM, (unsigned char *)&ul, MR_WAVEFORM_CNT);
Serial.print("Waveform: 0x");
Serial.println(ul, HEX);
#if 0
ul = 0;
ADE_read(MR_AENERGY, (unsigned char *)&ul, MR_AENERGY_CNT);
Serial.println((long)ul, HEX);
ul = 0;
ADE_read(MR_RAENERGY, (unsigned char *)&ul, MR_RAENERGY_CNT);
Serial.println((long)ul, HEX);
ul = 0;
ADE_read(MR_LAENERGY, (unsigned char *)&ul, MR_LAENERGY_CNT);
Serial.println((long)ul, HEX);
ADE_read(MR_VAENERGY, (unsigned char *)&ul, MR_VAENERGY_CNT);
Serial.println((long)ul, HEX);
ul = 0;
ADE_read(MR_RVAENERGY, (unsigned char *)&ul, MR_RVAENERGY_CNT);
Serial.println((long)ul, HEX);
ul = 0;
ADE_read(MR_LVAENERGY, (unsigned char *)&ul, MR_LVAENERGY_CNT);
Serial.println((long)ul, HEX);
#endif
ui = 0;
ADE_read(MR_MODE, (unsigned char *)&ui, MR_MODE_CNT);
Serial.print("Mode: 0x");
Serial.println(ui, HEX);
ui = 0;
ADE_read(MR_IRQEN, (unsigned char *)&ui, MR_IRQEN_CNT);
Serial.print("IRQEN: 0x");
Serial.println(ui, HEX);
ui = 0;
ADE_read(MR_IRQ, (unsigned char *)&ui, MR_IRQ_CNT);
Serial.print("IRQ: 0x");
Serial.println(ui, HEX);
#if 0
ui = 0;
ADE_read(MR_RSTIRQ, (unsigned char *)&ui, MR_RSTIRQ_CNT);
Serial.print("RSTIRQ: 0x---");
Serial.println((long)ui, HEX);
uc = 0;
ADE_read(MR_CH1OS, (unsigned char *)&uc, MR_CH1OS_CNT);
Serial.println((long)uc, HEX);
uc = 0;
ADE_read(MR_CH2OS, (unsigned char *)&uc, MR_CH2OS_CNT);
Serial.println((long)uc, HEX);
uc = 0;
ADE_read(MR_GAIN, (unsigned char *)&uc, MR_GAIN_CNT);
Serial.println((long)uc, HEX);
uc = 0;
ADE_read(MR_PHCAL, (unsigned char *)&uc, MR_PHCAL_CNT);
Serial.println((long)uc, HEX);
ui = 0;
ADE_read(MR_APOS, (unsigned char *)&ui, MR_APOS_CNT);
Serial.println((long)ui, HEX);
ui = 0;
ADE_read(MR_WGAIN, (unsigned char *)&ui, MR_WGAIN_CNT);
Serial.println((long)ui, HEX);
uc = 0;
ADE_read(MR_WDIV, (unsigned char *)&uc, MR_WDIV_CNT);
Serial.println((long)uc, HEX);
ui = 0;
ADE_read(MR_CFNUM, (unsigned char *)&ui, MR_CFNUM_CNT);
Serial.println((long)ui, HEX);
ui =0;
ADE_read(MR_CFDEN, (unsigned char *)&ui, MR_CFDEN_CNT);
Serial.println((long)ui, HEX);
#endif
ul = 0;
ADE_read(MR_IRMS, (unsigned char *)&ul, MR_IRMS_CNT);
Serial.print("IRMS 0x");
Serial.println(ul, HEX);
ul = 0;
ADE_read(MR_VRMS, (unsigned char *)&ul, MR_VRMS_CNT);
Serial.print("VRMS 0x");
Serial.println(ul, HEX);
#if 0
ui = 0;
ADE_read(MR_IRMSOS, (unsigned char *)&ui, MR_IRMSOS_CNT);
Serial.println((long)ui, HEX);
ui = 0;
ADE_read(MR_VRMSOS, (unsigned char *)&ui, MR_VRMSOS_CNT);
Serial.println((long)ui, HEX);
ui = 0;
ADE_read(MR_VAGAIN, (unsigned char *)&ui, MR_VAGAIN_CNT);
Serial.println((long)ui, HEX);
uc = 0;
ADE_read(MR_VADIV, (unsigned char *)&uc, MR_VADIV_CNT);
Serial.println((long)uc, HEX);
ui = 0;
ADE_read(MR_LINECYC, (unsigned char *)&ui, MR_LINECYC_CNT);
Serial.println((long)ui, HEX);
ui = 0;
ADE_read(MR_ZXTOUT, (unsigned char *)&ui, MR_ZXTOUT_CNT);
Serial.println((long)ui, HEX);
uc = 0;
ADE_read(MR_SAGCYC, (unsigned char *)&uc, MR_SAGCYC_CNT);
Serial.println((long)uc, HEX);
uc = 0;
ADE_read(MR_SAGLVL, (unsigned char *)&uc, MR_SAGLVL_CNT);
Serial.println((long)uc, HEX);
uc = 0;
ADE_read(MR_IPKLVL, (unsigned char *)&uc, MR_IPKLVL_CNT);
Serial.println((long)uc, HEX);
uc = 0;
ADE_read(MR_VPKLVL, (unsigned char *)&uc, MR_VPKLVL_CNT);
Serial.println((long)uc, HEX);
#endif
ul = 0;
ADE_read(MR_IPEAK, (unsigned char *)&ul, MR_IPEAK_CNT);
Serial.print("IPEAK 0x");
Serial.println(ul, HEX);
#if 0
ul = 0;
ADE_read(MR_RSTIPEAK, (unsigned char *)&ul, MR_RSTIPEAK_CNT);
Serial.println(ul, HEX);
#endif
ul = 0;
ADE_read(MR_VPEAK, (unsigned char *)&ul, MR_VPEAK_CNT);
Serial.print("VPEAK 0x");
Serial.println(ul, HEX);
#if 0
ul = 0;
ADE_read(MR_RSTVPEAK, (unsigned char *)&ul, MR_RSTVPEAK_CNT);
Serial.println((long)ul, HEX);
#endif
#if 0
uc = 0;
ADE_read(MR_TEMP, (unsigned char *)&uc, MR_TEMP_CNT);
Serial.println((long)uc, HEX);
ui = 0;
ADE_read(MR_PERIOD, (unsigned char *)&ui, MR_PERIOD_CNT);
Serial.println((long)ui, HEX);
uc = 0;
ADE_read(MR_TMODE, (unsigned char *)&uc, MR_TMODE_CNT);
Serial.println((long)uc, HEX);
uc = 0;
ADE_read(MR_CHKSUM, (unsigned char *)&uc, MR_CHKSUM_CNT);
Serial.println((long)uc, HEX);
#endif
uc = 0;
ADE_read(MR_DIEREV, (unsigned char *)&uc, MR_DIEREV_CNT);
Serial.print("DIEREV 0x");
Serial.println(uc, HEX);
} /* end ADE_dump */