English
Language : 

AN939 Datasheet, PDF (5/18 Pages) Microchip Technology – Designing Energy Meters with the PIC16F873A
FIRMWARE
A high-level overview of the energy metering firmware
is shown in Figures 3 through 6.
Before entering the main metering routine, the firmware
initializes itself. Measurement buffers and Fault flags
are cleared and the timers for the measurement
routines are reset. If a calibration signal is present
(done by setting a jumper), the firmware waits for
calibration data on the serial port. If the signal is absent
or no data is received, the data EEPROM is
interrogated for a calibration flag. If this flag is set, the
calibration data stored there is loaded for the firmware’s
use. If no calibration data is present, default values in
the firmware are used instead.
Initialization also gives users the opportunity to perform
a calibration routine, which downloads data from an
external meter calibration utility through a serial
connection. Details are provided in “Calibrating the
Meter” (page 11).
The main loop is responsible for updating the kWh
counter and maintaining the visual display shown on
the LCD. The kWh counter is incremented on the basis
of a status flag, set in an interrupt driven power mea-
surement routine, discussed below. A separate display
timer is used to determine how long each measured
value is displayed before rolling over to the next value.
The default value for this application is 6.26s.
An interrupt signal from an external RTC causes the
firmware to compare the total energy consumed since
the last interrupt to the value stored in data EEPROM
and update the value if the consumed power is greater.
This provides a record for tracking peak demand for one
month. The default value for the interval is 30 minutes.
Voltage and current measurement are performed during
an interrupt service routine triggered by the Timer0 inter-
rupt. Measurements are performed in a specific
sequence, with the entire sequence being repeated
every 2.5 ms. The measurement routine is an Interrupt
Service Routine triggered by the Timer0 interrupt. Inter-
leaving samples of voltage and current are taken, with
the simultaneous voltage values for the current measure-
ments being interpolated by the application. Calibrations
for phase error are included in this calculation by using a
delay counter to adjust the voltage readings; in this appli-
cation, each increment of the counter delays the voltage
reading by 1.4 μs (assuming a 20 MHz clock). The entire
sequence of five measurements and their corresponding
conversions takes much less time than the interval
between sequences, typically less than 300 μs.
To provide an added measure of accuracy, the offset
voltage is periodically monitored to check for a stable
baseline. This is done much less frequently than other
measurements, approximately once every 1.25 seconds.
Adjustments to the offset value, subtracted from voltage
and current measurements, are made as needed.
AN939
A single sampling sequence is taken during every inter-
rupt routine. The accumulated energy to that point,
adjusted by Cg, is compared with the values for D x C,
as well as a fraction of the D xC equivalent to
1/3200 kWh. Prior to calibration, the application uses a
value of 200 for both C and Cg. After calibration, Cg
may change to compensate for gain inaccuracies; C
remains at 200. An increment or decrement of one in
Cg can be used to compensate for a deviation of
±0.5%. Increasing Cg compensates for negative
deviations, while decreasing it compensates for
positive deviations. In practical terms, the adjustment
range possible runs from -25 to +100%.
If the accumulated value exceeds 1/3200 kWh, an
external signal is generated for calibration purposes. If
the energy equals or exceeds the adjusted value of D,
the kWh flag is set; this will cause the kWh counter to
be incremented by one during the next execution of the
main loop. For amounts exceeding D, the difference
between the energy buffer and D is saved and added
to the next round of accumulation.
The ISR executes a housekeeping routine once every
20 interrupts to monitor for other conditions that may
affect the energy reading. These include current
channel amplifier gain, ground Faults, negative
accumulated value and noise.
Because of the large dynamic range for load current, a
selectable gain amplifier is used to match the current
range to the range of the ADC. The gain is controlled
by summing the current for 2½ cycles of the supply
frequency. If the value exceeds an equivalent of 3A, the
amplifier’s gain is switched to low. If the value falls
below an equivalent of 2.8A, the gain is switched to
high. The small overlap between switching thresholds
provides a built-in hysteresis, which prevents constant
switching between gain states.
The same summed values are also compared for
differences. If the line current exceeds the neutral cur-
rent by 8% or more, a ground Fault is indicated. When
this happens, the meter switches from metering on the
neutral current (the default) to the line current. An
external signal is also sent to indicate a possible
tamper condition.
Also included in the measurement routine is logic to
handle reverse-current conditions. Although the energy
calculations involve signed operations, the result of the
calculation after 2½ cycles should always be positive.
This assumes that the current sensors are connected to
produce voltage and current signals in phase (or nearly
so) at Unity Power Factor (UPF). If this is properly done,
the accumulated energy will always be positive, even
across a PF range from 0.5 lag to 0.8 lead. Neverthe-
less, a reverse-current condition, where voltage and
current are 180° out of phase, will still produce a nega-
tive accumulated energy result. If this happens, a flag is
set to permit the energy algorithm to produce a correct
(positive) result from the next cycle onward.
© 2005 Microchip Technology Inc.
DS00939A-page 5