INTERFACING 16X2 LCD with PIC16F877 microcontroller




This Post will cover interfacing of 16X2 character LCD with PIC16F series microcontroller to display a message "HELLO NIGERIA SL WELCOME YOU". PIC16F877 is the most preferred microcontroller among hobbyist because of its wide range of features available at less cost. HD44780 is a commonly used 16X2 character LCD because it is easy to interface and compatible with various microcontrollers. so, in this tutorial we are going to interface HD44780 LCD module with PIC16F877 micrcontroller. Before Proceeding, read the datasheet of PIC16F877 to have a overview on it PIC16F877 .

HD44780 - Description 

HD44780 has two lines of display with 16 characters per line. Each character is displayed in the 5X7 matrix block.It has 14 pins for interfacing with external device and it has two 8 bit register namely, instruction register and data register to store and execute the instruction code and data transfer. Some LCD's have extra two pins LED+ and LED- for backlight power adjustments.


  •     Register Select (RS) - Logic '0' on RS pin is used to select instruction register and Logic '1' for data register.
  •     Read/Write (R/W) - Logic '0' to write in LCD module and Logic '1' to read from LCD module.
  •     Enable (E) - Logic '0' to disable LCD module and Logic '1' will enable it. Enable acts as the trigger for LCD operations.
  •     Data (D0 -D7) - Eight data lines for data transfer between LCD and microcontroller.
  •     Vdd & Vss - Power supply for LCD Module(+5V & GND).
  •     Vee - Contrast Adjustment of Display. It is connected through potentiometer to vary the voltage depending on contrast.

LCD Registers


    Instruction Register - IR stores the instruction code also called as LCD commands such as clear screen,cursor blink,cursor shift,etc., and address information for DDRAM and CGRAM
    Data Register - DR stores the data to be read from and write in DDRAM and CGRAM.

LCD Memory


    Display Data RAM (DDRAM) - It acts as the data buffer for the display. It stores the data to be displayed in LCD in the form of ASCII values. Each position in the LCD display has corresponding address location in the DDRAM. (0x80 - 0x8F) represents the address location for first line and (0xc0 - 0xcF) for second line.
    Character Generation ROM (CGROM) - The standard character pattern for 5X7 matrix to the corresponding ASCII Value is stored in CGROM. For example, ASCII Value '65' on certain position in DDRAM will display the character 'A' becuase of standard character pattern 'A' is stored in CGROM for ASCII Value '65'.
    Character Generation RAM (CGRAM) - It allows to create custom character by redefining patterns in CGRAM corresponding address locations.

LCD Instructions

For LCD commands to execute, the instructions has to be stored and processed from instruction register hence RS pin should be low for LCD commands. To write LCD commands, R/W should also be low. The 8-bit values on D0-D7 expresses the LCD commands. Enable E should be triggered ON & OFF to execute the LCD commands. The above table shows various LCD commands normally used in LCD operations.


                                        LCD interfacing with PIC16F877A
Here I will interface one of the simple LCD, whose type is DMC and LCD is ( 2*16 ). 2*16 means, 2 lines and in each line at most you can display 16 characters, so all together you can display 32 characters at a time.
Note: One thing must be noted that LCD understand only ASCII( American Standard code for Information Interchange) format. So it is must important you provide data for display in ASCII format, for this you can provide direct ASCII code in your program or provide data in string format eg. LCD_dispaly(“HELLO NIGERIA SL WELCOME YOU”).


2* 16 LCD has got 16 pin, so let me describe each function of LCD.
Pin Description:
1.   Vss -GND
2. Vcc- +5V DC Supply
3. Vee - Control LCD Contrast
4. RS- Register Select
5. R/W- Read /Write
6. EN- Enable
7. D0- 8 bit data lines
8. D1
9. D2
10. D3
11. D4
12. D5
13. D6
14. D7
15. Vcc ( + led for back light)
16. Vss ( - led for back light)
RS-0      Command Read(Byte provided to LCD will be process as a command)
RS- 1     Data Read Byte provided to LCD will be process as a data for display purpose )
R/W:     0, WR operation 
R/W:    1, RD operation
EN:        for enable provide high to low logic.

 Some of the important command for initialization , carriage return, clear screen  for LCD etc. 

  • 38h  Set up 2lines (Matrix )
  • 01h  Clear Screen
  • 06h  Right Shifting(Cursor)
  • 0eh  Display on Cursor not blinking
  • 0fh  Display on Cursor blinking
  • 80h  Force cursor to 1st line
  • C0h Force cursor to 2st line
  • 02h  Return home
  • 04h  Decrement cursor (shift cursor to left)

 

 Coding is done in Embedded C using Compiler Hi-TechC on MPLAB IDE 




Embedded C Code


// Name : Interfacing LCD in 8bit mode
// Author : sltech
// Website : www.sltechservices.com.ng


Program for interfacing LCD in 8-bit format for displaying message

 “ HELLO NIGERIA SL WELCOMES YOU “ in LCD.


#include<pic.h>

#include<htc.h>

void delay();

void delay1();

void lcd_data(unsigned char);

void lcd_com(unsigned char );

void comn_data(unsigned char,unsigned char *);

void lcd_ini();

#define lcd PORTB

#define rs RC0

#define rw RC1

#define en RC2

void main()

{

  TRISB=TRISC=0x00;

        lcd_ini();

        comn_data(0x80,"HELLO NIGERIA");

        comn_data(0xc4,"SL WELCOMES YOU");

        while(1);

}

void lcd_ini()

{      lcd_com(0x38);

          lcd_com(0x0e);

     lcd_com(0x01);

 lcd_com(0x06);

lcd_com(0x80);}


void lcd_com(unsigned char com)

{ lcd=com;

 rs=0;

 rw=0;

en=1;

 delay();

en=0;

 delay1();}

void lcd_data(unsigned char dat){

    lcd=dat; rs=1; rw=0; en=1;delay(); en=0;delay1();

}


void comn_data(unsigned char com ,unsigned char *dat)

{ lcd_com(com);

 while(*dat)  {

   lcd_data(*dat++);}      }

void delay(){

  unsigned char i;

   for(i=0;i<200;i++);

}

void delay1()

{   unsigned int i,j;

   for(i=0;i<60000;i++);

}// program end here.






SNAPSHOT OF PROGRAM RUNNING ON PROTEUS
 













1 comment: