Calculator Using Postfix Notation

By T. Anand. He is a co-founder of Knewron Technologies, has over 12 years of direct experience with small-, medium- and large-scale projects in manufacturing, service sectors, electronics and electrical systems, IT, etc. A chartered engineer by profession, he also has broad experience in R&D of IT, white goods, electronic and electrical systems.

8551
 

Software

BED_PartsThe program code is written in ‘C’ language and compiled using AVR Studio 6.0. The hex file is burnt into the microcontroller using a suitable programmer.

Postfix algorithm

The method is stack-and-queue dependant and uses a stack for storing functions (aka operators) and a simple queue to hold numbers (aka operands). Due to the nature of operations performed in this algorithm, it is also called a ‘shunting yard algorithm’ since the operation resembles a railroad shunting-yard methodology. Table II shows step-by-step evaluation of a regular expression using postfix algorithm.

Once parsing is done, rest of the part is much easier to perform. For evaluating the answer following steps have been performed in the source code:

1. Initialise an empty stack
2. Scan the postfix notation string from left to right
3. If the token read is operand, push it into stack
4. Else, if the token read is operator (that would mean that there are at least two operands already present in the stack):
(i) Pop these two operands from stack
(ii) Perform the operation as per the operator
(iii) Push the results back into the stack
5. Once the string scanning is complete, there would be only one element present in the stack, which is the final answer.

AEE_Table-2

Construction and testing

A single side PCB layout for the postfix calculator circuit is shown in Fig. 4 and its component layout in Fig. 5. Assemble the circuit on the recommended PCB to save time and minimise assembly errors. Use an IC base for microcontroller IC1. IC2 being an SMD package it should be mounted on the solder side.

Once the PCB is ready, follow the steps below to get the project running:
1. Install the FTDI drivers on your computer from the FTDI website corresponding to your operating system.
2. Connect the PCB to the computer through the provided USB interface, and you should see USB-to-serial converter detected in the device manager.
3. The microcontroller should already be programmed. Now press the reset button and the LCD will show ‘my calci’ as in Fig. 1.
4. Open hyper-terminal or a similar program and select the right COM port.
5. Write the equation to be calculated in the hyper-terminal window and write ‘=’ at the end.

Fig. 4: An actual-size, single-side PCB layout for the postfix calculator
Fig. 4: A single side PCB layout for the postfix calculator
Fig. 5: Component layout for the PCB
Fig. 5: Component layout for the PCB

Download PCB and Component Layout PDFs: click here

Download Source Code: cllck here

On completion of the last step, the equation and the calculated result appear on the window as well as the LCD.

To test the circuit for proper functioning, check 5V supply at TP1 with respect to TP0. The presence of supply is also indicated by LED1. The reset to the system can be observed at TP2. LED2 and LED3 indicate to and fro data communication between IC2 and IC1. Ensure that correct baud rate is selected (19200 bps as mentioned in the source code) for communication between MCU and PC.


SHARE YOUR THOUGHTS & COMMENTS

Please enter your comment!
Please enter your name here