LCD Keypad : How to use an lcd and keypad on ONE 8 bit microcontroller port.

This LCD keypad project saves you lots of valuable port pins by giving you twelve input keys and one LCD display on a single 8-bit port. Normally you need a single port pin for each input key but this method multiplexes the keys onto 7 pins and shares the port with the LCD. The LCD is a standard HD44780 type and the keypad is a 12 key keypad arranged as four rows by three columns.

It works if you keep the LCD strobe line (E) low while controlling the keypad. This stops the LCD from reacting to the other port pins (the strobe line when strobed high starts a read or write action).

Since you only need seven port lines to access the keypad the eighth is used for the LCD strobe line (RB2).

LCD WRn Line Not Used.

In fact you don't really need the WRn line since usually people only write to the LCD and use delays to wait a suitable length of time before updating the display again. The HD44780 datasheet specifies these delays. If you wanted ultimate performance then you could read the LCD busy data bit before writing to the LCD again.

Reflecting this trend the new MikroC compiler with its built in LCD library does not allow you to control the WRn line, so at the LCD tie it low i.e. permanent write (or set the control line low for existing code).

It is shown connected to RB1, in the diagram below, and you could write an LCD routine to read the LCD if you want to. Note RB1 is used in reading the keypad so you do not gain an extra free pin either way. 

LCD Keypad : Circuit of LCD and keypad on one 8 bit port

(Click the diagram to open a pdf version)

LCD and 12 key keypad on one port

Note: Attach LED to RC1 via 330R resistor to GND, for processor alive indicator.

LCD Keypad project details.

LCD Keypad Project
Compiler MikroC PRO C Compiler
Target 16F877A (retargetable to other PICs that have an 8 bit port!).
Software level Easy.
Software notes Switching between keypad and LCD.
Hardware level Easy.
Hardware notes Use internal pullups on portB (you must add pullups to row inputs when using a different port).
Project version 1.00 Initial Release
Project version 1.01 Mod to operate fully at 20Mhz (0,# lost). Allow settling time after set col outputs.
Project version 1.02 Update for compiler version V6.0.1
Project version 1.03 Update for compiler version V6.4.0
Project files

Download Here.

You can program the PIC in circuit through the ICSP connector.

LCD Keypad Port Directions

When using the LCD you need to set all LCD pins as outputs and for the keypad you need to set rows as inputs and columns as outputs.

To make it work all row inputs must be pulled up to 5V (either by internal port pullups or by external 10k resistors).

Note : During keypad access the LCD strobe signal (E) must be kept at zero so that the LCD is not activated.

Reading the keypad

The software generates a walking zero through the column bits.

Whenever you press a key the keypad shorts a column to the row so the zero from the column output appears at the row input, pulling the voltage to ground. When there are no keys pressed the pullup resistors pull the row inputs to 5V.

The routine exits with the first key detected. This key is derived from the returned row value and the current mask value and you can decode it using the definitions found in lcd_keypad.h e.g. KEY_2 has the value 226 which is the value returned by read_KEYPAD when key 2 is hit.

Column shorts

Pressing two keys in different columns and rows shorts together two columns that are at different voltages (one at 0V and one at 5V). The column drivers are isolated by the three 10k resistors to prevent damage. They also allow the LCD to operate correctly even when keys are pressed.

Keypad wiring

The following diagram shows the keypad wiring, which is a little awkward - probably to allow a simple pcb routing path.

12 key keypad

The software consists of three main files (the rest are MikroC compiler files):

16F887A_lcd_keypad.c
lcd_keypad.c
lcd_keypad.h

The most important routines are KEYPAD_access() and LCD_access() - which set the port up to let it control the keypad and lcd respectively. The interesting point about the code is that the standard LCD routines (provided by compiler manufacturer) are used after lcd_access() is executed.

Pressing a key displays the result on the lower right of the 2 row LCD.

Note that key debouncing has not been done as it does not affect the demonstration but you must use it for a 'real' application.


Jump from LCD keypad Page to
Best-microcontroller-projects home page


Privacy Policy | Contact | About Me

Site Map | Terms of Use


Share this page:
Enjoy this page? Please pay it forward. Here's how...

Would you prefer to share this page with others by linking to it?

  1. Click on the HTML link code below.
  2. Copy and paste it, adding a note of your own, into your blog, a Web page, forums, a blog comment, your Facebook account, or anywhere that someone would find this page valuable.

Comments

Have your say about what you just read! Leave me a comment in the box below.

Don’t see the comments box? Log in to your Facebook account, give Facebook consent, then return to this page and refresh it.