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





 

Four Functions and Beyond
Tips for Designing an RPN Calculator


FIRMWARE

As I wrote the firmware for the HP-45 RPN-based calculator, I didn’t attempt a complete emulation—that would have required extra hardware and development. Instead, I opted to emulate the basic features and leave out the most complex ones. As such, the calculator is useful as a demo or learning tool; however, it is not ideal for tasks that require additional precision and accuracy.

The stack manipulation functions that handle the pushing, popping, and exchanging are straightforward. You may download the functions from the Circuit Cellar ftp site. Note that it didn’t require complete stack-handling capabilities because of the small number of elements in the stack. The evaluating function I used is the same as the one for the four-function calculator (calc1.c). The only difference is how the HP-45 application calls the function. You may download the firmware for the RPN calculator (hp45.c), as well as the firmware for the trigonometric and scientific functions (mathtst.c), from the Circuit Cellar ftp site.

I employed various C compiler flags, which allowed me to include drivers for the different hardware configurations used during the development process. This includes flags for the LED drivers, LCD drivers, and keypad drivers (if used).

In addition, I have flags that support the serial port and debugging (using MPSIM). I used the flags to save space on the PIC. By enabling or disabling each flag, I was able to maximize the PIC’s storage capacity depending on the hardware configuration (e.g., PICDEMO2, ICD2, or my own RPN calculator hardware). Again, I would like to integrate all of these features in one application, but to accomplish the task, I need to optimize the software or use a microcontroller with more capacity.

The values entered in the HP-45 display register, X, are sent to the LCD, LED, or serial port depending on which driver flags have been defined. If the keypad driver flag hasn’t been defined, input is redirected from the keypad to the serial port. Otherwise, input comes directly from the keypad, which means the application may hang if the keypad isn’t connected to the four-function calculator hardware.

The software contained in the myprintf.c file implements a floating-point format I/O routine similar to the Visual C++ printf function for displaying floating-point numbers on the LCD and LED displays. It only provides one kind of output format that is similar to the %f format used by printf.

SOFTWARE ISSUES

The application can be programmed in a PIC18F452 (flash memory) or PIC18C452 (EEPROM). Use the hp45.hex file from the Circuit Cellar ftp site to program your own.

If you are interested in customizing the software, I divided the firmware for various hardware functions into separate C files to facilitate development, debugging, and maintenance. The code for the hp45.c application reuses code from modules I developed for the four-function calculator and other applications (see Figure 3). In my opinion, this is the best way to organize a large application, because it may be successfully tested and integrated in the hardware one module at a time.

(Click here to enlarge)

Figure 3—Certain C software modules make up the current RPN calculator’s firmware configuration.

There are better methods for organizing the firmware (e.g., the object-oriented programming methodology with a language such as C++ or Java). Although I would like to see a C++ or Java compiler for the PIC, it would require a high-end PIC with lots of on-chip RAM and flash memory.

The source lines of code (SLOC) metric is used by most software developers as a productivity metric to estimate how long it will take to develop software and to determine its cost. The numbers range from 200 to 1000 SLOC per man month (SLOC/MM), depending on the kind of development system and computer language. For instance, if assembly code is used for an embedded real-time microcontroller, the SLOC could be around 250 SLOC/MM. On the other hand, if the application is a GUI written in Visual C++, the SLOC count could be as high as 1000 SLOC/MM. Each line of C code converted to assembly could take from five to 10 additional assembly instructions to do the same thing, depending on your skill level and the optimization efficiency of a high-level compiler.

The SLOC count for the RPN calculator application is shown in Table 1. Even though the SLOC count for the hp45.c application is modest, the reused drivers and utilities from other modules increase the SLOC count. Although I wasn’t keeping track of the time required for each module (I developed the code in my spare time), the figure shows the complexity of the application. Actually, according to the map file generated by the PIC18C linker, I used approximately 87% of the PIC’s memory resources for the four-function calculator’s firmware (calc1.c).

Table 1—The calculator’s firmware is complicated. Listed is a count of all the C source statements in each file (excluding comments). I didn’t use all of the code from each file in this application.

   
previous