Four
Functions and Beyond
Tips
for Designing an RPN Calculator
CALCULATOR
MODES
The
calculator controller has eight modes of operation (see
Table 2). I still haven’t implemented modes 1, 2, 5,
and 7, but I will work on them if time permits. The
remaining modes are activated using C compiler flags
and the selected application files (calc1.c, hp45.c,
or mathtst.c). I plan on adding an embedded menu to
select these modes via a PC or laptop connected to the
four-function calculator’s hardware with a standard
serial RS-232.
| Table
2—Modes 1, 2, 5, and 7 have not been implemented.
Activate the remaining modes using C compiler flags
and the selected application files (calc1.c, hp45.c,
or mathtst.c). The mode switch selection is not
available. |
You
can also select the modes with the optional switches—S1,
S2, and S3—connected to port C and RC0 through RC2.
When the calculator is powered, it runs in the mode
indicated by the switches. RA4 is an optional push button
connected to port A designated as the calculator’s Gold
key, or Shift key, which gives you access to the secondary
functions. Color-coded labels are handy for indicating
which functions may be accessed with the Gold key.
The
RPN calculator mode is the default mode when the unit
is powered up. Table 3 is a list of four-function calculator
controller commands and expressions. In this mode, the
firmware reads keystrokes from the serial port for user
input, converts them from ASCII to IEEE floating-point
operands, and uses them to build simple arithmetic expressions
with the floating-point numbers and the basic operators—“+,”
“–,” “×,” “/,” and “E”—as delimiters.
| Table
3—The serial interface and 4 × 4 keypad support
certain functions, expressions, and characters with
the calculator hardware and firmware configured
for using the RPN calculator mode. In order to clear
the display, press any key after the calculation
has been performed. Note that these are the operators
and character set currently used by the RPN calculator. |
The
“E” operator represents the Enter key, which copies
the value in the X register to the Y register, the Y
register to the Z register, and the Z register to the
T register. The value held in the T register is lost.
If you press a “+,” “–,” “×,” or “/” operator
key, the firmware determines which function was selected
by the mathematical operator that was keyed in, and
it invokes the appropriate subroutine to perform the
arithmetic operation. Then, the value is displayed in
the X register on the PICDEMO2 LCD and the HyperTerminal
display (if used as a serial communications program).
The stack is also dropped. Let’s look at an example.
To
add the numbers 4.5 and 5.5 using RPN notation, enter
the following:
4.5E5.5+
The
result, 10.0, should appear on the LCD and HyperTerminal
window (if connected). Keep in mind that my floating-point
conversion routines don’t round up, so some answers
won’t appear as they do on commercial calculators (e.g.,
1.999998 appears as opposed to 2.0).
ADVANCED
CALCULATIONS
The
source code examples were written for Borland C++, but
I was able to convert most of them to Visual C without
difficulty. I was also able to convert some of the math
functions in Crenshaw’s book to PIC18C. The only problems
I ran into during the conversion were partly the result
of the different word lengths (i.e., 32 bits for the
PC versus 8 bits for the PIC) and floating-point-type
limitations (i.e., double float for the PC versus float
for the PIC). Thus, I only managed to get sine, cosine,
tangent, and square root functions running on the PIC
in time for this article. With a little more time and
effort, I could easily convert the remaining functions
to PIC18C with the information provided in Crenshaw’s
book.
To
test my PIC18C floating-point math functions—including
N!, square root, trigonometric, and scientific functions—I
wrote test programs that generated tables of sines,
cosines, tangents, and arctangents from 0 to 360° in
addition to square roots of squares from zero to 1024
(see Listing 1). Then, I compared the results to the
same program executed with Visual C++. The answers were
close to machine precision, as shown in the mathtst.log
screen capture file, which you may download from the
Circuit Cellar ftp site.
| Listing
1—You can use the JMATH package to generate
a sine, cosine, and square root table. |
You
could build a more advanced calculator with slight modifications
to the current four-function calculator design. In addition
to the larger 4 × 5 keypad, additional functions would
be necessary (e.g., look-up tables or Taylor Series
polynomials for computing sine, cosine, tangent, and
exponential and logarithmic functions).
Advanced
expression evaluation that either completely eliminates
parentheses or allows the use of parentheses in a mathematical
expression may be explored with calculator languages
such as RPN, AOS, Forth, and Basic. Special unit conversion
functions, such as those used in physics, astronomy,
electronics, and mechanics, are also easy to add. In
addition, modern calculators now have advanced graphing
capabilities with an LCD. You could include these capabilities
with the proper software.
Four-function
calculators have a few drawbacks. For instance, unlike
the advanced calculators sold by HP, TI, and Casio,
they cannot readily handle parentheses and complicated
expressions without storing intermediate results. The
special calculator languages include RPN, AOS, Forth,
and Basic. You may upgrade the four-function calculator’s
firmware to handle many of these calculator languages.