MicroController Pros Home Page My Account  Cart Contents  Checkout  

****Note: We no longer process and ship international orders. Only orders from USA and Canada are now accepted.****
You can place international orders for our products on E-Bay. Just copy the item's title description from our website and paste it into the search box on E-Bay and place your order there.

  Store » PCAM+ 8051 Tutorial 10 My Account  |  Cart Contents  |  Checkout   
Quick Find
 
Enter keywords to find the product you are looking for in the Quick Find field above

or use
Advanced Search
Categories
Accessory Boards->
8051->
ADI Blackfin
Arduino->
ARM->
Atmel AVR->
Cypress PSoC
Freescale->
FTDI->
Locktronics
Microchip PIC->
MIPS
Parallax->
Renesas
Silicon Labs
ST Microelectronics->
Texas Instruments->
Tibbo->
Books->
Displays->
E-Blocks->
EEPROM/EPROM/FLASH
Embedded Ethernet->
Embedded Software->
I/O Modules->
Parts & Components->
Pick & Place Tools
Programmable Logic (PLD)
Prototype PCBs->
Robotics
ROM/Flash Emulators
Test & Measurement->
Tutorial Software
Universal Programmers->
Wireless->
Information
Intro to Embedded Tools
Embedded News Digest
Useful Resources
Shipping & Returns
Warranty & Liability
Privacy Notice
Conditions of Use
Contact Us
PCAM+ 8051 Tutorial 10: 4×4 Matrix Keypad

Back to PCAM+ page.


Back to Chapter 9

4×4 Keypad and LCD Interface with 89S52 Microcontroller

The 4-by-4 keypad is a commonly used interface circuit in embedded applications. This keypad has eight lines with 16 switches arranged in a matrix format.

In this application, we will have the LCD of the PCAM+ display a number, 1 through 16, depending on which key is pressed on the keypad.

We will need to use 19 of the 20 connecting wires in the kit:

  • Connect P3.7, P3.6, P3.5 of the 89S52 to the LCD section's RS, RW, EN pins respectively.
  • Connect pins 0-7 of the LCD to the respective P0 port pins of the 89S52.
  • Connect pins 0-7 of the Keypad to the respective P2 port pins of the 89S52.

Write the following code in 8051IDE:

;For LCD
RS      EQU     P3.7    ;change as per your connections
RW      EQU     P3.6    ;change as per your connections
EN      EQU     P3.5    ;change as per your connections

PDATA   EQU     P0      ;change as per your connections

LCALL INIT_LCD
LCALL CLEAR_LCD

START:
       MOV P2,#0FFh
       CLR P2.7
               JNB P2.0,Key_1
               JNB P2.1,Key_2
               JNB P2.2,Key_3
               JNB P2.3,Key_4
         MOV P2,#0FFh
       CLR P2.6
               JNB P2.0,Key_5
               JNB P2.1,Key_6
               JNB P2.2,Key_7
               JNB P2.3,Key_8
       MOV P2,#0FFh
       CLR P2.5
               JNB P2.0,Key_9
               JNB P2.1,Key_10
               JNB P2.2,Key_11
               JNB P2.3,Key_12
         MOV P2,#0FFh
       CLR P2.4
               JNB P2.0,Key_13
               JNB P2.1,Key_14
               JNB P2.2,Key_15
               JNB P2.3,Key_16

       SJMP START

Key_1:
     LJMP K_1

Key_2:
     LJMP K_2

Key_3:
     LJMP K_3

Key_4:
     LJMP K_4

Key_5:
     LJMP K_5

Key_6:
     LJMP K_6

Key_7:
     LJMP K_7

Key_8:
     LJMP K_8

Key_9:
     LJMP K_9

Key_10:
     LJMP K_10

Key_11:
     LJMP K_11

Key_12:
     LJMP K_12

Key_13:
     LJMP K_13

Key_14:
     LJMP K_14

Key_15:
     LJMP K_15

Key_16:
     LJMP K_16


K_1:
       LCALL CLEAR_LCD
       MOV A,#'1'
       LCALL WRITE_TEXT
       JNB P2.0,$
       LJMP START

K_2:
       LCALL CLEAR_LCD
       MOV A,#'2'
       LCALL WRITE_TEXT
       JNB P2.1,$
       LJMP START

K_3:
       LCALL CLEAR_LCD
       MOV A,#'3'
       LCALL WRITE_TEXT
       JNB P2.2,$
       LJMP START

K_4:
       LCALL CLEAR_LCD
       MOV A,#'4'
       LCALL WRITE_TEXT
       JNB P2.3,$
       LJMP START

K_5:
       LCALL CLEAR_LCD
       MOV A,#'5'
       LCALL WRITE_TEXT
       JNB P2.0,$
       LJMP START

K_6:
       LCALL CLEAR_LCD
       MOV A,#'6'
       LCALL WRITE_TEXT
       JNB P2.1,$
       LJMP START

K_7:
       LCALL CLEAR_LCD
       MOV A,#'7'
       LCALL WRITE_TEXT
       JNB P2.2,$
       LJMP START

