July
1998, Issue 96
A
PIC-Based AC Power Meter
DISPLAY
CONCERNS
Now that Ive
discussed how the E accumulator is maintained,
I want to turn to how the energy accumulation is displayed.
If watt-hour
readings are the finest resolution displayed, then the
user might have to wait 2 min. to see 0.001 kWh, depending
on the load. To solve this problem, I implemented autoranging
using three display modes.
Display mode
d1 displays readings in the range of 0.0009.999
Wh. Once 10 Wh are accumulated, display mode automatically
switches to d2 mode.
The d2 mode
displays readings in the range of 0.0109.999 kWh.
Once 10 kWh are accumulated, the display mode automatically
switches to display mode d3, which displays readings
in the range 10.0016.77 kWh.
Display mode
d1 is implemented by converting the low-order 16 bits
of E (milliwatt-hours) to BCD, then displaying
the four least significant digits with the decimal point
three places to the left (i.e., 0.000). The move of
the decimal point effects a multiply by 1000, causing
watt-hours to be displayed.
Display mode
d2 requires display of kilowatt-hours, and has a least
significant digit of watt-hours. Divide E (in
milliwatt-hours) by 1000 to get the result in watt-hours.
This is implemented by using the upper 16 bits of E,
effecting a divide by 256 operation on E.
Recall that
canned math routines support 16-bit math, not 24-bit
math. So, by taking the upper 16 bits of E and
performing a divide by 4, a divide by 1024 is implemented,
which approximates the correct divide by 1000. This
result is converted to BCD, and then the four least
significant digits are displayed with the decimal point
set three places to the left.
Display mode
d3 also requires display in kilowatt-hours. The only
difference between the d2 and d3 modes is that d3 needs
an additional divide by 10 to provide a reading in the
range (10.0016.77). By taking advantage of the
fact that the BCD conversion routine returns a five-digit
result, a divide by 10 is done just by displaying the
four most significant digits and placing the decimal
point two places to the left.
The LCD services
are designed such that the LCD routines expect data
where the BCD math routine deposits its result. Although
I dont elaborate on these routines in detail here,
I mention them because they cooperate with the PIC math
routines well.