You are on page 1of 26


This project explains the development of a basic calculator using a PIC

microcontroller and an LCD (Liquid Crystal Display) as the display. This project can be
self-operational in contrqlling the operation of the input and output device used. The
input device used is a 4x4 keypad and the output device used is the LCD display. A
program is written using C language by using the MPLAB IDE Environment. The
program is then assembled to be connected into a hexadecimal file and downloaded into
the PIC microcontroller. This project is able to analyze data from the press keypad
through microcontroller and display it at LCD display. The hnctionality is implemented
in software (MPLAB IDE). The work that will be done includes development of PIC
circuit, software for PIC to taking input from keypad for calculation and displayed it to
LCD display. The project executes simple calculation and the result is displayed on the


Projek ini menerangkan mengenai pembangunan kalkulator asas yang

menggunakan pengawal mikro dan pemapar LCD sebagai paparan. Projek ini beroperasi
secara bersendirian dalam mengawal operasi peranti masukan dan peranti keluaran yang
digunakan. Peranti masukan yang digunakan ialah 4 x 4 papan kekunci, manakala
pemapar LCD digunakan sebagai peranti keluaran. Aturcara ditulis dengan
menggunakan bahasa C di MPLAB IDE.Setemsnya aturcara tersebut ditukarkan ke fail
hexadesimal dan dimuat turun ke dalarn pengawal mikro PIC. Projek ini mampu untuk
menganalisa data dari kekunci yang ditekan dan memaparkan keputusannya di pemapar
LCD. Kerja-kerja yang dijalankan meliputi pembangunan litar pengawal mikro dan
perisian bagi pengawal mikro untuk menerima masukan daripada papan kekunci dan
memaparkannya pada pemapar LCD. Unit ini dapat befingsi sebagai kalkulator asas
dan keputusan bagi operasi pengiraan yang dibuat dipaparkan di pemapar LCD.





The aim of this final project is to develop a software and hardware for the use of
simple calculator by using LCD (Liquid Crystal Display) with a microcontroller. This
unit is built to control the microcontroller so that it can be self operational in controlling
the operation of the input and output device used.

For the input device, the 4 x 4 keypad is used. Meanwhile the output device used
is the LCD. The microcontroller 16F877 is used because it is required no support device
such as serial port is connected to it. This unit is able to analyze data from the press
keypad and display it at LCD (Liquid Crystal Display) at the same time.


Project Objective

The objective of this final project is to gain knowledge in interfacing between

keypad, microcontroller and LCD. It is built to learn interfacing between software and
hardware and gain experience and practice the electronics theories into practice.

It is also to enhance knowledge in assembly language and C language

programming. In this project, MPLAB IDE software is used to compile the program for
the microcontroller. This unit also can be self-operational in controlling the operation
without the computer hose.


Project Scope

This project consists of two parts as mention earlier which is hardware and
software development. While implementing this unit, a microcontroller assembly
language programming is learned. Besides, it is also can get more knowledge and learn
about the keypad, PIC microcontroller and LCD.

This unit are consisting a development of a basic calculator using chosen PIC
microcontroller, keypad and LCD. There are PIC 16F877, keypad 4 x 4 and Hitachi
HD44780 16 x 2 lines.

A microcontroller circuit is built which can receive and process input from the

keypad and displaying the result to the LCD. A program is written using C language to
initialize the PIC, set the input port, receive the input from the keypad, and process the
input and lastly displaying the result to the LCD.





The main important things for this project are divided to two main parts, which is
hardware and software development. The hardware parts consisting of a PIC 16F84,
LCD and keypad. Meanwhile the software parts are consisting of MPLAB IDE
(assembly language), EEPROM programmer and cross assembler.


Problem Statements

The development of the project requires knowledge of assembly language, C++

and electronic theories. This knowledge is although this subject was taught during the
degree course; they would be implemented in an unfamiliar environment. The
specification and the programming of the PIC and LCD have to be studied thoroughly.

The system block diagram of this project is shown in Figure 2.1. For the
beginning, interface circuit between PIC 16F877, LCD and keypad is built. MPLAB

IDE software brings an ease of software development previously unseen in the 8-bit
micro controller. The MPLAB IDE allowing editing source files in assembly language.
Assembly language is a set of rules used in writing a program of PIC and the assembler
is as a translator for assembly language into a machine language (hexadecimal).The
ability to use MPLAB IDE with multiple debugging tools allows users to easily switch
from the cost-effective simulator to a full-featured emulator with minimal retraining.

23.2 Flow Chart

This is a flow chart, which showed the outline of the project processing.

Project Discussion
- Method
- D~~cculties
- Decision

Submit the Project

System of Project

- Datasheet of PIC, LCD

- Designing the schematic


- Outlook schematic

