You are on page 1of 11

EXPERIMENT 09

Interfacing Keypad and LCD with PIC18F452


Objective:
 Familiarizing students to interface keypad with PIC18F452 and display results on SSD
 Familiarizing students to interface LCD with PIC18F452 and display data on the LCD
 Familiarizing students to interface a 4x3 keypad with PIC18F452 and display results on
LCD
Equipment:
 Hardware:
1. PIC18F452 microcontroller
2. 4x3 Keypad
3. 16x2 LCD
4. RIMS Trainer
5. SSD
 Software:
1. mikroC Pro
2. SmartPRO 5000U
3. Proteus
Introduction:
Interfacing PIC microcontroller with keypad helps in the design of various applications which
control different processes and actions by just a key press. One can design a password protected
application using keypad digits or provide users with different options represented by different
digits by using keypad interfacing.
Keypad:
A keypad is a block of buttons set with an arrangement of digits, symbols, or alphabetical letters.
A keypad is a specially designed circuitry which works on row and column selection operation.
When a key is pressed, a row is internally shorted with a column corresponding to that digit.
A 4x3 keypad has12 individual push buttons and the configuration requires 13 input pins in order
to make them work. However, with matrix arrangement, one only needs 7 microcontroller pins to
scan through the keypad.
Scanning a Keypress:
A microcontroller can scan a keypress by following the given steps:
1. A microcontroller sets all the row lines to HIGH.
2. Then, it picks a row and sets it LOW and checks the column lines one at a time.
3. If the column connection stays HIGH, the button on the row has not been pressed.
4. If it goes LOW, the microcontroller figures which button was pressed corresponding to
the detected row and column.
LCD:
An LCD screen is an electronic display module that uses liquid crystal to produce a visible
image. The 16×2 LCD display is a very basic module which displays 16 characters per line and
has 2 such lines. Each character in this LCD is displayed in a 5×8 pixel matrix. The main
benefits of using this module are that LCDs are inexpensive and easily programmable. There are
no limitations in displaying custom characters, and even animations, etc.
The 44780 standard requires 3 control lines as well as either 4 or 8 I/O lines for the data bus. The
user may select whether the LCD is to operate with a 4-bit data bus or an 8-bit data bus. If a 4-bit
data bus is used, the LCD will require a total of 7 data lines. If an 8-bit data bus is used, the LCD
will require a total of 11 data lines. The three control lines are EN, RS, and RW.
Interfacing PIC18F452 with LCD Using MikroC:
We define global variables before using LCD library, such that:
 LCD_D7: Data bit 7
 LCD_D6: Data bit 6
 LCD_D5: Data bit 5
 LCD_D4: Data bit 4
 LCD_RS: Register Select (data/instruction) signal pin
 LCD_EN: Enable signal pin
 LCD_D7_Direction: Direction of the Data 7 pin
 LCD_D6_Direction: Direction of the Data 6 pin
 LCD_D5_Direction: Direction of the Data 5 pin
 LCD_D4_Direction: Direction of the Data 4 pin
 LCD_RS_Direction: Direction of the Register Select pin
 LCD_EN_Direction: Direction of the Enable signal pin
The mikroC PRO for PIC provides a library for communication with Lcds (with HD44780
compliant controllers) through the 4-bit interface. The LCD library for PIC has following
important functions:

Function Syntax Description


Lcd_Init Lcd_Init(); To Initializes LCD module

Lcd_Out Prints text on LCD starting from specified


Lcd_Out(1,3,"Hello!"); position
Lcd_Out_Cp
Lcd_Out_Cp("Here!"); Prints text on LCD at current cursor position
Lcd_Chr
Lcd_Chr(2, 3, 'i'); Prints character on LCD at specified position
Lcd_Chr_Cp Prints character on LCD at current cursor
Lcd_Chr_Cp('e'); position

Lcd_Cmd
Lcd_Cmd(_LCD_CLEAR); Sends command to LCD
Some important LCD commands along with their functions are listed below:
Command Description

_LCD_FIRST_ROW Move cursor to the 1st row

_LCD_SECOND_ROW Move cursor to the 2nd row

_LCD_CLEAR Clear display

_LCD_CURSOR_OFF Turn off cursor

_LCD_UNDERLINE_ON Underline cursor on

_LCD_BLINK_CURSOR_ON Blink cursor on

_LCD_MOVE_CURSOR_LEFT Move cursor left without changing display data RAM

_LCD_MOVE_CURSOR_RIGHT Move cursor right without changing display data RAM

_LCD_TURN_ON Turn LCD display on

_LCD_TURN_OFF Turn LCD display off

_LCD_SHIFT_LEFT Shift display left without changing display data RAM

