Lavatory Pros

AN001 APPLICATION NOTE

Interfacing A HD44780 (Model: JHD 162A Series) LCD to a Microchip PIC16F877A
By Lennon Low Kah Weng (HK2006-3782) Date: 24 March 2009

Keywords: HD4480 LCD, PIC16F877A, sample code, interface, display.

1.0 Abstract

In this application note, a detail description about how to interface a HD44780 type LCD (Model: JHD 162A) to a microcontroller (PIC16F877A) is explained. Initially, the general operation of the HD44780 type LCD module is introduced. Then the functions of each of its control lines are explained thoroughly. The dimensions and pin configuration of the LCD module are also included in this application note. Before proceeding into initializing or programming the LCD, a full schematic of how to connect and interface the LCD with the PIC16F877A is shown. A general flow chart for initialization of the LCD is then provided. Finally, this application note concluded with a sample program (how to apply the LCD in project) for the project “Weight Scale Integrated Into Toilet Seat”. In the Appendix section, a sample coding for interfacing the LCD module with the PIC16F877A is provided.

2.0 Introduction

LCD modules are one of the most common and useful device for graphical and alphanumerical display from a system. Even for simple display application purposes, LCD has been preferred over other display devices such as seven segment display because interfacing a LCD is so much simpler nowadays compare to the older days. There is a vast variety of LCD models available in the market today but in this application notes, we will be only focusing on the standard HD44780 type of LCD display. The Model JHD 162A Series LCD is the typical 16 characters x 2 row LCD display module that can be easily found in the market. In this application notes, simple technique to interface the LCD and display characters from the system will be described.

3.0 Objectives

The objective of this application note is to be a general guide for beginner user in interfacing a HD44780 standard LCD to a microcontroller.

4.0 Operation of General HD44780 LCD

In general the HD44780 type LCD module can operates in 2 modes which are the 4-bit data interface mode and 8-bit data interface mode. When operating in 4-bit mode, 2 transfers per character per command are required whereas is it easier to implement in 8-bit mode which requires less program memory but as tradeoff it requires 4 more additional I/O ports. Most common data interface from microcontroller are:
pdfMachine A pdf writer that produces quality PDF files with ease! Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across nearly all Windows platforms, simply open the document you want to convert, click “print”, select the “Broadgun pdfMachine printer” and that’s it! Get yours now!

1

Lavatory Pros
AN001 APPLICATION NOTE

1. Full 8-bit interface 2. 4-bit interface using high nibble of the port 3. 4-bit interface using low nibble of the port.

In this application note, only the 8-bit interface mode will be described which means that 8-bit data bus will be used to connect the microcontroller to the LCD module.

Apart from the 8-bit data bus, the LCD also requires 3 more pins of control lines which are the EN (enable) pin, RS (register select) pin and the R/W (read/write mode) pin to interface with the microcontroller (in this application note, the Microchip PIC16F877A will be used).

5.0 Functions of the Control Lines

The 8 data bus or data lines are referred as DB0 to DB7. As stated earlier the 3 control lines are referred as EN, RS and R/W.

EN is the enable line. It used to send pulses to the LCD module which indicates the start of a transmission data or byte to the LCD module. To indicate the start of transmission, this EN bit is set high and when the transmission is done, this EN bit is set back to low.

RS is the register select line which determines whether the data byte sent to the LCD controller is a command or text data to be display on the LCD screen. When sending command, this RS bit is set low whereas when text data are to be sent, this RS bit is set to high.

Finally the third control line is the R/W line which is the read or write control line. When this line is low, the data on the data bus is written to the LCD module while when this line is high, the LCD can read to check its own status.

Table 5.0 Function Control Line EN Cause data or control state to be latched Rising edge – latching control state (RS and R/W) Falling edge – latching data Register Select Control 0 – Command mode 1 – Data mode Read/Write Control 0 – Read Data 1 – Write Data

RS

R/W

