{"id":249,"date":"2018-07-27T06:16:33","date_gmt":"2018-07-27T06:16:33","guid":{"rendered":"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=249"},"modified":"2018-07-27T07:16:36","modified_gmt":"2018-07-27T07:16:36","slug":"interfacing-external-devices-using-embedded-c","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/chapter\/interfacing-external-devices-using-embedded-c\/","title":{"rendered":"Interfacing External Devices using Embedded C"},"content":{"raw":"&nbsp;\r\n<p style=\"text-align: justify\">In this lecture interfacing of external devices with 8051 will be discussed. The Interfacing of external memory with 8051 will be discussed. The Embedded C program for the interfacing of 8051 with external devices will be discussed in detail with examples.<\/p>\r\n&nbsp;\r\n<ol>\r\n \t<li><strong>LCD<\/strong><\/li>\r\n<\/ol>\r\n<p style=\"text-align: justify\">A liquid crystal display (LCD) is a flat panel display that uses the light modulating properties of liquid crystals (LCs). LCD Modules can present textual information to user. LCD is used to provide user interface for normal output as well as for debugging purpose.<\/p>\r\n&nbsp;\r\n\r\n<strong>1.1<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD Interfacing<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">LCD modules are available in different configurations. Here, we will consider the 2x16 character module available from Optrex to illustrate the interfacing with the microcontroller. The 2x16 character LCD module supports both 4-bit and 8-bit modes. These modes differ in how data is sent to LCD. In 8-bit mode, to write a character, 8-bit ASCII data is sent through data lines D0-D7 and data strobe is given through pin E of the LCD. But 4-bit mode uses only 4 data lines D4-D7. In this mode, 8-bit ASCII data and command data are divided into two parts and sent sequentially through the data lines. It is used in communication because, less pins will be used. Therefore 4-bit mode is preferred than 8-bit even with speed difference. Figure 1 shown below gives the details of interfacing 4-bit LCD to Microcontroller.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-252 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-138.png\" alt=\"\" width=\"501\" height=\"314\" \/>\r\n\r\n&nbsp;\r\n\r\nPin Descriptions of LCD are described in Table 1 and Pin Positions for various LCDs are shown in Figure 2.\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-253 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-139.png\" alt=\"\" width=\"656\" height=\"241\" \/>\r\n\r\n<img class=\"size-full wp-image-254 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-140.png\" alt=\"\" width=\"656\" height=\"353\" \/>\r\n<div>\r\n\r\n<strong>1.2 LCD command codes<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The LCD\u2019s internal controller can accept several commands and modify the display accordingly.<\/p>\r\nThese commands would be things like:\r\n\r\n\u25cf\u00a0\u00a0\u00a0\u00a0 Clear screen\r\n\r\n\u25cf\u00a0\u00a0\u00a0\u00a0 Return home\r\n\r\n\u25cf\u00a0\u00a0\u00a0\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. We need to insert time delay between any two commands or data sent to LCD. The LCD command codes are described in Table 2 shown below.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-255 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-141.png\" alt=\"\" width=\"438\" height=\"387\" \/>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>1.3 Circuit diagram to Interface 4-bit LCD with 8051<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe circuit diagram for interfacing 4-bit LCD with 8051 is shown in Figure 3 below.\r\n\r\n<\/div>\r\n<img class=\"size-full wp-image-256 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-142.png\" alt=\"\" width=\"540\" height=\"395\" \/>\r\n<div>\r\n<p style=\"text-align: center\"><strong>Figure 3 Interfacing 4-bit LCD with 8051<\/strong><\/p>\r\n&nbsp;\r\n\r\nExample 1 described below gives the details about how to write embedded C program for interfacing of LCD to 8051.\r\n\r\n&nbsp;\r\n\r\n<strong>Example 1<\/strong>\r\n\r\n&nbsp;\r\n\r\nWrite an 8051 C program to send letters \u2018M\u2019, \u2018D\u2019, and \u2018E\u2019 to the LCD using the busy flag method.\r\n\r\nPort1 connected to LCD data pins D0-D7.\r\n\r\nP2.0=RS , P2.1=R\/W,\u00a0 P2.2=E pins.\r\n\r\n<strong>Solution:<\/strong>\r\n\r\n#include &lt;reg51.h&gt;\r\n\r\nsfr ldata = 0x90; \/\/P1=LCD data pins\r\n\r\nsbit rs = P2^0;\r\n\r\nsbit rw = P2^1;\r\n\r\nsbit en = P2^2;\r\n\r\nsbit busy = P1^7;\r\n\r\n&nbsp;\r\n\r\nVoid main()\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nlcdcmd(0x38);\/\/ 2 lines 5x7 matrix\r\n\r\nlcdcmd(0x0E);\/\/display on cursor blinking\r\n\r\nlcdcmd(0x01);\/\/clear display screen\r\n\r\nlcdcmd(0x06);\/\/increment cursor\r\n\r\nlcdcmd(0x86); \/\/line 1, position 6\r\n\r\nlcddata(\u2018M\u2019);\r\n\r\nlcddata(\u2018D\u2019);\r\n\r\nlcddata(\u2018E\u2019);\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nvoid lcdcmd(unsigned char value)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nlcdready(); \/\/check the LCD busy flag\r\n\r\nldata = value; \/\/put the value on the pins port1\r\n\r\nrs = 0; \/\/ for command rs=0\r\n\r\nrw = 0;\/\/ r\/w=0 for write\r\n\r\nen = 1; \/\/strobe the enable pin\r\n\r\nMSDelay(1);\r\n\r\nen = 0; \/\/ high low pulse to E\r\n\r\nreturn;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nvoid lcddata(unsigned char value)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nlcdready(); \/\/check the LCD busy flag\r\n\r\nldata = value; \/\/put the value on the pins\r\n\r\nrs = 1;\u00a0 \/\/ rs=1 for data\r\n\r\nrw = 0; \/\/ for write\r\n\r\nen = 1; \/\/strobe the enable pin\r\n\r\n<\/div>\r\n&nbsp;\r\n<div>\r\n\r\nMSDelay(1);\r\n\r\nen = 0;\u00a0 \/\/high low pulse\r\n\r\nreturn;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nvoid lcdready()\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nbusy = 1; \/\/make the busy pin at input\r\n\r\nrs = 0;\r\n\r\nrw = 1;\r\n\r\nwhile(busy==1){ \/\/wait here for busy flag\r\n\r\nen = 0; \/\/strobe the enable pin\r\n\r\nMSDelay(1);\r\n\r\nen = 1;\r\n\r\n}\r\n\r\nvoid msdelay (unsigned int itime)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nunsigned int i, j;\r\n\r\nfor(i=0;i&lt;itime;i++)\r\n\r\nfor(j=0;j&lt;1275;j++);\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nThe above program sends the letters \u2018M\u2019, \u2018D\u2019, and \u2018E\u2019 to the LCD using the busy flag method.\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>2. Keyboard Interfacing<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Keys in keyboards are arranged in a matrix of rows and columns. Controller accesses both rows and columns through ports. Using two ports we can connect 8x8 matrix key board. When a key is pressed, a row and column make contact; otherwise there is no contact.<\/p>\r\n&nbsp;\r\n\r\n<strong>4 x 4 Keyboard:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A 4x4 matrix is connected to two ports.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 the 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. The matrix keyboard connection to ports is shown in Figure 4.<\/p>\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-257 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-143.png\" alt=\"\" width=\"426\" height=\"289\" \/>\r\n<div>\r\n\r\n<strong>Key Scan:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">To find out the key pressed, the controller grounds all rows by sending \u20180\u2019 on the port, then it reads the column data. If the data from column is D3-D0=1111, then no key is pressed. If any bit of the column is \u20180\u2019, it indicates key is pressed.<\/p>\r\n&nbsp;\r\n\r\n<strong>Example:\u00a0<\/strong>\r\n\r\n1110 \u2013 key pressed in\u00a0 column 0\r\n\r\n1101 \u2013 key pressed in column 1\r\n\r\n1011 \u2013 key pressed in column 2\r\n\r\n0111 \u2013 key pressed in column 3\r\n\r\n&nbsp;\r\n\r\n<strong>2.1 Steps to find out key pressed<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe following are the steps to find out how the key is pressed in the keyboard\r\n\r\n&nbsp;\r\n\r\n\u25cf\u00a0\u00a0\u00a0\u00a0 Beginning with the row 0, the microcontroller grounds it by providing a low to row D0 only.\r\n\r\n&nbsp;\r\n\r\n\u25cf\u00a0\u00a0\u00a0\u00a0 It reads the columns(port2). If the data read is all 1s, no key in that row is activated and the process is moved to the next row.\r\n\r\n&nbsp;\r\n\r\n\u25cf\u00a0\u00a0\u00a0\u00a0 It grounds the next row, reads the columns, and checks for any zero.\r\n\r\n&nbsp;\r\n\r\n\u25cf\u00a0\u00a0\u00a0\u00a0 This process continues until the row is identified.\r\n\r\n&nbsp;\r\n\r\n\u25cf\u00a0\u00a0\u00a0\u00a0 After identification of the row in which the key has been pressed, find out which column the pressed key belongs to by looking for a zero on it.\r\n\r\n&nbsp;\r\n\r\n<strong>Example:<\/strong>\r\n\r\n&nbsp;\r\n\r\n(a)\u00a0 If D3 \u2013 D0 = 1101 for the row, D3 \u2013 D0 = 1011 for the column, then the key located at row 1 and column 3, (key 6) is pressed.\r\n\r\n&nbsp;\r\n\r\n(b)\u00a0 If D3 \u2013 D0 = 1011 for the row, D3 \u2013 D0 = 0111 for the column, then the key located at row 2 and column 3, (key \u2018B\u2019) is pressed.\r\n\r\n&nbsp;\r\n\r\nThe Figure 5 shown below gives the interfacing of keyboard with 8051.\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-258 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-144.png\" alt=\"\" width=\"685\" height=\"440\" \/>\r\n<div>\r\n\r\n<strong>2.2 Keyboard interfacing program in C<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The example 2 given below describes how to read the input from keyboard and send the result to serial port. For this 9600 baud rate is used.<\/p>\r\n&nbsp;\r\n\r\n<strong>Example 2<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Write a C program to read the keypad and send result to first serial port P1.0 to P1.3 connected to rows, and P2.0 to P2.3 connected to columns. Configure serial port for 9600 baud, 8 data bits and one stop bit.<\/p>\r\n&nbsp;\r\n\r\n<\/div>\r\n<img class=\"size-full wp-image-259 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-145.png\" alt=\"\" width=\"631\" height=\"502\" \/>\r\n\r\n<\/div>\r\n<img class=\" wp-image-260 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-146.png\" alt=\"\" width=\"592\" height=\"759\" \/>\r\n\r\n<img class=\"size-full wp-image-261 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-147.png\" alt=\"\" width=\"580\" height=\"501\" \/>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This section described about LCD and keyboard interfacing with 8051.The following section describes interfacing of 8051 with external ROM.<\/p>\r\n&nbsp;\r\n\r\n<strong>3.8051 Interfacing with External ROM<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The reason for interfacing 8051 with external ROM is that it has a limited amount of on-chip ROM. Hence for sufficient memory allocation 8051 is interfaced with external ROM. CPU gives the address of the data required. Decoding circuit has to locate the selected memory block. Memory chips have CS (chip select) pin, which must be activated for the memory\u2019s contents to be accessed.<\/p>\r\n&nbsp;\r\n\r\n<strong>EA pin:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Connect the EA pin to Vcc to indicate that the program code is stored in the microcontroller on-chip ROM. To indicate that the program code is stored in external ROM, this pin must be connected to GND. The figure 6 shown below gives the details of 8051 pin diagram.<\/p>\r\n\r\n<\/div>\r\n<img class=\"size-full wp-image-262 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-148.png\" alt=\"\" width=\"425\" height=\"461\" \/>\r\n<div>\r\n\r\n<strong>3.1 Steps to be followed when connecting external memory<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe following are the steps to follow when connecting to external memory:\r\n\r\n&nbsp;\r\n\r\n1.\u00a0\u00a0\u00a0 CPU data bus is connected directly to the data pins of the memory chip.\r\n\r\n&nbsp;\r\n\r\n2.\u00a0\u00a0\u00a0 Control signals RD (read) and WR (memory write) from the CPU are connected to the OE (output enable) and WE (write enable) pins of the memory chip.\r\n\r\n&nbsp;\r\n\r\n3.\u00a0\u00a0\u00a0 The lower bits of the address from the CPU is connected directly to the memory chip of the address pins.\r\n\r\n&nbsp;\r\n\r\n4.\u00a0\u00a0\u00a0 The upper ones are used to activate the CS pin of the memory chip.\r\n\r\n&nbsp;\r\n\r\n<strong>P0 and P2 role in providing addresses:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The PC (program counter) of the 8031\/51 is 16-bit, it is capable of accessing up to 64K bytes of program code. 16-bit address is provided by port 0 and port 2 to access external memory. P0 provides the lower 8-bit address A0 \u2013 A7 whereas P2 provides the upper 8-bit address A8 \u2013 A15. P0 is also used to provide the 8-bit data bus D0\u2013D7. P0.0 \u2013 P0.7 are used for both the address and data paths (address\/data multiplexing), to extract the addresses from the P0 pins. It will connect to 74LS373 D Latch.<\/p>\r\n&nbsp;\r\n\r\n<strong>ALE (Address Latch Enable):<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">ALE (address latch enable) pin is an output pin for 8031\/51. If ALE = 0, P0 is used for data path, whereas if ALE = 1, P0 is used for address path. Separation of address and data can be obtained by using 74LS373. The Figure 7 shown below gives the specifics about 74LS373 D\u00a0<span style=\"text-align: initial;font-size: 1em\">latch.<\/span><\/p>\r\n\r\n<\/div>\r\n<img class=\"size-full wp-image-263 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-149.png\" alt=\"\" width=\"334\" height=\"355\" \/>\r\n\r\nThe Data, Address, Control Buses for the 8031\/8051 are shown in Figure 8 below.\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-264\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-150.png\" alt=\"\" width=\"756\" height=\"373\" \/>\r\n\r\n<strong>8051 connection to external data RAM:<\/strong>\r\n\r\n&nbsp;\r\n\r\n8051 connection to external data RAM is shown in Figure 9 below.\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-265 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-151.png\" alt=\"\" width=\"720\" height=\"375\" \/>\r\n\r\n<strong>A single ROM for both program and data:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">For certain applications we need program ROM, Data ROM and Data RAM in a system. To allow a single ROM chip to provide both program code space and data code space, we use an AND gate to signal the OE pin of the ROM chip. The figure 10 shown below gives the details about connection of 8051 with single ROM for both program and data.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-266 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-152.png\" alt=\"\" width=\"660\" height=\"258\" \/>\r\n<div>\r\n\r\n<strong>3<\/strong>.<strong>2 External memory interfacing program in C<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In some applications, we need a large amount of memory to store data. However, the 8051 supports only 64K bytes of external data memory,since DPTR is 16-bit. To solve this problem, we connect A0 - A15 of the 8051 directly to the external memory\u2019s A0 - A15 pins and use some of the P1 pin to access this 64K bytes block inside the single 256K X 8 memory chip.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"font-size: 1em;text-align: initial\">Example 3 shown below describes how to store ASCII letters to external RAM address starting at 0. It then gets data from external RAM and sends it to PI, one byte at a time.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>Example 3:<\/strong>\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-267 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-153.png\" alt=\"\" width=\"634\" height=\"436\" \/>\r\n\r\n&nbsp;\r\n\r\nExample 4 given below describes how to write the C program to read 30 bytes of data from an external ROM and send it to port P1. An external ROM uses 8051 data space to store the value in lookup table for the DAC data.\r\n\r\n&nbsp;\r\n\r\n<strong>Example 4<\/strong>\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-268 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-154.png\" alt=\"\" width=\"672\" height=\"248\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Example 5 given below describes how to write the C program to move the message into\u00a0<span style=\"font-size: 1em;text-align: initial\">external RAM and read the same in RAM and send it to serial port. For this SFR register is used to declare the new addresses and timer 1 is used for baud rate generation.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>Example 5<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Example 6 given below describes how to write the C program to access 1KB SRAM of the DS89C4XO chip. But the ASCII letters are stored before itself in SRAM, for which SBUF register is used. For this SFR register is used to declare the new addresses and timer 1 is used for baud rate generation.<\/p>\r\n&nbsp;\r\n\r\n<strong>Example 6<\/strong>\r\n\r\n<\/div>\r\n<img class=\"size-full wp-image-269 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-155.png\" alt=\"\" width=\"702\" height=\"481\" \/>\r\n<ol start=\"4\">\r\n \t<li><strong> Summary<\/strong><\/li>\r\n<\/ol>\r\n<p style=\"text-align: justify\">In this lecture, basics of interfacing of 8051 to external devices are discussed. The Interfacing of 8051 to external memory has also been discussed. Interfacing of 8051 with external devices in Embedded C is discussed in detail with examples.<\/p>\r\n&nbsp;\r\n<ol start=\"5\">\r\n \t<li><strong>References<\/strong><\/li>\r\n<\/ol>\r\n<ol>\r\n \t<li>Muhammad Ali\u00a0 Mazidi,\u00a0 Janice\u00a0 Gillispie\u00a0 Mazidi,\u00a0 Rolin\u00a0 D.\u00a0 McKinlay,\u00a0 \u201cThe\u00a0 8051 Microcontroller and Embedded Systems Using Assembly and C -Second Edition\u201d, New Delhi(2000).<\/li>\r\n<\/ol>\r\n&nbsp;","rendered":"<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In this lecture interfacing of external devices with 8051 will be discussed. The Interfacing of external memory with 8051 will be discussed. The Embedded C program for the interfacing of 8051 with external devices will be discussed in detail with examples.<\/p>\n<p>&nbsp;<\/p>\n<ol>\n<li><strong>LCD<\/strong><\/li>\n<\/ol>\n<p style=\"text-align: justify\">A liquid crystal display (LCD) is a flat panel display that uses the light modulating properties of liquid crystals (LCs). LCD Modules can present textual information to user. LCD is used to provide user interface for normal output as well as for debugging purpose.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1.1<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>LCD Interfacing<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">LCD modules are available in different configurations. Here, we will consider the 2&#215;16 character module available from Optrex to illustrate the interfacing with the microcontroller. The 2&#215;16 character LCD module supports both 4-bit and 8-bit modes. These modes differ in how data is sent to LCD. In 8-bit mode, to write a character, 8-bit ASCII data is sent through data lines D0-D7 and data strobe is given through pin E of the LCD. But 4-bit mode uses only 4 data lines D4-D7. In this mode, 8-bit ASCII data and command data are divided into two parts and sent sequentially through the data lines. It is used in communication because, less pins will be used. Therefore 4-bit mode is preferred than 8-bit even with speed difference. Figure 1 shown below gives the details of interfacing 4-bit LCD to Microcontroller.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-252 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-138.png\" alt=\"\" width=\"501\" height=\"314\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-138.png 501w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-138-300x188.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-138-65x41.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-138-225x141.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-138-350x219.png 350w\" sizes=\"auto, (max-width: 501px) 100vw, 501px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Pin Descriptions of LCD are described in Table 1 and Pin Positions for various LCDs are shown in Figure 2.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-253 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-139.png\" alt=\"\" width=\"656\" height=\"241\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-139.png 656w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-139-300x110.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-139-65x24.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-139-225x83.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-139-350x129.png 350w\" sizes=\"auto, (max-width: 656px) 100vw, 656px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-254 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-140.png\" alt=\"\" width=\"656\" height=\"353\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-140.png 656w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-140-300x161.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-140-65x35.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-140-225x121.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-140-350x188.png 350w\" sizes=\"auto, (max-width: 656px) 100vw, 656px\" \/><\/p>\n<div>\n<p><strong>1.2 LCD command codes<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The LCD\u2019s internal controller can accept several commands and modify the display accordingly.<\/p>\n<p>These commands would be things like:<\/p>\n<p>\u25cf\u00a0\u00a0\u00a0\u00a0 Clear screen<\/p>\n<p>\u25cf\u00a0\u00a0\u00a0\u00a0 Return home<\/p>\n<p>\u25cf\u00a0\u00a0\u00a0\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. We need to insert time delay between any two commands or data sent to LCD. The LCD command codes are described in Table 2 shown below.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-255 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-141.png\" alt=\"\" width=\"438\" height=\"387\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-141.png 438w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-141-300x265.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-141-65x57.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-141-225x199.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-141-350x309.png 350w\" sizes=\"auto, (max-width: 438px) 100vw, 438px\" \/><\/p>\n<\/div>\n<div>\n<p><strong>1.3 Circuit diagram to Interface 4-bit LCD with 8051<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The circuit diagram for interfacing 4-bit LCD with 8051 is shown in Figure 3 below.<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-256 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-142.png\" alt=\"\" width=\"540\" height=\"395\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-142.png 540w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-142-300x219.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-142-65x48.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-142-225x165.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-142-350x256.png 350w\" sizes=\"auto, (max-width: 540px) 100vw, 540px\" \/><\/p>\n<div>\n<p style=\"text-align: center\"><strong>Figure 3 Interfacing 4-bit LCD with 8051<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Example 1 described below gives the details about how to write embedded C program for interfacing of LCD to 8051.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 1<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Write an 8051 C program to send letters \u2018M\u2019, \u2018D\u2019, and \u2018E\u2019 to the LCD using the busy flag method.<\/p>\n<p>Port1 connected to LCD data pins D0-D7.<\/p>\n<p>P2.0=RS , P2.1=R\/W,\u00a0 P2.2=E pins.<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>#include &lt;reg51.h&gt;<\/p>\n<p>sfr ldata = 0x90; \/\/P1=LCD data pins<\/p>\n<p>sbit rs = P2^0;<\/p>\n<p>sbit rw = P2^1;<\/p>\n<p>sbit en = P2^2;<\/p>\n<p>sbit busy = P1^7;<\/p>\n<p>&nbsp;<\/p>\n<p>Void main()<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>lcdcmd(0x38);\/\/ 2 lines 5&#215;7 matrix<\/p>\n<p>lcdcmd(0x0E);\/\/display on cursor blinking<\/p>\n<p>lcdcmd(0x01);\/\/clear display screen<\/p>\n<p>lcdcmd(0x06);\/\/increment cursor<\/p>\n<p>lcdcmd(0x86); \/\/line 1, position 6<\/p>\n<p>lcddata(\u2018M\u2019);<\/p>\n<p>lcddata(\u2018D\u2019);<\/p>\n<p>lcddata(\u2018E\u2019);<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>void lcdcmd(unsigned char value)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>lcdready(); \/\/check the LCD busy flag<\/p>\n<p>ldata = value; \/\/put the value on the pins port1<\/p>\n<p>rs = 0; \/\/ for command rs=0<\/p>\n<p>rw = 0;\/\/ r\/w=0 for write<\/p>\n<p>en = 1; \/\/strobe the enable pin<\/p>\n<p>MSDelay(1);<\/p>\n<p>en = 0; \/\/ high low pulse to E<\/p>\n<p>return;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>void lcddata(unsigned char value)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>lcdready(); \/\/check the LCD busy flag<\/p>\n<p>ldata = value; \/\/put the value on the pins<\/p>\n<p>rs = 1;\u00a0 \/\/ rs=1 for data<\/p>\n<p>rw = 0; \/\/ for write<\/p>\n<p>en = 1; \/\/strobe the enable pin<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div>\n<p>MSDelay(1);<\/p>\n<p>en = 0;\u00a0 \/\/high low pulse<\/p>\n<p>return;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>void lcdready()<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>busy = 1; \/\/make the busy pin at input<\/p>\n<p>rs = 0;<\/p>\n<p>rw = 1;<\/p>\n<p>while(busy==1){ \/\/wait here for busy flag<\/p>\n<p>en = 0; \/\/strobe the enable pin<\/p>\n<p>MSDelay(1);<\/p>\n<p>en = 1;<\/p>\n<p>}<\/p>\n<p>void msdelay (unsigned int itime)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>unsigned int i, j;<\/p>\n<p>for(i=0;i&lt;itime;i++)<\/p>\n<p>for(j=0;j&lt;1275;j++);<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>The above program sends the letters \u2018M\u2019, \u2018D\u2019, and \u2018E\u2019 to the LCD using the busy flag method.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>2. Keyboard Interfacing<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Keys in keyboards are arranged in a matrix of rows and columns. Controller accesses both rows and columns through ports. Using two ports we can connect 8&#215;8 matrix key board. When a key is pressed, a row and column make contact; otherwise there is no contact.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>4 x 4 Keyboard:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A 4&#215;4 matrix is connected to two ports.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 the 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. The matrix keyboard connection to ports is shown in Figure 4.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-257 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-143.png\" alt=\"\" width=\"426\" height=\"289\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-143.png 426w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-143-300x204.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-143-65x44.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-143-225x153.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-143-350x237.png 350w\" sizes=\"auto, (max-width: 426px) 100vw, 426px\" \/><\/p>\n<div>\n<p><strong>Key Scan:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">To find out the key pressed, the controller grounds all rows by sending \u20180\u2019 on the port, then it reads the column data. If the data from column is D3-D0=1111, then no key is pressed. If any bit of the column is \u20180\u2019, it indicates key is pressed.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example:\u00a0<\/strong><\/p>\n<p>1110 \u2013 key pressed in\u00a0 column 0<\/p>\n<p>1101 \u2013 key pressed in column 1<\/p>\n<p>1011 \u2013 key pressed in column 2<\/p>\n<p>0111 \u2013 key pressed in column 3<\/p>\n<p>&nbsp;<\/p>\n<p><strong>2.1 Steps to find out key pressed<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The following are the steps to find out how the key is pressed in the keyboard<\/p>\n<p>&nbsp;<\/p>\n<p>\u25cf\u00a0\u00a0\u00a0\u00a0 Beginning with the row 0, the microcontroller grounds it by providing a low to row D0 only.<\/p>\n<p>&nbsp;<\/p>\n<p>\u25cf\u00a0\u00a0\u00a0\u00a0 It reads the columns(port2). If the data read is all 1s, no key in that row is activated and the process is moved to the next row.<\/p>\n<p>&nbsp;<\/p>\n<p>\u25cf\u00a0\u00a0\u00a0\u00a0 It grounds the next row, reads the columns, and checks for any zero.<\/p>\n<p>&nbsp;<\/p>\n<p>\u25cf\u00a0\u00a0\u00a0\u00a0 This process continues until the row is identified.<\/p>\n<p>&nbsp;<\/p>\n<p>\u25cf\u00a0\u00a0\u00a0\u00a0 After identification of the row in which the key has been pressed, find out which column the pressed key belongs to by looking for a zero on it.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>(a)\u00a0 If D3 \u2013 D0 = 1101 for the row, D3 \u2013 D0 = 1011 for the column, then the key located at row 1 and column 3, (key 6) is pressed.<\/p>\n<p>&nbsp;<\/p>\n<p>(b)\u00a0 If D3 \u2013 D0 = 1011 for the row, D3 \u2013 D0 = 0111 for the column, then the key located at row 2 and column 3, (key \u2018B\u2019) is pressed.<\/p>\n<p>&nbsp;<\/p>\n<p>The Figure 5 shown below gives the interfacing of keyboard with 8051.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-258 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-144.png\" alt=\"\" width=\"685\" height=\"440\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-144.png 685w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-144-300x193.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-144-65x42.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-144-225x145.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-144-350x225.png 350w\" sizes=\"auto, (max-width: 685px) 100vw, 685px\" \/><\/p>\n<div>\n<p><strong>2.2 Keyboard interfacing program in C<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The example 2 given below describes how to read the input from keyboard and send the result to serial port. For this 9600 baud rate is used.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 2<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Write a C program to read the keypad and send result to first serial port P1.0 to P1.3 connected to rows, and P2.0 to P2.3 connected to columns. Configure serial port for 9600 baud, 8 data bits and one stop bit.<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-259 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-145.png\" alt=\"\" width=\"631\" height=\"502\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-145.png 631w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-145-300x239.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-145-65x52.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-145-225x179.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-145-350x278.png 350w\" sizes=\"auto, (max-width: 631px) 100vw, 631px\" \/><\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-260 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-146.png\" alt=\"\" width=\"592\" height=\"759\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-146.png 400w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-146-234x300.png 234w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-146-65x83.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-146-225x289.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-146-350x449.png 350w\" sizes=\"auto, (max-width: 592px) 100vw, 592px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-261 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-147.png\" alt=\"\" width=\"580\" height=\"501\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-147.png 580w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-147-300x259.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-147-65x56.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-147-225x194.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-147-350x302.png 350w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This section described about LCD and keyboard interfacing with 8051.The following section describes interfacing of 8051 with external ROM.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>3.8051 Interfacing with External ROM<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The reason for interfacing 8051 with external ROM is that it has a limited amount of on-chip ROM. Hence for sufficient memory allocation 8051 is interfaced with external ROM. CPU gives the address of the data required. Decoding circuit has to locate the selected memory block. Memory chips have CS (chip select) pin, which must be activated for the memory\u2019s contents to be accessed.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>EA pin:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Connect the EA pin to Vcc to indicate that the program code is stored in the microcontroller on-chip ROM. To indicate that the program code is stored in external ROM, this pin must be connected to GND. The figure 6 shown below gives the details of 8051 pin diagram.<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-262 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-148.png\" alt=\"\" width=\"425\" height=\"461\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-148.png 425w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-148-277x300.png 277w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-148-65x71.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-148-225x244.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-148-350x380.png 350w\" sizes=\"auto, (max-width: 425px) 100vw, 425px\" \/><\/p>\n<div>\n<p><strong>3.1 Steps to be followed when connecting external memory<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The following are the steps to follow when connecting to external memory:<\/p>\n<p>&nbsp;<\/p>\n<p>1.\u00a0\u00a0\u00a0 CPU data bus is connected directly to the data pins of the memory chip.<\/p>\n<p>&nbsp;<\/p>\n<p>2.\u00a0\u00a0\u00a0 Control signals RD (read) and WR (memory write) from the CPU are connected to the OE (output enable) and WE (write enable) pins of the memory chip.<\/p>\n<p>&nbsp;<\/p>\n<p>3.\u00a0\u00a0\u00a0 The lower bits of the address from the CPU is connected directly to the memory chip of the address pins.<\/p>\n<p>&nbsp;<\/p>\n<p>4.\u00a0\u00a0\u00a0 The upper ones are used to activate the CS pin of the memory chip.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>P0 and P2 role in providing addresses:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The PC (program counter) of the 8031\/51 is 16-bit, it is capable of accessing up to 64K bytes of program code. 16-bit address is provided by port 0 and port 2 to access external memory. P0 provides the lower 8-bit address A0 \u2013 A7 whereas P2 provides the upper 8-bit address A8 \u2013 A15. P0 is also used to provide the 8-bit data bus D0\u2013D7. P0.0 \u2013 P0.7 are used for both the address and data paths (address\/data multiplexing), to extract the addresses from the P0 pins. It will connect to 74LS373 D Latch.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>ALE (Address Latch Enable):<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">ALE (address latch enable) pin is an output pin for 8031\/51. If ALE = 0, P0 is used for data path, whereas if ALE = 1, P0 is used for address path. Separation of address and data can be obtained by using 74LS373. The Figure 7 shown below gives the specifics about 74LS373 D\u00a0<span style=\"text-align: initial;font-size: 1em\">latch.<\/span><\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-263 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-149.png\" alt=\"\" width=\"334\" height=\"355\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-149.png 334w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-149-282x300.png 282w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-149-65x69.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-149-225x239.png 225w\" sizes=\"auto, (max-width: 334px) 100vw, 334px\" \/><\/p>\n<p>The Data, Address, Control Buses for the 8031\/8051 are shown in Figure 8 below.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-264\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-150.png\" alt=\"\" width=\"756\" height=\"373\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-150.png 756w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-150-300x148.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-150-65x32.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-150-225x111.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-150-350x173.png 350w\" sizes=\"auto, (max-width: 756px) 100vw, 756px\" \/><\/p>\n<p><strong>8051 connection to external data RAM:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>8051 connection to external data RAM is shown in Figure 9 below.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-265 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-151.png\" alt=\"\" width=\"720\" height=\"375\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-151.png 720w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-151-300x156.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-151-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-151-225x117.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-151-350x182.png 350w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/p>\n<p><strong>A single ROM for both program and data:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">For certain applications we need program ROM, Data ROM and Data RAM in a system. To allow a single ROM chip to provide both program code space and data code space, we use an AND gate to signal the OE pin of the ROM chip. The figure 10 shown below gives the details about connection of 8051 with single ROM for both program and data.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-266 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-152.png\" alt=\"\" width=\"660\" height=\"258\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-152.png 660w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-152-300x117.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-152-65x25.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-152-225x88.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-152-350x137.png 350w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/p>\n<div>\n<p><strong>3<\/strong>.<strong>2 External memory interfacing program in C<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In some applications, we need a large amount of memory to store data. However, the 8051 supports only 64K bytes of external data memory,since DPTR is 16-bit. To solve this problem, we connect A0 &#8211; A15 of the 8051 directly to the external memory\u2019s A0 &#8211; A15 pins and use some of the P1 pin to access this 64K bytes block inside the single 256K X 8 memory chip.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 1em;text-align: initial\">Example 3 shown below describes how to store ASCII letters to external RAM address starting at 0. It then gets data from external RAM and sends it to PI, one byte at a time.<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>Example 3:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-267 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-153.png\" alt=\"\" width=\"634\" height=\"436\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-153.png 634w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-153-300x206.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-153-65x45.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-153-225x155.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-153-350x241.png 350w\" sizes=\"auto, (max-width: 634px) 100vw, 634px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Example 4 given below describes how to write the C program to read 30 bytes of data from an external ROM and send it to port P1. An external ROM uses 8051 data space to store the value in lookup table for the DAC data.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 4<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-268 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-154.png\" alt=\"\" width=\"672\" height=\"248\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-154.png 672w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-154-300x111.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-154-65x24.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-154-225x83.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-154-350x129.png 350w\" sizes=\"auto, (max-width: 672px) 100vw, 672px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Example 5 given below describes how to write the C program to move the message into\u00a0<span style=\"font-size: 1em;text-align: initial\">external RAM and read the same in RAM and send it to serial port. For this SFR register is used to declare the new addresses and timer 1 is used for baud rate generation.<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>Example 5<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Example 6 given below describes how to write the C program to access 1KB SRAM of the DS89C4XO chip. But the ASCII letters are stored before itself in SRAM, for which SBUF register is used. For this SFR register is used to declare the new addresses and timer 1 is used for baud rate generation.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 6<\/strong><\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-269 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-155.png\" alt=\"\" width=\"702\" height=\"481\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-155.png 702w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-155-300x206.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-155-65x45.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-155-225x154.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-155-350x240.png 350w\" sizes=\"auto, (max-width: 702px) 100vw, 702px\" \/><\/p>\n<ol start=\"4\">\n<li><strong> Summary<\/strong><\/li>\n<\/ol>\n<p style=\"text-align: justify\">In this lecture, basics of interfacing of 8051 to external devices are discussed. The Interfacing of 8051 to external memory has also been discussed. Interfacing of 8051 with external devices in Embedded C is discussed in detail with examples.<\/p>\n<p>&nbsp;<\/p>\n<ol start=\"5\">\n<li><strong>References<\/strong><\/li>\n<\/ol>\n<ol>\n<li>Muhammad Ali\u00a0 Mazidi,\u00a0 Janice\u00a0 Gillispie\u00a0 Mazidi,\u00a0 Rolin\u00a0 D.\u00a0 McKinlay,\u00a0 \u201cThe\u00a0 8051 Microcontroller and Embedded Systems Using Assembly and C -Second Edition\u201d, New Delhi(2000).<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n","protected":false},"author":2,"menu_order":21,"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-249","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\/249","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":3,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/chapters\/249\/revisions"}],"predecessor-version":[{"id":270,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/chapters\/249\/revisions\/270"}],"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\/249\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/wp\/v2\/media?parent=249"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/chapter-type?post=249"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/wp\/v2\/contributor?post=249"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/wp\/v2\/license?post=249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}