Discussion in "8051 Discussion Forum" started by    Ansh12    Nov 17, 2017.
Fri Nov 17 2017, 09:37 am
#1
I am looking keypad interfacing program. I was reading tutorial Keypad programming for 8051 http://www.8051projects.net/keypad-interfacing/8051-programming.php
I have seen program in link. its not complete program. Also I didn't understand keypad reading functions. I am trying to understand keypad reading in c programming. Please help me..

Mon Nov 20 2017, 02:05 am
#2
As a starting point, can you tell us how much of the tutorial example you understand ?
Do you have access to the Proteus program to simulate a circuit ?

Mon Nov 20 2017, 07:45 am
#3


As a starting point, can you tell us how much of the tutorial example you understand ?
Do you have access to the Proteus program to simulate a circuit ?

ExperimenterUK


I understand logic of keypad but I am having difficulties to implement program. That's the reason first I tried to implement function for keypad reading.

I have simulator software. I can simply add keypad and lcd to 8051 controller.

In tutorial, there is only program for keypad interfacing, no program for both lcd and keypad. I wanted to see sample example for lcd and keypad interfacing program. once I see its working on proteus. I can try to understand the whole program. I have keil compiler and at89c51 controller.
Tue Nov 21 2017, 03:48 am
#4
Here is a version of the tutorial that reads a key and prints it to a serial terminal.
There is also a Proteus project to run the program and display the code as a hex value.
In Proteus ->Virtual Terminal, make sure "Hex display mode" is ticked
 Ansh12 like this.
Mon Feb 19 2018, 05:47 pm
#5


Here is a version of the tutorial that reads a key and prints it to a serial terminal.

ExperimenterUK


Please see the below code. I have taken idea from your code to read keypad. there is no error in code but it doesn't print anything on screen.
AT89c51 & Keil
#include<reg51.h>

#define keyport P1	//keypad connected to P1
#define lcdport P2	//lcd connected to P2

sbit C1 = P1^0;     //Column1
sbit C2 = P1^1;     //Column2
sbit C3 = P1^2;     //Column3
sbit C4 = P1^3;     //Column4

sbit E    = P3^6;     //E pin for LCD
sbit RS   = P3^7;     //RS pin for LCD

//Function declarations
void init_port(void);
void Delay(int);
void LCD_Init(void);
void LCD_Command(int);
void LCD_Data(char);
char Get_key(void);

int main(void)
{
   char key;         // key char for keeping record of pressed key
	
	 init_port();      // Make input and output pins as required
           
        LCD_Init();       // Initilize LCD

   while(1)
   { 
           key = Get_key();       // Get pressed key
	   LCD_Data(key+1);        // Echo the key pressed to LCD
   }
}


void init_port(void)
{
	P0 = 0x00;   //not used
	P1 = 0x0f;   //used for generating outputs and taking inputs from Keypad
	P2 = 0x00;   //used as data port for LCD
	P3 = 0x00;   //used for RS and E   
}

void Delay(int a)
{
   int i;
   for(i=0;i<a;i++);   //Delay
}

void LCD_Data(char t)
{
   RS = 1;             // This is data
   P2 = t;             //Data transfer
   E  = 1;             //  E = 1
   Delay(150);
   E  = 0;             // E = 0
   Delay(150);
}


void LCD_Command(int z)
{
   RS = 0;             // This is command
   P2 = z;             //Data transfer
   E  = 1;             // E = 1
   Delay(150);
   E  = 0;             // E = 0
   Delay(150);
}

void LCD_Init(void)
{
 
     Delay(15000);
     LCD_Command(0x30);
     Delay(4500);
     LCD_Command(0x30);
     Delay(300);
     LCD_Command(0x30);
     Delay(650);
 
    LCD_Command(0x38);    //function set
    LCD_Command(0x0c);    //display on,cursor off,blink off
    LCD_Command(0x01);    //clear display
    LCD_Command(0x06);    //entry mode, set increment
}

 char get_key()
{
	unsigned char i, k, key = 0;
	k = 1;
	for(i = 0; i < 4; i++){		//loop for 4 rows
		
		keyport &=~ (0x80>
>
i);	//to make rows low 1 by 1
		 
  		               if(!C1){	            //check if key1 is pressed
				 Delay(10000);
				key = k+0;	        //set key number
				while(!C1);	        //wait for release
				return key;	        //return key number
			        }

			      if(!C2){	             //check if key2 is pressed
				 Delay(10000);
				key = k+1;	         //set key number
				while(!C2);	         //wait for release
				return key;	         //return key number
			         }

			         if(!C3){	             //check if key3 is pressed
				 Delay(10000);
				key = k+2;         	//set key number
				while(!C3);	        //wait for release
				return key;	        //return key number
			         }

    			      if(!C4){	            //check if key4 is pressed
				 Delay(10000);
				key = k+3;	        //set key number
				while(!C4);	        //wait for release
				return key;	        //return key number
			}
		k+=4;			              //next row key number
		keyport |= 0x80>
>
i;	    //make the row high again
	}
	return 0;			            //return 0 if no key pressed
}



[ Edited Mon Feb 19 2018, 06:22 pm ]
Tue Feb 20 2018, 09:46 pm
#6
Change .....
while(1)
{
key = Get_key(); // Get pressed key
LCD_Data(key+1); // Echo the key pressed to LCD
}

to...

while(1)
{
key = Get_key(); // Get pressed key if any
if(key) //only print if a key is pressed
LCD_Data(key+0x60); // Send a printable value to LCD

}

Adding 0x60 ,moves the code it into the printable range a-p


[ Edited Tue Feb 20 2018, 10:02 pm ]
Wed Feb 21 2018, 06:23 am
#7



while(1)
{
key = Get_key(); // Get pressed key if any
if(key) //only print if a key is pressed
LCD_Data(key+0x60); // Send a printable value to LCD

}

ExperimenterUK


Thanks for your fast feedback. I did replace that portion of code But this code is not working there nothing on the screen

Please take look at the connection . It may be one of the reason

Wed Feb 21 2018, 11:11 pm
#8
Can you post your Proteus files.
You will need pull-up resistors on port 0.

Thu Feb 22 2018, 07:06 am
#9


Can you post your Proteus files.

ExperimenterUK


Thanks I have attached file. Please look at this
Fri Feb 23 2018, 06:42 am
#10
Add pull- ups for EN and RS.
change your defines to

sbit E = P0^7; //E pin for LCD
sbit RS = P0^5; //RS pin for LCD

R1,3,4 and 5 are not needed.

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

Bobbyerilar
Thu Mar 28 2024, 08:08 am
pb58
Thu Mar 28 2024, 05:54 am
Clarazkafup
Thu Mar 28 2024, 02:24 am
Walterkic
Thu Mar 28 2024, 01:19 am
Davidusawn
Wed Mar 27 2024, 08:30 pm
Richardsop
Tue Mar 26 2024, 10:33 pm
Stevencog
Tue Mar 26 2024, 04:26 pm
Bernardwarge
Tue Mar 26 2024, 11:15 am