{"id":182,"date":"2018-07-26T11:56:31","date_gmt":"2018-07-26T11:56:31","guid":{"rendered":"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=182"},"modified":"2018-08-13T06:01:35","modified_gmt":"2018-08-13T06:01:35","slug":"i-o-devices-interfacing","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/chapter\/i-o-devices-interfacing\/","title":{"rendered":"I\/O devices Interfacing"},"content":{"raw":"<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In this lecture, the interfacing of 8051 with an output device and an input device are described. The output device considered is LCD display, and the input device is a matrix-type keyboard. The operation modes of LCD are first discussed. Interfacing of the LCD to an 8051 microcontroller using Assembly and C is also discussed. This is followed by the keyboard interface details.<\/p>\r\n&nbsp;\r\n\r\n<strong>1. LCD (Liquid Crystal Display)interface<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">LCDs can display numbers, characters, and graphics. To produce a proper display, the information has to be periodically refreshed. This can be done by the CPU or internally by the LCD device itself. Incorporating a refreshing controller into the LCD, relieves the CPU of this task and hence many LCDs have built-in controllers. These controllers also facilitate flexible programming for characters and graphics. Figure 1.1 shows the pin description of an LCD from Optrex.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-185 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-103.png\" alt=\"\" width=\"768\" height=\"506\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Vss and VDD provide +5v and ground, V0 is used for controlling LCD contrast. If RS=0, the instruction command register is selected, allowing the user to send a command such as clear display, cursor at home, etc. If RS=1 the data register is selected, allowing the user to send data to be displayed on the LCD. R\/W input allows the user to Read\/ Write the information to the LCD. The enable pin is used by the LCD to latch information presented to its data pins. The 8-bit data pins are used to send information to LCD. Section 1.1 discusses about command codes for writing the instructions on the LCD register.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"font-size: 1em;text-align: initial\">Section 1.2 gives an example program for displaying a character on the LCD. Section 1.3 shows the communication of LCD with a busy flag and section 1.4 gives a detailed explanation of LCD data sheet.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>1.1 LCD Command Codes<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe LCD\u2019s internal controller can accept several commands and modify the display accordingly.\r\n\r\n&nbsp;\r\n\r\nThese commands would be things like:\r\n\r\n&nbsp;\r\n\r\n\u2713 Clear screen\r\n\r\n&nbsp;\r\n\r\n\u2713 Return home\r\n\r\n&nbsp;\r\n\r\n\u2713\u00a0 Decrement\/Increment cursor\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">After writing to the LCD, it takes some time for it to complete its internal operations. During this time, it will not accept any new commands or data. Figure 1.2 shows the command codes of LCD and Figure 1.3 shows the LCD interfacing.<\/p>\r\n&nbsp;\r\n\r\n\u2713\u00a0 We need to insert a time delay between any two commands or data sent to LCD.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u2713\u00a0 A long delay has to be given between issuing data or commands to the LCD.<\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"size-full wp-image-186 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-104.png\" alt=\"\" width=\"520\" height=\"511\" \/>\r\n\r\n<img class=\"size-full wp-image-187 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-105.png\" alt=\"\" width=\"438\" height=\"470\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>1.2 Program to Display characters on LCD<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">To send any of the commands to the LCD, make pin RS=0. For data, make RS=1. Then send a high-to-low pulse to the E pin to enable the internal latch of the LCD. This is shown in the code below.<\/p>\r\n&nbsp;\r\n\r\n;calls a time delay before sending next data\/command ;P1.0-P1.7 are connected to LCD data pins D0-D7\r\n\r\n<span style=\"font-size: 1em;text-align: initial\">;P2.0 is connected to RS pin of LCD<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">;P2.1 is connected to R\/W pin of LCD<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">;P2.2 is connected to E pin of LCD<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0ORG 0H\r\n\r\n&nbsp;\r\n\r\nMOV A,#38H\u00a0 ;INIT. LCD 2 LINES, 5X7 MATRIX\r\n\r\n&nbsp;\r\n\r\nACALL COMNWRT\u00a0;call command subroutine\r\n\r\n&nbsp;\r\n\r\nACALL DELAY\u00a0;give LCD some time\r\n\r\n&nbsp;\r\n\r\nMOV A,#0EH\u00a0;display on, cursor on\r\n\r\n&nbsp;\r\n\r\nACALL COMNWRT\u00a0;call command subroutine\r\n\r\n&nbsp;\r\n\r\nACALL DELAY\u00a0;give LCD some time\r\n\r\n&nbsp;\r\n\r\nMOV A,#01\u00a0;clear LCD\r\n\r\n&nbsp;\r\n\r\nACALL COMNWRT\u00a0;call command subroutine\r\n\r\n&nbsp;\r\n\r\nACALL DELAY\u00a0\u00a0<span style=\"text-align: initial;font-size: 1em\">;give LCD some time<\/span>\r\n\r\n&nbsp;\r\n\r\n<\/div>\r\n<div>\r\n<table style=\"width: 60%\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td><\/td>\r\n<td>MOV A,#06H<\/td>\r\n<td>;shift cursor right<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>ACALL COMNWRT<\/td>\r\n<td>;call command subroutine<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>ACALL DELAY<\/td>\r\n<td>;give LCD some time<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>MOV A,#84H<\/td>\r\n<td>;cursor at line 1, pos. 4<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>ACALL COMNWRT<\/td>\r\n<td>;call command subroutine<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>ACALL DELAY<\/td>\r\n<td>;give LCD some time<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>MOV A,#\u2019N\u2019<\/td>\r\n<td>;display letter N<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>ACALL DATAWRT<\/td>\r\n<td>;call display subroutine<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>ACALL DELAY<\/td>\r\n<td>;give LCD some time<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>MOV A,#\u2019O\u2019<\/td>\r\n<td>;display letter O<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>ACALL DATAWRT<\/td>\r\n<td>;call display subroutine<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>AGAIN:<\/td>\r\n<td>SJMP AGAIN<\/td>\r\n<td>;stay here<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>COMNWRT:<\/td>\r\n<td><\/td>\r\n<td>;send command to LCD<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>MOV P1,A<\/td>\r\n<td>;copy reg A to port 1<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>CLR P2.0<\/td>\r\n<td>;RS=0 for command<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>CLR P2.1<\/td>\r\n<td>;R\/W=0 for write<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>SETB P2.2<\/td>\r\n<td>;E=1 for high pulse<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>CLR P2.2<\/td>\r\n<td>;E=0 for H-to-L pulse<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>RET<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>DATAWRT:<\/td>\r\n<td><\/td>\r\n<td>;write data to LCD<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>MOV P1,A<\/td>\r\n<td>;copy reg A to port 1<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>SETB P2.0<\/td>\r\n<td>;RS=1 for DATA<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>CLR P2.1<\/td>\r\n<td>;R\/W=0 for write<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>SETB P2.2<\/td>\r\n<td>;E=1 for high pulse<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>CLR P2.2<\/td>\r\n<td>;E=0 for H-to-L pulse<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>RET<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>DELAY:<\/td>\r\n<td>MOV R3,#50<\/td>\r\n<td>;50 or higher for fast CPUs<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>HERE2:<\/td>\r\n<td>MOV R4,#255<\/td>\r\n<td>;R4 = 255<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>HERE:<\/td>\r\n<td>DJNZ R4,HERE<\/td>\r\n<td>;stay until R4 becomes 0<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>DJNZ R3,HERE2<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>RET<\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/div>\r\n<div>\r\n\r\nEND\r\n\r\n&nbsp;\r\n\r\n<strong>1.3 Communicating with LCD using the busy flag <\/strong>The code to communicate with the LCD is given below.\r\n\r\n&nbsp;\r\n\r\n;Check busy flag before sending data, command to LCD ;p1=data pin\r\n\r\n&nbsp;\r\n\r\n;P2.0 connected to RS pin\u00a0;init. LCD 2 lines ,5x7 matrix\r\n\r\n&nbsp;\r\n\r\n;P2.1 connected to R\/W pin\u00a0;issue command\r\n\r\n&nbsp;\r\n\r\n;P2.2 connected to E pin\u00a0;LCD on, cursor on\r\n\r\n<\/div>\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0MOV A,#38H\u00a0\u00a0<\/span>;issue command\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">CALL COMMAND\u00a0;clear LCD command<\/span>\r\n<div>\r\n\r\n\u00a0 \u00a0 MOV A,#0EH\u00a0;issue command\r\n\r\n&nbsp;\r\n\r\nACALL COMMAND\u00a0;shift cursor right\r\n\r\n&nbsp;\r\n\r\nMOV A,#01H\u00a0;issue command\r\n\r\n&nbsp;\r\n\r\nACALL COMMAND\u00a0;cursor: line 1, pos. 6\r\n\r\n&nbsp;\r\n\r\nMOV A,#06H\u00a0;command subroutine\r\n\r\n&nbsp;\r\n\r\nACALL COMMAND\u00a0<span style=\"text-align: initial;font-size: 1em\">;display letter N<\/span>\r\n\r\n&nbsp;\r\n\r\nMOV A,#86H\r\n\r\n&nbsp;\r\n\r\nACALL COMMAND\r\n\r\n&nbsp;\r\n\r\nMOV A,#\u2019N\u2019\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">ACALL DATA_DISPLAY<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"font-size: 1em;text-align: initial\">MOV A,#\u2019O\u2019\u00a0;display letter O<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"font-size: 1em;text-align: initial\">ACALL DATA_DISPLAY<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">HERE: SJMP HERE\u00a0<\/span><span style=\"text-align: initial;font-size: 1em\">;STAY HERE<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"font-size: 1em;text-align: initial\">COMMAND:<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"font-size: 1em;text-align: initial\">ACALL READY\u00a0;is LCD ready?<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">MOV P1,A\u00a0;issue command code<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">CLR P2.0\u00a0;RS=0 for command<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">CLR P2.1\u00a0;R\/W=0 to write to LCD<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">SETB P2.2\u00a0 ;E=1 for H-to-L pulse<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">CLR P2.2\u00a0;E=0,latch in<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">RET<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"font-size: 1em;text-align: initial\">DATA_DISPLAY:\u00a0;is LCD ready?<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">ACALL READY\u00a0 ;issue data<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">MOV P1,A\u00a0;RS=1 for data<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">SETB P2.0\u00a0;R\/W =0 to write to LCD<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">CLR P2.1\u00a0;E=1 for H-to-L pulse<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">SETB P2.2\u00a0 ;E=0,latch in<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">CLR P2.2<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">RET<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">READY:<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">BACK:<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">SETB P1.7\u00a0;make P1.7 input port<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">CLR P2.0\u00a0;RS=0 access command reg<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">SETB P2.1\u00a0;R\/W=1 read command reg<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">SETB P2.2\u00a0;read command reg and check busy flag<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">CLR P2.2\u00a0;E=1 for H-to-L pulse<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">JB P1.7,BACK\u00a0 ;E=0 H-to-L pulse<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">RET\u00a0;stay until busy flag=0<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">END<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>1.4 LCD data sheet<\/strong>\r\n\r\n<\/div>\r\n<div><\/div>\r\n<img class=\"size-full wp-image-188 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-106.png\" alt=\"\" width=\"691\" height=\"243\" \/>\r\n\r\n<img class=\"size-full wp-image-189 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-107.png\" alt=\"\" width=\"764\" height=\"503\" \/>\r\n\r\n<img class=\"size-full wp-image-190 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-108.png\" alt=\"\" width=\"803\" height=\"497\" \/>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>1.5 Sending information to LCD with MOVC instruction<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The following program shows how to use the MOVC instruction to send data and commands to an LCD<\/p>\r\n\r\n<\/div>\r\n<img class=\"aligncenter size-full wp-image-454\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-257.png\" alt=\"\" width=\"492\" height=\"375\" \/>\r\n\r\n<img class=\"aligncenter size-full wp-image-455\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-258.png\" alt=\"\" width=\"571\" height=\"398\" \/>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>2. Keyboard interface<\/strong>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">We will now look at interfacing keyboards to the microcontroller. Keys in a keyboard are arranged in a matrix of rows and columns. The controller access both rows and columns through ports. Using two ports, we can connect to an 8x8 or a 4x4 matrix keyboard. When a key is pressed, a row and column make a contact, otherwise there is no contact. We will look at the details using a 4x4 keyboard.<\/p>\r\n&nbsp;\r\n\r\n<strong>2.1 4x4 Keyboard<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A 4x4 matrix is connected to two ports as shown in Figure 2.1. The rows are connected to an output port and the columns are connected to an input port. Port1 of 8051 is connected to the rows of key matrix, hence it acts as an output port. Port 2 of 8051 is connected to the columns of the key matrix, hence it acts as an input port. A scanning process is used to identify the key that is pressed.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-192 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-110.png\" alt=\"\" width=\"348\" height=\"280\" \/>\r\n\r\n<strong>2.2 Key scan<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">To find out the key pressed , the controller grounds a row by sending a \u20180\u2019 on the corresponding line of the output port. It then reads the data at the columns using the input port. If data from columns is D3-D0=1111, then no key is pressed. If any bit of the column is \u20180\u2019, it indicates that a key is pressed in that column. In this example, the column is identified by the following values:<\/p>\r\n&nbsp;\r\n\r\n1110 \u2013 key pressed in column 0\r\n\r\n&nbsp;\r\n\r\n1101 \u2013 key pressed in column 1\r\n\r\n&nbsp;\r\n\r\n1011 \u2013 key pressed in column 2\r\n\r\n&nbsp;\r\n\r\n0111 \u2013 key pressed in column 3\r\n\r\n&nbsp;\r\n\r\nThis proceeds as follows.\r\n\r\n&nbsp;\r\n\r\n<strong><em>2.2.1 Steps to find out key pressed<\/em><\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Beginning with the row 0, the microcontroller grounds it by providing a low to row D0 only. It then reads the columns(port2). If the data read is all 1s, then no key in that row is activated and the process is moved to the next row. It then grounds the next row, reads the columns, and checks for any zero. This process continues until a row with a zero is identified. After identification of the row in which the key has been pressed, the column to which the pressed key belongs is identified as discussed above - by looking for a zero in the input values read.<\/p>\r\n&nbsp;\r\n\r\nExample:\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">(a) D3 \u2013 D0 = 1101 for the row, D3 \u2013 D0 = 1011 for the column, indicate row 1 and column 3 are selected. This indicates that key 6 is pressed.<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">(b) D3 \u2013 D0 = 1011 for the row, D3 \u2013 D0 = 0111 for the column, indicate row 2 and column 3 are selected. Then key \u2018B\u2019 is pressed.<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>2.3 Program:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The program used for detection and identification of the key activated goes through the following stages:<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">1.\u00a0 To make sure that the preceding key has been released, 0s are output to all rows at once, and the columns are read and checked repeatedly until all the columns are high.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u25cf\u00a0 When all columns are found to be high, the program waits for a short amount of time before it goes to the next stage of waiting for a key to be pressed.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">2. To see if any key is pressed, the columns are scanned over and over in an infinite loop until one of them has a 0 on it.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u25cf Remember that the output latch is connected to rows, still have their initial zeros (in stage 1), making them grounded.<\/p>\r\n&nbsp;\r\n\r\n\u25cf After the keypress detection, it waits for 20-ms for the bounce and then scans the columns again.\r\n\r\n&nbsp;\r\n\r\na) It ensures that the first key press detection was not an erroneous one due to spike noise.\r\n\r\n&nbsp;\r\n\r\nb) After the 20-ms delay, if the key is still pressed, then it goes to the loop (step 3) to detect the actual key pressed.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">3. To detect which row the key pressed belongs to, it grounds one row at a time, reading the columns each time.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u2022 If it finds that all columns are high, this means that the key press does not belong to that row. Therefore, it grounds the next row and continues until it finds the row, that the key pressed belongs to.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u2022 Upon finding the row that the key pressed belongs to, it sets up the starting address for the lookup table holding the scan codes for that row.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">4. To identify the key pressed, it rotates the column bits, one bit at a time, into the carry flag and checks to see if it is low.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u2022 Upon finding the zero, it pulls out the ASCII code for that key from the look-up table.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u2022 Otherwise, it increments the pointer to point to the next element of the look-up table.<\/p>\r\n&nbsp;\r\n\r\n<strong>2.4 Keyboard Program<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The program used for scanning and identifying the pressed key is shown below. The key press detection is standard for all keyboards but the process for determining which key is pressed varies. The look-up table method is shown in the following program. It can be modified to work with any matrix upto 8 x 8.<\/p>\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<img class=\"aligncenter size-full wp-image-456\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-259.png\" alt=\"\" width=\"577\" height=\"412\" \/>\r\n\r\n<img class=\"aligncenter size-full wp-image-457\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-260.png\" alt=\"\" width=\"526\" height=\"359\" \/>\r\n\r\n<img class=\"aligncenter size-full wp-image-458\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-261.png\" alt=\"\" width=\"553\" height=\"473\" \/>\r\n\r\n<strong>(Program Ref:<\/strong>The 8051 Microcontroller and Embedded Systems Using Assembly and C -Second Edition <strong>)<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>3. Summary<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">LCD display, interfacing of LCD with 8051 and the assembly programming are discussed. The Keyboard, interfacing of Keyboard to 8051 and assembly programming for interfacing are also discussed.<\/p>\r\n&nbsp;\r\n\r\n<strong>4. References<\/strong>\r\n<ol>\r\n \t<li style=\"text-align: justify\">Muhammad Ali Mazidi, Janice Gillispie Mazidi, Rolin D. McKinlay, \u201cThe 8051 Microcontroller and Embedded Systems Using Assembly and C -Second Edition\u201d, New Delhi (2000).<\/li>\r\n<\/ol>","rendered":"<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In this lecture, the interfacing of 8051 with an output device and an input device are described. The output device considered is LCD display, and the input device is a matrix-type keyboard. The operation modes of LCD are first discussed. Interfacing of the LCD to an 8051 microcontroller using Assembly and C is also discussed. This is followed by the keyboard interface details.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1. LCD (Liquid Crystal Display)interface<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">LCDs can display numbers, characters, and graphics. To produce a proper display, the information has to be periodically refreshed. This can be done by the CPU or internally by the LCD device itself. Incorporating a refreshing controller into the LCD, relieves the CPU of this task and hence many LCDs have built-in controllers. These controllers also facilitate flexible programming for characters and graphics. Figure 1.1 shows the pin description of an LCD from Optrex.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-185 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-103.png\" alt=\"\" width=\"768\" height=\"506\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-103.png 768w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-103-300x198.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-103-65x43.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-103-225x148.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-103-350x231.png 350w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Vss and VDD provide +5v and ground, V0 is used for controlling LCD contrast. If RS=0, the instruction command register is selected, allowing the user to send a command such as clear display, cursor at home, etc. If RS=1 the data register is selected, allowing the user to send data to be displayed on the LCD. R\/W input allows the user to Read\/ Write the information to the LCD. The enable pin is used by the LCD to latch information presented to its data pins. The 8-bit data pins are used to send information to LCD. Section 1.1 discusses about command codes for writing the instructions on the LCD register.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 1em;text-align: initial\">Section 1.2 gives an example program for displaying a character on the LCD. Section 1.3 shows the communication of LCD with a busy flag and section 1.4 gives a detailed explanation of LCD data sheet.<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>1.1 LCD Command Codes<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The LCD\u2019s internal controller can accept several commands and modify the display accordingly.<\/p>\n<p>&nbsp;<\/p>\n<p>These commands would be things like:<\/p>\n<p>&nbsp;<\/p>\n<p>\u2713 Clear screen<\/p>\n<p>&nbsp;<\/p>\n<p>\u2713 Return home<\/p>\n<p>&nbsp;<\/p>\n<p>\u2713\u00a0 Decrement\/Increment cursor<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">After writing to the LCD, it takes some time for it to complete its internal operations. During this time, it will not accept any new commands or data. Figure 1.2 shows the command codes of LCD and Figure 1.3 shows the LCD interfacing.<\/p>\n<p>&nbsp;<\/p>\n<p>\u2713\u00a0 We need to insert a time delay between any two commands or data sent to LCD.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u2713\u00a0 A long delay has to be given between issuing data or commands to the LCD.<\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-186 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-104.png\" alt=\"\" width=\"520\" height=\"511\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-104.png 520w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-104-300x295.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-104-65x64.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-104-225x221.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-104-350x344.png 350w\" sizes=\"auto, (max-width: 520px) 100vw, 520px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-187 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-105.png\" alt=\"\" width=\"438\" height=\"470\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-105.png 438w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-105-280x300.png 280w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-105-65x70.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-105-225x241.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-105-350x376.png 350w\" sizes=\"auto, (max-width: 438px) 100vw, 438px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>1.2 Program to Display characters on LCD<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">To send any of the commands to the LCD, make pin RS=0. For data, make RS=1. Then send a high-to-low pulse to the E pin to enable the internal latch of the LCD. This is shown in the code below.<\/p>\n<p>&nbsp;<\/p>\n<p>;calls a time delay before sending next data\/command ;P1.0-P1.7 are connected to LCD data pins D0-D7<\/p>\n<p><span style=\"font-size: 1em;text-align: initial\">;P2.0 is connected to RS pin of LCD<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">;P2.1 is connected to R\/W pin of LCD<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">;P2.2 is connected to E pin of LCD<\/span><\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0ORG 0H<\/p>\n<p>&nbsp;<\/p>\n<p>MOV A,#38H\u00a0 ;INIT. LCD 2 LINES, 5X7 MATRIX<\/p>\n<p>&nbsp;<\/p>\n<p>ACALL COMNWRT\u00a0;call command subroutine<\/p>\n<p>&nbsp;<\/p>\n<p>ACALL DELAY\u00a0;give LCD some time<\/p>\n<p>&nbsp;<\/p>\n<p>MOV A,#0EH\u00a0;display on, cursor on<\/p>\n<p>&nbsp;<\/p>\n<p>ACALL COMNWRT\u00a0;call command subroutine<\/p>\n<p>&nbsp;<\/p>\n<p>ACALL DELAY\u00a0;give LCD some time<\/p>\n<p>&nbsp;<\/p>\n<p>MOV A,#01\u00a0;clear LCD<\/p>\n<p>&nbsp;<\/p>\n<p>ACALL COMNWRT\u00a0;call command subroutine<\/p>\n<p>&nbsp;<\/p>\n<p>ACALL DELAY\u00a0\u00a0<span style=\"text-align: initial;font-size: 1em\">;give LCD some time<\/span><\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<div>\n<table style=\"width: 60%\">\n<tbody>\n<tr>\n<td><\/td>\n<td>MOV A,#06H<\/td>\n<td>;shift cursor right<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>ACALL COMNWRT<\/td>\n<td>;call command subroutine<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>ACALL DELAY<\/td>\n<td>;give LCD some time<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>MOV A,#84H<\/td>\n<td>;cursor at line 1, pos. 4<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>ACALL COMNWRT<\/td>\n<td>;call command subroutine<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>ACALL DELAY<\/td>\n<td>;give LCD some time<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>MOV A,#\u2019N\u2019<\/td>\n<td>;display letter N<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>ACALL DATAWRT<\/td>\n<td>;call display subroutine<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>ACALL DELAY<\/td>\n<td>;give LCD some time<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>MOV A,#\u2019O\u2019<\/td>\n<td>;display letter O<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>ACALL DATAWRT<\/td>\n<td>;call display subroutine<\/td>\n<\/tr>\n<tr>\n<td>AGAIN:<\/td>\n<td>SJMP AGAIN<\/td>\n<td>;stay here<\/td>\n<\/tr>\n<tr>\n<td>COMNWRT:<\/td>\n<td><\/td>\n<td>;send command to LCD<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>MOV P1,A<\/td>\n<td>;copy reg A to port 1<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>CLR P2.0<\/td>\n<td>;RS=0 for command<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>CLR P2.1<\/td>\n<td>;R\/W=0 for write<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>SETB P2.2<\/td>\n<td>;E=1 for high pulse<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>CLR P2.2<\/td>\n<td>;E=0 for H-to-L pulse<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>RET<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>DATAWRT:<\/td>\n<td><\/td>\n<td>;write data to LCD<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>MOV P1,A<\/td>\n<td>;copy reg A to port 1<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>SETB P2.0<\/td>\n<td>;RS=1 for DATA<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>CLR P2.1<\/td>\n<td>;R\/W=0 for write<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>SETB P2.2<\/td>\n<td>;E=1 for high pulse<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>CLR P2.2<\/td>\n<td>;E=0 for H-to-L pulse<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>RET<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>DELAY:<\/td>\n<td>MOV R3,#50<\/td>\n<td>;50 or higher for fast CPUs<\/td>\n<\/tr>\n<tr>\n<td>HERE2:<\/td>\n<td>MOV R4,#255<\/td>\n<td>;R4 = 255<\/td>\n<\/tr>\n<tr>\n<td>HERE:<\/td>\n<td>DJNZ R4,HERE<\/td>\n<td>;stay until R4 becomes 0<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>DJNZ R3,HERE2<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>RET<\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div>\n<p>END<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1.3 Communicating with LCD using the busy flag <\/strong>The code to communicate with the LCD is given below.<\/p>\n<p>&nbsp;<\/p>\n<p>;Check busy flag before sending data, command to LCD ;p1=data pin<\/p>\n<p>&nbsp;<\/p>\n<p>;P2.0 connected to RS pin\u00a0;init. LCD 2 lines ,5&#215;7 matrix<\/p>\n<p>&nbsp;<\/p>\n<p>;P2.1 connected to R\/W pin\u00a0;issue command<\/p>\n<p>&nbsp;<\/p>\n<p>;P2.2 connected to E pin\u00a0;LCD on, cursor on<\/p>\n<\/div>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0MOV A,#38H\u00a0\u00a0<\/span>;issue command<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">CALL COMMAND\u00a0;clear LCD command<\/span><\/p>\n<div>\n<p>\u00a0 \u00a0 MOV A,#0EH\u00a0;issue command<\/p>\n<p>&nbsp;<\/p>\n<p>ACALL COMMAND\u00a0;shift cursor right<\/p>\n<p>&nbsp;<\/p>\n<p>MOV A,#01H\u00a0;issue command<\/p>\n<p>&nbsp;<\/p>\n<p>ACALL COMMAND\u00a0;cursor: line 1, pos. 6<\/p>\n<p>&nbsp;<\/p>\n<p>MOV A,#06H\u00a0;command subroutine<\/p>\n<p>&nbsp;<\/p>\n<p>ACALL COMMAND\u00a0<span style=\"text-align: initial;font-size: 1em\">;display letter N<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>MOV A,#86H<\/p>\n<p>&nbsp;<\/p>\n<p>ACALL COMMAND<\/p>\n<p>&nbsp;<\/p>\n<p>MOV A,#\u2019N\u2019<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">ACALL DATA_DISPLAY<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 1em;text-align: initial\">MOV A,#\u2019O\u2019\u00a0;display letter O<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 1em;text-align: initial\">ACALL DATA_DISPLAY<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">HERE: SJMP HERE\u00a0<\/span><span style=\"text-align: initial;font-size: 1em\">;STAY HERE<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 1em;text-align: initial\">COMMAND:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 1em;text-align: initial\">ACALL READY\u00a0;is LCD ready?<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">MOV P1,A\u00a0;issue command code<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">CLR P2.0\u00a0;RS=0 for command<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">CLR P2.1\u00a0;R\/W=0 to write to LCD<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">SETB P2.2\u00a0 ;E=1 for H-to-L pulse<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">CLR P2.2\u00a0;E=0,latch in<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">RET<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 1em;text-align: initial\">DATA_DISPLAY:\u00a0;is LCD ready?<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">ACALL READY\u00a0 ;issue data<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">MOV P1,A\u00a0;RS=1 for data<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">SETB P2.0\u00a0;R\/W =0 to write to LCD<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">CLR P2.1\u00a0;E=1 for H-to-L pulse<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">SETB P2.2\u00a0 ;E=0,latch in<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">CLR P2.2<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">RET<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">READY:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">BACK:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">SETB P1.7\u00a0;make P1.7 input port<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">CLR P2.0\u00a0;RS=0 access command reg<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">SETB P2.1\u00a0;R\/W=1 read command reg<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">SETB P2.2\u00a0;read command reg and check busy flag<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">CLR P2.2\u00a0;E=1 for H-to-L pulse<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">JB P1.7,BACK\u00a0 ;E=0 H-to-L pulse<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">RET\u00a0;stay until busy flag=0<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">END<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>1.4 LCD data sheet<\/strong><\/p>\n<\/div>\n<div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-188 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-106.png\" alt=\"\" width=\"691\" height=\"243\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-106.png 691w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-106-300x105.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-106-65x23.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-106-225x79.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-106-350x123.png 350w\" sizes=\"auto, (max-width: 691px) 100vw, 691px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-189 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-107.png\" alt=\"\" width=\"764\" height=\"503\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-107.png 764w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-107-300x198.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-107-65x43.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-107-225x148.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-107-350x230.png 350w\" sizes=\"auto, (max-width: 764px) 100vw, 764px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-190 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-108.png\" alt=\"\" width=\"803\" height=\"497\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-108.png 803w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-108-300x186.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-108-768x475.png 768w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-108-65x40.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-108-225x139.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-108-350x217.png 350w\" sizes=\"auto, (max-width: 803px) 100vw, 803px\" \/><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>1.5 Sending information to LCD with MOVC instruction<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The following program shows how to use the MOVC instruction to send data and commands to an LCD<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-454\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-257.png\" alt=\"\" width=\"492\" height=\"375\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-257.png 492w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-257-300x229.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-257-65x50.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-257-225x171.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-257-350x267.png 350w\" sizes=\"auto, (max-width: 492px) 100vw, 492px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-455\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-258.png\" alt=\"\" width=\"571\" height=\"398\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-258.png 571w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-258-300x209.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-258-65x45.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-258-225x157.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-258-350x244.png 350w\" sizes=\"auto, (max-width: 571px) 100vw, 571px\" \/><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>2. Keyboard interface<\/strong><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We will now look at interfacing keyboards to the microcontroller. Keys in a keyboard are arranged in a matrix of rows and columns. The controller access both rows and columns through ports. Using two ports, we can connect to an 8&#215;8 or a 4&#215;4 matrix keyboard. When a key is pressed, a row and column make a contact, otherwise there is no contact. We will look at the details using a 4&#215;4 keyboard.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>2.1 4&#215;4 Keyboard<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A 4&#215;4 matrix is connected to two ports as shown in Figure 2.1. The rows are connected to an output port and the columns are connected to an input port. Port1 of 8051 is connected to the rows of key matrix, hence it acts as an output port. Port 2 of 8051 is connected to the columns of the key matrix, hence it acts as an input port. A scanning process is used to identify the key that is pressed.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-192 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-110.png\" alt=\"\" width=\"348\" height=\"280\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-110.png 348w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-110-300x241.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-110-65x52.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-110-225x181.png 225w\" sizes=\"auto, (max-width: 348px) 100vw, 348px\" \/><\/p>\n<p><strong>2.2 Key scan<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">To find out the key pressed , the controller grounds a row by sending a \u20180\u2019 on the corresponding line of the output port. It then reads the data at the columns using the input port. If data from columns is D3-D0=1111, then no key is pressed. If any bit of the column is \u20180\u2019, it indicates that a key is pressed in that column. In this example, the column is identified by the following values:<\/p>\n<p>&nbsp;<\/p>\n<p>1110 \u2013 key pressed in column 0<\/p>\n<p>&nbsp;<\/p>\n<p>1101 \u2013 key pressed in column 1<\/p>\n<p>&nbsp;<\/p>\n<p>1011 \u2013 key pressed in column 2<\/p>\n<p>&nbsp;<\/p>\n<p>0111 \u2013 key pressed in column 3<\/p>\n<p>&nbsp;<\/p>\n<p>This proceeds as follows.<\/p>\n<p>&nbsp;<\/p>\n<p><strong><em>2.2.1 Steps to find out key pressed<\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Beginning with the row 0, the microcontroller grounds it by providing a low to row D0 only. It then reads the columns(port2). If the data read is all 1s, then no key in that row is activated and the process is moved to the next row. It then grounds the next row, reads the columns, and checks for any zero. This process continues until a row with a zero is identified. After identification of the row in which the key has been pressed, the column to which the pressed key belongs is identified as discussed above &#8211; by looking for a zero in the input values read.<\/p>\n<p>&nbsp;<\/p>\n<p>Example:<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">(a) D3 \u2013 D0 = 1101 for the row, D3 \u2013 D0 = 1011 for the column, indicate row 1 and column 3 are selected. This indicates that key 6 is pressed.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">(b) D3 \u2013 D0 = 1011 for the row, D3 \u2013 D0 = 0111 for the column, indicate row 2 and column 3 are selected. Then key \u2018B\u2019 is pressed.<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>2.3 Program:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The program used for detection and identification of the key activated goes through the following stages:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">1.\u00a0 To make sure that the preceding key has been released, 0s are output to all rows at once, and the columns are read and checked repeatedly until all the columns are high.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u25cf\u00a0 When all columns are found to be high, the program waits for a short amount of time before it goes to the next stage of waiting for a key to be pressed.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">2. To see if any key is pressed, the columns are scanned over and over in an infinite loop until one of them has a 0 on it.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u25cf Remember that the output latch is connected to rows, still have their initial zeros (in stage 1), making them grounded.<\/p>\n<p>&nbsp;<\/p>\n<p>\u25cf After the keypress detection, it waits for 20-ms for the bounce and then scans the columns again.<\/p>\n<p>&nbsp;<\/p>\n<p>a) It ensures that the first key press detection was not an erroneous one due to spike noise.<\/p>\n<p>&nbsp;<\/p>\n<p>b) After the 20-ms delay, if the key is still pressed, then it goes to the loop (step 3) to detect the actual key pressed.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">3. To detect which row the key pressed belongs to, it grounds one row at a time, reading the columns each time.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u2022 If it finds that all columns are high, this means that the key press does not belong to that row. Therefore, it grounds the next row and continues until it finds the row, that the key pressed belongs to.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u2022 Upon finding the row that the key pressed belongs to, it sets up the starting address for the lookup table holding the scan codes for that row.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">4. To identify the key pressed, it rotates the column bits, one bit at a time, into the carry flag and checks to see if it is low.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u2022 Upon finding the zero, it pulls out the ASCII code for that key from the look-up table.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u2022 Otherwise, it increments the pointer to point to the next element of the look-up table.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>2.4 Keyboard Program<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The program used for scanning and identifying the pressed key is shown below. The key press detection is standard for all keyboards but the process for determining which key is pressed varies. The look-up table method is shown in the following program. It can be modified to work with any matrix upto 8 x 8.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-456\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-259.png\" alt=\"\" width=\"577\" height=\"412\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-259.png 577w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-259-300x214.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-259-65x46.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-259-225x161.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-259-350x250.png 350w\" sizes=\"auto, (max-width: 577px) 100vw, 577px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-457\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-260.png\" alt=\"\" width=\"526\" height=\"359\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-260.png 526w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-260-300x205.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-260-65x44.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-260-225x154.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-260-350x239.png 350w\" sizes=\"auto, (max-width: 526px) 100vw, 526px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-458\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-261.png\" alt=\"\" width=\"553\" height=\"473\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-261.png 553w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-261-300x257.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-261-65x56.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-261-225x192.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-261-350x299.png 350w\" sizes=\"auto, (max-width: 553px) 100vw, 553px\" \/><\/p>\n<p><strong>(Program Ref:<\/strong>The 8051 Microcontroller and Embedded Systems Using Assembly and C -Second Edition <strong>)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>3. Summary<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">LCD display, interfacing of LCD with 8051 and the assembly programming are discussed. The Keyboard, interfacing of Keyboard to 8051 and assembly programming for interfacing are also discussed.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>4. References<\/strong><\/p>\n<ol>\n<li style=\"text-align: justify\">Muhammad Ali Mazidi, Janice Gillispie Mazidi, Rolin D. McKinlay, \u201cThe 8051 Microcontroller and Embedded Systems Using Assembly and C -Second Edition\u201d, New Delhi (2000).<\/li>\n<\/ol>\n","protected":false},"author":2,"menu_order":15,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-k-vani"],"pb_section_license":""},"chapter-type":[],"contributor":[58],"license":[],"class_list":["post-182","chapter","type-chapter","status-publish","hentry","contributor-dr-k-vani"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/chapters\/182","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/wp\/v2\/users\/2"}],"version-history":[{"count":6,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/chapters\/182\/revisions"}],"predecessor-version":[{"id":459,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/chapters\/182\/revisions\/459"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/chapters\/182\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/wp\/v2\/media?parent=182"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/chapter-type?post=182"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/wp\/v2\/contributor?post=182"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/wp\/v2\/license?post=182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}