Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

3X4 keypad + LCD display

Status
Not open for further replies.

isabella

Junior Member level 3
Joined
Mar 23, 2012
Messages
31
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,523
Hi guys!

I am using a 3X4 keypad and an lcd display for this project. Its quite simple. When a number is pressed on the keypad, a certain message must be displayed on a lcd screeen. Example: 10. = hello world, 11= goodbye world.
The # key is ENTER and the * key is CLEAR.
Can someone help me with a sample code?
I'm using MPLAB and C18.
 

I'm using pic 18f4520
 

Hi there, I think I have the same problem with isabella, but I'm using AT89S52..
I already build my own project, but it read my first push and display it again and again..
could you help me please..

thanks..
 

here is the code

Code:
#pragma SMALL DB OE
#include<stdio.h>
#include<reg52.h>
//#include"stdlib2.h"

#define port P3
#define dataport P2
#define key P1

sbit rs=port^7;
sbit rw=port^5;
sbit en=port^6;

sbit col1=key^4;
sbit col2=key^5;
sbit col3=key^6;
sbit row1=key^0;
sbit row2=key^1;
sbit row3=key^2;
sbit row4=key^3;
sbit out=P0^7;
sbit pir1=P0^0;
sbit pir2=P0^1;

static unsigned int time;
static unsigned int count;
static unsigned timer,timer1;
static unsigned int bcounter=1;
int digit[3]={0,0,0};

int i,k;

void delay(unsigned int msec)
{
	int i,j;
	for(i=0;i<msec;i++);
	for(j=0;j<1275;j++);
}

void lcd_cmd(unsigned char item) // Function to send command to LCD
{
dataport = item;
rs= 0;
rw=0;
en=1;
delay(1);
en=0;
return;
}

void lcd_data(unsigned char item) // Function to send data to LCD
{
dataport = item;
rs= 1;
rw=0;
en=1;
delay(1);
en=0;
return;
}

void lcd_data_string(unsigned char *str) // Function to send data to string
{
while(*str)
{
  lcd_data(*str++);
  delay(1);
}
return;
}

void lcd(unsigned char str[10])
{
lcd_cmd(0x38);
lcd_cmd(0x0e);
lcd_data_string(str);
}

void lcd_data_int(int time_val)			// Function to send three digit number
{
	int int_amt;
	int_amt = time_val/100;
	lcd_data(int_amt+48);
	time_val = time_val%100;
	int_amt = time_val/10;
	lcd_data(int_amt+48);
	int_amt = time_val%10;	 
	lcd_data(int_amt+48);
}

void digit_set(int num)			
{
	if(count==4)
	{
		lcd_cmd(0x01);
		lcd_data_string("done");
	}
	else
	{
		if(count==1)
			digit[0]=num;
		else
		{
			if(count==2)
			{
				digit[1]=digit[0];
				digit[0]=num; 
			}
			else
				if(count==3)
				{
					digit[2]=digit[1];
					digit[1]=digit[0];
					digit[0]=num;
				}
		}
		lcd_data((num+48));
	}
}

void check_col1()		 // Col1 check
{
	row1=row2=row3=row4=1;
	row1=0;
	if(col1==0)				 	// Key 1
	{  
		delay(10);
		count=count+1;
		digit[i]=0;
	}
	row1=1;
	row2=0;
	if(col1==0)					// Key 4
	{
		delay(10);
		count=count+1;
		digit[i]=0;
	}
	row2=1;
	row3=0;
	if(col1==0)			  		// Key 7
	{ 
		delay(10);
		count=count+1;
		digit[i]=0;
	}
	row3=1;
	row4=0;
	if(col1==0)					// Key *
	{ 
		delay(10);
		count=count+1;
		if(count==4)
		{
			digit_set(10);
		}
		else
		{
			count=4;
			digit_set(10);
		}
	}
	row4=1;
}

void check_col2()     //Col2 check
{
	row1=row2=row3=row4=1;
	row1=0;
	if(col2==0)					// Key 2
	{
		delay(10);
		count=count+1;
		digit_set(2);
	}
	row1=1;
	row2=0;
	if(col2==0)			 		// Key 5
	{
		delay(10);
		count=count+1;
		digit_set(5);
	}
	row2=1;
	row3=0;
	if(col2==0)	       				// Key 8
	{
		delay(10);
		count=count+1;
		digit_set(8);
	}
	row3=1;
	row4=0;
	if(col2==0)		 			// Key 0
	{
		delay(10);
		count=count+1;
		digit_set(0);
	}
	row4=1;
}

void check_col3()	 // Col3 check
{
	row1=row2=row3=row4=1;
	row1=0;
	if(col3==0)					// Key 3
	{
		delay(10);
		count=count+1;
		digit_set(3);
	}
	row1=1;
	row2=0;
	if(col3==0)			   		// Key 6
	{
		delay(10);
		count=count+1;
		digit_set(6);
	}
	row2=1;
	row3=0;
	if(col3==0)				  	// Key 9
	{
		delay(10);
		count=count+1;
		digit_set(9);
	}
	row3=1;
	row4=0;
	if(col3==0)					// Key #
	{
		delay(10);
		EA=1;
		lcd_cmd(0x01);
		lcd_data_string("counting down . . .");
	}
	row4=1;
}

void keypad()
{ 
row1=row2=row3=row4=0;
while(col1==1&&col2==1&&col3==1);
row1=row2=row3=row4=0;
	if(col1==0)
		check_col1();
	else
		if(col2==0)
			check_col2();
		else
			if(col3==0)
				check_col3();
	delay(10);
}

void main()
{
unsigned int k;

TMOD=0x01;
EA=0;
TH0=50621/256;TL0=50621%256;
ET0=1;
EX0=1;
IT0=1;
PX0=1;
TF0=0;
//TR0=1;

count=0;			//initial count value
k=time;

pir1=pir2=0;
	

out=0;
 lcd_cmd(0x38);
 delay(2); 
 lcd_cmd(0x01);  // Clear Screen
 lcd("WELCOME");
				delay(50000);
		count=0;
		delay(1);
		lcd_cmd(0x01);
		lcd_cmd(0x80);
		lcd("Enter time interval(second):");
				
	
for(i=0;i<3;i++)
		{ 
		delay(50);
		lcd_cmd(0xc4+i);
		row1=row2=row3=row4=0;
		while(col1==1&&col2==1&&col3==1);
		keypad();
		time=(digit[0]+(digit[1]*10)+(digit[2]*100));		
		delay(1);
		}

	while(1);
	{
		int out;
 		if(pir1==0 && pir2==0)
  		out=0;
  		if(pir1==1 && pir2==0)
  		out=1;
 		if(pir1==0 && pir2==1)
  		out=1;
 		if(pir1==1 && pir2==1)
  		out=1;
		if(out==1) {
		EA=1;
		}
		if(time==0)
			out=0;
		
	}
	}


void isr_t0()interrupt 1 using 2
{
 TR0=0;
 TF0=0;
 TH0=50621/256;TL0=50621%256;
 if(++timer==20)
 {--time;//bcounter^=1;
  timer=0;
   }
  if(++timer1==5){(bcounter^=1);timer1=0;}
 TR0=1;
 }
 
void key_isr() interrupt 0 using 1
{
EX0=0;
if(col3==0&&row4==0)
{
TR0=~TR0;
if(TR0)
time=0;
}
EX0=1;
}

and here is the schematic
schematic.jpg


thanks

- - - Updated - - -

here is the code

Code:
#pragma SMALL DB OE
#include<stdio.h>
#include<reg52.h>
//#include"stdlib2.h"

#define port P3
#define dataport P2
#define key P1

sbit rs=port^7;
sbit rw=port^5;
sbit en=port^6;

sbit col1=key^4;
sbit col2=key^5;
sbit col3=key^6;
sbit row1=key^0;
sbit row2=key^1;
sbit row3=key^2;
sbit row4=key^3;
sbit out=P0^7;
sbit pir1=P0^0;
sbit pir2=P0^1;

static unsigned int time;
static unsigned int count;
static unsigned timer,timer1;
static unsigned int bcounter=1;
int digit[3]={0,0,0};

int i,k;

void delay(unsigned int msec)
{
	int i,j;
	for(i=0;i<msec;i++);
	for(j=0;j<1275;j++);
}

void lcd_cmd(unsigned char item) // Function to send command to LCD
{
dataport = item;
rs= 0;
rw=0;
en=1;
delay(1);
en=0;
return;
}

void lcd_data(unsigned char item) // Function to send data to LCD
{
dataport = item;
rs= 1;
rw=0;
en=1;
delay(1);
en=0;
return;
}

