circuitcellar.com
Magazine Support   Digital Library   Products & Services   Suppliers Directory 
 
 





 

Four Functions and Beyond
Tips for Designing an RPN Calculator


AOS

Texas Instruments first used the AOS in the late 1970s for its SR-52 calculator. The SR-52 allowed you to enter equations without having to restructure them, as is the case with RPN. Mathematical expressions are entered into an AOS-based calculator exactly as they appear in standard expressions (including parentheses). Casio calculators also use the AOS method of evaluating expressions. They also include the bracket keys and indicate the current nesting level number on the LCD.

The price paid for this convenience is a more complex expression evaluation algorithm. If the programmable features are implemented, an AOS language interpreter or compiler needs to be developed, which isn’t an easy task. The PIC may encounter memory and storage limitations if this is implemented using the current generation of PIC chips.

The four-function calculator I described last month was a good starting point for the AOS system, particularly because it allows you to enter binary expressions for evaluation in their natural order: X = Y + Z; X = Y – Z; X = Y × Z; and X = Y/Z. Note that X, Y, and Z represent floating-point numbers. The contents of X are displayed on the LED digits or LCD screen. However, the calculator cannot handle the following expression because of the nested parentheses and order of operations required for evaluation:

 The calculator could handle the expression only if it was broken into smaller binary expressions and the intermediate values were written down or stored in a memory register.

One possibility is to convert the expression in postfix notation to a Polish or reverse Polish string in infix notation and then interpret the string to get the final answer. There are a couple of ways to carry out the transformation, but they require advanced compiler design techniques and Unix tools, YACC, and LEX to produce a lexical analyzer and a parser.

Implementing a complete AOS-based calculator application on a microcontroller such as a PIC, Atmel, or 68HC12 is challenging because of the limited RAM and flash memory. The application would make a great school project that would require assembly language to maximize the microcontroller’s resources. You can build a simple parser for an AOS calculator using the recursive descent algorithms described in Niklaus Wirth’s classic text, Algorithms + Data Structures = Programs.

The following calculator program allows you to add one to register 1 and display the value on the TI-59:

CLR
1
LBL
A
1
SUM
1
RCL
1
PAUSE
GOTO
A
BASIC

Radio Shack and Sharp were the first manufacturers to develop a calculator that was programmable in Basic. The Basic interpreter converts source statements to tokens, which may be executed by the interpreter. It’s possible to develop a Tiny Basic interpreter that can run a smaller version of Basic for evaluating mathematical expressions and writing small programs. You can use the serial EEPROM to store the Basic tokens, which can be fetched by the Tiny Basic interpreter when required. The PIC’s SRAM can provide variable storage. The only problem is the PIC18C452’s limited EPROM and SRAM capacity. My DIY design would require the use of external RAM to store the tokenized Basic statements. Listing 2 is a short Basic calculator program for converting centimeters to inches.

Listing 2—Want to convert centimeters to inches? If so, try this Basic program.

FORTH

The Forth language is similar to the RPN notation used by HP calculators; it is an interpreter and a compiler. Each keyword or variable is delimited with a blank. You can insert the Forth interpreter in the four-function calculator’s firmware via the serial EEPROM or additional SRAM as a large stack.

Forth is composed of a threaded dictionary containing all the Forth keywords. Use a pointer to access the desired word in the dictionary. Each keyword is a function or subroutine performing a different action. New keywords are added to the Forth dictionary with the : operator.