MicroController Pros Home Page
My Account  Cart Contents  Checkout  
  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->
Fujitsu
Locktronics
Maxim
Microchip PIC->
MIPS
National Semiconductor
Parallax->
Renesas->
Silicon Labs
ST Microelectronics->
Texas Instruments->
Tibbo->
Zilog
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 CD-ROMs
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: 44 Matrix Keypad

Back to PCAM+ page.


Back to Chapter 9

44 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 44 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
TFT Developer Kit PRO 2 - Tiva C Series: EasyMx, GUI S/W, Pascal
TFT Developer Kit PRO 2 - Tiva C Series: EasyMx, GUI S/W, Pascal
US$467.30
Specials more
SOIC-to-DIP 16-Pin Adapter PCB
SOIC-to-DIP 16-Pin Adapter PCB
US$1.50
US$0.90
Bestsellers
01. PIC16F877A-I/P Microcontroller, 40 DIP, 20 MHz
02. PIC16F628A-I/P Microcontroller, 18 DIP, 20 MHz
03. LCD 4-mux, 7 digits x 14 segments, progress bar, arrows
04. Atmel ATtiny2313-20PU AVR Microcontroller, 20 DIP, 20 MHz
05. LCD 4-mux, 7.1 digits x 7 segments, progress bar, arrows
06. USB Universal In-Circuit Programmer PIC AVR ARM MSP 8051 EEPROM
07. ARM JTAG Debugger & Programmer, parallel port
08. Power Supply 3-12V DC, U.S. plug, 6 connection tips
09. Power Supply 3-12V DC, US/UK/Euro/AU Plugs, 100-240VAC
10. Atmel AVR JTAG In-Circuit Debugger & Programmer, USB
Reviews more
XBee Module Breakout Board, 3.3V Voltage Regulator, Signal LEDs
There is an upgraded board from the supplier SparkFun. It a ..
3 of 5 Stars!
  Thursday 31 July, 2014   List of all our Products

Copyright © 2003-2014 MicroController Pros LLC
Powered by osCommerce