July
1998, Issue 96
A
PIC-Based AC Power Meter
HARDWARE
DESIGN
Figure
1 shows the four subsystems of the power meter.
The power-supply subsystem supplies DC power to the
other subsystems. The analog interface contains Woodwards
circuit [1].
The
microcontroller and A/D subsystem acquire data and compute
power readings that are then displayed by the LCD subsystem.
Figure 2 shows how the subsystems
link together.
A
9-V battery connects to J6 that feeds a 78L05 regulator.
The 5.1-kW 1% resistors in a voltage divider network
provide the 2.5-V bias voltage for the analog section.
They also provide the 9-V sense voltage to the microcontroller
for low-battery detection.
A
resistor divider network is selected over a 2.5-V reference
diode because of cost to provide the 2.5-VDC reference
used in the analog section. I chose 5.1-kW resistors
since Im already using this value in the analog
interface.
Woodwards
original circuit used ±15-VDC power supplies and an
OP27 precision op-amp. My goal was to modify his circuit
using a +5-VDC single supply and a common LM358 op-amp.
The
redesign for single-supply operation is straightforward:
swap 2.5-V DC bias for ground, ground for 15 V,
and +5 V for +15 V. Woodwards circuit indicates
power delivered to and from the load, with power delivered
to the load being below the 2.5-V bias.
Power
readings range between ground and +2.5 V, effectively
reducing the ADC resolution to seven bits. Therefore,
128 discrete output values are possible between no load
and full scale.
A
pot, P1, calibrates the full-scale value to approximately
1200 W nominal. By adjusting the pot to give a full-scale
reading of 1280 W, the actual power (in watts) is obtained
by:

So,
if ADvalue equals 0x76, the power is 100 W.
I
initially chose the PIC16C71 because of its low cost
and onboard four-channel eight-bit ADC. However, you
can save even more by replacing the 16C71 ($12.30)
with a separate ADC, Nationals ADC0831 ($3.29),
in combination with the ADC-less PIC16C61 ($6.15).You
save $2.86, but you need space for one more 8-pin DIP.
The
National ADC0831 is a low-cost 8-bit ADC that has a
simple three-wire serial interface (chip select, clock,
and data) and a 32-ms conversion time.
Note
there are no pull-up resistors on the two momentary
switches. Pullups are provided internally by the PIC16C61.
Again, you see the 5.1-kW 1% resistor used as the MCLR
pullup, which means the MCLR pin can be shorted to ground
without shorting out the +5-V supply.
This
component could be replaced with a 0-W jumper for production.
Currently, a crystal is used as the microcontroller
clock, but you could replace it with a ceramic resonator
for more cost savings.
The
display subsystem uses a nonmultiplexed Varitronix LCD,
with a Microchip AY0438 LCD driver. The AY0438 operates
up to 32 segments of an LCD, providing a simple three-wire
serial interface (data, clock, and load), and its
capable of generating the AC waveforms required to illuminate
LCD segments.
For
a LCD segment to be on, there must be voltage differential
between the segment pin and the backplane. However,
this voltage cannot be static (non-time-varying) or
the display gets permanently damaged. To drive the LCD
correctly, a low-frequency (100 Hz) square wave is applied
to the backplane pin.
For
a segment to be on, a segment pin must have the inverted
backplane signal applied. For a segment to be off, the
segment pin must have the in-phase backplane signal
applied.
The
AY0438 generates this backplane waveform and the correct
segment waveforms without processor involvement. Just
hang a capacitor on the LCDf pin to control the onboard
oscillator.
Initially,
I used the onboard oscillator of the AY0438 to generate
the AC waveforms needed and to get the display up and
running. However, the AY0438 can only drive 32 segments.
I
needed 33 segments to use the arrow segment and all
four digits, three decimals, and the colon. With a couple
of unused pins on the PIC, I figured it couldnt
be that hard to drive the LCD directly.
And,
driving the LCD is easy. Just remove the cap from the
LCDf pin, then drive this pin (backplane) and the thirty-third
segment directly from the PIC. Remember that you cant
drive the LCD segment pin alone because the AY0438 oscillator
(backplane) would be asynchronous with respect to the
PIC.
The
software must toggle the backplane and the thirty-third
segment every 10 ms. Note that the AY0438 still manages
the other 32 segments and provides a serial interface.
The AY0438 just gets its backplane frequency reference
from the PIC. This is a case where a watchdog timer
should be used because display damage can result if
the backplane doesnt toggle at least every 10100
ms.
Register
definitions of external interfaces to the PIC are shown
in Figure 3. The PIC16C61 is
an 18-pin DIP and only has 13 I/O pins. Obviously, when
youre using an I/O-limited micro like the PIC,
its important to choose peripherals with low pin-count
interfaces.
Five
pins are used for the LCD subsystem. It could have been
three if I didnt need the thirty-third segment.
Three pins are used for the ADC and two for the mode
switches.