void lcd_data_string(unsigned char *str) // Function to send data to string
{
while(*str)
{
  lcd_data(*str++);
  delay(1);
}
return;
}

void lcd(unsigned char str[10])
{
lcd_cmd(0x38);
lcd_cmd(0x0e);
lcd_data_string(str);
}

void lcd_data_int(int time_val)			// Function to send three digit number
{
	int int_amt;
	int_amt = time_val/100;
	lcd_data(int_amt+48);
	time_val = time_val%100;
	int_amt = time_val/10;
	lcd_data(int_amt+48);
	int_amt = time_val%10;	 
	lcd_data(int_amt+48);
}

void digit_set(int num)			
{
	if(count==4)
	{
		lcd_cmd(0x01);
		lcd_data_string("done");
	}
	else
	{
		if(count==1)
			digit[0]=num;
		else
		{
			if(count==2)
			{
				digit[1]=digit[0];
				digit[0]=num; 
			}
			else
				if(count==3)
				{
					digit[2]=digit[1];
					digit[1]=digit[0];
					digit[0]=num;
				}
		}
		lcd_data((num+48));
	}
}

void check_col1()		 // Col1 check
{
	row1=row2=row3=row4=1;
	row1=0;
	if(col1==0)				 	// Key 1
	{  
		delay(10);
		count=count+1;
		digit[i]=0;
	}
	row1=1;
	row2=0;
	if(col1==0)					// Key 4
	{
		delay(10);
		count=count+1;
		digit[i]=0;
	}
	row2=1;
	row3=0;
	if(col1==0)			  		// Key 7
	{ 
		delay(10);
		count=count+1;
		digit[i]=0;
	}
	row3=1;
	row4=0;
	if(col1==0)					// Key *
	{ 
		delay(10);
		count=count+1;
		if(count==4)
		{
			digit_set(10);
		}
		else
		{
			count=4;
			digit_set(10);
		}
	}
	row4=1;
}

void check_col2()     //Col2 check
{
	row1=row2=row3=row4=1;
	row1=0;
	if(col2==0)					// Key 2
	{
		delay(10);
		count=count+1;
		digit_set(2);
	}
	row1=1;
	row2=0;
	if(col2==0)			 		// Key 5
	{
		delay(10);
		count=count+1;
		digit_set(5);
	}
	row2=1;
	row3=0;
	if(col2==0)	       				// Key 8
	{
		delay(10);
		count=count+1;
		digit_set(8);
	}
	row3=1;
	row4=0;
	if(col2==0)		 			// Key 0
	{
		delay(10);
		count=count+1;
		digit_set(0);
	}
	row4=1;
}

void check_col3()	 // Col3 check
{
	row1=row2=row3=row4=1;
	row1=0;
	if(col3==0)					// Key 3
	{
		delay(10);
		count=count+1;
		digit_set(3);
	}
	row1=1;
	row2=0;
	if(col3==0)			   		// Key 6
	{
		delay(10);
		count=count+1;
		digit_set(6);
	}
	row2=1;
	row3=0;
	if(col3==0)				  	// Key 9
	{
		delay(10);
		count=count+1;
		digit_set(9);
	}
	row3=1;
	row4=0;
	if(col3==0)					// Key #
	{
		delay(10);
		EA=1;
		lcd_cmd(0x01);
		lcd_data_string("counting down . . .");
	}
	row4=1;
}

void keypad()
{ 
row1=row2=row3=row4=0;
while(col1==1&&col2==1&&col3==1);
row1=row2=row3=row4=0;
	if(col1==0)
		check_col1();
	else
		if(col2==0)
			check_col2();
		else
			if(col3==0)
				check_col3();
	delay(10);
}

void main()
{
unsigned int k;

TMOD=0x01;
EA=0;
TH0=50621/256;TL0=50621%256;
ET0=1;
EX0=1;
IT0=1;
PX0=1;
TF0=0;
//TR0=1;

count=0;			//initial count value
k=time;

pir1=pir2=0;
	

out=0;
 lcd_cmd(0x38);
 delay(2); 
 lcd_cmd(0x01);  // Clear Screen
 lcd("WELCOME");
				delay(50000);
		count=0;
		delay(1);
		lcd_cmd(0x01);
		lcd_cmd(0x80);
		lcd("Enter time interval(second):");
				
	
for(i=0;i<3;i++)
		{ 
		delay(50);
		lcd_cmd(0xc4+i);
		row1=row2=row3=row4=0;
		while(col1==1&&col2==1&&col3==1);
		keypad();
		time=(digit[0]+(digit[1]*10)+(digit[2]*100));		
		delay(1);
		}

	while(1);
	{
		int out;
 		if(pir1==0 && pir2==0)
  		out=0;
  		if(pir1==1 && pir2==0)
  		out=1;
 		if(pir1==0 && pir2==1)
  		out=1;
 		if(pir1==1 && pir2==1)
  		out=1;
		if(out==1) {
		EA=1;
		}
		if(time==0)
			out=0;
		
	}
	}


