{"id":203,"date":"2010-11-07T14:53:43","date_gmt":"2010-11-07T19:53:43","guid":{"rendered":"http:\/\/embedded-lab.com\/blog\/?p=203"},"modified":"2012-07-30T09:29:31","modified_gmt":"2012-07-30T13:29:31","slug":"lab-4-interfacing-a-character-lcd","status":"publish","type":"post","link":"https:\/\/embedded-lab.com\/blog\/lab-4-interfacing-a-character-lcd\/","title":{"rendered":"Lab 4: Interfacing a character LCD"},"content":{"rendered":"<p><strong>Description<\/strong><\/p>\n<p>HD44780 based LCD displays are very popular among hobbyists because they are cheap and they can display characters. Besides they are very easy to interface with microcontrollers and most of the present day high-level compilers have in-built library routines for them. Today, we will see how to interface an HD44780 based character LCD to a PIC16F688 microcontroller. The interface requires 6 I\/O lines of the PIC16F688 microcontroller: 4 data lines and 2 control lines. A blinking test message, &#8220;Welcome to Embedded-Lab.com&#8221;, will be displayed on the LCD screen.<\/p>\n<p><strong>Required Theory<\/strong><\/p>\n<p>All HD44780 based character LCD displays are connected through 14 pins: 8 data pins (D0-D7), 3 control pins (RS, E, R\/W), and three power lines (Vdd, Vss, Vee). Some LCDs have LED backlight feature that helps to read the data on the display during low illumination conditions. So they have two additional connections (LED+ and LED-), making altogether 16 pin. A 16-pin LCD module with its pin diagraam is shown below.<\/p>\n<p style=\"text-align: center;\"><a rel=\"attachment wp-att-771\" href=\"http:\/\/embedded-lab.com\/blog\/?attachment_id=771\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-771 aligncenter\" title=\"LCD Pins\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/LCD-Pins-550x368.jpg\" alt=\"\" width=\"550\" height=\"368\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/LCD-Pins-550x368.jpg 550w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/LCD-Pins.jpg 805w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><\/p>\n<p><!--more--><br \/>\n<em>Control pins<\/em><br \/>\nThe control pin RS determines if the data transfer between the LCD module and an external microcontroller are actual character data or command\/status. When the microcontroller needs to send commands to LCD or to read the LCD status, it must be pulled low. Similarly, this must be pulled high if character data is to be sent to and from the LCD module.<\/p>\n<p>The direction of data transfer is controlled by the R\/W pin. If it is pulled Low, the commands or character data is written to the LCD module. And, when it is pulled high, the character data or status information from the LCD registers is read. Here, we will use one way data transfer, i.e., from microcontroller to LCD module, so the R\/W pin will be grounded permanently.<\/p>\n<p>The enable pin (E) initiates the actual data transfer. When writing to the LCD display, the data is transferred only on the high to low transition of the E pin.<\/p>\n<p><em>Power supply pins<\/em><br \/>\nAlthough most of the LCD module data sheets recommend +5V d.c. supply for operation, some LCDs may work well for a wider range (3.0 to 5.5 V). The Vdd pin should be connected to the positive power supply and Vss to ground. Pin 3 is Vee, which is used to adjust the contrast of the display. In most of the cases, this pin is connected to a voltage between 0 and 2V by using a preset potentiometer.<\/p>\n<p><em>Data pins<\/em><br \/>\nPins 7 to 14 are data lines (D0-D7). Data transfer to and from the display can be achieved either in 8-bit or 4-bit mode. The 8-bit mode uses all eight data lines to transfer a byte, whereas, in a 4-bit mode, a byte is transferred as two 4-bit nibbles. In the later case, only the upper 4 data lines (D4-D7) are used. This technique is beneficial as this saves 4 input\/output pins of microcontroller. We will use the 4-bit mode.<\/p>\n<p>For further details on LCDs, I recommend to read these two articles first from Everyday Practical Electronics magazine : How to use intelligent LCDs <a href=\"http:\/\/lcd-linux.sourceforge.net\/pdfdocs\/lcd1.pdf\" target=\"_blank\"><em><strong>Part 1<\/strong><\/em><\/a>, and <a href=\"http:\/\/lcd-linux.sourceforge.net\/pdfdocs\/lcd2.pdf\" target=\"_blank\"><em><strong>Part 2<\/strong><\/em><\/a>.<\/p>\n<p><strong>Circuit Diagram<\/strong><\/p>\n<p>Data transfer between the MCU and the LCD module will occur in the 4-bit mode. The R\/W pin (5) of the LCD module is permanently grounded as there won&#8217;t be any data read from the LCD module. RC0-RC3 serves the 4-bit data lines (D4-D7, pins 11-14) of the LCD module. Control lines, RS and E, are connected to RC4 and RC5. Thus, altogether 6 I\/O pins of the PIC16F688 microcontrollers are used by the LCD module. The contrast adjustment is done with a 5K potentiometer as shown below. If your LCD module has backlight LED, use a 68? resistance in series with the pin 15 or 16 to limit the current through the LED. The detail of the circuit diagram is shown below.<\/p>\n<p style=\"text-align: center;\"><a rel=\"attachment wp-att-685\" href=\"http:\/\/embedded-lab.com\/blog\/?attachment_id=685\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-685 aligncenter\" title=\"Circuit_LCD_Interface_PIC16F688\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/Circuit_LCD_Interface_PIC16F688-475x450.jpg\" alt=\"\" width=\"475\" height=\"450\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/Circuit_LCD_Interface_PIC16F688-475x450.jpg 475w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/Circuit_LCD_Interface_PIC16F688.jpg 738w\" sizes=\"auto, (max-width: 475px) 100vw, 475px\" \/><\/a><\/p>\n<p style=\"text-align: center;\">\n<p style=\"text-align: left;\">The whole circuit constructed on the breadboard is shown below. If you want to reduce some of the wire connections on the breadboard and save some space, you can first construct my <a href=\"http:\/\/embedded-lab.com\/blog\/?p=574\" target=\"_blank\"><em><strong>PIC16F688 breadboard module<\/strong><\/em><\/a>, and then insert it on the breadboard (shown in the following pictures).<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/SetUp.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-204 aligncenter\" title=\"SetUp\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/SetUp.jpg\" alt=\"\" width=\"550\" height=\"339\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/SetUp.jpg 550w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/SetUp-486x300.jpg 486w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/Img_LCDtest_SetUp_PIC16F688.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-732 aligncenter\" title=\"Img_LCDtest_SetUp_PIC16F688\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/Img_LCDtest_SetUp_PIC16F688-550x380.jpg\" alt=\"\" width=\"550\" height=\"380\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/Img_LCDtest_SetUp_PIC16F688-550x380.jpg 550w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/Img_LCDtest_SetUp_PIC16F688.jpg 580w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><\/p>\n<p style=\"text-align: center;\">LCD interfacing to PIC16F688 Breadboard Module<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/Img_LCDtest_PGM_PIC16F688.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-733 aligncenter\" title=\"Img_LCDtest_PGM_PIC16F688\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/Img_LCDtest_PGM_PIC16F688-550x385.jpg\" alt=\"\" width=\"550\" height=\"385\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/Img_LCDtest_PGM_PIC16F688-550x385.jpg 550w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/Img_LCDtest_PGM_PIC16F688.jpg 580w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><\/p>\n<div id=\"_mcePaste\"><strong>Software<\/strong><\/div>\n<div><strong><br \/>\n<\/strong><\/div>\n<div>The LCD data and control lines are driven through PORTC, so it must be defined as output port (TRISC=0). You need to disable the comparator functions (CMCON0=7), and select all I\/O pins as digital (ANSEL=0). The programming of an HD44780 LCD is a little bit complex procedure as it requires accurate timing and proper sequence of various control signals and commands. But luckily, the MikroC Pro for PIC compiler has in-built library routines to communicate with a standard HD44780 based LCD module using the 4-bit mode. This makes programming a lot easier. Before using the built-in functions for LCD, the connections of LCD pins to the PIC microcontrollers must be defined. The comments are provided in the source code to understand how to use the library routines for LCDs in MikroC Pro for PIC.<\/div>\n<pre><span style=\"color: #003366;\">\/*<\/span><\/pre>\n<pre><span style=\"color: #003366;\">Lab 4: Blink Character Message on LCD<\/span><\/pre>\n<pre><span style=\"color: #003366;\">Internal Clock @ 4MHz, MCLR Enabled, PWRT Enabled, WDT OFF<\/span><\/pre>\n<pre><span style=\"color: #003366;\">Copyright @ Rajendra Bhatt<\/span><\/pre>\n<pre><span style=\"color: #003366;\">October 9, 2010<\/span><\/pre>\n<pre><span style=\"color: #003366;\">*\/<\/span><\/pre>\n<pre><span style=\"color: #003366;\">\/\/ Define LCD module connections.<\/span><\/pre>\n<pre><span style=\"color: #003366;\"> sbit LCD_RS at RC4_bit;<\/span><\/pre>\n<pre><span style=\"color: #003366;\"> sbit LCD_EN at RC5_bit;<\/span><\/pre>\n<pre><span style=\"color: #003366;\"> sbit LCD_D4 at RC0_bit;<\/span><\/pre>\n<pre><span style=\"color: #003366;\"> sbit LCD_D5 at RC1_bit;<\/span><\/pre>\n<pre><span style=\"color: #003366;\"> sbit LCD_D6 at RC2_bit;<\/span><\/pre>\n<pre><span style=\"color: #003366;\"> sbit LCD_D7 at RC3_bit;<\/span><\/pre>\n<pre><span style=\"color: #003366;\"> sbit LCD_RS_Direction at TRISC4_bit;<\/span><\/pre>\n<pre><span style=\"color: #003366;\"> sbit LCD_EN_Direction at TRISC5_bit;<\/span><\/pre>\n<pre><span style=\"color: #003366;\"> sbit LCD_D4_Direction at TRISC0_bit;<\/span><\/pre>\n<pre><span style=\"color: #003366;\"> sbit LCD_D5_Direction at TRISC1_bit;<\/span><\/pre>\n<pre><span style=\"color: #003366;\"> sbit LCD_D6_Direction at TRISC2_bit;<\/span><\/pre>\n<pre><span style=\"color: #003366;\"> sbit LCD_D7_Direction at TRISC3_bit;<\/span><\/pre>\n<pre><span style=\"color: #003366;\">\/\/ End LCD module connection definition<\/span><\/pre>\n<pre><span style=\"color: #003366;\">\/\/ Define Messages<\/span><\/pre>\n<pre><span style=\"color: #003366;\"> char message1[] = \"Welcome to\";<\/span><\/pre>\n<pre><span style=\"color: #003366;\"> char message2[] = \"Embedded-Lab.com\";<\/span><\/pre>\n<pre><span style=\"color: #003366;\"> void main() {<\/span><\/pre>\n<pre><span style=\"color: #003366;\">  ANSEL = 0b00000000; \/\/All I\/O pins are configured as digital<\/span><\/pre>\n<pre><span style=\"color: #003366;\">  CMCON0 = 0x07 ; \/\/ Disbale comparators<\/span><\/pre>\n<pre><span style=\"color: #003366;\">  TRISC = 0b00000000; \/\/ PORTC All Outputs<\/span><\/pre>\n<pre><span style=\"color: #003366;\">  TRISA = 0b00000000; \/\/ PORTA All Outputs, Except RA3<\/span><\/pre>\n<pre><span style=\"color: #003366;\">  Lcd_Init(); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ Initialize LCD<\/span><\/pre>\n<pre><span style=\"color: #003366;\">  do {<\/span><\/pre>\n<pre><span style=\"color: #003366;\">   Lcd_Cmd(_LCD_CLEAR); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ CLEAR display<\/span><\/pre>\n<pre><span style=\"color: #003366;\">   Lcd_Cmd(_LCD_CURSOR_OFF); \u00a0 \u00a0 \u00a0 \u00a0\/\/ Cursor off<\/span><\/pre>\n<pre><span style=\"color: #003366;\">   Lcd_Out(1,4,message1); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0 \/\/ Write message1 in 1st row<\/span><\/pre>\n<pre><span style=\"color: #003366;\">   Lcd_Out(2,1,message2); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ Write message2 in 2nd row<\/span><\/pre>\n<pre><span style=\"color: #003366;\">   Delay_ms(1000);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Wait for 1 sec<\/span><\/pre>\n<pre><span style=\"color: #003366;\">   Lcd_Cmd(_LCD_CLEAR); \u00a0 \u00a0 \/\/ Clear display<\/span><\/pre>\n<pre><span style=\"color: #003366;\">   Delay_ms(1000); \/\/ Wait for 1 sec<\/span><\/pre>\n<pre><span style=\"color: #003366;\">  } while(1);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Infinite Loop <\/span><\/pre>\n<pre><span style=\"color: #003366;\"> }<\/span><\/pre>\n<div><span style=\"color: #003366;\"><br \/>\n<\/span><\/div>\n<div style=\"text-align: right;\"><a href=\"http:\/\/embedded-lab.com\/uploads\/HexFiles\/PIC16F688\/TestLCD.zip\" target=\"_blank\">Download HEX file<\/a><\/div>\n<div><strong>Output<\/strong><\/div>\n<div><strong><br \/>\n<\/strong><\/div>\n<div>After the hex file is loaded into the PIC16F688 microcontroller, and the power is turned on, you will see the message, &#8220;Welcome to Embedded-Lab.com&#8221;, on the LCD display, blinking every second.<\/div>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-734\" title=\"Img_LCDtest_OP_PIC16F688\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/Img_LCDtest_OP_PIC16F688-550x433.jpg\" alt=\"\" width=\"550\" height=\"433\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/Img_LCDtest_OP_PIC16F688-550x433.jpg 550w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/Img_LCDtest_OP_PIC16F688.jpg 586w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Description HD44780 based LCD displays are very popular among hobbyists because they are cheap and they can display characters. Besides they are very easy to interface with microcontrollers and most of the present day high-level compilers have in-built library routines for them. Today, we will see how to interface an HD44780 based character LCD to a PIC16F688 microcontroller. The interface requires 6 I\/O lines of the PIC16F688 microcontroller: 4 data lines and 2 control lines. A blinking test message, &#8220;Welcome to Embedded-Lab.com&#8221;, will be displayed on the LCD screen. Required Theory All HD44780 based character LCD displays are connected through<\/p>\n","protected":false},"author":2,"featured_media":204,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,4,38],"tags":[82,83,9,20],"class_list":["post-203","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-embedded-labs","category-pic-projects","category-pic-tutorials","tag-hd44780","tag-lcd-display","tag-mikroc","tag-pic16f688"],"_links":{"self":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/comments?post=203"}],"version-history":[{"count":38,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/203\/revisions"}],"predecessor-version":[{"id":5838,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/203\/revisions\/5838"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media\/204"}],"wp:attachment":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media?parent=203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/categories?post=203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/tags?post=203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}