-
Notifications
You must be signed in to change notification settings - Fork 2
/
PowerMeter.ino
209 lines (153 loc) · 4.79 KB
/
PowerMeter.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
/**********************************************************************
*
* Home energy monitor using ADE7763
*
*
* This program is used to monitor home electrical consumption by
* instrumenting the electrical panel with current transformers (CT).
*
* This monitor is based on an Arduino for basic command and control of
* the ADE7763 energy monitoring chip.
* To support multiple CTs, this system has multiple sensor nodes (SN).
* Each SN has a 4 position mux that routes four CT signals to the ADE.
* To control the mux, a saml PIC (16F627A) sits on a data/clock bus that
* the Arduino can drive to select the proper channel.
*
*********************************************************************/
#include <SPI.h>
#include "PowerMeter.h"
#include "Pins.h"
#include "ADE7763Reg.h"
#include "ADE7763.h"
#include "ADE7763LL.h"
unsigned long target; /* Target time for next data dump */
char cmd[32]; /* Command string for user interface */
unsigned char iCmd = 0; /* Index into current command */
unsigned char run = 1; /* Run data loop flag */
const long deltaT = 1000*2; /* Milli seconds to delay */
/**********************************************************************
*
* Set up routine
*
*********************************************************************/
void setup(void) {
unsigned char revision;
unsigned char uc;
Node_t node;
/* Initialize the serial port to host */
Serial.begin(9600);
/****************************/
/* Initialize SPI Bus */
/****************************/
BUS_init();
/*****************************/
/* Initialize the ADE */
/*****************************/
ADE_config();
/* Set first target time for data dump */
target = millis();
Serial.println("setup complete");
return;
} /* end setup */
/**********************************************************************
*
* Main loop
*
*********************************************************************/
void loop(void) {
int i, v, p;
int bi;
char b;
unsigned char uc;
unsigned int ui;
unsigned long ul;
/* If the target time has come up */
if ((millis() >= target) && run) {
/* Wait for data to settle */
delay(1000);
/* Print the data */
ADE_dump();
/* Compute next target time (five seconds in future) */
target = millis() + deltaT;
} /* if target time */
/* If we have a byte waitting */
if (Serial.available() > 0) {
/* Get the byte, exit if blank */
bi = Serial.read();
if (bi == -1) {
return;
}
/* Truncate long commands */
if (iCmd == 31) bi = '\r';
/* If this string has been terminated */
if (bi != '\r') {
/* Acculuate byte */
cmd[iCmd] = (char)bi;
iCmd++;
} else {
/* Null terminate the string */
cmd[iCmd] = 0;
/* Free up the fring for next command */
iCmd = 0;
/* Parse the string */
Serial.print("Got cmd: ");
Serial.println(cmd);
/* Switch based on first byte */
switch (cmd[0]) {
case 'g':
run = 0;
break;
case 'G':
target = millis();
run = 1;
break;
case 'r':
/* Reset the ADE */
Serial.println("Reset ADE");
ADE_reset();
break;
case 'D':
Serial.println("Dump");
ADE_dump();
break;
case 'R':
Serial.println("Reset");
ADE_resetPeaks();
break;
case 'i':
ul = 0;
ADE_read(MR_IRMS, (unsigned char *)&ul, MR_IRMS_CNT);
Serial.print("IRMS 0x");
Serial.println(ul, HEX);
break;
case 'v':
ul = 0;
ADE_read(MR_VRMS, (unsigned char *)&ul, MR_VRMS_CNT);
Serial.print("VRMS 0x");
Serial.println(ul, HEX);
break;
case 'I':
ul = 0;
ADE_read(MR_IPEAK, (unsigned char *)&ul, MR_IPEAK_CNT);
Serial.print("IPEAK 0x");
Serial.println(ul, HEX);
break;
case 'V':
ul = 0;
ADE_read(MR_VPEAK, (unsigned char *)&ul, MR_VPEAK_CNT);
Serial.print("VPEAK 0x");
Serial.println(ul, HEX);
break;
case '.':
/* Reset interrupt status */
ADE_read (MR_IRQ, (unsigned char *)&ui, MR_IRQ_CNT);
Serial.print("IRQ: 0x:");
Serial.println(ui, HEX);
break;
default:
Serial.println("unknown command");
break;
}
}
} /* byte available */
} /* end loop */