- Programming
- Approval of the project


- Electronic components

-- Cable/enclosure

I - Mechanical parts

Assembled the circuit on

the breadboard

Test Run


Figure 2.2: Flow Chart of Project Development





The hardware development, it is consists of a microcontroller 16F877, keypad

and LCD (Liquid Crystal Displays). 4 x 4 keypad is used as an input.
The purpose of display at LCD is able to display the number that has typed in and also
the result of the calculation.

A microcontroller is a computer control system on a single chip. It has many

electronic circuits built into it, which can decode written instructions and convert them
to electrical signals. The microcontroller will then step through these instructions and
execute them one by one. The list of these instructions given to the microcontroller is
called a program.



In order to choose a microcontroller for a particular control system, there are

many things to consider with it. The most obvious choice then for the microcontroller is
how many digital inputs, analogue inputs and outputs the system requires. This would
then specify the minimum number of inputs and outputs 0/0) that the microcontroller
must have. The block diagram of the microcontroller system shown in Figure 3.1 below.


Figure 3.1 :Basic Microcontroller System

According to Figure 3.1, the input components would consist of digital devices
such as push button, keypads, radio receivers etc. The control unit is of course the
microcontroller. The microcontroller will monitor the inputs and as a result the program
would turn output on and off. The microcontroller stores the program in its memory and
executes the instructions under the control of the clock circuit. Meanwhile for the output
devices would be made up from LEDs, 7-segment displays, LCD etc.

The next consideration would be what size of program memory storage is

required and the clock frequency determines the speed at which the instructions are
executed. This is important if any lengthy calculations are being undertaken. The higher
the clock frequency the quicker the micro will finish one task and start another. Other
considerations are the number of interrupts and timer circuits required and how much
data EEPROM if any is needed.

In this project, the PIC microcontroller 16F877 is used to control and executes
the data from the inputs. Keypad is used as an input and the output is a LCD.


PIC Microcontroller 16F877

In this project the PIC 16F877 is used. It range is similar to the 16F877 but
bigger and better. It has more VO, program memory, data memory and EEPROM data
memory than the 16F84. It is also have more analogue inputs than the 16C711 and used
10-bit A/D conversion instead of 8 bits, so the A D resolution is 1024 instead of 256.



16F877 8k

Data Memory











Table 3.1 :PIC 16F877 Specification


PIC 16F877 Memory Map

Inside the microcontroller the program is stored in an area called EPROM

(Electrically Programmable Read Only Memory). This memory is non-volatile and is
remembered when the power is switched off. The memory is electrically programmed by

a piece of hardware called a programmer. The instructions program into the

microcontroller work by moving and manipulating data in memory locations known as
user files and registers. This memory is called RAM (Random Access Memory).

The PIC 16F877 devices have more functions than have seen previously. These
functions of course need registers in order to make the various selections. The memory
map of the PIC 16F877 showing these registers is shown in appendix. There are three
memory blocks in each of the PIC16F877 MCUs. The Program Memory and Data
Memory have separate buses so that concurrent access can occur.

3.23 Microcontroller Clock

In order to step through the instructions the microcontroller needs a clock

fi-equency to orchestrate the movement of the data around its electronic circuits. Usually,
it is provided by two capacitors and a crystal. The common crystal frequencies are 1
MHz, 4 MHz, 10 MHz and 20 MHz.

Inside the microcontroller there is an area where the processing such as logical
operations and mathematical are performed, known as the central processing unit or
CPU. There is also a region where event timing is performed and another for interfacing
to the outside world through ports.

33.3 Programming The PIC microcontroller 16F877

In order to have the microcontroller perform as a simple calculator, it is needed

to communicate and tell what those instructions are to be. The program language is used
to communicate with a microcontroller. The program language for the PIC
microcontroller uses 35 words (instructions) in its vocabulary. A few more instruction is
used in the bigger microcontrollers. In order to communicate with the microcontroller,
these 35 instructions are learnt.



Keypads are come in many different shapes and sizes and most of them align
keys into a matrix of columns and rows. Keypads are usually arranged in a matrix
format to reduce the number of VO connections. There are a 12 key keypad ia arranged
in a 3 x 4 format requiring seven connections and a 16 key keypad is arranged in a 4 x 4
format requiring eight connections.

In this project, the keypad (4 x 4) is used as an input as mention earlier. Keypads

are an excellent way of entering data into the microcontroller. The keys are usually
numbered but it could be labeled as functions keys such as in a remote control handset to
adjust the colour, sound etc. Besides, keypads also can find applications in burglar
alarms, door entry system, calculators etc.


4 x 4 Keypad

Figure 3.5: 4 x 4 Keypad