pdfMachine A pdf writer that produces quality PDF files with ease! Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across nearly all Windows platforms, simply open the document you want to convert, click “print”, select the “Broadgun pdfMachine printer” and that’s it! Get yours now!

2

Lavatory Pros
AN001 APPLICATION NOTE

6.0 Dimensions and Pin Configuration of the Model JHD 162A LCD

Figure 6.0: Dimensions of the Model JHD 162A LCD

Pin Configuration: 1 2 3 Vss Vcc Vee

4 RS

5 R/W

6 EN

7 8 9 0 11 12 13 14 DB0 DB1 DB2 DB3 DB4 DB5 DB6 DB7

15 L+

16 L-

7.0 Connecting the HD44780 LCD to the PIC

pdfMachine A pdf writer that produces quality PDF files with ease! Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across nearly all Windows platforms, simply open the document you want to convert, click “print”, select the “Broadgun pdfMachine printer” and that’s it! Get yours now!

Figure 7.0: Connections between LCD and PIC16F877A 3

Lavatory Pros
AN001 APPLICATION NOTE

8.0 Schematic for Interfacing the LCD Module

LCD1
LM016L

B1
5V VSS VDD VEE RS RW E 4 5 6

1 2 3

U1
13 14 OSC1/CLKIN OSC2/CLKOUT RB0/INT RB1 RB2 RB3/PGM RB4 RB5 RB6/PGC RB7/PGD 33 34 35 36 37 38 39 40 15 16 17 18 23 24 25 26 19 20 21 22 27 28 29 30

X1
CRYSTAL

2 3 4 5 6 7 8 9 10

R1
4k7

1

RA0/AN0 RA1/AN1 RA2/AN2/VREF-/CVREF RA3/AN3/VREF+ RA4/T0CKI/C1OUT RA5/AN4/SS/C2OUT RC0/T1OSO/T1CKI RE0/AN5/RD RC1/T1OSI/CCP2 RE1/AN6/WR RC2/CCP1 RE2/AN7/CS RC3/SCK/SCL RC4/SDI/SDA MCLR/Vpp/THV RC5/SDO RC6/TX/CK RC7/RX/DT RD0/PSP0 RD1/PSP1 RD2/PSP2 RD3/PSP3 RD4/PSP4 RD5/PSP5 RD6/PSP6 RD7/PSP7 PIC16F877A PROGRAM=..\..\..\..\LCD TESTING.HEX

Figure 8.0: Schematic for connections between LCD and PIC16F877A using Proteus

9.0 Initialization for the LCD Module

Before we can display text on the LCD screen, the LCD module must be first configured accordingly. Configuring the LCD module is very important as this is the initialization step to start up the LCD module. Initialization is sort of like telling the LCD what we want the LCD module to do. Figure shown below is the general flow chart for initialization of HD44780 type LCD module.

pdfMachine A pdf writer that produces quality PDF files with ease! Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across nearly all Windows platforms, simply open the document you want to convert, click “print”, select the “Broadgun pdfMachine printer” and that’s it! Get yours now!

7 8 9 10 11 12 13 14

D0 D1 D2 D3 D4 D5 D6 D7

4

Lavatory Pros
AN001 APPLICATION NOTE

Figure 9.0: Flow Chart for Initialization of LCD Module

pdfMachine A pdf writer that produces quality PDF files with ease! Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across nearly all Windows platforms, simply open the document you want to convert, click “print”, select the “Broadgun pdfMachine printer” and that’s it! Get yours now!

5

Lavatory Pros
AN001 APPLICATION NOTE

Some of the general commands to configure or initialize the LCD module are shown as below:

10.0 Results

Using 8-bit data mode and the same connections as shown in the schematic above, the following results (display on LCD screen) on the right is obtained. The sample code for interfacing the LCD with the PIC16F877A is included in the Appendix section.

