Вы находитесь на странице: 1из 13

Microcontrollers Lab

NAVIGATE

LCD INTERFACING WITH PIC16F877A MICROCONTROLLER

HomepagePic Microcontroller Tutorials

LCD INTERFACING WITH PIC MICROCONTROLLER,In this tutorial, you will learn to interface an LCD with a
microcontroller. It is a very simple and easy to understand project for the beginners and is commonly
used in several electronic products. LCD (Liquid Crystal Display)provides user interface and can be very
useful for debugging purpose. After completion of this tutorial, you will be able to display data on an
LCD using mikro C compiler. You should know how to use microcontroller and how to program pic
microcontroller. You should also check how to use Mikro c for pic, if you have a beginner in Mikro c for
pic programming.

Table of Contents

TASK of LCD interfacing with pic microcontroller

LCD Display Commands in Mikro c for pic

CIRCUIT DIAGRAM OF LCD INTERFACE WITH PIC16F877A microcontroller

LCD Connections with pic microcontroller

C-CODE FOR LCD INTERFACE WITH PIC MCU

LCD display with cursor position control USING PIC MICROCONTROLLER

TASK of LCD interfacing with pic microcontroller

To interface LCD with PIC16F877A and display the text ‘LCD INTERFACE’ on it. LCDs come in different
sizes and shapes. For this project, we have selected a 16×2 character, alphanumeric LCD. It contains 2
rows of 16 character.

Its pin configuration is given as follows:

Pin Number Name Use

1 Vss Ground
2 Vdd Power

3 Vee To adjust the contrast

4 RS 1=Data input

0=Instruction input

5 R/W 1=Read from LCD

0=Write to LCD

6 Enable (EN) From 1 to 0 = Data is written to the LCD

7 DB0 Data Bus Lines

8 DB1

9 DB2

10 DB3

11 DB4

12 DB5

13 DB6

14 DB7

15 LED+ Backlight

16 LED-

The LCD can work in two modes, 4-bit and 8-bit. In this tutorial, we have used the 4-bit mode which uses
only 4 data lines, thus saving pins of the microcontroller. So It is recommended to use LCD in four bits
mode to save pins of the microcontroller for other purposes.

LCD Display Commands in Mikro c for pic

We have used 16×2 LCD which means there are 2 rows and 16 characters in each row. So we can display
a total of 32 characters at a time in two rows with 16 characters in each row.

Void Lcd_Out (char row, char column, char*text);


This is the main command which prints our text on LCD. It also gives the privilege to specify the position
of text. In horizontal direction we count rows number and in vertical direction we count the column
number. In above command

row specifies the starting position of text in row. We specify the number of row like 1 or 2 according to
our requirement in which row we want to print text.

column specifies the starting position of text in column. We specify the number of column like
(1,2,3,..16) according to our requirement in which row we want to print text.

text specifies the text we want to display on LCD

For example the command Lcd_Out (1, 1, “LCD”); will print the text LCD starting from position of row 1
and column 1.

Lcd_Out (2, 1, “LCD”); will print the text LCD starting from position of row 2 and column

Lcd_Out (1, 5, “LCD”); will print the text LCD starting from position of row 1 and column 5.

However if your string is longer than the number of characters that could be displayed in a row from
starting position, the last characters will not be displayed. E.g. Lcd_Out (1, 6 “LCD Interface”); will display
text in row 1 starting from column position 6 and will display only LCD Interfa the rest of the characters
will not be displayed as there is no room for them.

void Lcd_Out_Cp(char *text); will start printing the text from current cursor position. For example after
printing Lcd_Out (1, 1, “LCD”); if you write Lcd_Out_Cp(“Hello”); it will display “Hello” at position from
column position of 4 in row 1.

void Lcd_Chr(char row, char column, char out_char); allows only single characters to be displayed at
specified positions. E.g. Lcd_Chr(2, 5, ‘A’); will print only A on column 5 row 2.

void Lcd_Chr_Cp(char out_char); allows to print only single character from current cursor position like
after Lcd_Chr(2, 5, ‘A’); if your write Lcd_Chr_Cp(‘B’); it will be printed at row 2 column 6.

CIRCUIT DIAGRAM OF LCD INTERFACE WITH PIC16F877A microcontroller


circuit diagram of LCD interfacing with PIC16F877A microcontroller

circuit diagram of LCD interfacing with PIC16F877A microcontroller

LCD Connections with pic microcontroller

Connect pin1 of LCD to ground and pin2 to Vdd.

Pin3 of LCD is used to adjust the contrast of the display. Leave it unconnected.

RS (pin4) of LCD is connected to RB2 of PORTB. It distinguishes between data input and command input.

RW (pin5) is grounded, since we have to write the data on LCD.

