Sei sulla pagina 1di 8

Interfacing LCD with PIC Microcontroller MPLAB XC8

B Y LI G O GE O R GE / 77 CO MME N TS

In this tutorial we will see How to Interface a 162 character LCD module with PIC
16F877A Microcontroller using MPLAB X IDE and MPLAB XC8 C Compiler.
162 Character LCD is a very basic and low cost LCD module which is commonly
used in electronic products and projects. 162 means it contains 2 rows that can
display 16 characters. Its other variants such as 161 and 164 are also available in
the market. In these displays, each character is displayed using 58 or 510 dot
matrix.
For controlling LCD using MPLAB XC8 compiler we need to know the hardware of
LCD. These LCDs commonly uses HD44780 compliant controllers. So we need to
learn HD44780 Dot Matrix LCD Controllers Datasheet. Dont worry we already
developed an LCD library including commonly used functions, so you can use it
without any hardware knowledge of LCD.

162 LCD Pin Diagram

First two pins GND and VCC (VSS and VDD) are for providing power to LCD display.
3ed pin VEE is used to control the contrast of the LCD display. A 10K preset whose
fixed ends connected to VDD, VSS and variable end connected to VEE can be used
to control contrast of the LCD. A microcontroller or microprocessor need to send 2
types of information for operating this LCD Module, Data Information and Command
Information. Data Information is the ASCII value of the characters to be displayed in

the LCD screen and Command Information determines other operations such as
position to be displayed, clear screen, shift etc. Data and Command Information are
send to LCD through same data lines (DB0 DB7) which are multiplexed using RS
(Register Select) pin of LCD. When RS is HIGH LCD treats DB0 DB7 data pins
information as Data to be displayed and when it is LOW LCD treats it as Command
Information. Enable (E) input of the LCD is used to give Data Strobe. HIGH (5V)
Voltage Level in the Enable (E) pin tells the LCD that DB0 DB7 contains valid
information. The input signal R/W (Read or Write) determines whether data is written
to or read from the LCD. In normal cases we need only writing hence it is tied to
GROUND in circuit shown below.
The interface between this LCD and Microcontroller can be 8 bit or 4 bit and the
difference between them is in how the data or commands are send to LCD. In the 8
bit mode, 8 bit data and commands are send through the data lines DB0 DB7 and
data strobe is given through E input of the LCD. But 4 bit mode uses only 4 data
lines. In this 8 bit data and commands are splitted into 2 parts (4 bits each) and are
sent sequentially through data lines DB4 DB7 with its own data strobe through E
input. The idea of 4 bit communication is introduced to save pins of a microcontroller.
You may think that 4 bit mode will be slower than 8 bit. But the speed difference is
only minimal. As LCDs are slow speed devices, the tiny speed difference between
these modes is not significant. Just remember that microcontroller is operating at
high speed in the range of MHz and we are viewing LCD with our eyes. Due to
Persistence of Vision of our eyes we will not even feel the speed difference.

Hope that you got rough idea about how this LCD Module works. Actually you need
to read the datasheet of HD44780 LCD driver used in this LCD Module to write a
MPLAB XC8 program for PIC. But we solved this problem by creating a header file
lcd.h which includes all the commonly used functions using 4 bit mode. Just include it
and enjoy.

Circuit Diagram

Interfacing LCD with PIC Microcontroller Circuit Diagram

LCD Library

Lcd_Init() : This function will initialize the LCD Module connected to the
following defined pins.

#define RS RD2
#define EN RD3
#define D4 RD4
#define D5 RD5
#define D6 RD6
#define D7 RD7

These connections must be defined for the proper working of LCD.

Lcd_Clear() : To clear the display.

Lcd_Set_Cursor(int row, int column) : This function is used to set row and
column of the cursor on the LCD screen. By using this function we can change
the position of the character or string displayed by following functions.

Lcd_Write_Char(char) : To write a character to LCD on the current position.

Lcd_Write_String(char *string) : To write string to LCD on the current


position.

Lcd_Shift_Right() : To shift contents on the LCD screen Right once without


changing the data in the Display RAM.

Lcd_Shift_Left() : To shift contents on the LCD screen Left once without


changing the data in the Display RAM.

Note : The Pins to which LCD is connecting should be configured as Output Pins by
writing to TRIS Register.

MPLAB XC8 Code


#define _XTAL_FREQ 8000000

#define RS RD2
#define EN RD3
#define D4 RD4
#define D5 RD5
#define D6 RD6
#define D7 RD7

#include <xc.h>
#include "lcd.h";

// BEGIN CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming
Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM
code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection
off; all program memory may be written to by EECON control)

#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection
off)
//END CONFIG

int main()
{
unsigned int a;
TRISD = 0x00;
Lcd_Init();
while(1)
{
Lcd_Clear();
Lcd_Set_Cursor(1,1);
Lcd_Write_String("LCD Library for");
Lcd_Set_Cursor(2,1);
Lcd_Write_String("MPLAB XC8");
__delay_ms(2000);
Lcd_Clear();
Lcd_Set_Cursor(1,1);
Lcd_Write_String("Developed By");
Lcd_Set_Cursor(2,1);
Lcd_Write_String("electroSome");
__delay_ms(2000);
Lcd_Clear();
Lcd_Set_Cursor(1,1);
Lcd_Write_String("www.electroSome.com");

for(a=0;a<15;a++)
{
__delay_ms(300);
Lcd_Shift_Left();
}

for(a=0;a<15;a++)
{
__delay_ms(300);

Lcd_Shift_Right();
}

Lcd_Clear();
Lcd_Set_Cursor(2,1);
Lcd_Write_Char('e');
Lcd_Write_Char('S');
__delay_ms(2000);
}
return 0;
}

sprintf() to Format Outputs


sprintf() can be used to write formatted string to a variable. It can be used with this
LCD library to format displayed texts. This enables us to display integers and
floating point numbers on the LCD very easily.

Syntax :
sprintf(char * str, const char * format, ...);

Where,

str is the pointer to the buffer to which the resulting string is to be stored.

format is a constant string or null terminated array of characters. Formatting is


done in accordance with this argument.

(additional arguments) indicate a list of additional arguments that may


required for this function depending up on the format string. Each additional
argument contains a value that to be replace a format specifier in the format
string.

Format Specifiers
The generic form of format is %nt. where n is optional which may be 1 9 to
specify number of characters to be outputted or 01 09 to indicate leading zeros
or 1.1 to 9.9 for floating point and %w formats. t is the type which may be one of the
following.

Type

Character

Sting or Character

Unsigned Integer

Signed Integer

Lu Long Unsigned Integer


Ld Long Signed Integer
x

Hex Integer lower case

Hex Integer upper case

Lx

Hex Long Integer lower case

L
X

Hex Long Integer upper case

Float with truncated decimal

Float with rounded decimal

Float in exponential format

Unsigned Integer with decimal place inserted. Specify two numbers for n. The first is a total fie
second is the desired number of decimal places.

Example :
....
float f = 1.414;
int a = 56;
char s[20];
....
....
sprintf(s, "Float = %f", f);
Lcd_Set_Cursor(1,1);
Lcd_Write_String(s);
sprintf(s, "Integer = %d", a);
Lcd_Set_Cursor(2,1);
Lcd_Write_String(s);

This code will make following output on the LCD.

Display Integer and Floating Point Number on LCD

Potrebbero piacerti anche