void isr_t0()interrupt 1 using 2
{
 TR0=0;
 TF0=0;
 TH0=50621/256;TL0=50621%256;
 if(++timer==20)
 {--time;//bcounter^=1;
  timer=0;
   }
  if(++timer1==5){(bcounter^=1);timer1=0;}
 TR0=1;
 }
 
void key_isr() interrupt 0 using 1
{
EX0=0;
if(col3==0&&row4==0)
{
TR0=~TR0;
if(TR0)
time=0;
}
EX0=1;
}

and here is the schematic
schematic.jpg


thanks
 

Attachments

  • startkeil2.txt
    4.7 KB · Views: 85

The next solution shows the beauty of using multidimensional tables in C, which is a method I often use in applications I make. I'm not sure your PIC can support tables that large, but you'll be able to find a workaround if needed. The code is incomplete, you should find a way to integrate it in your program. Also, the code is not tested, I've written it on the fly, so might contain bugs...

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Defining the keypad settings
#define nCols 3
#define nRows 4
#define MsgLength 16
 
// Defining the messages for each button
char MsgTbl[nCols][nRows][MsgLength] = {
    // FIRST COLUMN
    {"MESSAGE: C=0 R=0",    // button 1
     "MESSAGE: C=0 R=1",    // button 4
     "MESSAGE: C=0 R=2",    // button 7
     "MESSAGE: C=0 R=3"     // button *
    },
    // SECOND COLUMN
    {"MESSAGE: C=1 R=0",    // button 2
     "MESSAGE: C=1 R=1",    // button 5
     "MESSAGE: C=1 R=2",    // button 8
     "MESSAGE: C=1 R=3"     // button 0
    },
    // THIRD COLUMN
    {"MESSAGE: C=2 R=0",    // button 3
     "MESSAGE: C=2 R=1",    // button 6
     "MESSAGE: C=2 R=2",    // button 9
     "MESSAGE: C=2 R=3"     // button #
    }
}
 
 
/* The next code is for the ISR routine */
char sel_COL, sel_ROW;
 
// assuming the variables for the keypad pins are named COL_0 .. 2 and ROW_0 .. 3
if (COL_0) sel_COL=0;   //
if (COL_1) sel_COL=1;   // reading the selected column 
if (COL_2) sel_COL=2;   //
 
if (ROW_0) sel_ROW=0;   //
if (ROW_1) sel_ROW=1;   // reading the selected row
if (ROW_2) sel_ROW=2;   //
if (ROW_3) sel_ROW=3;   //
 
 
 
/* the next code is to be used in your main() function or wherever you need it
    
    to print the message corresponding to the selected row and column print the
    text contained in:
    
    MsgTbl[sel_COL][sel_ROW]
    
    for example, with printf:
    
    printf("%s", MsgTbl[sel_COL][sel_ROW]);
    
    if you want to "clear"  or  "enter" (special key commands) you might want to
    check for the selected row and column and if a particular configuration is
    met, do something else than printing the message (whatever you want to do)
    
    Example code:
*/
if ((sel_COL == 0) && (sel_ROW == 3)){
    // * pressed
    printf("%s", "The * key has been pressed");
} else if ((sel_COL == 2) && (sel_ROW == 3)){
    // # pressed
    printf("%s", "The # key has been pressed");
    } else {
    // any other key has been pressed, print the corresponding message
    printf("%s", MsgTbl[sel_COL][sel_ROW]);
}

 
Last edited:
  • Like
Reactions: mentoz

    mentoz

    Points: 2
    Helpful Answer Positive Rating
As I mention above, I use atmel 89s52 and also keil compiler.. is it possible??

thanks.. :)
 

It's obviously possible, but I never used atmel and keil compiler, so I'm unable to port the code above to this MCU. Someone else may help you porting the code.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top