Figure 10.0: Displaying Text on the JHD162A LCD Module
pdfMachine A pdf writer that produces quality PDF files with ease! Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across nearly all Windows platforms, simply open the document you want to convert, click “print”, select the “Broadgun pdfMachine printer” and that’s it! Get yours now!

6

Lavatory Pros
AN001 APPLICATION NOTE

11.0 Sample Program Flowchart

Power On

Initialize display

Display Welcome Message on LCD

Display Height

Input of Height From User Using Keypad

Wait for User To Sit On the Toilet Seat

Measure Weight

Display Weight in 1st Row of LCD Screen

Display BMI in 2nd Row of LCD Screen

End

Figure 11.0: Sample Flow Chart For Project “Weight Scale Integrated Into Toilet Seat”

12.0 Conclusion

The standard HD44780 (Model: JHD 162A Series) LCD module is very suitable for displaying information. As long as configurations and initialization of the LCD module is set-up appropriately, programming of the LCD later using the microcontroller would not be much of a trouble. The source code provided can be easily modifiable to a designers specific application needs. Other display modules/drivers may be implemented with the appropriate modifications.
pdfMachine A pdf writer that produces quality PDF files with ease! Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across nearly all Windows platforms, simply open the document you want to convert, click “print”, select the “Broadgun pdfMachine printer” and that’s it! Get yours now!

7

Lavatory Pros
AN001 APPLICATION NOTE

13.0 Reference

1. Martin Bates. (2006). Interfacing PIC Microcontrollers-Embedded Design By Interactive Simulation. Elsevier. Oxford. UK. 2. Eamon Neary. (2003). AN-645 Application Note. Interfacing an HD44780 Character LCD to a MicroConverter. Analog Devices. 3. Application Note. Displaying Characters On a LCD Character Module. Hantronix Inc. 4. Mark Palmer. (1997). AN587 Application Note. Interfacing PICmicro® MCUs to An LCD Module. Microchip Technology Inc. 5. JHD162A Series Data Sheet 6. Microchip PIC16F87XA Data Sheet

14.0 Appendix A – ASCII Character Code

15.0 Appendix B – Sample Coding in C

//-----------------------------------------------------------------------------------------------------------------------------------------------#include <pic.h> //================================PIC6F877A PIN ASSIGNMENT================================= /*;PIN DES FUNC PIN Des ;1 MCLR Reset Button 40 RB7 DB7 ;2 RA0 39 RB6 DB6 ;3 RA1 38 RB5 DB5
pdfMachine A pdf writer that produces quality PDF files with ease! Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across nearly all Windows platforms, simply open the document you want to convert, click “print”, select the “Broadgun pdfMachine printer” and that’s it! Get yours now!

8

Lavatory Pros
AN001 APPLICATION NOTE

;4 ;5 ;6 ;7 ;8 ;9 ;10 ;11 ;12 ;13 ;14 ;15 ;16 ;17 ;18 ;19 ;20 */

RA2 RA3 RA4 RA5 RE0 RE1 RE2 VDD VSS OSC1 OSC2 RC0 RC1 RC2 RC3 RD0 RD1

5V GND Crystal (20MHz) Crystal (20MHz)

37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21

RB4 RB3 RB2 RB1 RB0 VDD VSS RD7 RD6 RD5 RD4 RC7 RC6 RC5 RC4 RD3 RD2

DB4 DB3 DB2 DB1 DB0 5V GND EN R/w RS

//-----------------------------------------------------------------------------------------------------------------------------------------------__CONFIG ( 0x3F32 ); //configuration for the microcontroller //-----------------------------------------------------------------------------------------------------------------------------------------------#define LCD_RS RD5 #define LCD_RW RD6 #define LCD_EN RD7 #define LCD_DATA PORTB #define write #define read 0 1

