PIC16f877 based simple calculator project

This PIC microcontroller tutorial provides a simple calculator implementation for PIC16F877 microcontroller. This is a simple one digit[1] calculator which implements only 4 functions addition(+), subtraction(-), multiplication(x) and division(/). The code for PIC16F877 is written in C language using MPLAB with HI-TECH C compiler. You can download this code from the ‘Downloads‘ section at the bottom of this page.

In this post, it is assumed that you know,

How to interface keypad with PIC16F877 microcontroller. If you don’t then please read this page.

How to interface LCD with PIC16F877 microcontroller. If you don’t then please read this page.

The following diagram (made in Proteus) shows the PIC microcontroller circuit diagram.

Figure 1. Circuit diagram for implementing calculator on PIC16F877

In the above figure, we can see that a result of “2+2=4” is shown on the screen. To achieve this result, first press ‘2’ from the keypad. Then press ‘+’ and then press ‘2’ again. After that, on pressing ‘=’ from the keypad the result ‘4’ is automatically displayed on the screen.

Features of this calculator

Error messages are displayed if wrong input is detected. For example, if calculator is expecting a number, but a function key is pressed then ‘Wrong Input‘ message is displayed. Similarly, ‘Wrong Function‘ message is displayed if wrong key is pressed instead of a function key.

In the main code, firstly keypad and LCD are initialized. Then the code waits for the first number[1] from the keypad. After getting this number LCD screen is cleared. And this number is displayed on the LCD. After that, code waits for the function key[2] from the user. After getting the function key, code waits for the second number[1] and then the equal sign. After getting the equal sign, according to the desired function the result is calculated and displayed on the screen.

You can leave your comments in the comment section below.

Notes and References

[1] You can give any single digit input from 0 to 9.[2] You can select any function i-e ‘+’ or ‘-‘ or ‘x’ or ‘/’.

Downloads

The calculator code for PIC16F877 was compiled in MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10. To download code and Proteus simulation click here.