E (pin6) is connected to RB3 of PORTB. It is a control line to inform the LCD that data has been sent.

D0-D3 of the LCD are grounded, since we are using a 4-bit mode. D4-D7 are connected to the PORTB
pins (RB4-RB7) of the controller.

C-CODE FOR LCD INTERFACE WITH PIC MCU

Write the following code in mikroC Compiler

/* LCD INTERFACING WITH PIC16F877A */

// LCD Module connections

sbit LCD_RS at RB2_bit;

sbit LCD_EN at RB3_bit;

sbit LCD_D7 at RB7_bit;


sbit LCD_D6 at RB6_bit;

sbit LCD_D5 at RB5_bit;

sbit LCD_D4 at RB4_bit;

// End LCD module connections

// LCD Pin direction

sbitLCD_RS_Direction at TRISB2_bit;

sbitLCD_EN_Direction at TRISB3_bit;

sbit LCD_D7_Direction at TRISB7_bit;

sbit LCD_D6_Direction at TRISB6_bit;

sbit LCD_D5_Direction at TRISB5_bit;

sbit LCD_D4_Direction at TRISB4_bit;

// End of LCD Pin direction


void main() {

Lcd_Init();// Initialize LCD

Lcd_Cmd(_LCD_CLEAR);// Clear Display

Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor Off

Lcd_Out(1,1,"LCD INTERFACE");// Write "LCD INTERFACE" in the first row

When using PIC microcontroller, the mikroC compiler has a built-in LCD library that supports the
commands to carry out LCD initialization. The library consists of a number of functions to control LCDs
with 4-bit data interface.

The main program first clears the LCD screen and then displays “LCD INTERFACE” in the first row of LCD.
The LCD pin directions are all set as outputs. The RS pin of LCD is set to 1, which indicates that the
information received from DB4-DB7 is a valid text to be printed on LCD screen. The EN pin is also set to 1
which indicates that data is send to the LCD.

LCD display with cursor position control USING PIC MICROCONTROLLER

This code will be used to display characters on cursor location and specific location on LCD.

// LCD Module connections

sbit LCD_RS at RB1_bit; // it can only access a pin not a port

//sbit LCD_RS at LATB1_bit; //same as "sbit LCD_RS at RB1_bit;"


//sbit LCD_RS at LATB.B1; //SAME AS "sbit LCD_RS at RB1_bit;"

//sbit LCD_RS at LATB;

//sbit LCD_RS at PORTB1_bit;

//sbit LCD_RS at PORTB.B1;

//sbit LCD_RS at PORTB;

sbit LCD_EN at RB0_bit;

sbit LCD_D7 at RB5_bit;

sbit LCD_D6 at RB4_bit;

sbit LCD_D5 at RB3_bit;

sbit LCD_D4 at RB2_bit;

// End LCD module connections

// LCD Pin direction

sbit LCD_RS_Direction at TRISB1_bit;

sbit LCD_EN_Direction at TRISB0_bit;

sbit LCD_D7_Direction at TRISB5_bit;

sbit LCD_D6_Direction at TRISB4_bit;

sbit LCD_D5_Direction at TRISB3_bit;

sbit LCD_D4_Direction at TRISB2_bit;

// End of LCD Pin direction

void main()

ANSELH = 0X00;

TRISB=0X00;

PORTB=0X00;

Lcd_Init();// Initialize LCD


Lcd_Cmd(_LCD_CLEAR);// Clear Display

Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor Off

while (1)

Lcd_Out(1,6,"LCD");// Write “LCD INTERFACE” in the first row

Delay_ms(1000);

Lcd_Out_Cp("Hello");

Delay_ms(1000);

Lcd_Chr(2, 7, 'i');

Delay_ms(1000);

Lcd_Chr_Cp('A');

Delay_ms(1000);

Lcd_Cmd(_LCD_RETURN_HOME);

Delay_ms(1000);

Lcd_Chr_Cp('B');

Delay_ms(1000);

Lcd_Cmd(_LCD_CURSOR_OFF) ;

Lcd_Chr_Cp('C');

Delay_ms(1000);

Lcd_Cmd(_LCD_UNDERLINE_ON) ;

Lcd_Chr_Cp('D');

Delay_ms(1000);

Lcd_Cmd(_LCD_BLINK_CURSOR_ON) ;

Lcd_Chr_Cp('E');
Delay_ms(1000);

Lcd_Cmd(_LCD_TURN_OFF) ;

Lcd_Chr_Cp('F');

Delay_ms(1000);

Lcd_Cmd(_LCD_TURN_ON) ;

Lcd_Chr_Cp('G');

Delay_ms(1000);

Lcd_Cmd(_LCD_SHIFT_LEFT) ;

Delay_ms(1000);

Lcd_Cmd(_LCD_CLEAR);

Delay_ms(1000);

APPLICATIONS

Programmed LCDs are vastly used for industrial as well as commercial applications. LCDs are used in
UPSs or inverters, where voltage and current readings are displayed on the screen. Instructions to be
followed are displayed on an LCD screen in airports, banks, hospitals, etc. If you still have any issue after
reading this article, feel free to comment on this post with your issues.

KEYPAD INTERFACING WITH PIC16F877A MICROCONTROLLER »

« LED BLINKING WITH PIC16f877A MICROCONTROLLER

View Comments (28)

gökberk says:November 27, 2019 at 9:15 pm


Hi sir,In case of resistance is out of range, " the system must give a warning as well" how can add this
part to the code? help please

satya says:October 11, 2019 at 6:56 pm

i am interfacing LCD 12864 (128x64)and i give the text but it displays only few seconds after there is no
text was displayed please help me in displayin senter of the screen and life span of the text .

satya says:September 18, 2019 at 5:59 pm

how to cantrollbrightness in 16*2 led display with the arduino.uno.

M.Nasim Khaliqhey says:June 1, 2019 at 2:41 pm

Dear: Do mean I should send a copy of component charter softwate that I have been using?

Wajahat Shah says:January 30, 2019 at 3:07 pm

Dear SIr,

with due respect it is stated that i need a code of pic micro for interfacing LCD 16*2 with PIC Micro

Greg Faxon says:January 11, 2019 at 10:09 am

I have been looking for information about LCDs. What I am trying to do is program a LCD read out for a
test vehicle I am working on. What I need is to preprogramed LCD readout that when an input is put to it
will simply go and display the readout. Actually real simple system, but I do not have the knowledge or
know where to start. Example would be like if the Oil Pressure unit is energized, so a positive 12vdc
would then turn on the written display and state that the pressure system is having a problem, or
something like that. Same as if fuel is low it will give me a readout. So I will also need to build a voltage
system that could be used over from 0 to lets say 13.4 dcv. The main problem is designing the readout,
preprogramed readout, where all it needs is a simple input to make it operate. If you could be of any
help it would be appreciated. Thank You, Greg

SPS Rathore says:September 14, 2019 at 9:29 am


It is possible and you need a circuit to sense different pressure levels, take that data as input. Keep your
preset levels in memory and compare the same. when your input data matches the preset level trigger
the display message on your LCD. Message could be anything you want within the range of 32
characters.

Kasun says:October 1, 2018 at 12:16 pm

Hi Dear Bilal,

Is there anyway to connect two LCD displays to one PIC16F877A and show different text in those
displays...?

sreejith says:February 1, 2018 at 12:08 pm

sir i want to lesson about micro controller programming

Nasim Khaliqhey says:September 3, 2017 at 7:35 pm

hi dear Bilal

I am using PIC16 F877A with drive frequency 8MHz crystal

I have calculated the delay timing as fallow

65536-((7*8000000)/(65536*4)) that gives me= 653 . for 7 secs result

when I get the LED s to drive by NPN transistor the out put volt dose n´t have enough current to drive

I tried with a NAND gate IC by entering the PIC outputs the inputs of NAND gate IC, it turns ON the NPN
transistor

but switching ON time is not Ending.should I use Higher drive frequency crystals or....?

wr Nasim

dare says:August 11, 2017 at 5:43 pm

i tried this not working help


sbit LCD_RS at RE0_bit;

sbit LCD_EN at RE2_bit;

sbit LCD_D4 at RD4_bit;

sbit LCD_D5 at RD5_bit;

sbit LCD_D6 at RD6_bit;

sbit LCD_D7 at RD7_bit;

sbit LCD_RS_Direction at TRISE0_bit;

sbit LCD_EN_Direction at TRISE2_bit;

sbit LCD_D4_Direction at TRISD4_bit;

sbit LCD_D5_Direction at TRISD5_bit;

sbit LCD_D6_Direction at TRISD6_bit;

sbit LCD_D7_Direction at TRISD7_bit;

// End LCD module connections

void main()

Lcd_Init(); // Initialize LCD

Lcd_Cmd(_LCD_CLEAR); // Clear display

Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off

Lcd_Out(1,1,"Hello World"); // Write text'Hello World' in first row

1 2 3 Next »
Leave a Comment

Related Post

Rotary Encoder Module interfacing with pic16f877a microcontroller

Sound detection module interfacing with PIC Microcontroller

AC load interfacing with pic microcontroller

DHT11 sensor interfacing with pic microcontroller

input output ports of dspic30f2010 microcontroller

Heart beat pulse sensor interfacing with pic microcontroller

View Non-AMP Version All Rights Reserved

whatsapp

Вам также может понравиться