K_8:
       LCALL CLEAR_LCD
       MOV A,#'8'
       LCALL WRITE_TEXT
       JNB P2.3,$
       LJMP START

K_9:
       LCALL CLEAR_LCD
       MOV A,#'9'
       LCALL WRITE_TEXT
       JNB P2.0,$
       LJMP START

K_10:
       LCALL CLEAR_LCD
       MOV A,#'1'
       LCALL WRITE_TEXT
       MOV A,#'0'
       LCALL WRITE_TEXT
       JNB P2.1,$
       LJMP START

K_11:
       LCALL CLEAR_LCD
       MOV A,#'1'
       LCALL WRITE_TEXT
       MOV A,#'1'
       LCALL WRITE_TEXT
       JNB P2.2,$
       LJMP START

K_12:
       LCALL CLEAR_LCD
       MOV A,#'1'
       LCALL WRITE_TEXT
       MOV A,#'2'
       LCALL WRITE_TEXT
       JNB P2.3,$
       LJMP START

K_13:
       LCALL CLEAR_LCD
       MOV A,#'1'
       LCALL WRITE_TEXT
       MOV A,#'3'
       LCALL WRITE_TEXT
       JNB P2.0,$
       LJMP START

K_14:
       LCALL CLEAR_LCD
       MOV A,#'1'
       LCALL WRITE_TEXT
       MOV A,#'4'
       LCALL WRITE_TEXT
       JNB P2.1,$
       LJMP START

K_15:
       LCALL CLEAR_LCD
       MOV A,#'1'
       LCALL WRITE_TEXT
       MOV A,#'5'
       LCALL WRITE_TEXT
       JNB P2.2,$
       LJMP START

K_16:
       LCALL CLEAR_LCD
       MOV A,#'1'
       LCALL WRITE_TEXT
       MOV A,#'6'
       LCALL WRITE_TEXT
       JNB P2.3,$
       LJMP START

INIT_LCD:
       CLR RS
       MOV PDATA,#00110100b
       SETB EN
       CLR EN
       LCALL WAIT_LCD

       CLR RS
       MOV PDATA,#00111100b
       SETB EN
       CLR EN
       LCALL WAIT_LCD

       CLR RS
       MOV PDATA,#0Ch
       SETB EN
       CLR EN
       LCALL WAIT_LCD

       CLR RS
       MOV PDATA,#06h
       SETB EN
       CLR EN
       LCALL WAIT_LCD
RET

;NOTE: Copy character to be displayed in A
WRITE_TEXT:
       SETB RS
       MOV PDATA,A
       SETB EN
       CLR EN
       LCALL WAIT_LCD
RET

CLEAR_LCD:
       CLR RS
       MOV PDATA,#01h
       SETB EN
       CLR EN
       LCALL WAIT_LCD
RET

WAIT_LCD:
       CLR EN
       CLR RS
       SETB RW
       MOV PDATA,#0FFh
       SETB EN
       MOV A,PDATA
       JB ACC.7,WAIT_LCD
       CLR EN
       CLR RW
RET

Assemble the code and program it to the 89S52 on PCAM+ using the HandyProgUSB programmer. Power up the PCAM+ and try pressing buttons on the 4×4 keypad. You should see a number displayed for each different button pressed.

End of Tutorial

Now you can experiment with what you have learned. For example, can you adapt this keypad code to display the numbers on the 7-segment LEDs? to turn a motor back and forth? to display and then scroll text on the LCD? to display the text on your PC via RS232? How about doing these things from the IR remote control?


Back to PCAM+ page.

Shopping Cart more
0 items
What's New? more
Active Probe for LAP-F Logic/Protocol Analyzer, Negative Logic
Active Probe for LAP-F Logic/Protocol Analyzer, Negative Logic
US$65.00
Bestsellers
01. 2x5 (10-pin) 0.05" pitch IDC Connector Flat Ribbon Cable, 20cm
02. 2x5 (10-pin) 0.05" pitch IDC Connector Flat Ribbon Cable, 12cm
03. 5-pin Press-Fit Header Strip, Breakaway Pins, 2.54mm Pitch
04. Parallel to USB Adapter: Connect USB printer to LPT port
05. 2x10 (20-pin) 0.1" pitch IDC Connector Flat Ribbon Cable, 15cm
06. 8-pin Press-Fit Header Strip, Breakaway Pins, 2.54mm Pitch
07. USB Universal In-Circuit Programmer PIC AVR ARM MSP 8051 EEPROM
08. 2x5 (10-pin) 0.1" pitch IDC Connector Flat Ribbon Cable, 15cm
09. ARM JTAG Debugger & Programmer, parallel port
10. Power Supply 3-12V DC, U.S. plug, 6 connection tips
Reviews more
Awesome, just awesome! I can't even begin to tell you how mu ..
5 of 5 Stars!
  Tuesday 19 March, 2024   List of all our Products

Copyright © 2003-2017 MicroController Pros LLC
Powered by osCommerce