Simple 8 bit 16x2 LCD interfacing with PIC 16F

In this tutorial you will learn how to interface a simple 8-bit 16x2 LCD with PIC 16F.

 

 

LCD PIN OUTS

 

 

(From left to right)

  • Vss- Ground
  • VDD- Power 5V
  • VEE- LCD Contrast Adjustment

 

LCD Pin Configuration

LCD

Control Signals

 

RS- Register Select

There are 2 very important registers in LCD

  • Command Code register
  • Data Register

If

RS=0 à Instruction command Code register is selected, allowing user to send command

RS=1 à Data register is selected allowing to send data that has to be displayed.

 

R\W- Read\Write

R\W input allows the user to write information to LCD or read information from it. How do we read data from LCD????? The data that is being currently displayed will be stored in a buffer memory DDRAM. This data could be read if necessary.

If

R\W=0 à Reading

R\W=1 à Writing

 

E- Enable

The enable Pin is used by the LCD to latch information at its data pins. When data is supplied to data pins, a high to low pulse must be applied to this pin in order for the LCD to latch the data present in the data pins.

Eà Toggle

 

Data Bus- D0-D7

Power for Backlighting LEDs

VDD-Power 5V

Vss- GND

 

General Explanation

We have to prepare an LCD properly before the character we need, has to be displayed. For this a number of commands have to be provided to the LCD before inputting the required data. The commands will be discussed in the later part of this tutorial.

LCD doesn’t know about the content (data or commands) supplied to its data bus. It is the user who has to specify whether the content at its data pins are data or commands.   For this, if a command is inputted then a particular combination of 0s and 1s has to be applied to the Control lines so as to specify it is a Command on the other hand if a data is inputted at the data lines then an another combination of 0s and 1s has to be applied to the control lines to specify it is Data. (Hope you are not confused!!!!). The combinations are as follows-

If

Command à RS=0, R\W=0, E=1\0

Dataà RS=1, R\W=0, E=1\0

 

 

Various Commands used in LCDs

 

LCD commands

Interfacing with PIC

 

LCD interfacing with PIC

 

Here 2 ports PORT B and PORT C of PIC 16F877A is taken. PORT B is used for providing control signals and PORT C is used for providing Data signals.

Of which pins of PORT B are specified as below.

RB0à RS

RB1à R\W

RB2 à E

 

Programming Steps

  • Before sending Data to be Displayed to the LCD, it should be prepared to hold that particular value.
  • For this certain initializations are to be done as per the Instructions.
    • Move Value 0X38, 3 times.( Applied max 3 times due to rise time factor)
    • Move Value 0X06, 1 time.
    • Move Value 0X0F, 1 time.
    • After each initializations command function and delay should be called.
  • After Initialization Move Data to the LCD
  • Call the Data Function and delay.

 

( 2 functions , one for command and the other for data is being written for the simplicity of the program)

 

Program

 

 

#include"pic.h"

#include"delay.h"

void main()

{

void command(); //function for command manipolation

void data(); //function for data manipulation

int i;

char a[ ]={"bOtskOOl-hari"};

TRISC=0X00; //Made Port C Out put port (to use with in main function)

TRISB=0X00; //Made Port B output port (to use with outer function)

DelayMs(1);

PORTC=0X38; //to select the mode of the LED 0X38 - 8 bit 2 lines

command(); // Call command function, shows that the given values are commands and not datas

DelayMs(1);

PORTC=0X38; //to select the mode of the LED 0X38 - 8 bit 2 lines

command(); // Call command function

DelayMs(1);

PORTC=0X06; // to activate entry mode

command();

DelayMs(1);

PORTC=0X0F; //to get a blinking curser display

command();

DelayMs(1);

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

{

PORTC= a[i];

data(); //To show that the given values are data

DelayMs(1);

}

while(1);

}

 

 

void command() //command function definition

{

RB0=0;

RB1=0;

RB2=1;

DelayUs(1);

RB2=0;

}

 

 

void data()  //data function definition

{

RB0=1;

RB1=0;

RB2=1;

DelayUs(1);

RB2=0;

}

 

 

PIC SIMULATOR IDE VIEW

 

LCD PIC Simulator 

- HARI

[email protected]

www.haripanangad.co.cc 

Comments

Member since:
13 December 2012
Last activity:
1 year 36 weeks

 Hi i try to write your code successfully , i want create my own application for lcd , i have just a question : how can i set line 2? you repeat just 2 times 0x38 but some display require 3 times , there is a reason for it? (to me work properly)

Thank's :)

claudio

 

Member since:
10 March 2009
Last activity:
5 years 1 week

Its HITECH PIC C 

Member since:
28 April 2009
Last activity:
4 years 21 weeks

Which compiler have you utilised ?