This document provides a tutorial on interfacing a 2x16 LCD and 4x4 keypad with an 8051 microcontroller in assembly language. It describes connecting the LCD data bus and control lines to specific ports and scanning the keypad by polling each column individually. The program displays the key pressed on the LCD. The code includes functions for sending commands, writing data, and delay. It initializes the LCD, continuously scans the keypad columns, and displays the pressed key.
This document provides a tutorial on interfacing a 2x16 LCD and 4x4 keypad with an 8051 microcontroller in assembly language. It describes connecting the LCD data bus and control lines to specific ports and scanning the keypad by polling each column individually. The program displays the key pressed on the LCD. The code includes functions for sending commands, writing data, and delay. It initializes the LCD, continuously scans the keypad columns, and displays the pressed key.
This document provides a tutorial on interfacing a 2x16 LCD and 4x4 keypad with an 8051 microcontroller in assembly language. It describes connecting the LCD data bus and control lines to specific ports and scanning the keypad by polling each column individually. The program displays the key pressed on the LCD. The code includes functions for sending commands, writing data, and delay. It initializes the LCD, continuously scans the keypad columns, and displays the pressed key.
This document provides a tutorial on interfacing a 2x16 LCD and 4x4 keypad with an 8051 microcontroller in assembly language. It describes connecting the LCD data bus and control lines to specific ports and scanning the keypad by polling each column individually. The program displays the key pressed on the LCD. The code includes functions for sending commands, writing data, and delay. It initializes the LCD, continuously scans the keypad columns, and displays the pressed key.
Download as DOCX, PDF, TXT or read online from Scribd
Download as docx, pdf, or txt
You are on page 1of 19
2x16 LCD And 4x4 Keypad Interfacing
With 8051 in Assembly Language
Microcontrollers are just silicon wafers until we tell them what to do, program them according to our requirement. Similarly any user interface is incomplete without an Input. One, two pushbuttons can be easily interfaced however if more user inputs are required it can take up a lot of I/O lines. So here is a small tutorial to interface a 4x4 Matrix Keypad and displaying the key pressed on a LCD. The microcontroller used is AT89C51 and the coding has been done in assembly language. The 4x4 Keypad has 16 keys and requires a single PORT or 8 I/O lines. Port 3 has been designed to handle keypad, LCD Data Bus D7-D0 is connected to PORT 1, while (Enable) EN is connected to P2.0 (Register Select Command or Data Register) RS is connected to P2.1 (Read/Write) RW is connected to P2.2 The LCD is based on Hitachi HD44780 Controller and datasheet is present online.
Working: To check for the keystroke, a polling method has been used.
PORT 3.0
Key 1
Key 2
Key 3
Key 4
PORT3.1
Key 5
Key 6
Key 7
Key 8
PORT3.2
Key 9
Key 10
Key 11
Key 12
PORT3.3
Key 13
Key 14
Key 15
Key 16
PORT3.4
PORT3.5
PORT3.6
PORT3.7
The connections are similar as shown over here. Now consider this, if I select the first column only, it has 4 keys, 1, 5,9,13. If a change of value (i.e. Binary 1 or 0) is made any one of these keys, it can be decoded and suitable message is displayed on the LCD. This is exactly what happens. Initially all the I/O lines are pulled high, then during Key Scan, every column linked is held low for a little time. If during that time a Key is pressed in that column a row I/O lines is also held low, thus the keystroke can be captured. The obvious question would be what if we press the key on a particular column and at that particular moment that column has not been pulled low, thus making no signal changes? The answer is simple, the microcontroller runs quite fast, even a convention 89c51 in which the internal frequency= external frequency clock/12 can achieve 2 MIPS at 24MHz. That is 2 Million instructions Per Second. This method is not foolproof, it has a drawback, while the Key Scan, It cannot perform other cumbersome operations which may take time and a Key Stroke could be missed. The program will work very well for small operations like activating a small relay or LED when a Key is pressed, but for people who want their systems to be near to perfect they may utilize other method.
Circuit Diagram
Each I/O line on PORT 1 should be pulled high with 4.7K Resistors
Program: EN equ P2.0 RS equ P2.1 RW equ P2.2 mov A,#38H ; Setting Up the LCD lcall command mov A,#0EH ; Display On lcall command mov A,#06H ; Entry Mode lcall command mov a,#82H lcall command lcall disp ; Function Disp Called mov a,#02H ; Setting DDRAM Address to Home position lcall command lcall delay1
; Function to Send Commands to LCD command: clr RW clr RS setB EN MOV P1,A lcall delay clr EN RET
; Function to Clear the DDRAM Content clear: mov A,#01H lcall command lcall delay mov A,#02H ; Set The DDRAM Address to Home Position lcall command lcall delay RET ; Function to Display Data on LCD Screen datw: SETB RS clr RW SETB EN MOV P1,A lcall delay clr EN RET
;Function to Display The Key Pressed datw1: lcall delay1 lcall disp lcall delay1 MOV A,R7 lcall datw RET
; Generating Small Delay delay: mov r0,#255 loop: DJNZ r0,loop; RET
; Generating a Bigger Delay delay1: mov r1,#255 loop1: mov r3,#120 loop2: djnz r3,loop2 djnz r1,loop1 RET
; Checking for Key Press on The First Column of 4x4 Matrix
mov a,#0c0H ; Setting DDRAM Address on Second Line To Display Key Pressed lcall command
; Display "KEY" and Pressed Information mov a,#' ' lcall datw mov a,#'K' lcall datw mov a,#'E' lcall datw mov a,#'Y' lcall datw mov a,#' ' lcall datw RET END
The code was written when I was learning assembly language myself and therefore the code is not optimized, but it is easy to understand if someone is willing to check the instruction set. People who want to optimize the code may wish to look into the DPTR Register and Addressing Modes Theory.
A View of the Simulation
The 7-12V DC Input had to be removed in this diagram, because that cannot be accepted as the Power Source in the Simulation, so do not get confused.
Comments & Suggestions are Welcome Thank You
Comments Submitted by vinay.hr on Tue, 2011-03-01 10:20. #1 Member since: 1 March 2011 Last activity: 1 year 2 days i am not able to watch the ciruitry clearly, because there is no options to magnify the image , please help me in getting the full magnified image Login or register to post comments Submitted by navam on Wed, 2011-02-23 02:00. #2 Member since: 23 February 2011 Last activity: 1 year 1 week Hii I faced the problem that if i didn't press any key, then the program works just fine for about 20 seconds and then the the text on lcd vanishes and the keypad scan stops to work and everything halts. Is it a software problem and will it occur in the hardware as well or is there some error in the code? Did u face this error during the simulation? A quick response will be appreciated. Navam
Login or register to post comments Submitted by hz2829 on Mon, 2010-06-07 09:15. #3 Member since: 7 June 2010 Last activity: 1 year 38 weeks can u send the proteus file's need to simulate this program please
Login or register to post comments Submitted by eng_jimy on Fri, 2009-12-18 19:16. #4 Member since: 18 December 2009 Last activity: 2 years 7 weeks I can't see the component magnitudes ... please help Login or register to post comments Submitted by shobhitkukreti on Mon, 2009-08-03 13:24. #5 Member since: 28 April 2009 Last activity: 1 year 47 weeks Sorry guys. Another tutorial is not possible right now. I had few tutorials lined up, but they require some editing and i can't devote time for them. I hope others will help you out Login or register to post comments Submitted by nataraja on Fri, 2009-06-12 10:53. #6 Member since: 11 June 2009 Last activity: 1 year 40 weeks and a RTOS tutorial would be nice Login or register to post comments Submitted by nataraja on Fri, 2009-06-12 10:51. #7 Member since: 11 June 2009 Last activity: 1 year 40 weeks can u send the proteus file's need to simulate this program.........