Experiment 09 Interfacing Keypad and LCD With PIC18F452 Objective
Experiment 09 Interfacing Keypad and LCD With PIC18F452 Objective
Experiment 09 Interfacing Keypad and LCD With PIC18F452 Objective
Lcd_Cmd
Lcd_Cmd(_LCD_CLEAR); Sends command to LCD
Some important LCD commands along with their functions are listed below:
Command Description
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 .