This is arranged in four columns and four rows as shown in Figure 3.5. There are
eight connections to the keypad; C1, C2, C3, C4, R1, R2, R3 and R4. This connection to
the microcontroller is shown in Table 3.2.

Columnl, C1

Column2, C2

Column3, C3

Column4, C4

Rowl, R1

Row2, R2

Row3, R3

Row4, R4

Table 3.2: Connection to the Microcontroller


Keypad Interface

Figure 3.6: 4 x 4 Keypad Matrix Yields 16 Keys.

Figure 3.6 shown the circuit for the terminal keypad. Four rows and four
columns require eight VO lines and support 16 individual keys. The microcontroller is
configured so that the columns are inputs and the rows are outputs. With no key pressed,
the input's voltage levels are floating. To avoid erroneous readings, pull-up resisters are
connected to each of the input lines.

Meanwhile, to read the keypad, the microcontroller scans the keys. All rows
(output) are set high and then one row at a time is set low. If any key on that row is
depressed, that keys columns will become low as well because the microcontroller
knows which row is low, it is simply has to read which column is low to identify the key
in the matrix that has been pressed.

By adding diodes to each of the rows, the microcontroller can only sink current
and not source it. Its mean device is expected to read more than one key at a time. For
example, when keys 1 and 5 are pressed. Row 1 is set low, bringing column 1 low. The
diode on row 2 is reverse-biased, thereby preventing a problem with row 2 driver.
Normally, these diodes are necessary to add a shift key.



More microcontroller devices are using 'smart LCD' displays to output visual
information. It is because of perfect choice for small, lightweight, low-power and low
cost text output. LCD is low powered because it emits no light of their own but only
reflects ambient light. The segment in a LCD can be polarized, permitting light to be
blocked or passed. This polarizing current is very low compared to an LED which is
emits light.

Figure 3.7: 16x2 Line Hitachi HD44780 Display (a) Front (b) Rear

In this project, the 16 character by 2 line display which incorporates a Hitachi's

LCD HD44780 module. This LCD is inexpensive, easy to use, and it is even possible to
produce a readout using the 8 x 80 pixels of the display. It is also have a standard ASCII

set of characters plus Japanese, Greek and mathematical symbols and has an onboard
character generator ROM which can display 192 character patterns.


LCD Interface

The LCD interface consists of 11 outputs from the microcontroller which is three
control lines and eight data lines connecting to the display. Table 3.3 shows the pin
assignments for the LCD.

1 = Read from LCD

Table 3.3: Pin Descriptions for a LCD

lUW is the &write

control line, RS is the register select and E is the chip

enable. The purpose of R/W line is to tell the display to expect data to be written to it or
to have data read from it. The data that is written to it is the address of the character, the
code for the character or the type of command required it to perform such as turn the
cursor off.

The RfS line selects either a command to perform (FUS

= 0)

such as clear display,

turn cursor off or on or selects a data transfer (RJS =I). Meanwhile, The E line enables

(E=l) and disables (E=O) the display. The R/W line also is used to write commands to
the micro and read the busy line which indicates when the relatively slow display has
processed the data.

Figure 3.8: Signal timing to complete a write operation to the LCD panel
Source: The Microcontroller Beginner's Handbook, 2ndEdition.
Lawrence A. Duarte

A common and useful feature in device specifications is the timing chart. The
timing chart for the LCD interface is shown in Figure 3.8. According to the Figure 3.8, a
line drawn high or low represents that line at +5V or OV respectively. When the line is

both high and low (as with RS) the line could be high or low at that time. Basically, the
high or low state represents different options, a high RS means sending character data
while a low RS means sending an instruction. The sequences of events transpire from
left to right as follows. Firstly, enable must be low, set read/write to low, then set RS
high (character) or low (instruction). Enable goes high but only after a minimum of
140ns. The data lines @BO-DB7) are now set for the instruction or the character. When
Enable is set low and the data is read into the LCD on the failing edge of enable. The
enable pulse must be at least 450ns and this is the end of operation. For the next
operation, it must wait 1p fiom the previous enable.

The RIW line is tied low representing all write functions. The power for the LCD
is +5V and ground. Meanwhile, the display contrast is adjusted with a 10k variable
resistor between the ground and pin 3 of the LCD interface.


Visible DDRAM addresses

When the LCD is initialized, it is ready to continue receiving data or instructions.

If it receives a character, it will write it on the display and move the cursor one space to
the right. The Cursor marks the next location where a character will be written. When
we want to write a string of characters, first we need to set up the starting address, and
then send one character at a time. Characters that can be shown on the display are stored
in data display @D) RAM. The size of DDRAM is 80 bytes.

The LCD display also possesses 64 bytes of Character-Generator (CG) RAM.

This memory is used for characters defined by the user. Data in CG RAM is represented