_LCD_SHIFT_RIGHT Shift display right without changing display data RAM


Return cursor to home position, returns a shifted display to
_LCD_RETURN_HOME
its original position. Display data RAM is unaffected.

Lab Task 1:
Write a code in mikroC to display your name on a 16x2 LCD using PIC18F452. Verify the
results on Proteus and hardware.

Procedure:
Code:
sbit LCD_RS at RC2_bit ;
sbit LCD_EN at RC3_bit ;
sbit LCD_D4 at RC4_bit ;
sbit LCD_D5 at RC5_bit ;
sbit LCD_D6 at RC6_bit ;
sbit LCD_D7 at RC7_bit ;
sbit LCD_RS_Direction at TRISC2_bit;
sbit LCD_EN_Direction at TRISC3_bit;
sbit LCD_D4_Direction at TRISC4_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D6_Direction at TRISC6_bit;
sbit LCD_D7_Direction at TRISC7_bit;
void main() {
char a[15];
char b[15];
Lcd_Init();
Lcd_cmd(_LCD_CLEAR);
Lcd_cmd(_LCD_CURSOR_OFF);
Lcd_out(1,1,"Hira") ;
Lcd_out(2,1,"Nawaz") ;

PROTEUS SIMULATION:
Lab Task 2:
Write a code in mikroC to interface a 4x3 Keypad with PIC18F452 such that the number pressed on
keypad is displayed on LCD. Verify the results on Proteus and hardware.

Procedure:

Code:
sbit LCD_RS at RC2_bit;
sbit LCD_EN at RC3_bit;
sbit LCD_D4 at RC4_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D6 at RC6_bit;
sbit LCD_D7 at RC7_bit;
sbit LCD_RS_Direction at TRISC2_bit;
sbit LCD_EN_Direction at TRISC3_bit;
sbit LCD_D4_Direction at TRISC4_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D6_Direction at TRISC6_bit;
sbit LCD_D7_Direction at TRISC7_bit;
char text1[]="1";
char text2[]="2";
char text3[]="3";
char text4[]="4";
char text5[]="5";
char text6[]="6";
char text7[]="7";
char text8[]="8";
char text9[]="9";
char text0[]="0";
void main() {
PORTB.f0=0;
PORTB.f1=0;
PORTB.f2=0;
TRISB.f3=0;
TRISB.f4=0;
TRISB.f5=0;
TRISB.f6=0;
while(1){
PORTB.f3 =1;
if(PORTB.f0==1){
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,"1");
}
else if(PORTB.f1==1){
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,text2);
}
else if(PORTB.f2==1){
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,text3);
}
PORTB.f3=0;
PORTB.f4=1;
if( PORTB.f0==1){
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,text4);
}
else if( PORTB.f1==1){
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,text5);
}
else if( PORTB.f2==1){
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,text6);
}
PORTB.f4=0;
PORTB.f5=1;
if( PORTB.f0==1){
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,text7);
}
else if( PORTB.f1==1){
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,text8);
}
else if( PORTB.f2==1){
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,text9);
}
PORTB.f5=0;
PORTB.f6=1;
if( PORTB.f1==1){
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,text0);
}
}
}

Results:
Lab Task 3:
Write a code in mikroC to interface a 4x3 Keypad with PIC18F452 such that the number pressed on
keypad is displayed on common anode SSD. Verify the results on Proteus and hardware.

Procedure:

Code:
void main() {
TRISB=0XFF;
TRISD=0X00;
while(1){
PORTB.f3 =1;
if(PORTB.f0==1){
LATD = 0b11111001;
}
else if(PORTB.f1==1){
LATD = 0b10100100;
}
else if(PORTB.f2==1){
LATD = 0b10110000;
}
PORTB.f3=0;
PORTB.f4=1;
if( PORTB.f0==1){
LATD = 0b10011001;
}
else if( PORTB.f1==1){
LATD = 0b10010010;
}
else if( PORTB.f2==1){
LATD = 0b10000010;
}
PORTB.f4=0;
PORTB.f5=1;
if( PORTB.f0==1){
LATD = 0b11111000;
}
else if( PORTB.f1==1){
LATD = 0b10000000;
}
else if( PORTB.f2==1){
LATD = 0b10010000;
}
PORTB.f5=0;
PORTB.f6=1;
if( PORTB.f1==1){
LATD = 0b11000000;
}
else
{ LATD = 0b00000000;
}}
}

RESULTS:
Conclusion:

This lab was about interfacing keypad with PIC18F452 . We were able to display results on lcd
an ssd. Keypads are used extensively in automotive applications Programmed Keypads can be
used in automated attendance system at schools, offices etc, where you enter your ID, to mark
your presence. Automatic door locks are usually accessed with a keypad .

You might also like