//-----------------------------------------------------------------------------------------------------------------------------------------------void init_io (void); void delay(unsigned long data); void send_config(unsigned char data); void send_char(unsigned char data); void lcd_goto(unsigned char data); void lcd_clr(void); void send_string(const char *s); //-----------------------------------------------------------------------------------------------------------------------------------------------void main(void) { //initialize all the ports init_io();
pdfMachine A pdf writer that produces quality PDF files with ease! Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across nearly all Windows platforms, simply open the document you want to convert, click “print”, select the “Broadgun pdfMachine printer” and that’s it! Get yours now!

9

Lavatory Pros
AN001 APPLICATION NOTE

//configure lcd send_config(0b00001000); send_config(0b00000001); send_config(0b00000010); send_config(0b00000110); send_config(0b00001100); send_config(0b00111000); //display startup message lcd_clr(); lcd_goto(0); send_string("Welcome To"); lcd_goto(20); send_string("Lavatory Pros"); delay(2000000); for(;;) { }

//off display //clear display at lcd //lcd return to home //entry mode set, cursor move right, display shift disable //display on, cursor off and cursor blink off //function set

//clear lcd //set the lcd cursor to location 0 //display “Welcome To” //set the lcd cursor to location 20 //display "Lavatory Pros" //delay for certain period

} //-----------------------------------------------------------------------------------------------------------------------------------------------// ==============================SUBROUTINES============================================== void init_io (void) { TRISA = 0b11111111; TRISB = 0b00000000; TRISC = 0b11111111; TRISD = 0b00000000; TRISE = 0b00000001; PORTA = 0b00000000; PORTB = 0b00000000; PORTC = 0b00000000; PORTD = 0b00000000; PORTE = 0b00000000; } //-----------------------------------------------------------------------------------------------------------------------------------------------void delay(unsigned long data) //delay function, the delay time { //depend on the given value for( ;data>0;data--); //Instruction Cycle Time = 1 / (20MHz / 4) = 0.2us per instruction } //-----------------------------------------------------------------------------------------------------------------------------------------------void send_config(unsigned char data) //send lcd configuration { LCD_RS=0; //set lcd to configuration mode pdfMachine 10
A pdf writer that produces quality PDF files with ease! Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across nearly all Windows platforms, simply open the document you want to convert, click “print”, select the “Broadgun pdfMachine printer” and that’s it! Get yours now!

Lavatory Pros
AN001 APPLICATION NOTE

LCD_DATA=data; LCD_EN=1; delay(50); LCD_EN=0; delay(50);

//lcd data port = data //pulse e to confirm the data

} //-----------------------------------------------------------------------------------------------------------------------------------------------void send_char(unsigned char data) //send lcd character { LCD_RS=1; //set lcd to display mode LCD_DATA=data; //lcd data port = data LCD_EN=1; //pulse e to confirm the data delay(10); LCD_EN=0; delay(10); } //-----------------------------------------------------------------------------------------------------------------------------------------------void lcd_goto(unsigned char data) //set the location of the lcd cursor { //if the given value is (0-15) the if(data<16) //cursor will be at the upper line { //if the given value is (20-35) the send_config(0x80+data); //cursor will be at the lower line } //location of the lcd cursor(2X16): else // ---------------------------------------------------------------------------------{ // | |00|01|02|03|04|05|06|07|08|09|10|11|12|13|14|15| | data=data-20; // ||20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35| | send_config(0xc0+data); // ----------------------------------------------------- ----------------------------} } //-----------------------------------------------------------------------------------------------------------------------------------------------void lcd_clr(void) //clear the lcd { send_config(0x01); delay(600); } //-----------------------------------------------------------------------------------------------------------------------------------------------void send_string(const char *s) //send a string to display in the lcd { while (s && *s)send_char (*s++); } //------------------------------------------------------------------------------------------------------------------------------------------------

pdfMachine A pdf writer that produces quality PDF files with ease! Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across nearly all Windows platforms, simply open the document you want to convert, click “print”, select the “Broadgun pdfMachine printer” and that’s it! Get yours now!

11

Sign up to vote on this title
UsefulNot useful